From 64f8b86b45d7fcad4840825771313f9f5add440a Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Mon, 26 Mar 2012 01:19:16 +0200 Subject: [PATCH] Handle multiple events at once for the same handler with $.on and $.off. --- 4chan_x.user.js | 46 ++++++++++++++++++++++++---------------------- script.coffee | 47 +++++++++++++++++++++++------------------------ 2 files changed, 47 insertions(+), 46 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 4b1e5649c..d7c10c426 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -422,11 +422,21 @@ if (properties) $.extend(el, properties); return el; }, - on: function(el, eventType, handler) { - return el.addEventListener(eventType, handler, false); + on: function(el, events, handler) { + var event, _i, _len, _ref; + _ref = events.split(' '); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + event = _ref[_i]; + el.addEventListener(event, handler, false); + } }, - off: function(el, eventType, handler) { - return el.removeEventListener(eventType, handler, false); + off: function(el, events, handler) { + var event, _i, _len, _ref; + _ref = events.split(' '); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + event = _ref[_i]; + el.removeEventListener(event, handler, false); + } }, open: function(url) { return (GM_openInTab || window.open)(url, '_blank'); @@ -1267,8 +1277,7 @@ } $.on(d, 'dragover', QR.dragOver); $.on(d, 'drop', QR.dropFile); - $.on(d, 'dragstart', QR.drag); - return $.on(d, 'dragend', QR.drag); + return $.on(d, 'dragstart dragend', QR.drag); }, node: function(post) { return $.on($('.quotejs + .quotejs', post.el), 'click', QR.quote); @@ -1706,7 +1715,7 @@ } }, dialog: function() { - var e, event, fileInput, input, mimeTypes, name, spoiler, ta, thread, threads, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3; + var e, fileInput, mimeTypes, name, spoiler, ta, thread, threads, _i, _j, _len, _len2, _ref, _ref2; QR.el = UI.dialog('qr', 'top:0;right:0;', '\
\ Quick Reply \ @@ -1785,17 +1794,12 @@ _ref2 = ['name', 'email', 'sub', 'com']; for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { name = _ref2[_j]; - input = $("[name=" + name + "]", QR.el); - _ref3 = ['input', 'keyup', 'change', 'paste']; - for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) { - event = _ref3[_k]; - $.on(input, event, function() { - QR.selected[this.name] = this.value; - if (QR.cooldown.auto && QR.selected === QR.replies[0] && parseInt(QR.status.input.value.match(/\d+/)) < 6) { - return QR.cooldown.auto = false; - } - }); - } + $.on($("[name=" + name + "]", QR.el), 'input keyup change paste', function() { + QR.selected[this.name] = this.value; + if (QR.cooldown.auto && QR.selected === QR.replies[0] && parseInt(QR.status.input.value.match(/\d+/)) < 6) { + return QR.cooldown.auto = false; + } + }); } $.sync('QR.persona', function(persona) { var key, val, _results; @@ -3247,16 +3251,14 @@ UI.hover(e); } $.on(this, 'mousemove', UI.hover); - $.on(this, 'mouseout', QuotePreview.mouseout); - return $.on(this, 'click', QuotePreview.mouseout); + return $.on(this, 'mouseout click', QuotePreview.mouseout); }, mouseout: function() { var el; if (el = $.id(this.hash.slice(1))) $.removeClass(el, 'qphl'); UI.hoverend(); $.off(this, 'mousemove', UI.hover); - $.off(this, 'mouseout', QuotePreview.mouseout); - return $.off(this, 'click', QuotePreview.mouseout); + return $.off(this, 'mouseout click', QuotePreview.mouseout); }, parse: function(req, id, threadID) { var doc, node, post, qp; diff --git a/script.coffee b/script.coffee index 2b956c0ba..56c3baa63 100644 --- a/script.coffee +++ b/script.coffee @@ -339,10 +339,14 @@ $.extend $, el = d.createElement tag $.extend el, properties if properties el - on: (el, eventType, handler) -> - el.addEventListener eventType, handler, false - off: (el, eventType, handler) -> - el.removeEventListener eventType, handler, false + on: (el, events, handler) -> + for event in events.split ' ' + el.addEventListener event, handler, false + return + off: (el, events, handler) -> + for event in events.split ' ' + el.removeEventListener event, handler, false + return open: (url) -> (GM_openInTab or window.open) url, '_blank' isDST: -> @@ -1039,10 +1043,9 @@ QR = if Conf['Persistent QR'] QR.dialog() QR.hide() if Conf['Auto Hide QR'] - $.on d, 'dragover', QR.dragOver - $.on d, 'drop', QR.dropFile - $.on d, 'dragstart', QR.drag - $.on d, 'dragend', QR.drag + $.on d, 'dragover', QR.dragOver + $.on d, 'drop', QR.dropFile + $.on d, 'dragstart dragend', QR.drag node: (post) -> $.on $('.quotejs + .quotejs', post.el), 'click', QR.quote @@ -1471,17 +1474,15 @@ QR = new QR.reply().select() # save selected reply's data for name in ['name', 'email', 'sub', 'com'] - input = $ "[name=#{name}]", QR.el - for event in ['input', 'keyup', 'change', 'paste'] - # The input event replaces keyup, change and paste events. - # Firefox 12 will support the input event. - # Oprah? - $.on input, event, -> - QR.selected[@name] = @value - # Disable auto-posting if you're typing in the first reply - # during the last 5 seconds of the cooldown. - if QR.cooldown.auto and QR.selected is QR.replies[0] and parseInt(QR.status.input.value.match /\d+/) < 6 - QR.cooldown.auto = false + # The input event replaces keyup, change and paste events. + # Firefox 12 will support the input event. + # Oprah? + $.on $("[name=#{name}]", QR.el), 'input keyup change paste', -> + QR.selected[@name] = @value + # Disable auto-posting if you're typing in the first reply + # during the last 5 seconds of the cooldown. + if QR.cooldown.auto and QR.selected is QR.replies[0] and parseInt(QR.status.input.value.match /\d+/) < 6 + QR.cooldown.auto = false # sync between tabs $.sync 'QR.persona', (persona) -> return unless QR.el.hidden @@ -2691,16 +2692,14 @@ QuotePreview = threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]', @).firstChild.id $.cache @pathname, (-> QuotePreview.parse @, id, threadID) UI.hover e - $.on @, 'mousemove', UI.hover - $.on @, 'mouseout', QuotePreview.mouseout - $.on @, 'click', QuotePreview.mouseout + $.on @, 'mousemove', UI.hover + $.on @, 'mouseout click', QuotePreview.mouseout mouseout: -> if el = $.id @hash[1..] $.removeClass el, 'qphl' UI.hoverend() $.off @, 'mousemove', UI.hover - $.off @, 'mouseout', QuotePreview.mouseout - $.off @, 'click', QuotePreview.mouseout + $.off @, 'mouseout click', QuotePreview.mouseout parse: (req, id, threadID) -> return unless (qp = UI.el) and qp.textContent is "Loading #{id}..."