Example usage for com.google.gwt.dom.client Document createScriptElement

List of usage examples for com.google.gwt.dom.client Document createScriptElement

Introduction

In this page you can find the example usage for com.google.gwt.dom.client Document createScriptElement.

Prototype

public ScriptElement createScriptElement() 

Source Link

Usage

From source file:com.denormans.googleanalyticsgwt.api.GoogleAnalytics.java

License:Apache License

private static boolean injectAnalytics(@Nullable final String defaultTrackerID) {
    if (defaultTrackerID != null) {
        setupDefaultTrackerID(defaultTrackerID);
    }//from   w w w.ja  v  a 2 s .  co  m

    if (IsAlreadyInjected) {
        return true;
    }

    IsAlreadyInjected = true;

    boolean isAlreadyLoaded = get() != null;
    if (isAlreadyLoaded) {
        return true;
    }

    String src;
    if (isSSL()) {
        src = "https://ssl.google-analytics.com/ga.js";
    } else {
        src = "http://www.google-analytics.com/ga.js";
    }

    Document doc = Document.get();
    ScriptElement script = doc.createScriptElement();
    script.setSrc(src);
    script.setType("text/javascript");
    script.setAttribute("async", "true");

    doc.getBody().appendChild(script);

    return false;
}

From source file:com.github.gwtbootstrap.showcase.client.NonResShowcase.java

License:Apache License

public void onModuleLoad() {
    initWidget(uiBinder.createAndBindUi(this));
    // addSectionToContainer("Get Started", "setup", new Setup());
    // addSectionToContainer("Get Support", "support", new Support());
    addSectionToContainer("Buttons", "buttons", new Buttons());
    addSectionToContainer("Grid System", "gridSystem", new GridSystem());
    addSectionToContainer("Navigation", "navigation", new Navigation());
    addSectionToContainer("Hero Unit", "hero", new HeroUnit());
    addSectionToContainer("Forms", "forms", new Forms());
    addSectionToContainer("Page Header", "pageheader", new PageHeader());
    addSectionToContainer("Progress Bar", "progressbar", new ProgressBar());
    addSectionToContainer("Alerts", "alerts", new Alerts());
    addSectionToContainer("Pagination", "pagination", new Pagination());
    addSectionToContainer("Dropdown", "dropdown", new Dropdown());
    addSectionToContainer("Modal", "modal", new Modal());
    addSectionToContainer("Datepicker", "datepicker", new Datepicker());
    addSectionToContainer("Datetimepicker", "datetimepicker", new Datetimepicker());
    addSectionToContainer("Tooltip", "tooltip", new Tooltips());
    addSectionToContainer("Popover", "popover", new Popovers());
    addSectionToContainer("Icons", "icons", new Icons());

    RootPanel.get("content").add(this);
    Document doc = Document.get();
    ScriptElement script = doc.createScriptElement();
    script.setSrc("https://apis.google.com/js/plusone.js");
    script.setType("text/javascript");
    script.setLang("javascript");
    doc.getBody().appendChild(script);/*from w  ww.ja  va 2  s.c o m*/

    github_buttons.getElement().setInnerHTML(
            "<iframe src=\"http://markdotto.github.com/github-buttons/github-btn.html?user=gwtbootstrap&repo=gwt-bootstrap&type=watch&count=true\"\n"
                    + "                                allowtransparency=\"true\" frameborder=\"0\" scrolling=\"0\" width=\"110px\" height=\"20px\"></iframe>"
                    + "<br /><iframe src=\"http://markdotto.github.com/github-buttons/github-btn.html?user=gwtbootstrap&repo=gwt-bootstrap&type=fork&count=true\"\n"
                    + "  allowtransparency=\"true\" frameborder=\"0\" scrolling=\"0\" width=\"95px\" height=\"20px\"></iframe>");

    JavaScriptInjector.inject(
            "!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");");
    JavaScriptInjector.inject(
            "(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));");

    Scheduler.get().scheduleFinally(new ScheduledCommand() {

        @Override
        public void execute() {
            History.fireCurrentHistoryState();
        }
    });
}

From source file:com.github.gwtbootstrap.showcase.client.Showcase.java

License:Apache License

