You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavaScript.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.ui;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import com.vaadin.server.AbstractExtension;
  20. import com.vaadin.server.Page;
  21. import com.vaadin.shared.communication.ServerRpc;
  22. import com.vaadin.shared.extension.javascriptmanager.ExecuteJavaScriptRpc;
  23. import com.vaadin.shared.extension.javascriptmanager.JavaScriptManagerState;
  24. import elemental.json.JsonArray;
  25. import elemental.json.JsonException;
  26. /**
  27. * Provides access to JavaScript functionality in the web browser. To get an
  28. * instance of JavaScript, either use Page.getJavaScript() or
  29. * JavaScript.getCurrent() as a shorthand for getting the JavaScript object
  30. * corresponding to the current Page.
  31. *
  32. * @author Vaadin Ltd
  33. * @since 7.0.0
  34. */
  35. public class JavaScript extends AbstractExtension {
  36. private Map<String, JavaScriptFunction> functions = new HashMap<String, JavaScriptFunction>();
  37. // Can not be defined in client package as this JSONArray is not available
  38. // in GWT
  39. public interface JavaScriptCallbackRpc extends ServerRpc {
  40. public void call(String name, JsonArray arguments);
  41. }
  42. /**
  43. * Creates a new JavaScript object. You should typically not this, but
  44. * instead use the JavaScript object already associated with your Page
  45. * object.
  46. */
  47. public JavaScript() {
  48. registerRpc(new JavaScriptCallbackRpc() {
  49. @Override
  50. public void call(String name, JsonArray arguments) {
  51. JavaScriptFunction function = functions.get(name);
  52. // TODO handle situation if name is not registered
  53. try {
  54. function.call(arguments);
  55. } catch (JsonException e) {
  56. throw new IllegalArgumentException(e);
  57. }
  58. }
  59. });
  60. }
  61. @Override
  62. protected JavaScriptManagerState getState() {
  63. return (JavaScriptManagerState) super.getState();
  64. }
  65. /**
  66. * Add a new function to the global JavaScript namespace (i.e. the window
  67. * object). The <code>call</code> method in the passed
  68. * {@link JavaScriptFunction} object will be invoked with the same
  69. * parameters whenever the JavaScript function is called in the browser.
  70. *
  71. * A function added with the name <code>"myFunction"</code> can thus be
  72. * invoked with the following JavaScript code:
  73. * <code>window.myFunction(argument1, argument2)</code>.
  74. *
  75. * If the name parameter contains dots, simple objects are created on demand
  76. * to allow calling the function using the same name (e.g.
  77. * <code>window.myObject.myFunction</code>).
  78. *
  79. * @param name
  80. * the name that the function should get in the global JavaScript
  81. * namespace.
  82. * @param function
  83. * the JavaScriptFunction that will be invoked if the JavaScript
  84. * function is called.
  85. */
  86. public void addFunction(String name, JavaScriptFunction function) {
  87. functions.put(name, function);
  88. getState().names.add(name);
  89. }
  90. /**
  91. * Removes a JavaScripFunction from the browser's global JavaScript
  92. * namespace.
  93. *
  94. * If the name contains dots and intermediate objects were created by
  95. * {@link #addFunction(String, JavaScriptFunction)}, these objects will not
  96. * be removed by this method.
  97. *
  98. * @param name
  99. * the name of the callback to remove
  100. */
  101. public void removeFunction(String name) {
  102. functions.remove(name);
  103. getState().names.remove(name);
  104. }
  105. /**
  106. * Executes the given JavaScript code in the browser.
  107. *
  108. * @param script
  109. * The JavaScript code to run.
  110. */
  111. public void execute(String script) {
  112. getRpcProxy(ExecuteJavaScriptRpc.class).executeJavaScript(script);
  113. }
  114. /**
  115. * Executes the given JavaScript code in the browser.
  116. *
  117. * @param script
  118. * The JavaScript code to run.
  119. */
  120. public static void eval(String script) {
  121. getCurrent().execute(script);
  122. }
  123. /**
  124. * Get the JavaScript object for the current Page, or null if there is no
  125. * current page.
  126. *
  127. * @see Page#getCurrent()
  128. *
  129. * @return the JavaScript object corresponding to the current Page, or
  130. * <code>null</code> if there is no current page.
  131. */
  132. public static JavaScript getCurrent() {
  133. Page page = Page.getCurrent();
  134. if (page == null) {
  135. return null;
  136. }
  137. return page.getJavaScript();
  138. }
  139. /**
  140. * JavaScript is not designed to be removed.
  141. *
  142. * @throws UnsupportedOperationException
  143. * when invoked
  144. */
  145. @Override
  146. public void remove() {
  147. throw new UnsupportedOperationException(
  148. "JavaScript is not designed to be removed.");
  149. }
  150. }