summaryrefslogtreecommitdiffstats
path: root/vendor
diff options
context:
space:
mode:
Diffstat (limited to 'vendor')
0 files changed, 0 insertions, 0 deletions
bded13ad5129ac03f2c2a32949a21'>LazyEvents.java
blob: 1d84d8a6169ea049885058bb9dc2741395b41a50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright 2011, The gwtquery team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.query.client.plugins;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.FormElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Node;
import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.query.client.js.JsUtils;
import com.google.gwt.query.client.plugins.events.EventsListener;
import com.google.gwt.query.client.plugins.events.EventsListener.SpecialEvent;
import com.google.gwt.query.client.plugins.events.GqEvent;
import com.google.gwt.user.client.Event;
import com.google.gwt.query.client.GQuery.Offset;
import com.google.gwt.query.client.LazyBase;

public interface LazyEvents<T> extends LazyBase<T>{

  /**
   * Binds a set of handlers to a particular Event for each matched element.
   *
   * The event handlers are passed as Functions that you can use to prevent default behavior. To
   * stop both default action and event bubbling, the function event handler has to return false.
   *
   * You can pass an additional Object data to your Function as the second parameter
   *
   */
  LazyEvents<T> bind(int eventbits, Object data, Function... funcs);

  /**
   * Binds a set of handlers to a particular Event for each matched element.
   *
   * The namespace is a way to group events of the same type, making easier unbind specific
   * handlers.
   *
   * The event handlers are passed as Functions that you can use to prevent default behavior. To
   * stop both default action and event bubbling, the function event handler has to return false.
   *
   * You can pass an additional Object data to your Function
   *
   */
  LazyEvents<T> bind(int eventbits, String namespace, Object data, Function... funcs);

  /**
   * Binds a set of handlers to a particular Event for each matched element.
   *
   * The name could contain a namespace which is a way to group events of the same type, making
   * easier unbind specific handlers.
   *
   * The event handlers are passed as Functions that you can use to prevent default behavior. To
   * stop both default action and event bubbling, the function event handler has to return false.
   *
   * You can pass an additional Object data to your Function
   *
   */
  LazyEvents<T> bind(String event, Object data, Function... funcs);

  GQuery die(int eventbits, String nameSpace);

  GQuery die(int eventbits);

  /**
   * Remove an event handlers previously attached using live() The selector used with it must match
   * exactly the selector initially used with live(). if <code>eventName</code> is null, all event
   * handlers corresponding of the GQuery selector will be removed
   */
  GQuery die(String eventName);

  GQuery live(int eventbits, Object data, Function... funcs);

  GQuery live(int eventbits, String nameSpace, Object data, Function... funcs);

  GQuery live(String eventName, Object data, Function... funcs);

  /**
   * Bind an event handler to be fired when the mouse enter an element, or trigger that handler on
   * an element if no functions are provided.
   *
   * The mouseenter event differs from mouseover in the way it handles event bubbling. When
   * mouseover is used on an element having inner element(s), then when the mouse pointer moves
   * hover of the Inner element, the handler would be triggered. This is usually undesirable
   * behavior. The mouseenter event, on the other hand, only triggers its handler when the mouse
   * enters the element it is bound to, not a descendant.
   */
  GQuery mouseenter(Function... f);

  /**
   * Bind an event handler to be fired when the mouse leaves an element, or trigger that handler on
   * an element if no functions are provided.
   *
   * The mouseleave event differs from mouseout in the way it handles event bubbling. When mouseout
   * is used on an element having inner element(s), then when the mouse pointer moves out of the
   * Inner element, the handler would be triggered. This is usually undesirable behavior. The
   * mouseleave event, on the other hand, only triggers its handler when the mouse leaves the
   * element it is bound to, not a descendant.
   */
  GQuery mouseleave(Function... fs);

  /**
   * Binds a handler to a particular Event (like Event.ONCLICK) for each matched element. The
   * handler is executed only once for each element.
   *
   * The event handler is passed as a Function that you can use to prevent default behavior. To stop
   * both default action and event bubbling, the function event handler has to return false.
   *
   * You can pass an additional Object data to your Function as the second parameter
   */
  LazyEvents<T> one(int eventbits, Object data, Function f);

  /**
   * Execute all handlers and behaviors attached to the matched elements for the given event types.
   *
   * Different event types can be passed joining these using the or bit wise operator.
   *
   * For keyboard events you can pass a second parameter which represents the key-code of the pushed
   * key.
   *
   * Example: fire(Event.ONCLICK | Event.ONFOCUS) Example: fire(Event.ONKEYDOWN. 'a');
   */
  LazyEvents<T> trigger(int eventbits, int... keys);

