From 487b9611062a16e78d3f5617474c36656560ea04 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Tue, 19 Jul 2011 20:46:04 +0200 Subject: [PATCH] Cleanup updater, cache elements. --- 4chan_x.user.js | 74 ++++++++++++++++++++----------------------- script.coffee | 83 +++++++++++++++++++++++-------------------------- 2 files changed, 72 insertions(+), 85 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 8c3ca8140..e9a335047 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -1563,51 +1563,47 @@ }; updater = { init: function() { - var autoUpT, checked, conf, dialog, html, input, interva, name, title, updNow, verbose, _i, _len, _ref; - html = "
-" + ($.config('Interval')) + "
"; + var autoUpT, checked, conf, dialog, html, input, interva, interval, name, title, updNow, verbose, _i, _len, _ref; + interval = $.config('Interval'); + html = "
-" + interval + "
"; conf = config.updater.checkbox; for (name in conf) { title = conf[name][1]; - checked = $.config(name) ? "checked" : ""; - html += "
"; + checked = $.config(name) ? 'checked' : ''; + html += "
"; } - name = 'Auto Update This'; - title = 'Controls whether *this* thread auotmatically updates or not'; checked = $.config('Auto Update') ? 'checked' : ''; - html += "
"; - html += "
"; - html += "
"; + html += "
"; dialog = ui.dialog('updater', { - bottom: '0px', - right: '0px' + bottom: '0', + right: '0' }, html); _ref = $$('input[type=checkbox]', dialog); for (_i = 0, _len = _ref.length; _i < _len; _i++) { input = _ref[_i]; $.bind(input, 'click', $.cb.checked); } - $.bind($('input[type=text]', dialog), 'change', $.cb.value); - verbose = $('input[name=\"Verbose\"]', dialog); - autoUpT = $('input[name=\"Auto Update This\"]', dialog); - interva = $('input[name=\"Interval\"]', dialog); + verbose = $('input[name=Verbose]', dialog); + autoUpT = $('input[name$=This]', dialog); + interva = $('input[name=Interval]', dialog); updNow = $('input[type=button]', dialog); $.bind(verbose, 'click', updater.cb.verbose); $.bind(autoUpT, 'click', updater.cb.autoUpdate); + $.bind(interva, 'change', $.cb.value); $.bind(updNow, 'click', updater.updateNow); + updater.count = $('#count', dialog); + updater.timer = $('#timer', dialog); $.append(d.body, dialog); updater.cb.verbose.call(verbose); return updater.cb.autoUpdate.call(autoUpT); }, cb: { verbose: function(e) { - var count, timer; - count = $('#count'); - timer = $('#timer'); if (this.checked) { - count.textContent = '+0'; - return $.show(timer); + updater.count.textContent = '+0'; + return $.show(updater.timer); } else { - $.extend(count, { + $.extend(updater.count, { className: '', textContent: 'Thread Updater' }); @@ -1622,15 +1618,13 @@ } }, update: function(e) { - var arr, body, br, count, id, input, replies, reply, timer, _i, _len, _ref, _ref2, _results; - count = $('#count'); - timer = $('#timer'); + var arr, body, br, id, input, replies, reply, _i, _len, _ref, _ref2, _results; if (this.status === 404) { - count.textContent = 404; - count.className = 'error'; - timer.textContent = ''; + updater.timer.textContent = ''; + updater.count.textContent = 404; + updater.count.className = 'error'; window.clearInterval(updater.intervalID); - _ref = $$('input[type=submit]'); + _ref = $$('#com_submit'); for (_i = 0, _len = _ref.length; _i < _len; _i++) { input = _ref[_i]; input.disabled = true; @@ -1651,13 +1645,13 @@ while ((reply = replies.pop()) && (reply.id > id)) { arr.push(reply.parentNode.parentNode.parentNode); } - timer.textContent = '-' + $.config('Interval'); + updater.timer.textContent = '-' + $.config('Interval'); if ($.config('Verbose')) { - count.textContent = '+' + arr.length; + updater.count.textContent = '+' + arr.length; if (arr.length === 0) { - count.className = ''; + updater.count.className = ''; } else { - count.className = 'new'; + updater.count.className = 'new'; } } _results = []; @@ -1668,23 +1662,21 @@ } }, timeout: function() { - var count, n, timer; - timer = $('#timer'); - n = Number(timer.textContent); - n += 1; + var n; + n = Number(updater.timer.textContent); + ++n; if (n === 10) { - count = $('#count'); - count.textContent = 'retry'; - count.className = ''; + updater.count.textContent = 'retry'; + updater.count.className = ''; n = 0; } - timer.textContent = n; + updater.timer.textContent = n; if (n === 0) { return updater.update(); } }, updateNow: function() { - $('#timer').textContent = 0; + updater.timer.textContent = 0; return updater.update(); }, update: function() { diff --git a/script.coffee b/script.coffee index b6daec2c4..0cf4e46d9 100644 --- a/script.coffee +++ b/script.coffee @@ -1268,34 +1268,36 @@ threadHiding = updater = init: -> - html = "
-#{$.config 'Interval'}
" + interval = $.config 'Interval' + html = "
-#{interval}
" conf = config.updater.checkbox for name of conf title = conf[name][1] - checked = if $.config name then "checked" else "" - html += "
" + checked = if $.config name then 'checked' else '' + html += "
" - name = 'Auto Update This' - title = 'Controls whether *this* thread auotmatically updates or not' checked = if $.config 'Auto Update' then 'checked' else '' - html += "
" + html += " +
+
+
" - html += "
" - html += "
" - - dialog = ui.dialog 'updater', bottom: '0px', right: '0px', html + dialog = ui.dialog 'updater', bottom: '0', right: '0', html for input in $$ 'input[type=checkbox]', dialog $.bind input, 'click', $.cb.checked - $.bind $('input[type=text]', dialog), 'change', $.cb.value - verbose = $ 'input[name=\"Verbose\"]', dialog - autoUpT = $ 'input[name=\"Auto Update This\"]', dialog - interva = $ 'input[name=\"Interval\"]', dialog - updNow = $ 'input[type=button]', dialog - $.bind verbose, 'click', updater.cb.verbose - $.bind autoUpT, 'click', updater.cb.autoUpdate - $.bind updNow, 'click', updater.updateNow + verbose = $ 'input[name=Verbose]', dialog + autoUpT = $ 'input[name$=This]', dialog + interva = $ 'input[name=Interval]', dialog + updNow = $ 'input[type=button]', dialog + $.bind verbose, 'click', updater.cb.verbose + $.bind autoUpT, 'click', updater.cb.autoUpdate + $.bind interva, 'change', $.cb.value + $.bind updNow, 'click', updater.updateNow + + updater.count = $ '#count', dialog + updater.timer = $ '#timer', dialog $.append d.body, dialog @@ -1304,13 +1306,11 @@ updater = cb: verbose: (e) -> - count = $ '#count' - timer = $ '#timer' if @checked - count.textContent = '+0' - $.show timer + updater.count.textContent = '+0' + $.show updater.timer else - $.extend count, + $.extend updater.count, className: '' textContent: 'Thread Updater' $.hide timer @@ -1320,15 +1320,12 @@ updater = else window.clearInterval updater.intervalID update: (e) -> - count = $ '#count' - timer = $ '#timer' - if @status is 404 - count.textContent = 404 - count.className = 'error' - timer.textContent = '' + updater.timer.textContent = '' + updater.count.textContent = 404 + updater.count.className = 'error' window.clearInterval updater.intervalID - for input in $$ 'input[type=submit]' + for input in $$ '#com_submit' input.disabled = true input.value = 404 d.title = d.title.match(/.+- /)[0] + 404 @@ -1346,36 +1343,34 @@ updater = while (reply = replies.pop()) and (reply.id > id) arr.push reply.parentNode.parentNode.parentNode #table - timer.textContent = '-' + $.config 'Interval' + updater.timer.textContent = '-' + $.config 'Interval' if $.config 'Verbose' - count.textContent = '+' + arr.length + updater.count.textContent = '+' + arr.length if arr.length is 0 - count.className = '' + updater.count.className = '' else - count.className = 'new' + updater.count.className = 'new' #XXX add replies in correct order so /b/acklinks resolve while reply = arr.pop() $.before br, reply timeout: -> - timer = $ '#timer' - n = Number timer.textContent - n += 1 + n = Number updater.timer.textContent + ++n - if n == 10 - count = $ '#count' - count.textContent = 'retry' - count.className = '' + if n is 10 + updater.count.textContent = 'retry' + updater.count.className = '' n = 0 - timer.textContent = n + updater.timer.textContent = n - if n == 0 + if n is 0 updater.update() updateNow: -> - $('#timer').textContent = 0 + updater.timer.textContent = 0 updater.update() update: ->