From cfaeb21f272e71f9cbda1d61f45fe66dd2b64495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sk=C3=B6ld?= Date: Tue, 14 Jan 2014 11:07:25 +0100 Subject: [PATCH] Added support for delegate events. And moved the event binding from the constructor to `Dropload#bind([selector[)` so it can be re-bound again. Breaking change: `bind()` is not called by the constructor anymore. --- index.js | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 4684c01..c71f8ed 100644 --- a/index.js +++ b/index.js @@ -26,22 +26,20 @@ var typeMap = { /** * Initialize a drop point - * on the given `el`. + * on the given `el`. Optionally use `selector` + * for delegated events. * * @param {Element} el + * @param {String} selector * @api public */ -function Dropload(el) { +function Dropload(el,selector) { if (!(this instanceof Dropload)) return new Dropload(el); Emitter.call(this); this.el = el; - this.classes = classes(el); + this.selector = selector || ''; this.events = events(el, this); - this.events.bind('drop'); - this.events.bind('dragenter'); - this.events.bind('dragleave'); - this.events.bind('dragover'); this.ignored = {}; } @@ -74,6 +72,25 @@ Dropload.prototype.ignoring = function(name){ return !! this.ignored[name]; }; +/** + * Bind event handlers. Optionally use `selector` + * to update the selector for delegated events. + * + * @param {String} selector + * + * @api public + */ + +Dropload.prototype.bind = function(selector){ + if( 'undefined' != typeof selector ){ + this.selector = selector; + } + this.events.bind('drop '+this.selector); + this.events.bind('dragenter '+this.selector); + this.events.bind('dragleave '+this.selector); + this.events.bind('dragover '+this.selector); +} + /** * Unbind event handlers. * @@ -89,7 +106,8 @@ Dropload.prototype.unbind = function(){ */ Dropload.prototype.ondragenter = function(e){ - this.classes.add('over'); + console.log('drag enter',e); + classes(e.delegateTarget).add('over'); }; /** @@ -105,7 +123,7 @@ Dropload.prototype.ondragover = function(e){ */ Dropload.prototype.ondragleave = function(e){ - this.classes.remove('over'); + classes(e.delegateTarget).remove('over'); }; /** @@ -115,7 +133,7 @@ Dropload.prototype.ondragleave = function(e){ Dropload.prototype.ondrop = function(e){ e.stopPropagation(); e.preventDefault(); - this.classes.remove('over'); + classes(e.delegateTarget).remove('over'); var items = e.dataTransfer.items; var files = e.dataTransfer.files; this.emit('drop', e);