  /**
   * Trigger a html event in all matched elements.
   *
   * @param htmlEvent A string representing the desired html event.
   * @functions a set of function to run if the event is not canceled.
   */
  LazyEvents<T> triggerHtmlEvent(String htmlEvent, Function... functions);

  /**
   * Trigger a html event in all matched elements.
   *
   * @param htmlEvent An string representing the desired html event.
   * @functions a set of function to run if the event is not canceled.
   */
  LazyEvents<T> triggerHtmlEvent(String htmlEvent, Object[] datas, Function... functions);

  /**
   * Removes all handlers, that matches the events bits passed, from each element.
   *
   * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER)
   */
  LazyEvents<T> unbind(int eventbits);

  LazyEvents<T> off();

  /**
   * Removes all handlers, that matches the events bits and the namespace passed, from each element.
   *
   * Example: unbind(Event.ONCLICK | Event.ONMOUSEOVER, "my.namespace")
   */
  LazyEvents<T> unbind(int eventbits, String name, Function f);

  /**
   * Removes all handlers, that matches the event name passed.
   *
   * This name could contain a namespace.
   *
   * Example: unbind("click.my.namespace")
   */
  LazyEvents<T> unbind(String name);

  /**
   * Removes the function passed as parameter from the event list matching the event name passed.
   * This name could contain a namespace.
   *
   * Example: unbind("click.my.namespace", myFunction)
   */
  LazyEvents<T> unbind(String name, Function f);

