este-library-oldschool
Version:
Library for github.com/steida/este.git
130 lines (122 loc) • 3.64 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview Simple and useful event delegation. This is special low level
component. For general usage este.ui.Component is preferred.
@see /demos/events/delegation.html
*/
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.superClass_ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
goog.provide('este.events.Delegation');
goog.provide('este.events.Delegation.create');
goog.require('este.Base');
goog.require('goog.dom');
goog.require('goog.userAgent');
/**
@param {Element} element
@param {Array.<string>|string} types
@param {boolean=} forceIE
@constructor
@extends {este.Base}
*/
este.events.Delegation = function(element1, types, forceIE) {
var i, isIe, len, type;
this.element = element1;
if (typeof types === 'string') {
types = [types];
}
for (i = 0, len = types.length; i < len; i++) {
type = types[i];
if (type === 'focus' || type === 'blur') {
isIe = forceIE || goog.userAgent.IE;
if (isIe) {
if (type === 'focus') {
type = 'focusin';
}
if (type === 'blur') {
type = 'focusout';
}
}
this.on(this.element, type, this.onElementType, !isIe);
} else {
this.on(this.element, type, this.onElementType);
}
}
este.events.Delegation.superClass_.constructor.call(this);
}
extend(este.events.Delegation, superClass);
/**
@param {Element} element
@param {Array.<string>|string} eventTypes
@param {function(Node): boolean=} targetFilter
@param {function(Node): boolean=} targetParentFilter
@return {este.events.Delegation}
*/
este.events.Delegation.create = function(element, eventTypes, targetFilter, targetParentFilter) {
var delegation;
delegation = new este.events.Delegation(element, eventTypes);
if (targetFilter) {
delegation.targetFilter = targetFilter;
}
if (targetParentFilter) {
delegation.targetParentFilter = targetParentFilter;
}
return delegation;
};
/**
@type {Element}
@protected
*/
este.events.Delegation.prototype.element = null;
/**
@type {function(Node): boolean}
*/
este.events.Delegation.prototype.targetFilter = function(node) {
return true;
};
/**
@type {function(Node): boolean}
*/
este.events.Delegation.prototype.targetParentFilter = function(node) {
return true;
};
/**
@param {goog.events.BrowserEvent} e
@protected
*/
este.events.Delegation.prototype.onElementType = function(e) {
if (!this.matchFilter(e)) {
return;
}
return this.dispatchEvent(e);
};
/**
@param {goog.events.BrowserEvent} e
@return {boolean} True for match
@protected
*/
este.events.Delegation.prototype.matchFilter = function(e) {
var element, ref, target, targetMatched, targetParentMatched;
targetMatched = false;
targetParentMatched = null;
element = e.target;
target = null;
while (goog.dom.isElement(element)) {
if (!targetMatched) {
targetMatched = this.targetFilter(element);
target = element;
} else if (!targetParentMatched) {
targetParentMatched = this.targetParentFilter(element);
} else {
break;
}
element = element.parentNode;
}
if (!targetMatched || targetParentMatched === false) {
return false;
}
e.target = target;
if ((ref = e.type) === 'mouseover' || ref === 'mouseout') {
return !e.relatedTarget || !goog.dom.contains(target, e.relatedTarget);
}
return true;
};