Handle multiple events at once for the same handler with $.on and $.off.

This commit is contained in:
Nicolas Stepien 2012-03-26 01:19:16 +02:00
parent f0b5c5aa27
commit 64f8b86b45
2 changed files with 47 additions and 46 deletions

View File

@ -422,11 +422,21 @@
if (properties) $.extend(el, properties); if (properties) $.extend(el, properties);
return el; return el;
}, },
on: function(el, eventType, handler) { on: function(el, events, handler) {
return el.addEventListener(eventType, handler, false); 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) { off: function(el, events, handler) {
return el.removeEventListener(eventType, handler, false); 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) { open: function(url) {
return (GM_openInTab || window.open)(url, '_blank'); return (GM_openInTab || window.open)(url, '_blank');
@ -1267,8 +1277,7 @@
} }
$.on(d, 'dragover', QR.dragOver); $.on(d, 'dragover', QR.dragOver);
$.on(d, 'drop', QR.dropFile); $.on(d, 'drop', QR.dropFile);
$.on(d, 'dragstart', QR.drag); return $.on(d, 'dragstart dragend', QR.drag);
return $.on(d, 'dragend', QR.drag);
}, },
node: function(post) { node: function(post) {
return $.on($('.quotejs + .quotejs', post.el), 'click', QR.quote); return $.on($('.quotejs + .quotejs', post.el), 'click', QR.quote);
@ -1706,7 +1715,7 @@
} }
}, },
dialog: function() { 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;', '\ QR.el = UI.dialog('qr', 'top:0;right:0;', '\
<div class=move>\ <div class=move>\
Quick Reply <input type=checkbox id=autohide title=Auto-hide>\ Quick Reply <input type=checkbox id=autohide title=Auto-hide>\
@ -1785,17 +1794,12 @@
_ref2 = ['name', 'email', 'sub', 'com']; _ref2 = ['name', 'email', 'sub', 'com'];
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
name = _ref2[_j]; name = _ref2[_j];
input = $("[name=" + name + "]", QR.el); $.on($("[name=" + name + "]", QR.el), 'input keyup change paste', function() {
_ref3 = ['input', 'keyup', 'change', 'paste']; QR.selected[this.name] = this.value;
for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) { if (QR.cooldown.auto && QR.selected === QR.replies[0] && parseInt(QR.status.input.value.match(/\d+/)) < 6) {
event = _ref3[_k]; return QR.cooldown.auto = false;
$.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;
}
});
}
} }
$.sync('QR.persona', function(persona) { $.sync('QR.persona', function(persona) {
var key, val, _results; var key, val, _results;
@ -3247,16 +3251,14 @@
UI.hover(e); UI.hover(e);
} }
$.on(this, 'mousemove', UI.hover); $.on(this, 'mousemove', UI.hover);
$.on(this, 'mouseout', QuotePreview.mouseout); return $.on(this, 'mouseout click', QuotePreview.mouseout);
return $.on(this, 'click', QuotePreview.mouseout);
}, },
mouseout: function() { mouseout: function() {
var el; var el;
if (el = $.id(this.hash.slice(1))) $.removeClass(el, 'qphl'); if (el = $.id(this.hash.slice(1))) $.removeClass(el, 'qphl');
UI.hoverend(); UI.hoverend();
$.off(this, 'mousemove', UI.hover); $.off(this, 'mousemove', UI.hover);
$.off(this, 'mouseout', QuotePreview.mouseout); return $.off(this, 'mouseout click', QuotePreview.mouseout);
return $.off(this, 'click', QuotePreview.mouseout);
}, },
parse: function(req, id, threadID) { parse: function(req, id, threadID) {
var doc, node, post, qp; var doc, node, post, qp;

View File

@ -339,10 +339,14 @@ $.extend $,
el = d.createElement tag el = d.createElement tag
$.extend el, properties if properties $.extend el, properties if properties
el el
on: (el, eventType, handler) -> on: (el, events, handler) ->
el.addEventListener eventType, handler, false for event in events.split ' '
off: (el, eventType, handler) -> el.addEventListener event, handler, false
el.removeEventListener eventType, handler, false return
off: (el, events, handler) ->
for event in events.split ' '
el.removeEventListener event, handler, false
return
open: (url) -> open: (url) ->
(GM_openInTab or window.open) url, '_blank' (GM_openInTab or window.open) url, '_blank'
isDST: -> isDST: ->
@ -1039,10 +1043,9 @@ QR =
if Conf['Persistent QR'] if Conf['Persistent QR']
QR.dialog() QR.dialog()
QR.hide() if Conf['Auto Hide QR'] QR.hide() if Conf['Auto Hide QR']
$.on d, 'dragover', QR.dragOver $.on d, 'dragover', QR.dragOver
$.on d, 'drop', QR.dropFile $.on d, 'drop', QR.dropFile
$.on d, 'dragstart', QR.drag $.on d, 'dragstart dragend', QR.drag
$.on d, 'dragend', QR.drag
node: (post) -> node: (post) ->
$.on $('.quotejs + .quotejs', post.el), 'click', QR.quote $.on $('.quotejs + .quotejs', post.el), 'click', QR.quote
@ -1471,17 +1474,15 @@ QR =
new QR.reply().select() new QR.reply().select()
# save selected reply's data # save selected reply's data
for name in ['name', 'email', 'sub', 'com'] for name in ['name', 'email', 'sub', 'com']
input = $ "[name=#{name}]", QR.el # The input event replaces keyup, change and paste events.
for event in ['input', 'keyup', 'change', 'paste'] # Firefox 12 will support the input event.
# The input event replaces keyup, change and paste events. # Oprah?
# Firefox 12 will support the input event. $.on $("[name=#{name}]", QR.el), 'input keyup change paste', ->
# Oprah? QR.selected[@name] = @value
$.on input, event, -> # Disable auto-posting if you're typing in the first reply
QR.selected[@name] = @value # during the last 5 seconds of the cooldown.
# Disable auto-posting if you're typing in the first reply if QR.cooldown.auto and QR.selected is QR.replies[0] and parseInt(QR.status.input.value.match /\d+/) < 6
# during the last 5 seconds of the cooldown. QR.cooldown.auto = false
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 between tabs
$.sync 'QR.persona', (persona) -> $.sync 'QR.persona', (persona) ->
return unless QR.el.hidden return unless QR.el.hidden
@ -2691,16 +2692,14 @@ QuotePreview =
threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]', @).firstChild.id threadID = @pathname.split('/').pop() or $.x('ancestor::div[@class="thread"]', @).firstChild.id
$.cache @pathname, (-> QuotePreview.parse @, id, threadID) $.cache @pathname, (-> QuotePreview.parse @, id, threadID)
UI.hover e UI.hover e
$.on @, 'mousemove', UI.hover $.on @, 'mousemove', UI.hover
$.on @, 'mouseout', QuotePreview.mouseout $.on @, 'mouseout click', QuotePreview.mouseout
$.on @, 'click', QuotePreview.mouseout
mouseout: -> mouseout: ->
if el = $.id @hash[1..] if el = $.id @hash[1..]
$.removeClass el, 'qphl' $.removeClass el, 'qphl'
UI.hoverend() UI.hoverend()
$.off @, 'mousemove', UI.hover $.off @, 'mousemove', UI.hover
$.off @, 'mouseout', QuotePreview.mouseout $.off @, 'mouseout click', QuotePreview.mouseout
$.off @, 'click', QuotePreview.mouseout
parse: (req, id, threadID) -> parse: (req, id, threadID) ->
return unless (qp = UI.el) and qp.textContent is "Loading #{id}..." return unless (qp = UI.el) and qp.textContent is "Loading #{id}..."