  LazyEvents<T> undelegate();

}
: arr.getString(idx); } else if (clz.equals(Boolean.class) || clz.isPrimitive() && clz == Boolean.TYPE) { try { ret = obj != null ? obj.getBoolean(attr): arr.getBoolean(idx); } catch (Exception e) { return Boolean.FALSE; } } else if (clz.equals(Byte.class) || clz.equals(Short.class) || clz.equals(Integer.class) || clz.isPrimitive() && (clz == Byte.TYPE || clz == Short.TYPE || clz == Integer.TYPE)) { try { ret = obj != null ? obj.getInt(attr): arr.getInt(idx); } catch (Exception e) { return 0; } } else if (clz.equals(Double.class) || clz.equals(Float.class) || clz.isPrimitive() && (clz == Double.TYPE || clz == Float.TYPE)) { try { ret = obj != null ? obj.getDouble(attr): arr.getDouble(idx); } catch (Exception e) { return .0; } } else if (clz.equals(Long.class) || clz.isPrimitive() && clz == Long.TYPE) { try { ret = obj != null ? obj.getLong(attr): arr.getLong(idx); } catch (Exception e) { return 0l; } } else { ret = obj != null ? obj.get(attr): arr.get(idx); if (ret instanceof JSONObject) { if (clz == Object.class) { ret = jsonFactory.createBinder((JSONObject)ret); } else if (Binder.class.isAssignableFrom(clz) && !clz.isAssignableFrom(ret.getClass())) { ret = jsonFactory.create(clz, (JSONObject)ret); } } } } catch (JSONException e) { } return ret; } private <T> JSONArray listToJsonArray(Object...l) throws Throwable { JSONArray ret = new JSONArray(); for (Object o: l) { setValue(ret, null, null, o, null); } return ret; } private Object setValue(JSONArray arr, JSONObject obj, String attr, Object o, Method method) { try { if (o == null) { return o; } if (o instanceof String) { return obj != null ? obj.put(attr, o) : arr.put(o); } else if (o instanceof Boolean) { return obj != null ? obj.put(attr, o) : arr.put(o); } else if (o instanceof Number) { return obj != null ? obj.put(attr, o) : arr.put(o); } else if (o instanceof Date) { return obj != null ? obj.put(attr, ((Date) o).getTime()) : arr.put(((Date) o).getTime()); } else if (o instanceof Binder) { return obj != null ? obj.put(attr, ((Binder) o).getBound()) : arr.put(((Binder) o).getBound()); } else if (o.getClass().isArray() || o instanceof List) { Object[] arg; if (o.getClass().isArray()) { arg = (Object[])o; } else { arg = ((List<?>)o).toArray(); } JSONArray a = listToJsonArray(arg); return obj != null ? obj.put(attr, a) : arr.put(a); } else { if (!(o instanceof Function)) { System.out.println("Unkown setter object " + attr + " " + o.getClass().getName() + " " + o); } return obj != null ? obj.put(attr, o) : arr.put(o); } } catch (Throwable e) { e.printStackTrace(); } return null; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String mname = method.getName(); Class<?>[] classes = method.getParameterTypes(); int largs = classes.length; Name name = method.getAnnotation(Name.class); String attr = name != null ? name.value() : deCapitalize(mname.replaceFirst("^[gs]et", "")); if ("getFieldNames".equals(mname)) { return JSONObject.getNames(jsonObject); } else if ("as".equals(mname)) { Class<? extends JsonBuilder> clz = (Class<? extends JsonBuilder>)args[0]; return jsonFactory.create(clz, jsonObject); } else if ("getJsonName".equals(mname)) { return JsonBuilderGenerator.classNameToJsonName(getDataBindingClassName(proxy.getClass())); } else if (mname.matches("getProperties|getBound")) { return jsonObject; } else if (largs > 0 && ("parse".equals(mname) || "load".equals(mname))) { jsonObject = new JSONObject(String.valueOf(args[0])); } else if (mname.matches("toString")) { return jsonObject.toString(); } else if (mname.matches("toJsonWithName")) { String jsonName = JsonBuilderGenerator.classNameToJsonName(getDataBindingClassName(proxy.getClass())); return "{\"" + jsonName + "\":"+ jsonObject.toString() + "}"; } else if (mname.matches("toJson")) { return jsonObject.toString() ; } else if ("toQueryString".equals(mname)) { return param(jsonObject); } else if (largs == 1 && mname.equals("get")) { Class<?> ret = method.getReturnType(); attr = String.valueOf(args[0]); return getValue(null, 0, jsonObject, attr, ret, method); } else if (largs == 0 || mname.startsWith("get")) { Class<?> ret = method.getReturnType(); return getValue(null, 0, jsonObject, attr, ret, method); } else if (largs == 2 && mname.equals("set")) { setValue(null, jsonObject, String.valueOf(args[0]), args[1], method); return proxy; } else if (largs == 1 || mname.startsWith("set")) { setValue(null, jsonObject, attr, args[0], method); return proxy; } return null; } private String deCapitalize(String s) { return s != null && s.length() > 0 ? s.substring(0, 1).toLowerCase() + s.substring(1) : s; } private String getDataBindingClassName(Class<?> type) { for (Class<?> c : type.getInterfaces()) { if (c.equals(JsonBuilder.class)) { return type.getName(); } else { return getDataBindingClassName(c); } } return null; } private String param(JSONObject o) { String ret = ""; for (String k : JSONObject.getNames(o)) { ret += ret.isEmpty() ? "" : "&"; JSONObject p = null; JSONArray a = null; String s = null; try { a = o.getJSONArray(k); } catch (Exception e) { } if (a != null) { for (int i = 0, l = a.length(); i < l ; i++) { ret += i > 0 ? "&" : ""; try { p = a.getJSONObject(i); } catch (Exception e) { try { s = String.valueOf(a.get(i)); } catch (Exception d) { } } if (p != null) { ret += k + "[]=" + p.toString(); } else if (s != null){ ret += k + "[]=" + s; } } } else { try { p = o.getJSONObject(k); } catch (Exception e) { try { s = String.valueOf(o.get(k)); } catch (Exception d) { } } if (p != null) { ret += k + "=" + p.toString(); } else if (s != null) { if (!"null".equalsIgnoreCase(s)) { ret += k + "=" + s; } } } } return ret; } } @SuppressWarnings("unchecked") public <T> T create(Class<T> clz, JSONObject jso) { InvocationHandler handler = new JsonBuilderHandler(jso); return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] {clz}, handler); } @SuppressWarnings("unchecked") public <T extends JsonBuilder> T create(Class<T> clz) { InvocationHandler handler = new JsonBuilderHandler(); return (T) Proxy.newProxyInstance(clz.getClassLoader(), new Class[] {clz}, handler); } public Binder createBinder() { InvocationHandler handler = new JsonBuilderHandler(); return (Binder)Proxy.newProxyInstance(Binder.class.getClassLoader(), new Class[] {Binder.class}, handler); } public Binder createBinder(JSONObject jso) { InvocationHandler handler = new JsonBuilderHandler(jso); return (Binder)Proxy.newProxyInstance(Binder.class.getClassLoader(), new Class[] {Binder.class}, handler); } @Override public Binder create(String s) { Binder ret = createBinder(); ret.parse(Properties.wrapPropertiesString(s)); return ret; } @Override public Binder create() { return createBinder(); } }