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.

JavaScriptManagerConnector.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.client.extensions.javascriptmanager;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import com.google.gwt.core.client.JavaScriptObject;
  20. import com.google.gwt.core.client.JsArray;
  21. import com.vaadin.client.ServerConnector;
  22. import com.vaadin.client.Util;
  23. import com.vaadin.client.communication.JavaScriptMethodInvocation;
  24. import com.vaadin.client.communication.ServerRpcQueue;
  25. import com.vaadin.client.communication.StateChangeEvent;
  26. import com.vaadin.client.extensions.AbstractExtensionConnector;
  27. import com.vaadin.shared.extension.javascriptmanager.ExecuteJavaScriptRpc;
  28. import com.vaadin.shared.extension.javascriptmanager.JavaScriptManagerState;
  29. import com.vaadin.shared.ui.Connect;
  30. import com.vaadin.ui.JavaScript;
  31. @Connect(JavaScript.class)
  32. public class JavaScriptManagerConnector extends AbstractExtensionConnector {
  33. private Set<String> currentNames = new HashSet<>();
  34. @Override
  35. protected void init() {
  36. registerRpc(ExecuteJavaScriptRpc.class, new ExecuteJavaScriptRpc() {
  37. @Override
  38. public void executeJavaScript(String Script) {
  39. eval(Script);
  40. }
  41. });
  42. }
  43. @Override
  44. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  45. super.onStateChanged(stateChangeEvent);
  46. Set<String> newNames = getState().names;
  47. // Current names now only contains orphan callbacks
  48. currentNames.removeAll(newNames);
  49. for (String name : currentNames) {
  50. removeCallback(name);
  51. }
  52. currentNames = new HashSet<>(newNames);
  53. for (String name : newNames) {
  54. addCallback(name);
  55. }
  56. }
  57. // TODO Ensure we don't overwrite anything (important) in $wnd
  58. private native void addCallback(String name)
  59. /*-{
  60. var m = this;
  61. var target = $wnd;
  62. var parts = name.split('.');
  63. for(var i = 0; i < parts.length - 1; i++) {
  64. var part = parts[i];
  65. if (target[part] === undefined) {
  66. target[part] = {};
  67. }
  68. target = target[part];
  69. }
  70. target[parts[parts.length - 1]] = $entry(function() {
  71. //Must make a copy because arguments is an array-like object (not instanceof Array), causing suboptimal JSON encoding
  72. var args = Array.prototype.slice.call(arguments, 0);
  73. m.@com.vaadin.client.extensions.javascriptmanager.JavaScriptManagerConnector::sendRpc(Ljava/lang/String;Lcom/google/gwt/core/client/JsArray;)(name, args);
  74. });
  75. }-*/;
  76. // TODO only remove what we actually added
  77. // TODO We might leave empty objects behind, but there's no good way of
  78. // knowing whether they are unused
  79. private native void removeCallback(String name)
  80. /*-{
  81. var target = $wnd;
  82. var parts = name.split('.');
  83. for(var i = 0; i < parts.length - 1; i++) {
  84. var part = parts[i];
  85. if (target[part] === undefined) {
  86. $wnd.console.log(part,'not defined in',target);
  87. // No longer attached -> nothing more to do
  88. return;
  89. }
  90. target = target[part];
  91. }
  92. $wnd.console.log('removing',parts[parts.length - 1],'from',target);
  93. delete target[parts[parts.length - 1]];
  94. }-*/;
  95. private static native void eval(String script)
  96. /*-{
  97. if(script) {
  98. (new $wnd.Function(script)).apply($wnd);
  99. }
  100. }-*/;
  101. public void sendRpc(String name, JsArray<JavaScriptObject> arguments) {
  102. Object[] parameters = new Object[] { name, Util.jso2json(arguments) };
  103. /*
  104. * Must invoke manually as the RPC interface can't be used in GWT
  105. * because of the JSONArray parameter
  106. */
  107. ServerRpcQueue rpcQueue = ServerRpcQueue.get(getConnection());
  108. rpcQueue.add(new JavaScriptMethodInvocation(getConnectorId(),
  109. "com.vaadin.ui.JavaScript$JavaScriptCallbackRpc", "call",
  110. parameters), false);
  111. rpcQueue.flush();
  112. }
  113. @Override
  114. public JavaScriptManagerState getState() {
  115. return (JavaScriptManagerState) super.getState();
  116. }
  117. @Override
  118. protected void extend(ServerConnector target) {
  119. // Nothing to do there as we are not interested in the connector we
  120. // extend (Page i.e. UI)
  121. }
  122. }