Example usage for com.vaadin.ui JavaScript getCurrent

List of usage examples for com.vaadin.ui JavaScript getCurrent

Introduction

In this page you can find the example usage for com.vaadin.ui JavaScript getCurrent.

Prototype

public static JavaScript getCurrent() 

Source Link

Document

Get the JavaScript object for the current Page, or null if there is no current page.

Usage

From source file:com.foc.web.modules.admin.FocUser_Form.java

License:Apache License

public static void changePasswordForUser(FocUser focUser) {
    String newPassStr = focUser.resetPassword();

    if (focUser != null && focUser.getContact() != null) {
        String email = focUser.getContact().getEMail();
        if (email != null && !email.isEmpty()) {
            String name = focUser.getContact().getFullName();
            String ENT = "%20%0d%0a";
            String javaScript = "window.location = 'mailto:" + email + "?body=Dear " + name + "," + ENT + ENT;
            javaScript += "You can have real time follow up of your goods delivery on our online website: "
                    + ENT + Page.getCurrent().getLocation() + ENT + ENT;
            javaScript += "Username: " + focUser.getName() + ENT;
            javaScript += "Password: " + newPassStr + ENT + ENT;
            javaScript += "Regards,'";
            JavaScript.getCurrent().execute(javaScript);
        }//from w ww.  j a v  a  2s. c om
    }
}

From source file:com.foc.web.modules.admin.FocUser_Login_Form.java

License:Apache License

private void addBrowserBackClick_JavaScript() {
    JavaScript.getCurrent().execute(
            "window.onbeforeunload = function leaveCurrentPage() { return 'Your work will be lost.;' };");
}

From source file:com.hybridbpm.ui.util.CookieManager.java

License:Apache License

public static void setCookie(String key, String value, String path) {
    JavaScript.getCurrent().execute(String.format("document.cookie = \"%s=%s; path=%s\";", key, value, path));
}

From source file:com.hybridbpm.ui.util.CookieManager.java

License:Apache License

public static void getCookieValue(String key, final Callback callback) {
    final String callbackid = "hybridbpmcookie" + UUID.randomUUID().toString().substring(0, 8);
    JavaScript.getCurrent().addFunction(callbackid, new JavaScriptFunction() {

        @Override// w  w w.  ja  v a  2s  . c om
        public void call(JsonArray arguments) {
            JavaScript.getCurrent().removeFunction(callbackid);
            if (arguments.length() == 0) {
                callback.onValue(null);
            } else {
                callback.onValue(arguments.getString(0));
            }
        }
    });
    JavaScript.getCurrent().execute(String.format(
            "var nameEQ = \"%2$s=\";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++) {var c = ca[i];while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) {%1$s( c.substring(nameEQ.length,c.length)); return;};} %1$s();",
            callbackid, key));

}

From source file:com.lst.deploymentautomation.vaadin.core.LspsUI.java

License:Open Source License

private void initLayout() {
    //load current user info

    boolean themeChange = false;

    userInfo = (UserInfo) getSession().getAttribute("user");
    if (userInfo == null) {
        userInfo = new UserInfo(LspsAppConnectorImpl.INJECTOR_INSTANCE);
        getSession().setAttribute("user", userInfo);

        String theme = userInfo.getSettingString("theme", Constants.DEFAULT_THEME);

        // if user have set other than default theme
        if (!theme.equals(Constants.DEFAULT_THEME)) {
            if (Constants.THEMES.contains(theme)) {
                this.setTheme(theme);
            } else {
                this.setTheme(Constants.DEFAULT_THEME);
            }/*from w w w  .j  a v a  2  s  .  c om*/
            themeChange = true;
        }
    }

    //get browser info
    WebBrowser browser = getPage().getWebBrowser();

    //set layout style
    String layoutSetting = "l-layout-" + userInfo.getSettingString("layoutSetting", "compact");
    JavaScript.getCurrent().execute("" + "if (!Array.prototype.indexOf) {"
            + "Array.prototype.indexOf = function(obj, start) {for (var i = (start || 0), j = this.length; i < j; i++) {if (this[i] === obj) { return i; }}return -1;}"
            + "}" + "var setting = '" + layoutSetting + "';"
            + "var classes = document.body.className.split(/\\s+/);" + "var index = classes.indexOf(setting);"
            + "~index || classes.push(setting);" + "document.body.className = classes.join(\" \");" + "");

    String fontSetting = "l-font-" + userInfo.getSettingString("fontSetting", "standard");
    JavaScript.getCurrent().execute("" + "document.documentElement.className='" + fontSetting + "';" + "");

    //set locale to the one preferred by the user
    Locale userLocale = userInfo.getLocale();
    if (userLocale == null) {
        userLocale = Locale.US;
    }
    setLocale(userLocale);

    //initialize the layout
    appLayout = new AppLayout();
    if (themeChange) {
        appLayout.setVisible(false);
    } else {
        if (browser.isTouchDevice()) {
            getUI().addStyleName("v-touch");
        } else {
            //use fixed-width layout by default
            getUI().addStyleName("fixed");
        }
    }
    setContent(appLayout);

    //setup navigation, register available views
    initNavigation();

    if (getPage().getUriFragment() == null) {
        openHomePage();
    }

    //if substitution is active, show warning
    if (getUser().getPerson().isSubstitutionActive()) {
        showWarningMessage("settings.substitutionActiveMessage");
    }
}

