예제 #1
0
 private void addFaceBookButton() {
   exportMyFunction();
   panel.setHTML(
       panel.getHTML()
           + "<fb:login-button show-faces=\"false\" width=\"400\" max-rows=\"1\" size=\"large\"></fb:login-button>");
   ScriptElement script = Document.get().createScriptElement();
   script.setSrc("http://localhost:8080/resources/facebook.js");
   Element span = Document.get().getElementsByTagName("div").getItem(0);
   span.appendChild(script);
 }
예제 #2
0
 /**
  * Method injecting native chart.js code into the browser<br>
  * In case code already been injected do nothing
  */
 public static void ensureInjected() { // TODO: do real injection (lazy loading)
   if (injected) return;
   Resources res = GWT.create(Resources.class);
   String source = res.chartJsSource().getText();
   ScriptElement scriptElement = Document.get().createScriptElement();
   scriptElement.setId("_chartjs");
   scriptElement.setInnerText(source);
   Document.get().getBody().appendChild(scriptElement);
   injected = true;
 }
  /**
   * 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
   * @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);
    }
  }
예제 #4
0
 private static ScriptElement createScriptElement() {
   ScriptElement script = Document.get().createScriptElement();
   script.setAttribute("type", "text/javascript");
   return script;
 }
예제 #5
0
 public static void intject(String javaScript) {
   HeadElement head = getHead();
   ScriptElement element = createScriptElement();
   element.setText(javaScript);
   head.appendChild(element);
 }