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,79 +819,80 @@
} }
}, },
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;
}
char = g.char;
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 { } else {
thread = getThread()[0]; if (!(image = $('td.replyhl span.filesize ~ a[target]', thread))) {
image = $('span.filesize ~ a[target]', thread);
}
return imageToggle(image);
}
},
qr: function(thread, quote) {
var e, qrLink;
if (!(qrLink = $('td.replyhl span[id] a:not(:first-child)', thread))) { if (!(qrLink = $('td.replyhl span[id] a:not(:first-child)', thread))) {
qrLink = $("span#nothread" + thread.id + " a:not(:first-child)", thread); qrLink = $("span[id^=nothread] a:not(:first-child)", thread);
} }
} if (quote) {
if (e.shiftKey) {
$.append(d.body, qr.dialog(qrLink));
return $('#qr textarea').focus();
} else {
e = { e = {
preventDefault: function() {}, preventDefault: function() {},
target: qrLink target: qrLink
}; };
return qr.cb.quote(e); return qr.cb.quote(e);
} else {
if (!$('#qr')) {
qr.dialog(qrLink);
} }
break; return $('#qr textarea').focus();
case "J":
if (e.shiftKey) {
if (!g.REPLY) {
root = getThread()[0];
} }
if (td = $('td.replyhl', root)) { },
open: function(thread, tab) {
var id, url;
id = thread.firstChild.id;
url = "http://boards.4chan.org/" + g.BOARD + "/res/" + id;
if (tab) {
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'; td.className = 'reply';
rect = td.getBoundingClientRect(); rect = td.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) { if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
@ -903,23 +904,19 @@
return; return;
} }
} }
replies = $$('td.reply', root); replies = $$('td.reply', thread);
for (_i = 0, _len = replies.length; _i < _len; _i++) { for (_i = 0, _len = replies.length; _i < _len; _i++) {
reply = replies[_i]; reply = replies[_i];
top = reply.getBoundingClientRect().top; top = reply.getBoundingClientRect().top;
if (top > 0) { if (top > 0) {
reply.className = 'replyhl'; reply.className = 'replyhl';
break; return;
} }
} }
} },
break; prev: function(thread) {
case "K": var bot, height, prev, rect, replies, reply, td, _i, _len;
if (e.shiftKey) { if (td = $('td.replyhl', thread)) {
if (!g.REPLY) {
root = getThread()[0];
}
if (td = $('td.replyhl', root)) {
td.className = 'reply'; td.className = 'reply';
rect = td.getBoundingClientRect(); rect = td.getBoundingClientRect();
if (rect.top > 0 && rect.bottom < d.body.clientHeight) { if (rect.top > 0 && rect.bottom < d.body.clientHeight) {
@ -931,31 +928,18 @@
return; return;
} }
} }
replies = $$('td.reply', root); replies = $$('td.reply', thread);
replies.reverse(); replies.reverse();
height = d.body.clientHeight; height = d.body.clientHeight;
for (_j = 0, _len2 = replies.length; _j < _len2; _j++) { for (_i = 0, _len = replies.length; _i < _len; _i++) {
reply = replies[_j]; reply = replies[_i];
bot = reply.getBoundingClientRect().bottom; bot = reply.getBoundingClientRect().bottom;
if (bot < height) { if (bot < height) {
reply.className = 'replyhl'; reply.className = 'replyhl';
break; return;
} }
} }
} }
break;
case "M":
if (e.shiftKey) {
return $("#imageExpand").click();
} else {
if (!g.REPLY) {
root = getThread()[0];
}
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
switch char
when "I"
if g.REPLY
unless qrLink = $ 'td.replyhl span[id] a:not(:first-child)'
qrLink = $ "span[id^=nothread] a:not(:first-child)"
else else
[thread] = getThread() unless image = $ 'td.replyhl span.filesize ~ a[target]', thread
image = $ 'span.filesize ~ a[target]', thread
imageToggle image
qr: (thread, quote) ->
unless qrLink = $ 'td.replyhl span[id] 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 qrLink = $ "span[id^=nothread] a:not(:first-child)", thread
if e.shiftKey
$.append d.body, qr.dialog qrLink if quote
$('#qr textarea').focus()
else
# qrLink.click() doesn't work, so use this hack # qrLink.click() doesn't work, so use this hack
e = e =
preventDefault: -> preventDefault: ->
target: qrLink target: qrLink
qr.cb.quote e qr.cb.quote e
when "J" else
if e.shiftKey unless $ '#qr'
if not g.REPLY then [root] = getThread() qr.dialog qrLink
if td = $ 'td.replyhl', root $('#qr textarea').focus()
open: (thread, tab) ->
id = thread.firstChild.id
url = "http://boards.4chan.org/#{g.BOARD}/res/#{id}"
if tab
GM_openInTab url
else
location.href = url
hl:
next: (thread) ->
if td = $ 'td.replyhl', thread
td.className = 'reply' td.className = 'reply'
rect = td.getBoundingClientRect() rect = td.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're visible if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
next = $.x 'following::td[@class="reply"]', td next = $.x 'following::td[@class="reply"]', td
rect = next.getBoundingClientRect() rect = next.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the next if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the next
next.className = 'replyhl' next.className = 'replyhl'
return return
replies = $$ 'td.reply', root
replies = $$ 'td.reply', thread
for reply in replies for reply in replies
top = reply.getBoundingClientRect().top top = reply.getBoundingClientRect().top
if top > 0 if top > 0
reply.className = 'replyhl' reply.className = 'replyhl'
break return
break
when "K" prev: (thread) ->
if e.shiftKey if td = $ 'td.replyhl', thread
if not g.REPLY then [root] = getThread()
if td = $ 'td.replyhl', root
td.className = 'reply' td.className = 'reply'
rect = td.getBoundingClientRect() rect = td.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #you're visible if rect.top > 0 and rect.bottom < d.body.clientHeight #you're fully visible
prev = $.x 'preceding::td[@class="reply"][1]', td prev = $.x 'preceding::td[@class="reply"][1]', td
rect = prev.getBoundingClientRect() rect = prev.getBoundingClientRect()
if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the prev if rect.top > 0 and rect.bottom < d.body.clientHeight #and so is the prev
prev.className = 'replyhl' prev.className = 'replyhl'
return return
replies = $$ 'td.reply', root
replies = $$ 'td.reply', thread
replies.reverse() replies.reverse()
height = d.body.clientHeight height = d.body.clientHeight
for reply in replies for reply in replies
bot = reply.getBoundingClientRect().bottom bot = reply.getBoundingClientRect().bottom
if bot < height if bot < height
reply.className = 'replyhl' reply.className = 'replyhl'
break return
break
when "M"
if e.shiftKey
$("#imageExpand").click()
else
if not g.REPLY then [root] = getThread()
unless image = $ 'td.replyhl span.filesize ~ a[target]', root
image = $ 'span.filesize ~ a[target]', root
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