From source file:com.lst.deploymentautomation.vaadin.core.LspsUI.java

License:Open Source License

/**
 * Sets theme for UI and then refreshes page to propagate the change.
 * @param theme//w  w  w.  j  a  v a 2 s .  c  o m
 */
@Override
public void setTheme(String theme) {
    this.getUser().setSetting("theme", theme);
    VaadinService.getCurrentRequest().getWrappedSession().setAttribute("theme", theme);
    // store theme in browser local storage, so if default theme is changed, next time
    // user will see his chosen theme even before login.. and refresh
    JavaScript.getCurrent()
            .execute("" + "var storage = window.localStorage;" + "if(typeof(Storage)!=='undefined')"
                    + "storage.setItem('deployment-automationlication-theme-name', '" + theme + "');"
                    + "window.location.reload();" + "");
}

From source file:com.lst.deploymentautomation.vaadin.page.SettingsView.java

License:Open Source License

private void createView() {
    LspsUI ui = (LspsUI) getUI();/*from w w w  . jav a2  s.  com*/
    Person user = ui.getUser().getPerson();
    userRights = user.getRights();

    setTitle(ui.getMessage(TITLE));

    Panel panel = new Panel();
    panel.addStyleName("l-border-none");
    setContent(panel);

    VerticalLayout layout = new VerticalLayout();
    layout.setSpacing(true);
    layout.setMargin(true);
    panel.setContent(layout);

    //user data and settings will be laid out next to each other
    HorizontalLayout topSection = new HorizontalLayout();
    topSection.setSpacing(true);
    topSection.setWidth("100%");
    layout.addComponent(topSection);

    VerticalLayout userData = createUserDataSection(ui, user);
    topSection.addComponent(userData);
    topSection.setExpandRatio(userData, 1);

    Label spacer = new Label();
    spacer.setWidth("20px");
    topSection.addComponent(spacer);

    VerticalLayout settings = createSettingsSection(ui);
    topSection.addComponent(settings);
    topSection.setExpandRatio(settings, 1);

    //substitution section
    VerticalLayout substitution = createSubstitutionSection(ui, user);
    layout.addComponent(substitution);

    Label spacer2 = new Label();
    spacer2.setHeight("10px");
    layout.addComponent(spacer2);

    //buttons
    HorizontalLayout buttons = new HorizontalLayout();
    buttons.setSpacing(true);
    Button saveButton = new Button(ui.getMessage("action.save"), new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            if (save()) {
                if (requestReload) {
                    JavaScript.getCurrent().execute("window.location.reload()");
                } else {
                    close();
                }
            }
        }
    });
    saveButton.setData(BUTTON_TYPE_SAVE);
    buttons.addComponent(saveButton);
    Button cancelButton = new Button(ui.getMessage("action.cancel"), new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            close();
        }
    });
    buttons.addComponent(cancelButton);
    layout.addComponent(buttons);
}

From source file:com.lst.deploymentautomation.vaadin.page.SettingsView.java

License:Open Source License

