este-library-oldschool
Version:
Library for github.com/steida/este.git
161 lines (150 loc) • 4.53 kB
JavaScript
// Generated by github.com/steida/coffee2closure 900.1.18
/**
@fileoverview Another special class for event delegation. You probably
don't need it.
```coffee
handler = new este.events.MatchedHandler el, [
id: 123
container: '.pw1001'
child: '.highlightEbook'
link: 'a'
, ..
]
goog.events.listen handler, 'click', (e) ->
* e.id == 123
* e.childIndex == 1
```
*/
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.MatchedHandler');
goog.provide('este.events.MatchedHandler.create');
goog.provide('este.events.MatchedHandler.Matcher');
goog.require('goog.events.EventTarget');
goog.require('este.dom');
/**
@param {Element} element
@param {Array.<este.events.MatchedHandler.Matcher>} matchers
@param {Function} getChildIndex
@param {string=} opt_eventType
@constructor
@extends {goog.events.EventTarget}
*/
este.events.MatchedHandler = function(element1, matchers1, getChildIndex, opt_eventType) {
this.element = element1;
this.matchers = matchers1;
this.getChildIndex = getChildIndex;
este.events.MatchedHandler.superClass_.constructor.call(this);
this.listenKey_ = goog.events.listen(this.element, opt_eventType != null ? opt_eventType : 'click', this);
return;
}
extend(este.events.MatchedHandler, superClass);
/**
@param {Element} element
@param {Array.<este.events.MatchedHandler.Matcher>} matchers
@return {este.events.MatchedHandler}
*/
este.events.MatchedHandler.create = function(element, matchers) {
return new este.events.MatchedHandler(element, matchers, este.events.MatchedHandler.getChildIndex);
};
/**
Compute child index, no CSS selector engine, solid and fast.
@param {Element} container
@param {Element} child
@param {string} childMatcher
@return {number}
*/
este.events.MatchedHandler.getChildIndex = function(container, child, childMatcher) {
var i, index, item, len, ref;
index = 0;
ref = goog.array.toArray(container.getElementsByTagName('*'));
for (i = 0, len = ref.length; i < len; i++) {
item = ref[i];
if (este.dom.match(item, childMatcher)) {
if (item === child) {
return index;
}
index++;
}
}
return -1;
};
/**
@type {Element}
*/
este.events.MatchedHandler.prototype.element = null;
/**
@type {Array.<este.events.MatchedHandler.Matcher>}
*/
este.events.MatchedHandler.prototype.matchers = null;
/**
@type {Function}
*/
este.events.MatchedHandler.prototype.getChildIndex = null;
/**
@type {?number|goog.events.ListenableKey}
@private
*/
este.events.MatchedHandler.prototype.listenKey_ = null;
/**
@param {goog.events.BrowserEvent} e
*/
este.events.MatchedHandler.prototype.handleEvent = function(e) {
var ancestor, ancestors, child, childIndex, container, i, index, j, len, len1, matcher, matchers, ref, target;
target = e.target;
ancestors = este.dom.getAncestors(target, true, true);
ref = this.matchers;
for (i = 0, len = ref.length; i < len; i++) {
matcher = ref[i];
matchers = [matcher['link'], matcher['child'], matcher['container']];
container = null;
child = null;
index = 0;
for (j = 0, len1 = ancestors.length; j < len1; j++) {
ancestor = ancestors[j];
if (container) {
break;
}
if (!este.dom.match(ancestor, matchers[index])) {
continue;
}
switch (index) {
case 1:
child = ancestor;
break;
case 2:
container = ancestor;
}
index++;
}
if (!container) {
continue;
}
childIndex = this.getChildIndex(container, child, matcher['child']);
this.dispatchEvent({
target: target,
type: e.type,
id: matcher['id'],
childIndex: childIndex
});
return;
}
ancestors.reverse();
return this.dispatchEvent({
target: target,
type: e.type,
domPath: este.dom.getDomPath(ancestors)
});
};
/**
@override
*/
este.events.MatchedHandler.prototype.disposeInternal = function() {
este.events.MatchedHandler.superClass_.disposeInternal.call(this);
goog.events.unlistenByKey(this.listenKey_);
delete this.listenKey_;
};
/**
@typedef {{id: string, container: string, child: string, link: string}}
*/
este.events.MatchedHandler.Matcher;