public void onModuleLoad() {

    CustomResources res = GWT.create(CustomResources.class);
    res.showcase().ensureInjected();//from w  w w.j ava 2  s . c om
    ClientFactory factory = GWT.create(ClientFactory.class);

    subnav = factory.getSubnav();

    initWidget(uiBinder.createAndBindUi(this));

    ActivityMapper activityMapper = new AppActivityMapper(factory);

    ActivityManager activityManager = new ActivityManager(activityMapper, factory.getEventBus());

    activityManager.setDisplay(main);
    PlaceHistoryMapper mapper = factory.getPlaceHistoryMapper();
    final PlaceHistoryHandler handler = new PlaceHistoryHandler(mapper);
    handler.register(factory.getPlaceController(), factory.getEventBus(), new OverviewPlace(""));

    final Map<Menus, NavLink> map = new EnumMap<Menus, NavLink>(Menus.class);
    for (Menus menu : Menus.values()) {
        NavLink link = new NavLink(menu.getDisplay());
        link.setHref("#" + menu.getName() + ":");

        if (menu == Menus.GWT) {
            nav.add(new Divider());
        }
        nav.add(link);

        map.put(menu, link);
    }

    factory.getEventBus().addHandler(PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() {

        @Override
        public void onPlaceChange(PlaceChangeEvent event) {
            Place place = event.getNewPlace();

            if (place instanceof HasMenuPlace) {
                HasMenuPlace menuPlace = (HasMenuPlace) place;

                for (Map.Entry<Menus, NavLink> entry : map.entrySet()) {
                    if (entry.getKey().equals(menuPlace.getMenu())) {
                        entry.getValue().setActive(true);
                    } else {
                        entry.getValue().setActive(false);
                    }
                }

            }
        }
    });

    // addSectionToContainer("Get Started", "setup", new Setup());
    // addSectionToContainer("Get Support", "support", new Support());
    // addSectionToContainer("Buttons", "buttons", new Buttons());
    // addSectionToContainer("Grid System", "gridSystem", new GridSystem());
    // addSectionToContainer("Navigation", "navigation", new Navigation());
    // addSectionToContainer("Hero Unit", "hero", new HeroUnit());
    // addSectionToContainer("Forms", "forms", new Forms());
    // addSectionToContainer("Page Header", "pageheader", new PageHeader());
    // addSectionToContainer("Progress Bar", "progressbar", new
    // ProgressBar());
    // addSectionToContainer("Alerts", "alerts", new Alerts());
    // addSectionToContainer("Pagination", "pagination", new Pagination());
    // addSectionToContainer("Dropdown", "dropdown", new Dropdown());
    // addSectionToContainer("Modal", "modal", new Modal());
    // addSectionToContainer("Datepicker", "datepicker", new Datepicker());
    // addSectionToContainer("Tooltip", "tooltip", new Tooltips());
    // addSectionToContainer("Popover", "popover", new Popovers());
    // addSectionToContainer("Icons", "icons", new Icons());

    RootPanel.get("content").add(this);
    Document doc = Document.get();
    ScriptElement script = doc.createScriptElement();
    script.setSrc("https://apis.google.com/js/plusone.js");
    script.setType("text/javascript");
    script.setLang("javascript");
    doc.getBody().appendChild(script);

    //        github_buttons
    //                .getElement()
    //                .setInnerHTML(
    //                        "<iframe src=\"http://ghbtns.com/github-btn.html?user=gwtbootstrap&repo=gwt-bootstrap&type=watch&count=true\"\n"
    //                                + "                                allowtransparency=\"true\" frameborder=\"0\" scrolling=\"0\" width=\"110px\" height=\"20px\"></iframe>"
    //                                + "<br /><iframe src=\"http://ghbtns.com/github-btn.html?user=gwtbootstrap&repo=gwt-bootstrap&type=fork&count=true\"\n"
    //                                + "  allowtransparency=\"true\" frameborder=\"0\" scrolling=\"0\" width=\"95px\" height=\"20px\"></iframe>");

    JavaScriptInjector.inject(
            "!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"//platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");");
    JavaScriptInjector.inject(
            "(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));");

    String token = History.getToken();
    if (!token.equals("")) {
        History.newItem("");
        History.newItem(token);
    }
}

From source file:com.google.appinventor.client.jsonp.JsonpConnection.java

License:Open Source License

/**
 * Sends a JSONP request by embedding a SCRIPT tag into the document.
 *
 * @param id the id used for the script tag and to identify the callback
 * @param request the request to be made
 * @param parameters the parameters for the request
 * @param function a function that transforms the response into the type
 *        that the callback needs/*from   w  w  w. j a v  a2  s  .  com*/
 * @param callback the callback that should be called with the transformed
 *        response
 */
private <T> void sendJsonpRequest(String id, String request, Map<String, Object> parameters,
        final Function<String, ? extends T> function, final AsyncCallback<T> callback) {
    Preconditions.checkNotNull(id);

    // Prepare an intermediate callback that converts the String result to T.
    if (callback != null) {
        Preconditions.checkNotNull(function);
        CALLBACKS.put(id, new AsyncCallback<String>() {
            @Override
            public void onSuccess(String jsonResult) {
                T result;
                try {
                    result = function.apply(jsonResult);
                } catch (RuntimeException e) {
                    callback.onFailure(e);
                    return;
                }
                callback.onSuccess(result);
            }

            @Override
            public void onFailure(Throwable caught) {
                callback.onFailure(caught);
            }
        });
    }

    // Insert a script tag into the document.
    Document document = Document.get();
    ScriptElement script = document.createScriptElement();
    String uri = makeURI(request, parameters, id);
    script.setSrc(uri);
    script.setId(id);
    Element bodyElement = document.getElementsByTagName("body").getItem(0);
    Element previous = document.getElementById(id);
    if (previous != null) {
        bodyElement.replaceChild(script, previous);
    } else {
        bodyElement.appendChild(script);
    }
}

From source file:com.google.gwt.sample.vanfood.client.TwitterPopup.java

public TwitterPopup(Vendor vendor) {

    super(true);//w w  w . ja v a 2  s  . c  om
    this.setStyleName("PopupPanel");

    int left = (Window.getClientWidth()) / 3;
    int top = (Window.getClientHeight()) / 2;
    this.setPopupPosition(left, top);

    String vendorName = null;
    try {
        vendorName = vendor.getName();
    } catch (Exception e) {

    }
    if ((vendorName == null) || (vendorName.equalsIgnoreCase("Name not available")))
        vendorName = "a street vendor";
    Label desc = new Label("You've added " + vendorName + " to your favourites list." + "\n"
            + "(Click outside the box to close)");
    mainPanel.add(desc);

    String url = "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-text=\"I just added "
            + vendorName + " to my list of favourite street vendors\" target=\"_blank\">Tweet</a>";
    HTML html = new HTML(url);

    mainPanel.add(html);

    Document doc = Document.get();
    ScriptElement script = doc.createScriptElement();
    script.setSrc("http://platform.twitter.com/widgets.js");
    script.setType("text/javascript");
    script.setLang("javascript");
    doc.getBody().appendChild(script);

    this.add(mainPanel);

}

From source file:com.googlecode.gwt.charts.client.ajaxloader.AjaxLoader.java

License:Apache License

/**
 * Adds a script element to the DOM that loads the Ajax API Loader main
 * script "jsapi"./*from  w  w w. j a  v a 2s . c  o  m*/
 * 
 * @param apiKey
 *            Optional API key value (pass null to omit the key). See
 *            http://code.google.com/apis/ajaxsearch/signup.html
 * @returns <code>true</code> if the API has already been loaded. Otherwise,
 *          returns <code>false</code>, meaning that the application should
 *          wait for a callback.
 */
private static boolean injectJsapi(String apiKey, String hostname) {
    if (alreadyInjected) {
        return true;
    }
    boolean alreadyLoaded = nativeCreateCallback();
    alreadyInjected = true;
    if (alreadyLoaded) {
        return true;
    }
    Document doc = Document.get();
    String key = (apiKey == null) ? "" : ("key=" + apiKey + "&");
    hostname = (hostname == null) ? "www.google.com" : hostname;
    String src = getProtocol() + "//" + hostname + "/jsapi?" + key + "callback=__gwt_AjaxLoader_onLoad";
    ScriptElement script = doc.createScriptElement();
    script.setSrc(src);
    script.setType("text/javascript");
    doc.getBody().appendChild(script);
    return false;
}

From source file:com.googlecode.gwt.charts.client.apiloader.ApiLoader.java

License:Apache License

/**
 * Adds a script element to the DOM that loads the API Loader main script "jsapi".
 * /*  ww w.ja  va 2s  . com*/
 * @param apiKey
 *        Optional API key value (pass null to omit the key). See
 *        http://code.google.com/apis/ajaxsearch/signup.html
 * @returns <code>true</code> if the API has already been loaded. Otherwise,
 *          returns <code>false</code>, meaning that the application should
 *          wait for a callback.
 */
private boolean injectJsApi(String apiKey) {
    if (alreadyInjected) {
        return true;
    }
    boolean alreadyLoaded = createCallback(this);
    alreadyInjected = true;
    if (alreadyLoaded) {
        return true;
    }
    Document doc = Document.get();
    String key = (apiKey == null) ? "" : ("key=" + apiKey + "&");
    String protocol = (Window.Location.getProtocol().equals("https:")) ? "https:" : "http:";
    String src = protocol + "//" + HOSTNAME + "/jsapi?" + key + "callback=__gwt_charts_AjaxLoader_onLoad";
    ScriptElement script = doc.createScriptElement();
    script.setSrc(src);
    script.setType("text/javascript");
    doc.getBody().appendChild(script);
    return false;
}

From source file:com.nnm.emenu.client.ultis.CustomApiLoader.java

License:Apache License

/**
 * Adds a script element to the DOM that loads the API Loader main script
 * "jsapi"./*from ww w . j  a  va 2  s  .  com*/
 * 
 * @param apiKey
 *            Optional API key value (pass null to omit the key). See
 *            http://code.google.com/apis/ajaxsearch/signup.html
 * @returns <code>true</code> if the API has already been loaded. Otherwise,
 *          returns <code>false</code>, meaning that the application should
 *          wait for a callback.
 */
private boolean injectJsApi(String apiKey) {
    System.out.println("Create Custom ApiLoader2");
    if (alreadyInjected) {
        System.out.println("Create Custom ApiLoader3");
        return true;
    }
    boolean alreadyLoaded = createCallback(this);
    System.out.println("Create Custom ApiLoader4");
    alreadyInjected = true;
    if (alreadyLoaded) {
        System.out.println("Create Custom ApiLoader5");
        return true;
    }
    Document doc = Document.get();
    String key = (apiKey == null) ? "" : ("key=" + apiKey + "&");
    String protocol = (Window.Location.getProtocol().equals("https:")) ? "https:" : "http:";
    String src = protocol + "//" + HOSTNAME + "/jsapi?" + key + "callback=__gwt_charts_AjaxLoader_onLoad";
    // String src = HOSTNAME + "/__gwt_charts_AjaxLoader_onLoad.js";
    System.out.println("ApiLoader SRC : " + src);
    ScriptElement script = doc.createScriptElement();
    script.setSrc(src);
    script.setType("text/javascript");
    doc.getBody().appendChild(script);
    return false;
}

From source file:com.thinqq.qsports.client.wireframe.QSportsWireFrameImpl.java

License:Open Source License

public QSportsWireFrameImpl(ClientFactory clientFactory) {
    this.clientFactory = clientFactory;
    style.ensureInjected();// w w  w. j  av  a2 s . co  m
    initWidget(binder.createAndBindUi(this));
    addHandlers();
    HTML tweetContent = new HTML(
            "<a class=\"twitter-timeline\"  href=\"https://twitter.com/CricketQSports/cricketq\"  data-widget-id=\"368013046121115648\">Tweets from @CricketQSports/cricketq</a> "
                    + "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\"://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>");
    //addTeams();
    tweet.add(tweetContent);
    Document doc = Document.get();
    ScriptElement script = doc.createScriptElement();
    script.setSrc("http://platform.twitter.com/widgets.js");
    script.setType("text/javascript");
    script.setLang("javascript");
    doc.getBody().appendChild(script);

    // TO TEST

    SafeHtml homeImage = SafeHtmlUtils.fromTrustedString("<img src='/images/icon-home.png'/>");
    SafeHtml addMatchImage = SafeHtmlUtils.fromTrustedString("<img src='/images/icon-batsman.png'/>");

    SafeHtml peopleImage = SafeHtmlUtils.fromTrustedString("<img src='/images/icon-people.png'/>");
    //       MenuBar home = new MenuBar(true);
    Command homeCommand = new Command() {

        @Override
        public void execute() {
            Window.alert("fire the view request event to show the home view");

        }
    };

    Command peopleCommand = new Command() {

        @Override
        public void execute() {
            // TODO Navigate to people tab.

        }
    };
    MenuBar addMatch = new MenuBar(true);

    testMenu.addItem(homeImage, homeCommand);
    testMenu.addItem(addMatchImage, addMatch);
    testMenu.addItem(peopleImage, peopleCommand);

    SafeHtml createMatchText = SafeHtmlUtils.fromSafeConstant("New Match");
    SafeHtml createTeamText = SafeHtmlUtils.fromSafeConstant("New Team");
    createMatch = new MenuItem(createMatchText);
    createMatch = new MenuItem(createMatchText);
    createMatch.addStyleName("fancybox");
    createTeam = new MenuItem(createTeamText);
    addMatch.addItem(createMatch);
    addMatch.addItem(createTeam);

    createTeam.setCommand(new Command() {

        @Override
        public void execute() {
            Genie.getEventmanager().fireEvent(new CreateNewTeamEvent());

        }
    });

    createMatch.setCommand(new Command() {

        @Override
        public void execute() {
            Genie.getEventmanager().fireEvent(new CreateNewMatchEvent());

        }
    });
}

From source file:edu.caltech.ipac.firefly.ui.JSLoad.java

public JSLoad(Loaded loaded, String... url) {
    _callbackList.add(loaded);//w  w w.  jav a 2 s  .c om
    Document document = Document.get();

    for (String u : url) {
        ScriptElement script = document.createScriptElement();
        script.setSrc(u);
        script.setType("text/javascript");
        _map.put(script, false);
        if (BrowserUtil.isIE() && BrowserUtil.getMajorVersion() < 9) {
            addIEListener(this, script, loaded);
        } else {
            addListener(this, script, loaded);
        }
        document.getBody().appendChild(script);
    }

}