private boolean save() {
    LspsUI ui = (LspsUI) getUI();// w  ww.j av a  2s  .  c om

    boolean active = Boolean.TRUE.equals(substitutionActive.getValue());
    @SuppressWarnings("unchecked")
    Set<Person> selection = (Set<Person>) substitutes.getValue();

    String pass = passwordField.getValue();
    String pass2 = confirmation.getValue();

    boolean valid = true;
    if (active && selection.size() == 0) {
        Utils.setComponentError(substitutes, "settings.noSubstitutesError");
        valid = false;
    } else {
        Utils.setComponentError(substitutes, null);
    }
    if (pass != null && !pass.equals(pass2)) {
        Utils.setComponentError(confirmation, "settings.passwordDoesNotMatchError");
        valid = false;
    } else {
        Utils.setComponentError(confirmation, null);
    }

    if (valid) {
        try {
            //save locale and update UI
            Locale locale = Locales.toLocale((String) languages.getValue());
            if (!locale.equals(ui.getUser().getLocale())) {
                requestReload = true;
                ui.getUser().setLocale(locale);
                UI.getCurrent().setLocale(locale);
            }

            // change theme if it is different than current
            String newTheme = themes.getValue().toString();
            if (!ui.getTheme().equals(newTheme)) {
                ((LspsUI) UI.getCurrent()).setTheme(newTheme);
            }

            String layoutSetting = "l-layout-" + ui.getUser().getSettingString("layoutSetting", "compact");
            JavaScript.getCurrent().execute("" + "var classes = document.body.className.split(/\\s+/);"
                    + "classes.pop();" + "document.body.className = classes.join(\" \");" + "");
            ui.getUser().setSetting("layoutSetting", layoutSettings.getValue().toString());
            layoutSetting = "l-layout-" + layoutSettings.getValue().toString();
            JavaScript.getCurrent().execute("" + "if (!Array.prototype.indexOf) {"
                    + "Array.prototype.indexOf = function(obj, start) {for (var i = (start || 0), j = this.length; i < j; i++) {if (this[i] === obj) { return i; }}return -1;}"
                    + "}" + "var setting = '" + layoutSetting + "';"
                    + "var classes = document.body.className.split(/\\s+/);"
                    + "var index = classes.indexOf(setting);" + "~index || classes.push(setting);"
                    + "document.body.className = classes.join(\" \");" + "");
            ui.getUser().setSetting("fontSetting", fontSettings.getValue().toString());
            String fontSetting = "l-font-" + fontSettings.getValue().toString();
            JavaScript.getCurrent()
                    .execute("" + "document.documentElement.className='" + fontSetting + "'" + "");

            //reload person to avoid stale object exceptions
            Person person = ui.getUser().reload();

            //update user details
            person.setDirectSubstitutes(selection);
            person.setSubstitutionActive(active);
            person.setEmail(email.getValue());
            person.setPhone(telephone.getValue());

            person = personManagementService.updatePerson(person);
            ui.getUser().setPerson(person);

            //update password
            if (pass != null && !"".equals(pass)) {
                personManagementService.updatePersonPassword(person.getId(), pass);
            }

        } catch (Exception e) {
            Utils.rethrow(e, "could not save settings", log);
        }
    }

    return valid;
}

From source file:com.mycollab.vaadin.web.ui.NotificationComponent.java

License:Open Source License

public NotificationComponent() {
    notificationItems = new ConcurrentHashSet<>();
    notificationContainer = new VerticalLayout();
    new Restrain(notificationContainer).setMaxWidth("500px");
    this.setContent(notificationContainer);
    this.setIcon(FontAwesome.BELL);
    this.setStyleName("notification-button");

    addPopupVisibilityListener(this);
    EventBusFactory.getInstance().register(this);

    // Register to receive broadcasts
    JavaScript.getCurrent().addFunction("com.mycollab.scripts.upgrade", jsonArray -> {
        String version = jsonArray.getString(0);
        String installerFile = jsonArray.getString(1);
        String manualDownloadLink = jsonArray.getString(2);
        UI.getCurrent().addWindow(new UpgradeConfirmWindow(version, manualDownloadLink, installerFile));
    });/*from  w ww.jav a 2 s.c om*/
}

From source file:com.mycollab.web.DesktopApplication.java

License:Open Source License

private void rememberTempAccount(String username, String password) {
    String storeVal = username + "$" + EnDecryptHelper.encryptText(password);
    String setCookieVal = String.format(
            "var now = new Date(); now.setTime(now.getTime() + 1 * 1800 * 1000); "
                    + "document.cookie = \"%s=%s; expires=\" + now.toUTCString() + \"; path=/\";",
            TEMP_ACCOUNT_COOKIE, storeVal);
    JavaScript.getCurrent().execute(setCookieVal);
}