finish keybinds rewrite

This commit is contained in:
James Campos 2011-04-16 19:10:55 -07:00
parent 10bd6787ab
commit b8fa5724f1
2 changed files with 172 additions and 197 deletions

View File

@ -59,7 +59,7 @@
*/ */
(function() { (function() {
var $, $$, NAMESPACE, autoWatch, callback, changeCheckbox, changeValue, config, d, delform, el, expand, expandComment, expandThread, g, imageClick, imageExpand, imageExpandClick, imageHover, imageResize, imageThumb, imageToggle, imageType, imageTypeChange, keyModeNormal, keybinds, log, nav, navtopr, nodeInserted, option, options, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4; var $, $$, NAMESPACE, autoWatch, callback, changeCheckbox, changeValue, config, d, delform, el, expand, expandComment, expandThread, g, imageClick, imageExpand, imageExpandClick, imageHover, imageResize, imageThumb, imageToggle, imageType, imageTypeChange, keybinds, log, nav, navtopr, nodeInserted, option, options, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _len, _len2, _len3, _ref, _ref2, _ref3, _ref4;
var __slice = Array.prototype.slice; var __slice = Array.prototype.slice;
if (typeof console != "undefined" && console !== null) { if (typeof console != "undefined" && console !== null) {
log = console.log; log = console.log;
@ -819,143 +819,127 @@
} }
}, },
normal: function(e) { normal: function(e) {
var id, thread, url; var thread;
thread = nav.getThread(); thread = nav.getThread();
switch (keybinds.key) { switch (keybinds.key) {
case 'I': case 'I':
break; return keybinds.qr(thread);
case 'J': case 'J':
break; return keybinds.hl.next(thread);
case 'K': case 'K':
break; return keybinds.hl.prev(thread);
case 'M': case 'M':
break; return keybinds.img(thread, true);
case 'O': case 'O':
id = thread.firstChild.id; return keybinds.open(thread);
url = "http://boards.4chan.org/" + g.BOARD + "/res/" + id;
return location.href = url;
case 'i': case 'i':
break; return keybinds.qr(thread, true);
case 'm': case 'm':
break; return keybinds.img(thread);
case 'n': case 'n':
return nav.next(); return nav.next();
case 'o': case 'o':
id = thread.firstChild.id; return keybinds.open(thread, true);
url = "http://boards.4chan.org/" + g.BOARD + "/res/" + id;
return GM_openInTab(url);
case 'p': case 'p':
return nav.prev(); return nav.prev();
case 'u': case 'u':
updater.update(); return updater.update();
break;
case 'w': case 'w':
return watcher.toggle(thread); return watcher.toggle(thread);
case 'x': case 'x':
return threadHiding.toggle(thread); return threadHiding.toggle(thread);
} }
} },
}; img: function(thread, all) {
keyModeNormal = function(e) { var image;
var bot, char, hash, height, image, next, prev, qrLink, rect, replies, reply, root, td, thread, top, _i, _j, _len, _len2; if (all) {
if (e.ctrlKey || e.altKey) { return $("#imageExpand").click();
return; } else {
} if (!(image = $('td.replyhl span.filesize ~ a[target]', thread))) {
char = g.char; image = $('span.filesize ~ a[target]', thread);
hash = location.hash;
switch (char) {
case "I":
if (g.REPLY) {
if (!(qrLink = $('td.replyhl span[id] a:not(:first-child)'))) {
qrLink = $("span[id^=nothread] a:not(:first-child)");
}
} else {
thread = getThread()[0];
if (!(qrLink = $('td.replyhl span[id] a:not(:first-child)', thread))) {
qrLink = $("span#nothread" + thread.id + " a:not(:first-child)", thread);
}
} }
if (e.shiftKey) { return imageToggle(image);
$.append(d.body, qr.dialog(qrLink)); }
return $('#qr textarea').focus(); },
} else { qr: function(thread, quote) {
e = { var e, qrLink;
preventDefault: function() {}, if (!(qrLink = $('td.replyhl span[id] a:not(:first-child)', thread))) {
target: qrLink qrLink = $("span[id^=nothread] a:not(:first-child)", thread);
}; }
return qr.cb.quote(e); if (quote) {
e = {
preventDefault: function() {},
target: qrLink
};
return qr.cb.quote(e);
} else {
if (!$('#qr')) {
qr.dialog(qrLink);
} }
break; return $('#qr textarea').focus();
case "J": }
if (e.shiftKey) { },
if (!g.REPLY) { open: function(thread, tab) {
root = getThread()[0]; var id, url;
} id = thread.firstChild.id;
if (td = $('td.replyhl', root)) { url = "http://boards.4chan.org/" + g.BOARD + "/res/" + id;
td.className = 'reply'; if (tab) {
rect = td.getBoundingClientRect(); return GM_openInTab(url);
} else {
return location.href = url;
}
},
hl: {
next: function(thread) {
var next, rect, replies, reply, td, top, _i, _len;
if (td = $('td.replyhl', thread)) {
td.className = 'reply';
rect = td.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
next = $.x('following::td[@class="reply"]', td);
rect = next.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) { if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
next = $.x('following::td[@class="reply"]', td); next.className = 'replyhl';
rect = next.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
next.className = 'replyhl';
}
return;
}
}
replies = $$('td.reply', root);
for (_i = 0, _len = replies.length; _i < _len; _i++) {
reply = replies[_i];
top = reply.getBoundingClientRect().top;
if (top > 0) {
reply.className = 'replyhl';
break;
} }
return;
} }
} }
break; replies = $$('td.reply', thread);
case "K": for (_i = 0, _len = replies.length; _i < _len; _i++) {
if (e.shiftKey) { reply = replies[_i];
if (!g.REPLY) { top = reply.getBoundingClientRect().top;
root = getThread()[0]; if (top > 0) {
reply.className = 'replyhl';
return;
} }
if (td = $('td.replyhl', root)) { }
td.className = 'reply'; },
rect = td.getBoundingClientRect(); prev: function(thread) {
var bot, height, prev, rect, replies, reply, td, _i, _len;
if (td = $('td.replyhl', thread)) {
td.className = 'reply';
rect = td.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
prev = $.x('preceding::td[@class="reply"][1]', td);
rect = prev.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) { if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
prev = $.x('preceding::td[@class="reply"][1]', td); prev.className = 'replyhl';
rect = prev.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
prev.className = 'replyhl';
}
return;
}
}
replies = $$('td.reply', root);
replies.reverse();
height = d.body.clientHeight;
for (_j = 0, _len2 = replies.length; _j < _len2; _j++) {
reply = replies[_j];
bot = reply.getBoundingClientRect().bottom;
if (bot < height) {
reply.className = 'replyhl';
break;
} }
return;
} }
} }
break; replies = $$('td.reply', thread);
case "M": replies.reverse();
if (e.shiftKey) { height = d.body.clientHeight;
return $("#imageExpand").click(); for (_i = 0, _len = replies.length; _i < _len; _i++) {
} else { reply = replies[_i];
if (!g.REPLY) { bot = reply.getBoundingClientRect().bottom;
root = getThread()[0]; if (bot < height) {
reply.className = 'replyhl';
return;
} }
if (!(image = $('td.replyhl span.filesize ~ a[target]', root))) {
image = $('span.filesize ~ a[target]', root);
}
return imageToggle(image);
} }
}
} }
}; };
nav = { nav = {
@ -982,10 +966,10 @@
next: function() { next: function() {
return nav.scroll(+1); return nav.scroll(+1);
}, },
threads: [],
getThread: function(full) { getThread: function(full) {
var bottom, i, rect, thread, _len, _ref, _results; var bottom, i, rect, thread, _len, _ref;
_ref = nav.threads; _ref = nav.threads;
_results = [];
for (i = 0, _len = _ref.length; i < _len; i++) { for (i = 0, _len = _ref.length; i < _len; i++) {
thread = _ref[i]; thread = _ref[i];
rect = thread.getBoundingClientRect(); rect = thread.getBoundingClientRect();
@ -997,7 +981,7 @@
return thread; return thread;
} }
} }
return _results; return null;
}, },
scroll: function(delta) { scroll: function(delta) {
var i, rect, thread, top, _ref; var i, rect, thread, top, _ref;

View File

@ -604,113 +604,101 @@ keybinds =
thread = nav.getThread() thread = nav.getThread()
switch keybinds.key switch keybinds.key
when 'I' when 'I'
#qr no text keybinds.qr thread
return
when 'J' when 'J'
#highlight next keybinds.hl.next thread
return
when 'K' when 'K'
#highlight prev keybinds.hl.prev thread
return
when 'M' when 'M'
#expand all keybinds.img thread, true
return
when 'O' when 'O'
id = thread.firstChild.id keybinds.open thread
url = "http://boards.4chan.org/#{g.BOARD}/res/#{id}"
location.href = url
when 'i' when 'i'
#qr keybinds.qr thread, true
return
when 'm' when 'm'
#expand img keybinds.img thread
return
when 'n' when 'n'
nav.next() nav.next()
when 'o' when 'o'
id = thread.firstChild.id keybinds.open thread, true
url = "http://boards.4chan.org/#{g.BOARD}/res/#{id}"
GM_openInTab url
when 'p' when 'p'
nav.prev() nav.prev()
when 'u' when 'u'
updater.update() updater.update()
return
when 'w' when 'w'
watcher.toggle thread watcher.toggle thread
when 'x' when 'x'
threadHiding.toggle thread threadHiding.toggle thread
keyModeNormal = (e) -> img: (thread, all) ->
return if e.ctrlKey or e.altKey if all
char = g.char $("#imageExpand").click()
hash = location.hash else
switch char unless image = $ 'td.replyhl span.filesize ~ a[target]', thread
when "I" image = $ 'span.filesize ~ a[target]', thread
if g.REPLY imageToggle image
unless qrLink = $ 'td.replyhl span[id] a:not(:first-child)'
qrLink = $ "span[id^=nothread] a:not(:first-child)" qr: (thread, quote) ->
else unless qrLink = $ 'td.replyhl span[id] a:not(:first-child)', thread
[thread] = getThread() qrLink = $ "span[id^=nothread] a:not(:first-child)", thread
unless qrLink = $ 'td.replyhl span[id] a:not(:first-child)', thread
qrLink = $ "span#nothread#{thread.id} a:not(:first-child)", thread if quote
if e.shiftKey # qrLink.click() doesn't work, so use this hack
$.append d.body, qr.dialog qrLink e =
$('#qr textarea').focus() preventDefault: ->
else target: qrLink
# qrLink.click() doesn't work, so use this hack qr.cb.quote e
e = else
preventDefault: -> unless $ '#qr'
target: qrLink qr.dialog qrLink
qr.cb.quote e $('#qr textarea').focus()
when "J"
if e.shiftKey open: (thread, tab) ->
if not g.REPLY then [root] = getThread() id = thread.firstChild.id
if td = $ 'td.replyhl', root url = "http://boards.4chan.org/#{g.BOARD}/res/#{id}"
td.className = 'reply' if tab
rect = td.getBoundingClientRect() GM_openInTab url
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're visible else
next = $.x 'following::td[@class="reply"]', td location.href = url
rect = next.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the next hl:
next.className = 'replyhl' next: (thread) ->
return if td = $ 'td.replyhl', thread
replies = $$ 'td.reply', root td.className = 'reply'
for reply in replies rect = td.getBoundingClientRect()
top = reply.getBoundingClientRect().top if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
if top > 0 next = $.x 'following::td[@class="reply"]', td
reply.className = 'replyhl' rect = next.getBoundingClientRect()
break if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the next
break next.className = 'replyhl'
when "K" return
if e.shiftKey
if not g.REPLY then [root] = getThread() replies = $$ 'td.reply', thread
if td = $ 'td.replyhl', root for reply in replies
td.className = 'reply' top = reply.getBoundingClientRect().top
rect = td.getBoundingClientRect() if top > 0
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're visible reply.className = 'replyhl'
prev = $.x 'preceding::td[@class="reply"][1]', td return
rect = prev.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the prev prev: (thread) ->
prev.className = 'replyhl' if td = $ 'td.replyhl', thread
return td.className = 'reply'
replies = $$ 'td.reply', root rect = td.getBoundingClientRect()
replies.reverse() if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
height = d.body.clientHeight prev = $.x 'preceding::td[@class="reply"][1]', td
for reply in replies rect = prev.getBoundingClientRect()
bot = reply.getBoundingClientRect().bottom if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the prev
if bot < height prev.className = 'replyhl'
reply.className = 'replyhl' return
break
break replies = $$ 'td.reply', thread
when "M" replies.reverse()
if e.shiftKey height = d.body.clientHeight
$("#imageExpand").click() for reply in replies
else bot = reply.getBoundingClientRect().bottom
if not g.REPLY then [root] = getThread() if bot < height
unless image = $ 'td.replyhl span.filesize ~ a[target]', root reply.className = 'replyhl'
image = $ 'span.filesize ~ a[target]', root return
imageToggle image
nav = nav =
#TODO page nav #TODO page nav
@ -737,6 +725,8 @@ nav =
next: -> next: ->
nav.scroll +1 nav.scroll +1
threads: []
getThread: (full) -> getThread: (full) ->
for thread, i in nav.threads for thread, i in nav.threads
rect = thread.getBoundingClientRect() rect = thread.getBoundingClientRect()
@ -745,6 +735,7 @@ nav =
if full if full
return [thread, i, rect] return [thread, i, rect]
return thread return thread
return null
scroll: (delta) -> scroll: (delta) ->
[thread, i, rect] = nav.getThread true [thread, i, rect] = nav.getThread true