From af8c3661755479954ba3a4f074c0bfc7b7a382f1 Mon Sep 17 00:00:00 2001 From: James Campos Date: Sat, 9 Apr 2011 14:33:01 -0700 Subject: [PATCH] rewrite get/setThread for cleaner api keep on using nav.up/down in keybinds so I don't depend on the api's implementation, which I might change again. --- 4chan_x.js | 75 +++++++++++++++++++++++++-------------------------- script.coffee | 65 ++++++++++++++++++++++---------------------- 2 files changed, 68 insertions(+), 72 deletions(-) diff --git a/4chan_x.js b/4chan_x.js index 46091b379..fb61f5d76 100644 --- a/4chan_x.js +++ b/4chan_x.js @@ -734,11 +734,11 @@ case 'm': break; case 'n': - return nav.down(); + return nav.next(); case 'o': break; case 'p': - return nav.up(); + return nav.prev(); case 'u': break; case 'w': @@ -870,54 +870,52 @@ }; nav = { init: function() { - var down, span, up; + var next, prev, span; span = $.el('span', { id: 'navlinks' }); - up = $.el('a', { + prev = $.el('a', { textContent: '▲' }); - down = $.el('a', { + next = $.el('a', { textContent: '▼' }); - $.bind(up, 'click', nav.up); - $.bind(down, 'click', nav.down); - $.append(span, up, $.tn(' '), down); - return $.append(d.body, span); + $.bind(prev, 'click', nav.prev); + $.bind(next, 'click', nav.next); + $.append(span, prev, $.tn(' '), next); + $.append(d.body, span); + return nav.threads = $$('div.thread'); }, - up: function() { - var i, rect, thread, top, _ref; - _ref = nav.getThread(), thread = _ref[0], i = _ref[1], rect = _ref[2]; - top = rect.top; - if (top > 1) { - i = -1; - } else if (Math.floor(Math.abs(top)) === 0) { - i -= 1; - } - return nav.setThread(i); + prev: function() { + return nav.scroll(-1); }, - down: function() { - var i, rect, thread, _ref; - _ref = nav.getThread(), thread = _ref[0], i = _ref[1], rect = _ref[2]; - if (!(rect.top > 1)) { - i += 1; - } - return nav.setThread(i); + next: function() { + return nav.scroll(+1); }, - getThread: function() { - var bottom, i, rect, thread, threads, _len; - nav.threads = threads = $$('div.thread'); - for (i = 0, _len = threads.length; i < _len; i++) { - thread = threads[i]; + getThread: function(full) { + var bottom, i, rect, thread, _len, _ref, _results; + _ref = nav.threads; + _results = []; + for (i = 0, _len = _ref.length; i < _len; i++) { + thread = _ref[i]; rect = thread.getBoundingClientRect(); bottom = rect.bottom; if (bottom > 0) { - return [thread, i, rect]; + if (full) { + return [thread, i, rect]; + } + return thread; } } + return _results; }, - setThread: function(i) { - var top; + scroll: function(delta) { + var i, rect, thread, top, _ref; + _ref = nav.getThread(true), thread = _ref[0], i = _ref[1], rect = _ref[2]; + top = rect.top; + if (!((delta === -1 && Math.ceil(top) < 0) || (delta === +1 && top > 1))) { + i += delta; + } if (i === -1) { window.scrollTo(0, 0); return; @@ -927,8 +925,7 @@ return; } top = nav.threads[i].getBoundingClientRect().top; - window.scrollBy(0, top); - return delete nav.threads; + return window.scrollBy(0, top); } }; scrollThread = function(count) { @@ -2205,12 +2202,12 @@ if ($.config('Thread Hiding')) { threadHiding.init(); } - if ($.config('Auto Watch')) { - $.bind($('form[name=post]'), 'submit', autoWatch); - } if ($.config('Thread Navigation')) { nav.init(); } + if ($.config('Auto Watch')) { + $.bind($('form[name=post]'), 'submit', autoWatch); + } if ($.config('Thread Expansion')) { omitted = $$('span.omittedposts'); for (_k = 0, _len3 = omitted.length; _k < _len3; _k++) { diff --git a/script.coffee b/script.coffee index 09c6604ab..222c40d66 100644 --- a/script.coffee +++ b/script.coffee @@ -552,12 +552,12 @@ keybinds = #expand img return when 'n' - nav.down() + nav.next() when 'o' #open in new tab return when 'p' - nav.up() + nav.prev() when 'u' #update now return @@ -661,54 +661,53 @@ nav = init: -> span = $.el 'span', id: 'navlinks' - up = $.el 'a', + prev = $.el 'a', textContent: '▲' - down = $.el 'a', + next = $.el 'a', textContent: '▼' - $.bind up, 'click', nav.up - $.bind down, 'click', nav.down + $.bind prev, 'click', nav.prev + $.bind next, 'click', nav.next - $.append span, up, $.tn(' '), down + $.append span, prev, $.tn(' '), next $.append d.body, span - up: -> - [thread, i, rect] = nav.getThread() - {top} = rect + nav.threads = $$ 'div.thread' - if top > 1 - i = -1 - else if Math.floor(Math.abs(top)) == 0 - #only move to prev thread if we're at the start of current one - #XXX fucking fractional scrolls - i -= 1 - nav.setThread i + prev: -> + nav.scroll -1 - down: -> - [thread, i, rect] = nav.getThread() - unless rect.top > 1 # if rect.top > 1, we're above the first thread - i += 1 - nav.setThread i + next: -> + nav.scroll +1 - getThread: -> - nav.threads = threads = $$ 'div.thread' - for thread, i in threads + getThread: (full) -> + for thread, i in nav.threads rect = thread.getBoundingClientRect() {bottom} = rect if bottom > 0 #we have not scrolled past - return [thread, i, rect] + if full + return [thread, i, rect] + return thread - setThread: (i) -> - if i == -1 + scroll: (delta) -> + [thread, i, rect] = nav.getThread true + {top} = rect + + #unless we're not at the beginning of the current thread + # (and thus wanting to move to beginning) + # or we're above the first thread and don't want to skip it + unless (delta is -1 and Math.ceil(top) < 0) or (delta is +1 and top > 1) + i += delta + + if i is -1 window.scrollTo 0, 0 return - if i == 10 + if i is 10 window.location = "#{g.PAGENUM + 1}#p0" return {top} = nav.threads[i].getBoundingClientRect() window.scrollBy 0, top - delete nav.threads scrollThread = (count) -> [thread, idx] = getThread() @@ -1754,12 +1753,12 @@ else #not reply if $.config 'Thread Hiding' threadHiding.init() - if $.config 'Auto Watch' - $.bind $('form[name=post]'), 'submit', autoWatch - if $.config 'Thread Navigation' nav.init() + if $.config 'Auto Watch' + $.bind $('form[name=post]'), 'submit', autoWatch + if $.config 'Thread Expansion' omitted = $$('span.omittedposts') for span in omitted