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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2000-2016 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<>();
  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(
  49. (JavaScriptCallbackRpc) (String name, JsonArray arguments) -> {
  50. JavaScriptFunction function = functions.get(name);
  51. // TODO handle situation if name is not registered
  52. try {
  53. function.call(arguments);
  54. } catch (JsonException e) {
  55. throw new IllegalArgumentException(e);
  56. }
  57. });
  58. }
  59. @Override
  60. protected JavaScriptManagerState getState() {
  61. return (JavaScriptManagerState) super.getState();
  62. }
  63. @Override
  64. protected JavaScriptManagerState getState(boolean markAsDirty) {
  65. return (JavaScriptManagerState) super.getState(markAsDirty);
  66. }
  67. /**
  68. * Add a new function to the global JavaScript namespace (i.e. the window
  69. * object). The <code>call</code> method in the passed
  70. * {@link JavaScriptFunction} object will be invoked with the same
  71. * parameters whenever the JavaScript function is called in the browser.
  72. *
  73. * A function added with the name <code>"myFunction"</code> can thus be
  74. * invoked with the following JavaScript code:
  75. * <code>window.myFunction(argument1, argument2)</code>.
  76. *
  77. * If the name parameter contains dots, simple objects are created on demand
  78. * to allow calling the function using the same name (e.g.
  79. * <code>window.myObject.myFunction</code>).
  80. *
  81. * @param name
  82. * the name that the function should get in the global JavaScript
  83. * namespace.
  84. * @param function
  85. * the JavaScriptFunction that will be invoked if the JavaScript
  86. * function is called.
  87. */
  88. public void addFunction(String name, JavaScriptFunction function) {
  89. functions.put(name, function);
  90. getState().names.add(name);
  91. }
  92. /**
  93. * Removes a JavaScripFunction from the browser's global JavaScript
  94. * namespace.
  95. *
  96. * If the name contains dots and intermediate objects were created by
  97. * {@link #addFunction(String, JavaScriptFunction)}, these objects will not
  98. * be removed by this method.
  99. *
  100. * @param name
  101. * the name of the callback to remove
  102. */
  103. public void removeFunction(String name) {
  104. functions.remove(name);
  105. getState().names.remove(name);
  106. }
  107. /**
  108. * Executes the given JavaScript code in the browser.
  109. *
  110. * @param script
  111. * The JavaScript code to run.
  112. */
  113. public void execute(String script) {
  114. getRpcProxy(ExecuteJavaScriptRpc.class).executeJavaScript(script);
  115. }
  116. /**
  117. * Executes the given JavaScript code in the browser.
  118. *
  119. * @param script
  120. * The JavaScript code to run.
  121. */
  122. public static void eval(String script) {
  123. getCurrent().execute(script);
  124. }
  125. /**
  126. * Get the JavaScript object for the current Page, or null if there is no
  127. * current page.
  128. *
  129. * @see Page#getCurrent()
  130. *
  131. * @return the JavaScript object corresponding to the current Page, or
  132. * <code>null</code> if there is no current page.
  133. */
  134. public static JavaScript getCurrent() {
  135. Page page = Page.getCurrent();
  136. if (page == null) {
  137. return null;
  138. }
  139. return page.getJavaScript();
  140. }
  141. /**
  142. * JavaScript is not designed to be removed.
  143. *
  144. * @throws UnsupportedOperationException
  145. * when invoked
  146. */
  147. @Override
  148. public void remove() {
  149. throw new UnsupportedOperationException(
  150. "JavaScript is not designed to be removed.");
  151. }
  152. }