rewrite thread expansion

This commit is contained in:
James Campos 2011-04-16 17:36:09 -07:00
parent eec5d403f9
commit 510f83954c
2 changed files with 156 additions and 120 deletions

View File

@ -59,7 +59,7 @@
*/ */
(function() { (function() {
var $, $$, NAMESPACE, a, as, 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, omitted, onloadComment, onloadThread, option, options, parseResponse, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, span, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _l, _len, _len2, _len3, _len4, _len5, _m, _ref, _ref2, _ref3, _ref4; var $, $$, NAMESPACE, a, as, 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, onloadComment, option, options, parseResponse, pathname, qr, recaptcha, recaptchaListener, recaptchaReload, redirect, replyHiding, replyNav, report, scroll, scrollThread, temp, text, threadHiding, tzOffset, ui, updateFavicon, updateTitle, updater, watcher, _config, _i, _j, _k, _l, _len, _len2, _len3, _len4, _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;
@ -439,38 +439,91 @@
id: href.match(/\d+/)[0] id: href.match(/\d+/)[0]
}); });
}; };
expandThread = function() { expandThread = {
var id, num, prev, r, span, table, xhr, _i, _len, _ref; init: function() {
id = $.x('preceding-sibling::input[1]', this).name; var a, span, _i, _len, _ref, _results;
span = this; _ref = $$('span.omittedposts');
if (span.textContent[0] === '-') { _results = [];
num = board === 'b' ? 3 : 5; for (_i = 0, _len = _ref.length; _i < _len; _i++) {
table = $.x("following::br[@clear][1]/preceding::table[" + num + "]", span); span = _ref[_i];
while ((prev = table.previousSibling) && (prev.nodeName === 'TABLE')) { a = $.el('a', {
$.remove(prev); textContent: "+ " + span.textContent,
className: 'omittedposts'
});
$.bind(a, 'click', expandThread.cb.toggle);
_results.push($.replace(span, a));
} }
span.textContent = span.textContent.replace('-', '+'); return _results;
return; },
} cache: {},
span.textContent = span.textContent.replace('+', 'X Loading...'); requests: {},
_ref = g.xhrs; cb: {
for (_i = 0, _len = _ref.length; _i < _len; _i++) { toggle: function(e) {
xhr = _ref[_i]; var thread;
if (xhr.id === id) { thread = e.target.parentNode;
onloadThread(xhr.r.responseText, span); return expandThread.toggle(thread);
return; },
load: function(xhr, thread, a) {
var html, id;
if (xhr.status === 404) {
a.textContent.replace('X Loading...', '404');
return $.unbind(a, 'click', expandThread.cb.toggle);
} else {
html = xhr.responseText;
id = thread.firstChild.id;
expandThread.cache[id] = html;
return expandThread.expand(html, thread, a);
}
} }
},
toggle: function(thread) {
var a, html, id, num, prev, table, _results;
id = thread.firstChild.id;
a = $('a.omittedposts', thread);
switch (a.textContent[0]) {
case '+':
a.textContent = a.textContent.replace('+', 'X Loading...');
if (html = expandThread.cache[id]) {
return expandThread.expand(html, thread, a);
} else {
return expandThread.requests[id] = $.get("res/" + id, (function() {
return expandThread.cb.load(this, thread, a);
}));
}
break;
case 'X':
a.textContent = a.textContent.replace('X Loading...', '+');
return expandThread.requests[id].abort();
case '-':
a.textContent = a.textContent.replace('-', '+');
num = g.BOARD === 'b' ? 3 : 5;
table = $.x("following::br[@clear][1]/preceding::table[" + num + "]", a);
_results = [];
while ((prev = table.previousSibling) && (prev.nodeName === 'TABLE')) {
_results.push($.remove(prev));
}
return _results;
}
},
expand: function(html, thread, a) {
var body, br, next, table, tables, _i, _len, _results;
a.textContent = a.textContent.replace('X Loading...', '-');
while ((next = a.nextSibling) && !next.clear) {
$.remove(next);
}
br = next;
body = $.el('body', {
innerHTML: html
});
tables = $$('form[name=delform] table', body);
tables.pop();
_results = [];
for (_i = 0, _len = tables.length; _i < _len; _i++) {
table = tables[_i];
_results.push($.before(br, table));
}
return _results;
} }
r = new XMLHttpRequest();
r.onload = function() {
return onloadThread(this.responseText, span);
};
r.open('GET', "res/" + id, true);
r.send();
return g.xhrs.push({
r: r,
id: id
});
}; };
replyHiding = { replyHiding = {
init: function() { init: function() {
@ -1004,31 +1057,6 @@
bq = $.x('ancestor::blockquote', a); bq = $.x('ancestor::blockquote', a);
return bq.innerHTML = html; return bq.innerHTML = html;
}; };
onloadThread = function(responseText, span) {
var div, next, opbq, replies, reply, _i, _j, _len, _len2, _ref, _results, _results2;
_ref = parseResponse(responseText), replies = _ref[0], opbq = _ref[1];
span.textContent = span.textContent.replace('X Loading...', '- ');
span.previousSibling.innerHTML = opbq.innerHTML;
while ((next = span.nextSibling) && !next.clear) {
$.remove(next);
}
if (next) {
_results = [];
for (_i = 0, _len = replies.length; _i < _len; _i++) {
reply = replies[_i];
_results.push($.before(next, $.x('ancestor::table', reply)));
}
return _results;
} else {
div = span.parentNode;
_results2 = [];
for (_j = 0, _len2 = replies.length; _j < _len2; _j++) {
reply = replies[_j];
_results2.push($.append(div, $.x('ancestor::table', reply)));
}
return _results2;
}
};
changeCheckbox = function() { changeCheckbox = function() {
return GM_setValue(this.name, this.checked); return GM_setValue(this.name, this.checked);
}; };
@ -2178,28 +2206,19 @@
$.bind($('form[name=post]'), 'submit', autoWatch); $.bind($('form[name=post]'), 'submit', autoWatch);
} }
if ($.config('Thread Expansion')) { if ($.config('Thread Expansion')) {
omitted = $$('span.omittedposts'); expandThread.init();
for (_k = 0, _len3 = omitted.length; _k < _len3; _k++) {
span = omitted[_k];
a = $.el('a', {
className: 'omittedposts',
textContent: "+ " + span.textContent
});
$.bind(a, 'click', expandThread);
$.replace(span, a);
}
} }
if ($.config('Comment Expansion')) { if ($.config('Comment Expansion')) {
as = $$('span.abbr a'); as = $$('span.abbr a');
for (_l = 0, _len4 = as.length; _l < _len4; _l++) { for (_k = 0, _len3 = as.length; _k < _len3; _k++) {
a = as[_l]; a = as[_k];
$.bind(a, 'click', expandComment); $.bind(a, 'click', expandComment);
} }
} }
} }
_ref4 = g.callbacks; _ref4 = g.callbacks;
for (_m = 0, _len5 = _ref4.length; _m < _len5; _m++) { for (_l = 0, _len4 = _ref4.length; _l < _len4; _l++) {
callback = _ref4[_m]; callback = _ref4[_l];
callback(); callback();
} }
$.bind(d.body, 'DOMNodeInserted', nodeInserted); $.bind(d.body, 'DOMNodeInserted', nodeInserted);

