/** Return true if the element is listening for the given eventBit or eventName. */ public boolean hasHandlers(int eventBits, String eventName) { for (int i = 0, j = elementEvents.length(); i < j; i++) { BindFunction function = elementEvents.get(i); if (function.hasEventType(eventBits) || function.isTypeOf(eventName)) { return true; } } return false; }
/** Dispatch an event in this element but changing the type, it's useful for special events. */ public void dispatchEvent(Event event, String eventName) { int etype = Event.getTypeInt(eventName); Object[] handlerData = $(element).data(EVENT_DATA); for (int i = 0, l = elementEvents.length(); i < l; i++) { BindFunction listener = elementEvents.get(i); String namespace = JsUtils.prop(event, "namespace"); boolean matchEV = listener != null && (listener.hasEventType(etype) || listener.isTypeOf(eventName)); boolean matchNS = matchEV && (isNullOrEmpty(namespace) || listener.nameSpace.equals(namespace)); if (matchEV && matchNS) { if (!listener.fire(event, handlerData)) { event.stopPropagation(); event.preventDefault(); } } } }
public void unbind(int eventbits, String namespace, String eventName, Function f) { JsObjectArray<BindFunction> newList = JsObjectArray.createArray().cast(); for (int i = 0; i < elementEvents.length(); i++) { BindFunction listener = elementEvents.get(i); boolean matchNS = isNullOrEmpty(namespace) || listener.nameSpace.equals(namespace); boolean matchEV = eventbits <= 0 || listener.hasEventType(eventbits); boolean matchEVN = matchEV || listener.isTypeOf(eventName); boolean matchFC = f == null || listener.isEquals(f); if (matchNS && matchEV && matchEVN && matchFC) { int currentEventbits = listener.unsink(eventbits); if (currentEventbits == 0) { // the BindFunction doesn't listen anymore on any events continue; } } newList.add(listener); } elementEvents = newList; }
@Override public boolean fire(Event event, Object[] eventData) { if (isEmpty()) { return true; } // first element where the event was fired Element eventTarget = getEventTarget(event); // last element where the event was dispatched on Element liveContextElement = getCurrentEventTarget(event); if (eventTarget == null || liveContextElement == null) { return true; } // Compute the live selectors which respond to this event type List<String> validSelectors = new ArrayList<String>(); for (String cssSelector : bindFunctionBySelector.keys()) { JsObjectArray<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector); for (int i = 0; bindFunctions != null && i < bindFunctions.length(); i++) { BindFunction f = bindFunctions.get(i); if (f.hasEventType(event.getTypeInt()) || f.isTypeOf(event.getType())) { validSelectors.add(cssSelector); break; } } } // Create a structure of elements which matches the selectors JsNamedArray<NodeList<Element>> realCurrentTargetBySelector = $(eventTarget).closest(validSelectors.toArray(new String[0]), liveContextElement); // nothing matches the selectors if (realCurrentTargetBySelector.length() == 0) { return true; } Element stopElement = null; GqEvent gqEvent = GqEvent.create(event); for (String cssSelector : realCurrentTargetBySelector.keys()) { JsObjectArray<BindFunction> bindFunctions = bindFunctionBySelector.get(cssSelector); for (int i = 0; bindFunctions != null && i < bindFunctions.length(); i++) { BindFunction f = bindFunctions.get(i); if (f.hasEventType(event.getTypeInt()) || f.isTypeOf(event.getType())) { NodeList<Element> n = realCurrentTargetBySelector.get(cssSelector); for (int j = 0; n != null && j < n.getLength(); j++) { Element element = n.getItem(j); // When an event fired in an element stops bubbling we have to fire also all other // handlers for this element bound to this element if (stopElement == null || element.equals(stopElement)) { gqEvent.setCurrentElementTarget(element); // data eventData = $(element).data(EVENT_DATA); if (!f.fire(gqEvent, eventData)) { stopElement = element; } } } } } } // trick to reset the right currentTarget on the original event on ie gqEvent.setCurrentElementTarget(liveContextElement); return stopElement == null; }