add $.event

This commit is contained in:
Nicolas Stepien 2013-02-11 15:34:37 +01:00
parent fec837ecdc
commit f4f53b92f2
3 changed files with 157 additions and 177 deletions

View File

@ -780,6 +780,11 @@
el.removeEventListener(event, handler, false); el.removeEventListener(event, handler, false);
} }
}, },
event: function(event, detail) {
return d.dispatchEvent(new CustomEvent(event, {
detail: detail
}));
},
open: function(url) { open: function(url) {
return (GM_openInTab || window.open)(url, '_blank'); return (GM_openInTab || window.open)(url, '_blank');
}, },
@ -1017,12 +1022,10 @@
href: 'javascript:;' href: 'javascript:;'
}); });
$.on(link, 'click', Settings.open); $.on(link, 'click', Settings.open);
d.dispatchEvent(new CustomEvent('AddMenuEntry', { $.event('AddMenuEntry', {
detail: {
type: 'header', type: 'header',
el: link el: link
} });
}));
link = $.el('a', { link = $.el('a', {
className: 'fourchan-settings-link', className: 'fourchan-settings-link',
textContent: '4chan Settings', textContent: '4chan Settings',
@ -1031,15 +1034,13 @@
$.on(link, 'click', function() { $.on(link, 'click', function() {
return $.id('settingsWindowLink').click(); return $.id('settingsWindowLink').click();
}); });
d.dispatchEvent(new CustomEvent('AddMenuEntry', { $.event('AddMenuEntry', {
detail: {
type: 'header', type: 'header',
el: link, el: link,
open: function() { open: function() {
return !Conf['Disable 4chan\'s extension']; return !Conf['Disable 4chan\'s extension'];
} }
} });
}));
if (!Conf['Disable 4chan\'s extension']) { if (!Conf['Disable 4chan\'s extension']) {
return; return;
} }
@ -1275,9 +1276,7 @@
type = _ref[_i]; type = _ref[_i];
entry.subEntries.push(Filter.menu.createSubEntry(type[0], type[1])); entry.subEntries.push(Filter.menu.createSubEntry(type[0], type[1]));
} }
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', entry);
detail: entry
}));
}, },
createSubEntry: function(text, type) { createSubEntry: function(text, type) {
var el; var el;
@ -1423,8 +1422,7 @@
makeStub = $.el('label', { makeStub = $.el('label', {
innerHTML: "<input type=checkbox checked=" + Conf['Stubs'] + "> Make stub" innerHTML: "<input type=checkbox checked=" + Conf['Stubs'] + "> Make stub"
}); });
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', {
detail: {
type: 'post', type: 'post',
el: div, el: div,
open: function(_arg) { open: function(_arg) {
@ -1443,8 +1441,7 @@
el: makeStub el: makeStub
} }
] ]
} });
}));
}, },
hide: function() { hide: function() {
var makeStub, thread; var makeStub, thread;
@ -1634,8 +1631,7 @@
makeStub = $.el('label', { makeStub = $.el('label', {
innerHTML: "<input type=checkbox name=makeStub checked=" + Conf['Stubs'] + "> Make stub" innerHTML: "<input type=checkbox name=makeStub checked=" + Conf['Stubs'] + "> Make stub"
}); });
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', {
detail: {
type: 'post', type: 'post',
el: div, el: div,
open: function(post) { open: function(post) {
@ -1656,8 +1652,7 @@
el: makeStub el: makeStub
} }
] ]
} });
}));
}, },
hide: function() { hide: function() {
var makeStub, parent, post, replies, thisPost; var makeStub, parent, post, replies, thisPost;
@ -1875,16 +1870,14 @@
textContent: 'Report this post' textContent: 'Report this post'
}); });
$.on(a, 'click', ReportLink.report); $.on(a, 'click', ReportLink.report);
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', {
detail: {
type: 'post', type: 'post',
el: a, el: a,
open: function(post) { open: function(post) {
ReportLink.post = post; ReportLink.post = post;
return !post.isDead; return !post.isDead;
} }
} });
}));
}, },
report: function() { report: function() {
var id, post, set, url; var id, post, set, url;
@ -1932,8 +1925,7 @@
return !!file; return !!file;
} }
}; };
d.dispatchEvent(new CustomEvent('AddMenuEntry', { $.event('AddMenuEntry', {
detail: {
type: 'post', type: 'post',
el: div, el: div,
open: function(post) { open: function(post) {
@ -1953,8 +1945,7 @@
return true; return true;
}, },
subEntries: [postEntry, fileEntry] subEntries: [postEntry, fileEntry]
} });
}));
return $.on(d, 'QRPostSuccessful', this.cooldown.start); return $.on(d, 'QRPostSuccessful', this.cooldown.start);
}, },
"delete": function() { "delete": function() {
@ -2045,8 +2036,7 @@
className: 'download-link', className: 'download-link',
textContent: 'Download file' textContent: 'Download file'
}); });
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', {
detail: {
type: 'post', type: 'post',
el: a, el: a,
open: function(_arg) { open: function(_arg) {
@ -2059,8 +2049,7 @@
a.download = file.name; a.download = file.name;
return true; return true;
} }
} });
}));
} }
}; };
@ -2093,9 +2082,7 @@
type = _ref[_i]; type = _ref[_i];
entry.subEntries.push(this.createSubEntry(type[0], type[1])); entry.subEntries.push(this.createSubEntry(type[0], type[1]));
} }
return d.dispatchEvent(new CustomEvent('AddMenuEntry', { return $.event('AddMenuEntry', entry);
detail: entry
}));
}, },
createSubEntry: function(text, type) { createSubEntry: function(text, type) {
var el, open; var el, open;

View File

@ -134,6 +134,8 @@ $.extend $,
for event in events.split ' ' for event in events.split ' '
el.removeEventListener event, handler, false el.removeEventListener event, handler, false
return return
event: (event, detail) ->
d.dispatchEvent new CustomEvent event, {detail}
open: (url) -> open: (url) ->
(GM_openInTab or window.open) url, '_blank' (GM_openInTab or window.open) url, '_blank'
hidden: -> hidden: ->

View File

@ -101,8 +101,7 @@ Settings =
textContent: '4chan X Settings' textContent: '4chan X Settings'
href: 'javascript:;' href: 'javascript:;'
$.on link, 'click', Settings.open $.on link, 'click', Settings.open
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'header' type: 'header'
el: link el: link
@ -112,8 +111,7 @@ Settings =
textContent: '4chan Settings' textContent: '4chan Settings'
href: 'javascript:;' href: 'javascript:;'
$.on link, 'click', -> $.id('settingsWindowLink').click() $.on link, 'click', -> $.id('settingsWindowLink').click()
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'header' type: 'header'
el: link el: link
open: -> !Conf['Disable 4chan\'s extension'] open: -> !Conf['Disable 4chan\'s extension']
@ -328,8 +326,7 @@ Filter =
# Add a sub entry for each filter type. # Add a sub entry for each filter type.
entry.subEntries.push Filter.menu.createSubEntry type[0], type[1] entry.subEntries.push Filter.menu.createSubEntry type[0], type[1]
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry', entry
detail: entry
createSubEntry: (text, type) -> createSubEntry: (text, type) ->
el = $.el 'a', el = $.el 'a',
@ -481,8 +478,7 @@ ThreadHiding =
makeStub = $.el 'label', makeStub = $.el 'label',
innerHTML: "<input type=checkbox checked=#{Conf['Stubs']}> Make stub" innerHTML: "<input type=checkbox checked=#{Conf['Stubs']}> Make stub"
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'post' type: 'post'
el: div el: div
open: ({thread, isReply}) -> open: ({thread, isReply}) ->
@ -634,8 +630,7 @@ ReplyHiding =
makeStub = $.el 'label', makeStub = $.el 'label',
innerHTML: "<input type=checkbox name=makeStub checked=#{Conf['Stubs']}> Make stub" innerHTML: "<input type=checkbox name=makeStub checked=#{Conf['Stubs']}> Make stub"
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'post' type: 'post'
el: div el: div
open: (post) -> open: (post) ->
@ -804,8 +799,7 @@ ReportLink =
href: 'javascript:;' href: 'javascript:;'
textContent: 'Report this post' textContent: 'Report this post'
$.on a, 'click', ReportLink.report $.on a, 'click', ReportLink.report
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'post' type: 'post'
el: a el: a
open: (post) -> open: (post) ->
@ -845,8 +839,7 @@ DeleteLink =
$.on fileEl, 'click', DeleteLink.delete $.on fileEl, 'click', DeleteLink.delete
!!file !!file
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'post' type: 'post'
el: div el: div
open: (post) -> open: (post) ->
@ -935,8 +928,7 @@ DownloadLink =
a = $.el 'a', a = $.el 'a',
className: 'download-link' className: 'download-link'
textContent: 'Download file' textContent: 'Download file'
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry',
detail:
type: 'post' type: 'post'
el: a el: a
open: ({file}) -> open: ({file}) ->
@ -975,8 +967,7 @@ ArchiveLink =
# Add a sub entry for each type. # Add a sub entry for each type.
entry.subEntries.push @createSubEntry type[0], type[1] entry.subEntries.push @createSubEntry type[0], type[1]
d.dispatchEvent new CustomEvent 'AddMenuEntry', $.event 'AddMenuEntry', entry
detail: entry
createSubEntry: (text, type) -> createSubEntry: (text, type) ->
el = $.el 'a', el = $.el 'a',