View File

@ -293,35 +293,73 @@ expandComment = (e) ->
id: href.match(/\d+/)[0] id: href.match(/\d+/)[0]
} }
expandThread = -> expandThread =
id = $.x('preceding-sibling::input[1]', this).name init: ->
span = this for span in $$ 'span.omittedposts'
#close expanded thread a = $.el 'a',
if span.textContent[0] is '-' textContent: "+ #{span.textContent}"
#goddamit moot className: 'omittedposts'
num = if board is 'b' then 3 else 5 $.bind a, 'click', expandThread.cb.toggle
table = $.x "following::br[@clear][1]/preceding::table[#{num}]", span $.replace span, a
while (prev = table.previousSibling) and (prev.nodeName is 'TABLE')
$.remove prev cache: {}
span.textContent = span.textContent.replace '-', '+' requests: {}
return
span.textContent = span.textContent.replace '+', 'X Loading...' cb:
#load cache toggle: (e) ->
for xhr in g.xhrs thread = e.target.parentNode
if xhr.id == id expandThread.toggle thread
#why can't we just xhr.r.onload()?
onloadThread xhr.r.responseText, span load: (xhr, thread, a) ->
return if xhr.status is 404
#create new request a.textContent.replace 'X Loading...', '404'
r = new XMLHttpRequest() $.unbind a, 'click', expandThread.cb.toggle
r.onload = -> else
onloadThread @responseText, span html = xhr.responseText
r.open 'GET', "res/#{id}", true id = thread.firstChild.id
r.send() expandThread.cache[id] = html
g.xhrs.push { expandThread.expand html, thread, a
r: r,
id: id toggle: (thread) ->
} id = thread.firstChild.id
a = $ 'a.omittedposts', thread
switch a.textContent[0]
when '+'
a.textContent = a.textContent.replace '+', 'X Loading...'
if html = expandThread.cache[id]
expandThread.expand html, thread, a
else
expandThread.requests[id] =
$.get "res/#{id}", (-> expandThread.cb.load this, thread, a)
when 'X'
a.textContent = a.textContent.replace 'X Loading...', '+'
expandThread.requests[id].abort()
when '-'
a.textContent = a.textContent.replace '-', '+'
#goddamit moot
num = if g.BOARD is 'b' then 3 else 5
table = $.x "following::br[@clear][1]/preceding::table[#{num}]", a
while (prev = table.previousSibling) and (prev.nodeName is 'TABLE')
$.remove prev
expand: (html, thread, a) ->
a.textContent = a.textContent.replace 'X Loading...', '-'
# eat everything, then replace with fresh full posts
while (next = a.nextSibling) and not next.clear #br[clear]
$.remove next
br = next
body = $.el 'body',
innerHTML: html
tables = $$ 'form[name=delform] table', body
tables.pop()
for table in tables
$.before br, table
replyHiding = replyHiding =
init: -> init: ->
@ -770,21 +808,6 @@ onloadComment = (responseText, a, href) ->
bq = $.x 'ancestor::blockquote', a bq = $.x 'ancestor::blockquote', a
bq.innerHTML = html bq.innerHTML = html
onloadThread = (responseText, span) ->
[replies, opbq] = parseResponse responseText
span.textContent = span.textContent.replace 'X Loading...', '- '
#make sure all comments are fully expanded
span.previousSibling.innerHTML = opbq.innerHTML
while (next = span.nextSibling) and not next.clear#<br clear>
$.remove next
if next
for reply in replies
$.before next, $.x('ancestor::table', reply)
else#threading
div = span.parentNode
for reply in replies
$.append div, $.x('ancestor::table', reply)
changeCheckbox = -> changeCheckbox = ->
GM_setValue @name, @checked GM_setValue @name, @checked
@ -1735,13 +1758,7 @@ else #not reply
$.bind $('form[name=post]'), 'submit', autoWatch $.bind $('form[name=post]'), 'submit', autoWatch
if $.config 'Thread Expansion' if $.config 'Thread Expansion'
omitted = $$('span.omittedposts') expandThread.init()
for span in omitted
a = $.el 'a',
className: 'omittedposts'
textContent: "+ #{span.textContent}"
$.bind a, 'click', expandThread
$.replace(span, a)
if $.config 'Comment Expansion' if $.config 'Comment Expansion'
as = $$('span.abbr a') as = $$('span.abbr a')