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

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