com.vaadin.ui.JavaScriptFunction Java Examples
The following examples show how to use
com.vaadin.ui.JavaScriptFunction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ChartJs.java From vaadin-chartjs with MIT License | 6 votes |
/** * Add a new item to the menu. * * @param menuTitle * The title to be displayed in the menu. * @param action * The action to be run when the item is clicked. */ public void addMenuEntry(String menuTitle, Runnable action) { if (menuTitle == null || menuTitle.length() == 0) { throw new IllegalArgumentException("menuTitle missing"); } if (action == null) { throw new IllegalArgumentException("action missing"); } // callback ID will be used as key in ChartJsState.menuItems and also as JavaScript function name String callbackId = "ChartJsMenuItem" + nextMenuId.incrementAndGet(); ChartJsState state = getState(); if (state.menuItems == null) { state.menuItems = new HashMap<>(); } state.menuItems.put(callbackId, menuTitle); addFunction(callbackId, new JavaScriptFunction() { @Override public void call(JsonArray arguments) { action.run(); } }); }
Example #2
Source File: BrowserCookie.java From viritin with Apache License 2.0 | 6 votes |
public static void detectCookieValue(String key, final Callback callback) { final String callbackid = "viritincookiecb"+UUID.randomUUID().toString().substring(0,8); JavaScript.getCurrent().addFunction(callbackid, new JavaScriptFunction() { private static final long serialVersionUID = -3426072590182105863L; @Override public void call(JsonArray arguments) { JavaScript.getCurrent().removeFunction(callbackid); if(arguments.length() == 0) { callback.onValueDetected(null); } else { callback.onValueDetected(arguments.getString(0)); } } }); JavaScript.getCurrent().execute(String.format( "var nameEQ = \"%2$s=\";var ca = document.cookie.split(';');for(var i=0;i < ca.length;i++) {var c = ca[i];while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) {%1$s( c.substring(nameEQ.length,c.length)); return;};} %1$s();", callbackid,key )); }
Example #3
Source File: WebJavaScriptComponent.java From cuba with Apache License 2.0 | 5 votes |
@Override public void addFunction(String name, Consumer<JavaScriptCallbackEvent> function) { component.addFunction(name, (JavaScriptFunction) arguments -> { JavaScriptCallbackEvent event = new JavaScriptCallbackEvent(this, arguments); function.accept(event); }); }
Example #4
Source File: ElementImpl.java From serverside-elements with Apache License 2.0 | 5 votes |
@Override public void addEventListener(String eventName, JavaScriptFunction listener, String... arguments) { String argumentBuilder = String.join(",", arguments); eval("e.addEventListener('" + eventName + "', function (event) { $0(" + argumentBuilder + ") })", listener); }
Example #5
Source File: RootImpl.java From serverside-elements with Apache License 2.0 | 5 votes |
void eval(ElementImpl element, String script, Object[] arguments) { // Param values JsonArray params = Json.createArray(); // Array of param indices that should be treated as callbacks JsonArray callbacks = Json.createArray(); for (int i = 0; i < arguments.length; i++) { Object value = arguments[i]; Class<? extends Object> type = value.getClass(); if (JavaScriptFunction.class.isAssignableFrom(type)) { // TODO keep sequence per element instead of "global" int cid = callbackIdSequence++; element.setCallback(cid, (JavaScriptFunction) value); value = Integer.valueOf(cid); type = Integer.class; callbacks.set(callbacks.length(), i); } EncodeResult encodeResult = JsonCodec.encode(value, null, type, null); params.set(i, encodeResult.getEncodedValue()); } addCommand("eval", element, Json.create(script), params, callbacks); }
Example #6
Source File: RootImpl.java From serverside-elements with Apache License 2.0 | 5 votes |
public void handleCallback(JsonArray arguments) { JsonArray attributeChanges = arguments.getArray(1); for (int i = 0; i < attributeChanges.length(); i++) { JsonArray attributeChange = attributeChanges.getArray(i); int id = (int) attributeChange.getNumber(0); String attribute = attributeChange.getString(1); JsonValue value = attributeChange.get(2); NodeImpl target = idToNode.get(Integer.valueOf(id)); if (value.getType() == JsonType.NULL) { target.node.removeAttr(attribute); } else { target.node.attr(attribute, value.asString()); } } JsonArray callbacks = arguments.getArray(0); for (int i = 0; i < callbacks.length(); i++) { JsonArray call = callbacks.getArray(i); int elementId = (int) call.getNumber(0); int cid = (int) call.getNumber(1); JsonArray params = call.getArray(2); ElementImpl element = (ElementImpl) idToNode .get(Integer.valueOf(elementId)); if (element == null) { System.out.println(cid + " detached?"); return; } JavaScriptFunction callback = element.getCallback(cid); callback.call(params); } }
Example #7
Source File: CubaJavaScriptComponent.java From cuba with Apache License 2.0 | 4 votes |
@Override public void addFunction(String functionName, JavaScriptFunction function) { super.addFunction(functionName, function); }
Example #8
Source File: Element.java From serverside-elements with Apache License 2.0 | 4 votes |
public void addEventListener(String eventName, JavaScriptFunction listener, String... arguments);
Example #9
Source File: ElementImpl.java From serverside-elements with Apache License 2.0 | 4 votes |
void setCallback(int cid, JavaScriptFunction callback) { callbacks.put(Integer.valueOf(cid), callback); }
Example #10
Source File: ElementImpl.java From serverside-elements with Apache License 2.0 | 4 votes |
JavaScriptFunction getCallback(int cid) { return callbacks.get(Integer.valueOf(cid)); }
Example #11
Source File: ElementImpl.java From serverside-elements with Apache License 2.0 | 4 votes |
@Override public void addEventListener(EventListener listener) { List<Method> listenerMethods = findInterfaceMethods( listener.getClass()); for (Method method : listenerMethods) { if (method.getDeclaringClass() == Object.class) { // Ignore continue; } String name = method.getName(); if (!name.startsWith("on")) { throw new RuntimeException(method.toString()); } name = name.substring(2).toLowerCase(); if (method.getParameterCount() != 1) { throw new RuntimeException(); } if (method.getReturnType() != void.class) { throw new RuntimeException(); } Map<String, Integer> methodOrder = new HashMap<>(); Class<?> eventType = method.getParameterTypes()[0]; Method[] eventGetters = eventType.getDeclaredMethods(); String[] argumentBuilders = new String[eventGetters.length]; for (int i = 0; i < eventGetters.length; i++) { Method getter = eventGetters[i]; if (getter.getParameterCount() != 0) { throw new RuntimeException(getter.toString()); } String paramName = ElementReflectHelper .getPropertyName(getter.getName()); methodOrder.put(getter.getName(), Integer.valueOf(i)); argumentBuilders[i] = "event." + paramName; } addEventListener(name, new JavaScriptFunction() { @Override public void call(final JsonArray arguments) { InvocationHandler invocationHandler = (proxy, calledMethod, args) -> { if (calledMethod.getDeclaringClass() == Object.class) { // Standard object methods return calledMethod.invoke(proxy, args); } else { String methodName = calledMethod.getName(); int indexOf = methodOrder.get(methodName) .intValue(); return JsonCodec.decodeInternalOrCustomType( calledMethod.getGenericReturnType(), arguments.get(indexOf), null); } }; Object event = Proxy.newProxyInstance( eventType.getClassLoader(), new Class[] { eventType }, invocationHandler); try { method.invoke(listener, event); } catch (Exception e) { throw new RuntimeException(e); } } }, argumentBuilders); } }
Example #12
Source File: JavaScriptUtil.java From usergrid with Apache License 2.0 | 4 votes |
private static void addCallback(String jsCallbackName, JavaScriptFunction jsCallback) { JavaScript.getCurrent().addFunction(jsCallbackName, jsCallback); }
Example #13
Source File: JavaScriptUtil.java From usergrid with Apache License 2.0 | 4 votes |
public static void loadChart(String chart, String jsCallbackName, JavaScriptFunction jsCallback) { execute(chart); addCallback(jsCallbackName, jsCallback); }