dom-delegate
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -109,3 +109,3 @@ (function(e){if("function"==typeof bootstrap)bootstrap("delegate",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeDelegate=e}else"undefined"!=typeof window?window.Delegate=e():global.Delegate=e()})(function(){var define,ses,bootstrap,module,exports; | ||
Delegate.prototype.captureForType = function(eventType) { | ||
return ['error', 'blur', 'focus'].indexOf(eventType) !== -1; | ||
return ['error', 'blur', 'focus', 'scroll'].indexOf(eventType) !== -1; | ||
}; | ||
@@ -132,3 +132,3 @@ | ||
* | ||
* @param {string} eventType Listen for these events (in a space-separated list) | ||
* @param {string} eventType Listen for these events | ||
* @param {string|undefined} selector Only handle events on elements matching this selector, if undefined match root element | ||
@@ -291,3 +291,3 @@ * @param {function()} handler Handler function - event data passed here will be in event.data | ||
Delegate.prototype.handle = function(event) { | ||
var i, l, root, listener, returned, listenerList, target, /** @const */ EVENTIGNORE = 'ftLabsDelegateIgnore'; | ||
var i, l, type = event.type, root, listener, returned, listenerList = [], target, /** @const */ EVENTIGNORE = 'ftLabsDelegateIgnore'; | ||
@@ -304,4 +304,16 @@ if (event[EVENTIGNORE] === true) { | ||
root = this.rootElement; | ||
listenerList = this.listenerMap[event.eventPhase === Event.CAPTURING_PHASE ? 1 : 0][event.type]; | ||
switch (event.eventPhase) { | ||
case Event.CAPTURING_PHASE: | ||
listenerList = this.listenerMap[1][type]; | ||
break; | ||
case Event.AT_TARGET: | ||
if (this.listenerMap[0] && this.listenerMap[0][type]) listenerList = listenerList.concat(this.listenerMap[0][type]); | ||
if (this.listenerMap[1] && this.listenerMap[1][type]) listenerList = listenerList.concat(this.listenerMap[1][type]); | ||
break; | ||
case Event.BUBBLING_PHASE: | ||
listenerList = this.listenerMap[0][type]; | ||
break; | ||
} | ||
// Need to continuously check | ||
@@ -409,2 +421,3 @@ // that the specific list is | ||
Delegate.prototype.matchesRoot = function(selector, element) { | ||
if (this.rootElement === window) return element === document; | ||
return this.rootElement === element; | ||
@@ -411,0 +424,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
(function(e){if("function"==typeof bootstrap)bootstrap("delegate",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeDelegate=e}else"undefined"!=typeof window?window.Delegate=e():global.Delegate=e()})(function(){return function(e,t,r){function n(r,i){if(!t[r]){if(!e[r]){var s="function"==typeof require&&require;if(!i&&s)return s(r,!0);if(o)return o(r,!0);throw Error("Cannot find module '"+r+"'")}var a=t[r]={exports:{}};e[r][0].call(a.exports,function(t){var o=e[r][1][t];return n(o?o:t)},a,a.exports)}return t[r].exports}for(var o="function"==typeof require&&require,i=0;r.length>i;i++)n(r[i]);return n}({1:[function(e,t){"use strict";function r(e){this.listenerMap=[{},{}],e&&this.root(e),this.handle=r.prototype.handle.bind(this)}t.exports=r,r.tagsCaseSensitive=null,r.prototype.root=function(e){var t,r=this.listenerMap;if("string"==typeof e&&(e=document.querySelector(e)),this.rootElement){for(t in r[1])r[1].hasOwnProperty(t)&&this.rootElement.removeEventListener(t,this.handle,!0);for(t in r[0])r[0].hasOwnProperty(t)&&this.rootElement.removeEventListener(t,this.handle,!1)}if(!e||!e.addEventListener)return this.rootElement&&delete this.rootElement,this;this.rootElement=e;for(t in r[1])r[1].hasOwnProperty(t)&&this.rootElement.addEventListener(t,this.handle,!0);for(t in r[0])r[0].hasOwnProperty(t)&&this.rootElement.addEventListener(t,this.handle,!1);return this},r.prototype.captureForType=function(e){return-1!==["error","blur","focus"].indexOf(e)},r.prototype.on=function(e,t,n,o){var i,s,a,h;if(!e)throw new TypeError("Invalid event type: "+e);if("function"==typeof t&&(o=n,n=t,t=null),void 0===o&&(o=this.captureForType(e)),"function"!=typeof n)throw new TypeError("Handler must be a type of Function");return i=this.rootElement,s=this.listenerMap[o?1:0],s[e]||(i&&i.addEventListener(e,this.handle,o),s[e]=[]),t?/^[a-z]+$/i.test(t)?(null===r.tagsCaseSensitive&&(r.tagsCaseSensitive="i"===document.createElement("i").tagName),h=r.tagsCaseSensitive?t:t.toUpperCase(),a=this.matchesTag):/^#[a-z0-9\-_]+$/i.test(t)?(h=t.slice(1),a=this.matchesId):(h=t,a=this.matches):(h=null,a=this.matchesRoot.bind(this)),s[e].push({selector:t,handler:n,matcher:a,matcherParam:h}),this},r.prototype.off=function(e,t,r,n){var o,i,s,a,h;if("function"==typeof t&&(n=r,r=t,t=null),void 0===n)return this.off(e,t,r,!0),this.off(e,t,r,!1),this;if(s=this.listenerMap[n?1:0],!e){for(h in s)s.hasOwnProperty(h)&&this.off(h,t,r);return this}if(a=s[e],!a||!a.length)return this;for(o=a.length-1;o>=0;o--)i=a[o],t&&t!==i.selector||r&&r!==i.handler||a.splice(o,1);return a.length||(delete s[e],this.rootElement&&this.rootElement.removeEventListener(e,this.handle,n)),this},r.prototype.handle=function(e){var t,r,n,o,i,s,a,h="ftLabsDelegateIgnore";if(e[h]!==!0)for(a=e.target,a.nodeType===Node.TEXT_NODE&&(a=a.parentNode),n=this.rootElement,s=this.listenerMap[e.eventPhase===Event.CAPTURING_PHASE?1:0][e.type],r=s.length;a&&r;){for(t=0;r>t&&(o=s[t],o);t++)if(o.matcher.call(a,o.matcherParam,a)&&(i=this.fire(e,a,o)),i===!1)return e[h]=!0,e.preventDefault(),void 0;if(a===n)break;r=s.length,a=a.parentElement}},r.prototype.fire=function(e,t,r){return r.handler.call(t,e,t)},r.prototype.matches=function(e){if(e){var t=e.prototype;return t.matchesSelector||t.webkitMatchesSelector||t.mozMatchesSelector||t.msMatchesSelector||t.oMatchesSelector}}(HTMLElement),r.prototype.matchesTag=function(e,t){return e===t.tagName},r.prototype.matchesRoot=function(e,t){return this.rootElement===t},r.prototype.matchesId=function(e,t){return e===t.id},r.prototype.destroy=function(){this.off(),this.root()}},{}]},{},[1])(1)}); | ||
(function(e){if("function"==typeof bootstrap)bootstrap("delegate",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeDelegate=e}else"undefined"!=typeof window?window.Delegate=e():global.Delegate=e()})(function(){return function(e,t,r){function n(r,i){if(!t[r]){if(!e[r]){var s="function"==typeof require&&require;if(!i&&s)return s(r,!0);if(o)return o(r,!0);throw Error("Cannot find module '"+r+"'")}var a=t[r]={exports:{}};e[r][0].call(a.exports,function(t){var o=e[r][1][t];return n(o?o:t)},a,a.exports)}return t[r].exports}for(var o="function"==typeof require&&require,i=0;r.length>i;i++)n(r[i]);return n}({1:[function(e,t){"use strict";function r(e){this.listenerMap=[{},{}],e&&this.root(e),this.handle=r.prototype.handle.bind(this)}t.exports=r,r.tagsCaseSensitive=null,r.prototype.root=function(e){var t,r=this.listenerMap;if("string"==typeof e&&(e=document.querySelector(e)),this.rootElement){for(t in r[1])r[1].hasOwnProperty(t)&&this.rootElement.removeEventListener(t,this.handle,!0);for(t in r[0])r[0].hasOwnProperty(t)&&this.rootElement.removeEventListener(t,this.handle,!1)}if(!e||!e.addEventListener)return this.rootElement&&delete this.rootElement,this;this.rootElement=e;for(t in r[1])r[1].hasOwnProperty(t)&&this.rootElement.addEventListener(t,this.handle,!0);for(t in r[0])r[0].hasOwnProperty(t)&&this.rootElement.addEventListener(t,this.handle,!1);return this},r.prototype.captureForType=function(e){return-1!==["error","blur","focus","scroll"].indexOf(e)},r.prototype.on=function(e,t,n,o){var i,s,a,h;if(!e)throw new TypeError("Invalid event type: "+e);if("function"==typeof t&&(o=n,n=t,t=null),void 0===o&&(o=this.captureForType(e)),"function"!=typeof n)throw new TypeError("Handler must be a type of Function");return i=this.rootElement,s=this.listenerMap[o?1:0],s[e]||(i&&i.addEventListener(e,this.handle,o),s[e]=[]),t?/^[a-z]+$/i.test(t)?(null===r.tagsCaseSensitive&&(r.tagsCaseSensitive="i"===document.createElement("i").tagName),h=r.tagsCaseSensitive?t:t.toUpperCase(),a=this.matchesTag):/^#[a-z0-9\-_]+$/i.test(t)?(h=t.slice(1),a=this.matchesId):(h=t,a=this.matches):(h=null,a=this.matchesRoot.bind(this)),s[e].push({selector:t,handler:n,matcher:a,matcherParam:h}),this},r.prototype.off=function(e,t,r,n){var o,i,s,a,h;if("function"==typeof t&&(n=r,r=t,t=null),void 0===n)return this.off(e,t,r,!0),this.off(e,t,r,!1),this;if(s=this.listenerMap[n?1:0],!e){for(h in s)s.hasOwnProperty(h)&&this.off(h,t,r);return this}if(a=s[e],!a||!a.length)return this;for(o=a.length-1;o>=0;o--)i=a[o],t&&t!==i.selector||r&&r!==i.handler||a.splice(o,1);return a.length||(delete s[e],this.rootElement&&this.rootElement.removeEventListener(e,this.handle,n)),this},r.prototype.handle=function(e){var t,r,n,o,i,s,a=e.type,h=[],l="ftLabsDelegateIgnore";if(e[l]!==!0){switch(s=e.target,s.nodeType===Node.TEXT_NODE&&(s=s.parentNode),n=this.rootElement,e.eventPhase){case Event.CAPTURING_PHASE:h=this.listenerMap[1][a];break;case Event.AT_TARGET:this.listenerMap[0]&&this.listenerMap[0][a]&&(h=h.concat(this.listenerMap[0][a])),this.listenerMap[1]&&this.listenerMap[1][a]&&(h=h.concat(this.listenerMap[1][a]));break;case Event.BUBBLING_PHASE:h=this.listenerMap[0][a]}for(r=h.length;s&&r;){for(t=0;r>t&&(o=h[t],o);t++)if(o.matcher.call(s,o.matcherParam,s)&&(i=this.fire(e,s,o)),i===!1)return e[l]=!0,e.preventDefault(),void 0;if(s===n)break;r=h.length,s=s.parentElement}}},r.prototype.fire=function(e,t,r){return r.handler.call(t,e,t)},r.prototype.matches=function(e){if(e){var t=e.prototype;return t.matchesSelector||t.webkitMatchesSelector||t.mozMatchesSelector||t.msMatchesSelector||t.oMatchesSelector}}(HTMLElement),r.prototype.matchesTag=function(e,t){return e===t.tagName},r.prototype.matchesRoot=function(e,t){return this.rootElement===window?t===document:this.rootElement===t},r.prototype.matchesId=function(e,t){return e===t.id},r.prototype.destroy=function(){this.off(),this.root()}},{}]},{},[1])(1)}); |
@@ -107,3 +107,3 @@ /*jshint browser:true, node:true*/ | ||
Delegate.prototype.captureForType = function(eventType) { | ||
return ['error', 'blur', 'focus'].indexOf(eventType) !== -1; | ||
return ['error', 'blur', 'focus', 'scroll'].indexOf(eventType) !== -1; | ||
}; | ||
@@ -288,3 +288,3 @@ | ||
Delegate.prototype.handle = function(event) { | ||
var i, l, root, listener, returned, listenerList, target, /** @const */ EVENTIGNORE = 'ftLabsDelegateIgnore'; | ||
var i, l, type = event.type, root, listener, returned, listenerList = [], target, /** @const */ EVENTIGNORE = 'ftLabsDelegateIgnore'; | ||
@@ -301,4 +301,16 @@ if (event[EVENTIGNORE] === true) { | ||
root = this.rootElement; | ||
listenerList = this.listenerMap[event.eventPhase === Event.CAPTURING_PHASE ? 1 : 0][event.type]; | ||
switch (event.eventPhase) { | ||
case Event.CAPTURING_PHASE: | ||
listenerList = this.listenerMap[1][type]; | ||
break; | ||
case Event.AT_TARGET: | ||
if (this.listenerMap[0] && this.listenerMap[0][type]) listenerList = listenerList.concat(this.listenerMap[0][type]); | ||
if (this.listenerMap[1] && this.listenerMap[1][type]) listenerList = listenerList.concat(this.listenerMap[1][type]); | ||
break; | ||
case Event.BUBBLING_PHASE: | ||
listenerList = this.listenerMap[0][type]; | ||
break; | ||
} | ||
// Need to continuously check | ||
@@ -406,2 +418,3 @@ // that the specific list is | ||
Delegate.prototype.matchesRoot = function(selector, element) { | ||
if (this.rootElement === window) return element === document; | ||
return this.rootElement === element; | ||
@@ -408,0 +421,0 @@ }; |
{ | ||
"name": "dom-delegate", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "FT Labs <enquiries@labs.ft.com> (http://labs.ft.com/)", | ||
@@ -5,0 +5,0 @@ "description": "Create and manage a DOM event delegator.", |
@@ -1,2 +0,2 @@ | ||
# ftdomdelegate [![Build Status](https://travis-ci.org/ftlabs/ftdomdelegate.png?branch=master)](https://travis-ci.org/ftlabs/ftdomdelegate) | ||
# ftdomdelegate [![Build Status](https://travis-ci.org/ftlabs/ftdomdelegate.svg?branch=master)](https://travis-ci.org/ftlabs/ftdomdelegate) | ||
@@ -9,2 +9,4 @@ FT's dom delegate library is a simple, easy-to-use component for binding to events on all target elements matching the given selector, irrespective of whether anything exists in the DOM at registration time or not. This allows developers to implement the [event delegation pattern](http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/). | ||
[![Browser Support](https://ci.testling.com/ftlabs/ftdomdelegate.png)](https://ci.testling.com/ftlabs/ftdomdelegate) | ||
The library has been deployed as part of the [FT Web App](http://app.ft.com/) and is tried and tested on the following browsers: | ||
@@ -11,0 +13,0 @@ |
37490
830
170