From: Manolo Carrasco Date: Mon, 22 Dec 2014 11:17:16 +0000 (+0100) Subject: Merge branch 'master' into mcm_special_events X-Git-Tag: gwtquery-project-1.4.3~4^2~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2a94310d0aea8b05b4cbc99fffc35735992dfc08;p=gwtquery.git Merge branch 'master' into mcm_special_events --- 2a94310d0aea8b05b4cbc99fffc35735992dfc08 diff --cc gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java index d2e4550c,5f3cf531..d8b80e55 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/EventsListener.java @@@ -26,7 -24,6 +26,7 @@@ import com.google.gwt.query.client.js.J import com.google.gwt.query.client.js.JsNamedArray; import com.google.gwt.query.client.js.JsObjectArray; import com.google.gwt.query.client.js.JsUtils; - import com.google.gwt.query.client.plugins.events.SpecialEvent.AbstractSpecialEvent; ++import com.google.gwt.query.client.plugins.events.SpecialEvent.DefaultSpecialEvent; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.EventListener; @@@ -50,63 -48,66 +50,47 @@@ public class EventsListener implements /** * Used for simulating mouseenter and mouseleave events */ - private static class MouseSpecialEvent extends AbstractSpecialEvent { - public static class MouseSpecialEvent implements SpecialEvent { - - private String originalType; - private String delegateType; - - public MouseSpecialEvent(String originalType, String delegateType) { - this.originalType = originalType; - this.delegateType = delegateType; - } - - public String getDelegateType() { - return delegateType; - } - - public String getOriginalType() { - return originalType; - } - - public Function createDelegateHandler(Function originalHandler) { - return new SpecialMouseEventHandler(originalHandler); ++ private static class MouseSpecialEvent extends DefaultSpecialEvent { + public MouseSpecialEvent(final String type, String delegateType) { + super(type, delegateType); + handler = new Function() { + public boolean f(Event e, Object... arg) { + EventTarget eventTarget = e.getCurrentEventTarget(); + Element target = eventTarget != null ? eventTarget. cast() : null; + + EventTarget relatedEventTarget = e.getRelatedEventTarget(); + Element related = relatedEventTarget != null ? relatedEventTarget. cast() : null; + + if (related == null || (related != target && !GQuery.contains(target, related))) { + getInstance(target).dispatchEvent(e, type); + } + return true; + }; + }; } } - /** - * Used for simulating mouseenter and mouseleave events - */ - private static class FocusSpecialEvent extends AbstractSpecialEvent { - public FocusSpecialEvent(final String type, String delegateType) { - super(type, delegateType); - handler = new Function() { - public boolean f(Event e, Object... arg) { - setEvent(e); - getInstance(getElement()).dispatchEvent(e, type); - return true; - }; - }; - } - private interface HandlerWrapper { - Function getOriginalHandler(); -- } - private static class SpecialMouseEventHandler extends Function implements HandlerWrapper { - - private Function delegateHandler; - - public SpecialMouseEventHandler(Function originalHandler) { - this.delegateHandler = originalHandler; - } - - @Override - public boolean f(Event e, Object... data) { - EventTarget eventTarget = e.getCurrentEventTarget(); - Element target = eventTarget != null ? eventTarget. cast() : null; - - EventTarget relatedEventTarget = e.getRelatedEventTarget(); - Element related = relatedEventTarget != null ? relatedEventTarget. cast() : null; -- - // For mousenter/leave call the handler if related is outside the target. - if (related == null || (related != target && !GQuery.contains(target, related))) { - return delegateHandler != null ? delegateHandler.f(e, data) : false; + /** + * Utility class to split a list of events with or without namespaces + */ + private static class EvPart { + String nameSpace; + String eventName; + public EvPart(String n, String e) { + nameSpace = n; + eventName = e; + } + + static List split(String events) { + List ret = new ArrayList(); + String[] parts = events.split("[\\s,]+"); + for (String event : parts) { + String[] tmp = event.split("\\.", 2); + String eventName = tmp[0]; + String nameSpace = tmp.length > 1 ? tmp[1] : null; + ret.add(new EvPart(nameSpace, eventName)); } - - return false; - } - - public Function getOriginalHandler() { - return delegateHandler; + return ret; } } @@@ -370,18 -378,13 +354,18 @@@ public static String MOUSEENTER = "mouseenter"; public static String MOUSELEAVE = "mouseleave"; + public static String FOCUSIN = "focusin"; + public static String FOCUSOUT = "focusout"; - public static JsMap special; + public static HashMap special; static { - special = JsMap.create(); + // Register some special events which already exist in jQuery + special = new HashMap(); special.put(MOUSEENTER, new MouseSpecialEvent(MOUSEENTER, "mouseover")); special.put(MOUSELEAVE, new MouseSpecialEvent(MOUSELEAVE, "mouseout")); - special.put(FOCUSIN, new FocusSpecialEvent(FOCUSIN, "focus")); - special.put(FOCUSOUT, new FocusSpecialEvent(FOCUSOUT, "blur")); ++ special.put(FOCUSIN, new DefaultSpecialEvent(FOCUSIN, "focus")); ++ special.put(FOCUSOUT, new DefaultSpecialEvent(FOCUSOUT, "blur")); } public static void clean(Element e) { diff --cc gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/SpecialEvent.java index cda229c0,00000000..cc4020b9 mode 100644,000000..100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/SpecialEvent.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/events/SpecialEvent.java @@@ -1,106 -1,0 +1,115 @@@ +/* + * Copyright 2014, 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.events; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.query.client.Function; +import com.google.gwt.user.client.Event; + +/** + * Interface used to register special events. + * + * Use EventsListeners.special.add( + */ +public interface SpecialEvent { + + /** - * Abstract implementation of SpecialEvents ++ * Default implementation of SpecialEvents for simple cases like ++ * creating event aliases. Extend this for creating more complex ++ * cases. + */ - public static abstract class AbstractSpecialEvent implements SpecialEvent { ++ public static class DefaultSpecialEvent implements SpecialEvent { + protected final String delegateType; + protected final String type; - protected Function handler = null; + - public AbstractSpecialEvent(String type, String delegateType) { ++ protected Function handler = new Function() { ++ public boolean f(Event e, Object... arg) { ++ setEvent(e); ++ EventsListener.getInstance(getElement()).dispatchEvent(e, type); ++ return true; ++ }; ++ }; ++ ++ public DefaultSpecialEvent(String type, String delegateType) { + this.type = type; + this.delegateType = delegateType; + } + + protected EventsListener listener(Element e) { + return EventsListener.getInstance(e); + } + + @Override + public void add(Element e, String eventType, String nameSpace, Object data, Function f) { - // Nothing to do, let gQuery use default eentEvents mechanism ++ // Nothing to do, let gQuery use default events mechanism + } + + @Override + public boolean hasHandlers(Element e) { + return listener(e).hasHandlers(Event.getTypeInt(type), type); + } + + @Override + public void remove(Element e, String eventType, String nameSpace, Function f) { - // Nothing to do, let gQuery use default eentEvents mechanism ++ // Nothing to do, let gQuery use default events mechanism + } + + @Override + public boolean setup(Element e) { + if (!hasHandlers(e)) { - listener(e).bind(Event.getTypeInt(delegateType), null, delegateType, null, handler, -1); ++ listener(e).bind(delegateType, null, handler); + } + return false; + } + + @Override + public boolean tearDown(Element e) { + if (!hasHandlers(e)) { - listener(e).unbind(Event.getTypeInt(delegateType), null, delegateType, handler); ++ listener(e).unbind(delegateType, handler); + } + return false; + } + } + + /** + * For each bind call the add function is called. + */ + void add(Element e, String eventType, String nameSpace, Object data, Function f); + + /** + * Return true if there are handlers bound to this special event. + */ + boolean hasHandlers(Element e); + + /** + * For each unbind call the remove function is called. + */ + void remove(Element e, String eventType, String nameSpace, Function f); + + /** + * When the first event handler is bound for an EventsListener gQuery executes the setup function. + * + * If the method returns false means that gQuery has to run the default bind for the event before + * calling add. + */ + boolean setup(Element e); + + /** + * The last unbind call triggers the tearDown method. + * + * If the method returns false means that gQuery has to run the default unbind for the event + * before calling remove. + */ + boolean tearDown(Element e); +}