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,28 +1422,26 @@
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) { var isReply, thread;
var isReply, thread; thread = _arg.thread, isReply = _arg.isReply;
thread = _arg.thread, isReply = _arg.isReply; if (isReply || thread.isHidden) {
if (isReply || thread.isHidden) { return false;
return false; }
} ThreadHiding.menu.thread = thread;
ThreadHiding.menu.thread = thread; return true;
return true; },
}, subEntries: [
subEntries: [ {
{ el: apply
el: apply }, {
}, { el: makeStub
el: makeStub }
} ]
] });
}
}));
}, },
hide: function() { hide: function() {
var makeStub, thread; var makeStub, thread;
@ -1634,30 +1631,28 @@
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) { if (!post.isReply || post.isClone) {
if (!post.isReply || post.isClone) { return false;
return false; }
} ReplyHiding.menu.post = post;
ReplyHiding.menu.post = post; return true;
return true; },
}, subEntries: [
subEntries: [ {
{ el: apply
el: apply }, {
}, { el: thisPost
el: thisPost }, {
}, { el: replies
el: replies }, {
}, { 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,29 +1925,27 @@
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) { var node, seconds;
var node, seconds; if (post.isDead) {
if (post.isDead) { return false;
return false; }
} DeleteLink.post = post;
DeleteLink.post = post; node = div.firstChild;
node = div.firstChild; if (seconds = DeleteLink.cooldown[post.fullID]) {
if (seconds = DeleteLink.cooldown[post.fullID]) { node.textContent = "Delete (" + seconds + ")";
node.textContent = "Delete (" + seconds + ")"; DeleteLink.cooldown.el = node;
DeleteLink.cooldown.el = node; } else {
} else { node.textContent = 'Delete';
node.textContent = 'Delete'; delete DeleteLink.cooldown.el;
delete DeleteLink.cooldown.el; }
} 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,22 +2036,20 @@
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) { var file;
var file; file = _arg.file;
file = _arg.file; if (!file) {
if (!file) { return false;
return false;
}
a.href = file.URL;
a.download = file.name;
return true;
} }
a.href = file.URL;
a.download = file.name;
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,10 +101,9 @@ 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
# 4chan settings link # 4chan settings link
link = $.el 'a', link = $.el 'a',
@ -112,11 +111,10 @@ 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']
return unless Conf['Disable 4chan\'s extension'] return unless Conf['Disable 4chan\'s extension']
settings = JSON.parse(localStorage.getItem '4chan-settings') or {} settings = JSON.parse(localStorage.getItem '4chan-settings') or {}
@ -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,16 +478,15 @@ 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}) -> if isReply or thread.isHidden
if isReply or thread.isHidden return false
return false ThreadHiding.menu.thread = thread
ThreadHiding.menu.thread = thread true
true subEntries: [el: apply; el: makeStub]
subEntries: [el: apply; el: makeStub]
hide: -> hide: ->
makeStub = $('input', @parentNode).checked makeStub = $('input', @parentNode).checked
{thread} = ThreadHiding.menu {thread} = ThreadHiding.menu
@ -634,16 +630,15 @@ 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) -> if !post.isReply or post.isClone
if !post.isReply or post.isClone return false
return false ReplyHiding.menu.post = post
ReplyHiding.menu.post = post true
true subEntries: [{el: apply}, {el: thisPost}, {el: replies}, {el: makeStub}]
subEntries: [{el: apply}, {el: thisPost}, {el: replies}, {el: makeStub}]
hide: -> hide: ->
parent = @parentNode parent = @parentNode
thisPost = $('input[name=thisPost]', parent).checked thisPost = $('input[name=thisPost]', parent).checked
@ -804,13 +799,12 @@ 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) -> ReportLink.post = post
ReportLink.post = post !post.isDead
!post.isDead
report: -> report: ->
{post} = ReportLink {post} = ReportLink
url = "//sys.4chan.org/#{post.board}/imgboard.php?mode=report&no=#{post}" url = "//sys.4chan.org/#{post.board}/imgboard.php?mode=report&no=#{post}"
@ -845,22 +839,21 @@ 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) -> return false if post.isDead
return false if post.isDead DeleteLink.post = post
DeleteLink.post = post node = div.firstChild
node = div.firstChild if seconds = DeleteLink.cooldown[post.fullID]
if seconds = DeleteLink.cooldown[post.fullID] node.textContent = "Delete (#{seconds})"
node.textContent = "Delete (#{seconds})" DeleteLink.cooldown.el = node
DeleteLink.cooldown.el = node else
else node.textContent = 'Delete'
node.textContent = 'Delete' delete DeleteLink.cooldown.el
delete DeleteLink.cooldown.el true
true subEntries: [postEntry, fileEntry]
subEntries: [postEntry, fileEntry]
$.on d, 'QRPostSuccessful', @cooldown.start $.on d, 'QRPostSuccessful', @cooldown.start
@ -935,15 +928,14 @@ 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}) -> return false unless file
return false unless file a.href = file.URL
a.href = file.URL a.download = file.name
a.download = file.name true
true
ArchiveLink = ArchiveLink =
init: -> init: ->
@ -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',