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.
This commit is contained in:
James Campos 2011-04-09 14:33:01 -07:00
parent 71443a5c31
commit af8c366175
2 changed files with 68 additions and 72 deletions

View File

@ -734,11 +734,11 @@
case 'm': case 'm':
break; break;
case 'n': case 'n':
return nav.down(); return nav.next();
case 'o': case 'o':
break; break;
case 'p': case 'p':
return nav.up(); return nav.prev();
case 'u': case 'u':
break; break;
case 'w': case 'w':
@ -870,54 +870,52 @@
}; };
nav = { nav = {
init: function() { init: function() {
var down, span, up; var next, prev, span;
span = $.el('span', { span = $.el('span', {
id: 'navlinks' id: 'navlinks'
}); });
up = $.el('a', { prev = $.el('a', {
textContent: '▲' textContent: '▲'
}); });
down = $.el('a', { next = $.el('a', {
textContent: '▼' textContent: '▼'
}); });
$.bind(up, 'click', nav.up); $.bind(prev, 'click', nav.prev);
$.bind(down, 'click', nav.down); $.bind(next, 'click', nav.next);
$.append(span, up, $.tn(' '), down); $.append(span, prev, $.tn(' '), next);
return $.append(d.body, span); $.append(d.body, span);
return nav.threads = $$('div.thread');
}, },
up: function() { prev: function() {
var i, rect, thread, top, _ref; return nav.scroll(-1);
_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);
}, },
down: function() { next: function() {
var i, rect, thread, _ref; return nav.scroll(+1);
_ref = nav.getThread(), thread = _ref[0], i = _ref[1], rect = _ref[2];
if (!(rect.top > 1)) {
i += 1;
}
return nav.setThread(i);
}, },
getThread: function() { getThread: function(full) {
var bottom, i, rect, thread, threads, _len; var bottom, i, rect, thread, _len, _ref, _results;
nav.threads = threads = $$('div.thread'); _ref = nav.threads;
for (i = 0, _len = threads.length; i < _len; i++) { _results = [];
thread = threads[i]; for (i = 0, _len = _ref.length; i < _len; i++) {
thread = _ref[i];
rect = thread.getBoundingClientRect(); rect = thread.getBoundingClientRect();
bottom = rect.bottom; bottom = rect.bottom;
if (bottom > 0) { if (bottom > 0) {
return [thread, i, rect]; if (full) {
return [thread, i, rect];
}
return thread;
} }
} }
return _results;
}, },
setThread: function(i) { scroll: function(delta) {
var top; 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) { if (i === -1) {
window.scrollTo(0, 0); window.scrollTo(0, 0);
return; return;
@ -927,8 +925,7 @@
return; return;
} }
top = nav.threads[i].getBoundingClientRect().top; top = nav.threads[i].getBoundingClientRect().top;
window.scrollBy(0, top); return window.scrollBy(0, top);
return delete nav.threads;
} }
}; };
scrollThread = function(count) { scrollThread = function(count) {
@ -2205,12 +2202,12 @@
if ($.config('Thread Hiding')) { if ($.config('Thread Hiding')) {
threadHiding.init(); threadHiding.init();
} }
if ($.config('Auto Watch')) {
$.bind($('form[name=post]'), 'submit', autoWatch);
}
if ($.config('Thread Navigation')) { if ($.config('Thread Navigation')) {
nav.init(); nav.init();
} }
if ($.config('Auto Watch')) {
$.bind($('form[name=post]'), 'submit', autoWatch);
}
if ($.config('Thread Expansion')) { if ($.config('Thread Expansion')) {
omitted = $$('span.omittedposts'); omitted = $$('span.omittedposts');
for (_k = 0, _len3 = omitted.length; _k < _len3; _k++) { for (_k = 0, _len3 = omitted.length; _k < _len3; _k++) {

View File

@ -552,12 +552,12 @@ keybinds =
#expand img #expand img
return return
when 'n' when 'n'
nav.down() nav.next()
when 'o' when 'o'
#open in new tab #open in new tab
return return
when 'p' when 'p'
nav.up() nav.prev()
when 'u' when 'u'
#update now #update now
return return
@ -661,54 +661,53 @@ nav =
init: -> init: ->
span = $.el 'span', span = $.el 'span',
id: 'navlinks' id: 'navlinks'
up = $.el 'a', prev = $.el 'a',
textContent: '' textContent: ''
down = $.el 'a', next = $.el 'a',
textContent: '' textContent: ''
$.bind up, 'click', nav.up $.bind prev, 'click', nav.prev
$.bind down, 'click', nav.down $.bind next, 'click', nav.next
$.append span, up, $.tn(' '), down $.append span, prev, $.tn(' '), next
$.append d.body, span $.append d.body, span
up: -> nav.threads = $$ 'div.thread'
[thread, i, rect] = nav.getThread()
{top} = rect
if top > 1 prev: ->
i = -1 nav.scroll -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
down: -> next: ->
[thread, i, rect] = nav.getThread() nav.scroll +1
unless rect.top > 1 # if rect.top > 1, we're above the first thread
i += 1
nav.setThread i
getThread: -> getThread: (full) ->
nav.threads = threads = $$ 'div.thread' for thread, i in nav.threads
for thread, i in threads
rect = thread.getBoundingClientRect() rect = thread.getBoundingClientRect()
{bottom} = rect {bottom} = rect
if bottom > 0 #we have not scrolled past if bottom > 0 #we have not scrolled past
return [thread, i, rect] if full
return [thread, i, rect]
return thread
setThread: (i) -> scroll: (delta) ->
if i == -1 [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 window.scrollTo 0, 0
return return
if i == 10 if i is 10
window.location = "#{g.PAGENUM + 1}#p0" window.location = "#{g.PAGENUM + 1}#p0"
return return
{top} = nav.threads[i].getBoundingClientRect() {top} = nav.threads[i].getBoundingClientRect()
window.scrollBy 0, top window.scrollBy 0, top
delete nav.threads
scrollThread = (count) -> scrollThread = (count) ->
[thread, idx] = getThread() [thread, idx] = getThread()
@ -1754,12 +1753,12 @@ else #not reply
if $.config 'Thread Hiding' if $.config 'Thread Hiding'
threadHiding.init() threadHiding.init()
if $.config 'Auto Watch'
$.bind $('form[name=post]'), 'submit', autoWatch
if $.config 'Thread Navigation' if $.config 'Thread Navigation'
nav.init() nav.init()
if $.config 'Auto Watch'
$.bind $('form[name=post]'), 'submit', autoWatch
if $.config 'Thread Expansion' if $.config 'Thread Expansion'
omitted = $$('span.omittedposts') omitted = $$('span.omittedposts')
for span in omitted for span in omitted