Merge pull request #336 from MayhemYDG/init

Init ASAP, simplify declarations in ui.drag, remove unnecessary (e)s
This commit is contained in:
James Campos 2011-10-28 09:11:46 -07:00
commit 9e1adc596d
2 changed files with 197 additions and 174 deletions

View File

@ -61,7 +61,7 @@
*/
(function() {
var $, $$, DAY, Favicon, HOUR, MINUTE, Main, NAMESPACE, QR, SECOND, Time, anonymize, conf, config, d, expandComment, expandThread, filter, firstRun, flatten, g, getTitle, imgExpand, imgGif, imgHover, imgPreloading, key, keybinds, log, nav, options, pathname, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, revealSpoilers, sauce, temp, threadHiding, threadStats, threading, titlePost, ui, unread, updater, val, watcher;
var $, $$, DAY, Favicon, HOUR, MINUTE, Main, NAMESPACE, QR, SECOND, Time, anonymize, conf, config, d, expandComment, expandThread, filter, firstRun, flatten, g, getTitle, imgExpand, imgGif, imgHover, imgPreloading, key, keybinds, log, nav, options, quoteBacklink, quoteInline, quoteOP, quotePreview, redirect, replyHiding, reportButton, revealSpoilers, sauce, threadHiding, threadStats, threading, titlePost, ui, unread, updater, val, watcher;
var __slice = Array.prototype.slice;
config = {
main: {
@ -232,19 +232,11 @@
drag: function(e) {
var bottom, left, right, style, top;
left = e.clientX - ui.dx;
if (left < 10) {
left = '0';
} else if (ui.width - left < 10) {
left = null;
}
right = left ? null : 0;
top = e.clientY - ui.dy;
if (top < 10) {
top = '0';
} else if (ui.height - top < 10) {
top = null;
}
bottom = top ? null : 0;
left = left < 10 ? 0 : ui.width - left < 10 ? null : left;
top = top < 10 ? 0 : ui.height - top < 10 ? null : top;
right = left === null ? 0 : null;
bottom = top === null ? 0 : null;
style = ui.el.style;
style.top = top;
style.right = right;
@ -275,7 +267,7 @@
return style.right = clientWidth - clientX + 45;
}
},
hoverend: function(e) {
hoverend: function() {
return ui.el.parentNode.removeChild(ui.el);
}
};
@ -510,14 +502,6 @@
val = conf[key];
conf[key] = $.get(key, val);
}
pathname = location.pathname.substring(1).split('/');
g.BOARD = pathname[0], temp = pathname[1];
if (temp === 'res') {
g.REPLY = temp;
g.THREAD_ID = pathname[2];
} else {
g.PAGENUM = parseInt(temp) || 0;
}
$$ = function(selector, root) {
if (root == null) {
root = d.body;
@ -684,14 +668,14 @@
return _results;
},
cb: {
toggle: function(e) {
toggle: function() {
var thread;
thread = this.parentNode;
return expandThread.toggle(thread);
}
},
toggle: function(thread) {
var a, backlink, num, prev, table, threadID, _i, _len, _ref, _ref2, _results;
var a, backlink, num, pathname, prev, table, threadID, _i, _len, _ref, _ref2, _results;
threadID = thread.firstChild.id;
pathname = "/" + g.BOARD + "/res/" + threadID;
a = $('.omittedposts', thread);
@ -794,12 +778,12 @@
});
},
cb: {
hide: function(e) {
hide: function() {
var reply;
reply = this.parentNode.nextSibling;
return replyHiding.hide(reply);
},
show: function(e) {
show: function() {
var div, table;
div = this.parentNode;
table = div.nextSibling;
@ -1354,7 +1338,7 @@
options.time.call(time);
return options.backlink.call(back);
},
clearHidden: function(e) {
clearHidden: function() {
$["delete"]("hiddenReplies/" + g.BOARD + "/");
$["delete"]("hiddenThreads/" + g.BOARD + "/");
this.textContent = "hidden: 0";
@ -1370,14 +1354,14 @@
$.set(this.name, key);
return conf[this.name] = key;
},
time: function(e) {
time: function() {
$.set('time', this.value);
conf['time'] = this.value;
Time.foo();
Time.date = new Date();
return $('#timePreview').textContent = Time.funk(Time);
},
backlink: function(e) {
backlink: function() {
$.set('backlink', this.value);
conf['backlink'] = this.value;
return $('#backlinkPreview').textContent = conf['backlink'].replace(/%id/, '123456789');
@ -1734,6 +1718,7 @@
},
sys: function() {
var recaptcha;
$.unbind(d, 'DOMContentLoaded', QR.sys);
if (recaptcha = $('#recaptcha_response_field')) {
$.bind(recaptcha, 'keydown', QR.keydown);
return;
@ -1806,7 +1791,7 @@
init: function() {
var a, hiddenThreads, op, thread, _i, _len, _ref, _results;
hiddenThreads = $.get("hiddenThreads/" + g.BOARD + "/", {});
_ref = $$('div.thread');
_ref = $$('.thread');
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
thread = _ref[_i];
@ -1821,12 +1806,12 @@
return _results;
},
cb: {
hide: function(e) {
hide: function() {
var thread;
thread = this.parentNode.parentNode;
return threadHiding.hide(thread);
},
show: function(e) {
show: function() {
var thread;
thread = this.parentNode.parentNode;
return threadHiding.show(thread);
@ -2093,10 +2078,10 @@
return _results;
},
cb: {
toggle: function(e) {
toggle: function() {
return watcher.toggle(this.parentNode);
},
x: function(e) {
x: function() {
var board, id, _, _ref;
_ref = this.nextElementSibling.getAttribute('href').substring(1).split('/'), board = _ref[0], _ = _ref[1], id = _ref[2];
return watcher.unwatch(board, id);
@ -2191,15 +2176,20 @@
};
Time = {
init: function() {
var chanOffset;
Time.foo();
chanOffset = 5 - new Date().getTimezoneOffset() / 60;
if ($.isDST()) {
chanOffset--;
}
this.parse = Date.parse('10/11/11(Tue)18:53') ? function(node) {
return new Date(Date.parse(node.textContent) + g.chanOffset * HOUR);
return new Date(Date.parse(node.textContent) + chanOffset * HOUR);
} : function(node) {
var day, hour, min, month, year, _, _ref;
_ref = node.textContent.match(/(\d+)\/(\d+)\/(\d+)\(\w+\)(\d+):(\d+)/), _ = _ref[0], month = _ref[1], day = _ref[2], year = _ref[3], hour = _ref[4], min = _ref[5];
year = "20" + year;
month -= 1;
hour = g.chanOffset + Number(hour);
hour = chanOffset + Number(hour);
return new Date(year, month, day, hour, min);
};
return g.callbacks.push(Time.node);
@ -2400,7 +2390,7 @@
return this.classList.toggle('inlined');
},
add: function(q, id) {
var el, inline, root, threadID;
var el, inline, pathname, root, threadID;
root = q.parentNode.nodeName === 'FONT' ? q.parentNode : q.nextSibling ? q.nextSibling : q;
if (el = $.id(id)) {
inline = quoteInline.table(id, el.innerHTML);
@ -2658,7 +2648,7 @@
return Favicon.update();
}
},
scroll: function(e) {
scroll: function() {
var bottom, height, i, reply, _len, _ref;
updater.focus = true;
height = d.body.clientHeight;
@ -2765,7 +2755,7 @@
return $.bind(thumb, 'mouseout', ui.hoverend);
});
},
mouseover: function(e) {
mouseover: function() {
ui.el = $.el('img', {
id: 'iHover',
src: this.parentNode.href
@ -2856,7 +2846,7 @@
e.preventDefault();
return imgExpand.toggle(this);
},
all: function(e) {
all: function() {
var thumb, _i, _j, _len, _len2, _ref, _ref2, _results, _results2;
imgExpand.on = this.checked;
if (imgExpand.on) {
@ -2877,7 +2867,7 @@
return _results2;
}
},
typeChange: function(e) {
typeChange: function() {
var klass;
switch (this.value) {
case 'full':
@ -2923,13 +2913,13 @@
thumb.hidden = true;
return $.add(a, img);
},
error: function(e) {
error: function() {
var req, thumb;
thumb = this.previousSibling;
imgExpand.contract(thumb);
if (navigator.appName !== 'Opera') {
req = $.ajax(this.src, null, 'head');
return req.onreadystatechange = function(e) {
return req.onreadystatechange = function() {
if (this.status !== 404) {
return setTimeout(imgExpand.retry, 10000, thumb);
}
@ -2966,7 +2956,7 @@
form = $('body > form');
return $.prepend(form, controls);
},
resize: function(e) {
resize: function() {
return imgExpand.style.innerHTML = ".fitheight img[md5] + img {max-height:" + d.body.clientHeight + "px;}";
}
};
@ -2992,30 +2982,29 @@
};
Main = {
init: function() {
var cutoff, hiddenThreads, id, lastChecked, nodes, now, timestamp, tzOffset, _ref;
$.unbind(document, 'DOMContentLoaded', Main.init);
var cutoff, hiddenThreads, id, lastChecked, now, pathname, temp, timestamp, _ref;
if (location.hostname === 'sys.4chan.org') {
QR.sys();
return;
}
if (conf['404 Redirect'] && d.title === '4chan - 404' && /^\d+$/.test(g.THREAD_ID)) {
redirect();
return;
}
if (!$('#navtopr')) {
if (d.body) {
QR.sys();
} else {
$.bind(d, 'DOMContentLoaded', QR.sys);
}
return;
}
$.bind(window, 'message', Main.message);
Favicon.init();
g.hiddenReplies = $.get("hiddenReplies/" + g.BOARD + "/", {});
tzOffset = (new Date()).getTimezoneOffset() / 60;
g.chanOffset = 5 - tzOffset;
if ($.isDST()) {
g.chanOffset--;
pathname = location.pathname.substring(1).split('/');
g.BOARD = pathname[0], temp = pathname[1];
if (temp === 'res') {
g.REPLY = temp;
g.THREAD_ID = pathname[2];
} else {
g.PAGENUM = parseInt(temp) || 0;
}
g.hiddenReplies = $.get("hiddenReplies/" + g.BOARD + "/", {});
lastChecked = $.get('lastChecked', 0);
now = Date.now();
if (lastChecked < now - 1 * DAY) {
Main.reqUpdate = lastChecked < now - 1 * DAY;
if (Main.reqUpdate) {
$.set('lastChecked', now);
cutoff = now - 7 * DAY;
hiddenThreads = $.get("hiddenThreads/" + g.BOARD + "/", {});
@ -3035,19 +3024,14 @@
$.set("hiddenThreads/" + g.BOARD + "/", hiddenThreads);
$.set("hiddenReplies/" + g.BOARD + "/", g.hiddenReplies);
}
$.addStyle(Main.css);
threading.init();
if (conf['Filter']) {
filter.init();
}
if (conf['Reply Hiding']) {
replyHiding.init();
}
if (conf['Image Expansion']) {
imgExpand.init();
}
if (conf['Image Auto-Gif']) {
imgGif.init();
if (conf['Anonymize']) {
anonymize.init();
}
if (conf['Time Formatting']) {
Time.init();
@ -3055,18 +3039,12 @@
if (conf['Sauce']) {
sauce.init();
}
if (conf['Reveal Spoilers'] && $('.postarea label')) {
revealSpoilers.init();
}
if (conf['Anonymize']) {
anonymize.init();
if (conf['Image Auto-Gif']) {
imgGif.init();
}
if (conf['Image Hover']) {
imgHover.init();
}
if (conf['Quick Reply']) {
QR.init();
}
if (conf['Report Button']) {
reportButton.init();
}
@ -3082,6 +3060,39 @@
if (conf['Indicate OP quote']) {
quoteOP.init();
}
if (g.REPLY) {
if (conf['Image Preloading']) {
imgPreloading.init();
}
}
if (d.body) {
return Main.onLoad();
} else {
return $.bind(d, 'DOMContentLoaded', Main.onLoad);
}
},
onLoad: function() {
var nodes;
$.unbind(d, 'DOMContentLoaded', Main.onLoad);
if (conf['404 Redirect'] && d.title === '4chan - 404' && /^\d+$/.test(g.THREAD_ID)) {
redirect();
return;
}
if (!$('#navtopr')) {
return;
}
$.addStyle(Main.css);
threading.init();
Favicon.init();
if (conf['Image Expansion']) {
imgExpand.init();
}
if (conf['Reveal Spoilers'] && $('.postarea label')) {
revealSpoilers.init();
}
if (conf['Quick Reply']) {
QR.init();
}
if (conf['Thread Watcher']) {
watcher.init();
}
@ -3092,34 +3103,31 @@
if (conf['Thread Updater']) {
updater.init();
}
if (conf['Image Preloading']) {
imgPreloading.init();
}
if (conf['Post in Title']) {
titlePost.init();
}
if (conf['Thread Stats']) {
threadStats.init();
}
if (conf['Unread Count']) {
unread.init();
}
if (conf['Reply Navigation']) {
nav.init();
}
if (conf['Post in Title']) {
titlePost.init();
}
if (conf['Unread Count']) {
unread.init();
}
} else {
if (conf['Thread Hiding']) {
threadHiding.init();
}
if (conf['Index Navigation']) {
nav.init();
}
if (conf['Thread Expansion']) {
expandThread.init();
}
if (conf['Comment Expansion']) {
expandComment.init();
}
if (conf['Index Navigation']) {
nav.init();
}
}
nodes = $$('.op, a + table');
g.callbacks.forEach(function(callback) {
@ -3417,9 +3425,5 @@
}\
'
};
if (d.body) {
Main.init();
} else {
$.bind(d, 'DOMContentLoaded', Main.init);
}
Main.init();
}).call(this);

View File

@ -152,13 +152,17 @@ ui =
ui.height = d.body.clientHeight - el.offsetHeight
drag: (e) ->
left = e.clientX - ui.dx
if left < 10 then left = '0'
else if ui.width - left < 10 then left = null
right = if left then null else 0
top = e.clientY - ui.dy
if top < 10 then top = '0'
else if ui.height - top < 10 then top = null
bottom = if top then null else 0
left =
if left < 10 then 0
else if ui.width - left < 10 then null
else left
top =
if top < 10 then 0
else if ui.height - top < 10 then null
else top
right = if left is null then 0 else null
bottom = if top is null then 0 else null
#using null instead of '' is 4% faster
#these 4 statements are 40% faster than 1 style.cssText
{style} = ui.el
@ -197,7 +201,7 @@ ui =
style.left = null
style.right = clientWidth - clientX + 45
hoverend: (e) ->
hoverend: ->
ui.el.parentNode.removeChild ui.el
###
@ -370,14 +374,6 @@ else
for key, val of conf
conf[key] = $.get key, val
pathname = location.pathname.substring(1).split('/')
[g.BOARD, temp] = pathname
if temp is 'res'
g.REPLY = temp
g.THREAD_ID = pathname[2]
else
g.PAGENUM = parseInt(temp) or 0
$$ = (selector, root=d.body) ->
Array::slice.call root.querySelectorAll selector
@ -480,7 +476,7 @@ expandThread =
$.replace span, a
cb:
toggle: (e) ->
toggle: ->
thread = @parentNode
expandThread.toggle thread
@ -560,11 +556,11 @@ replyHiding =
replyHiding.hide reply
cb:
hide: (e) ->
hide: ->
reply = @parentNode.nextSibling
replyHiding.hide reply
show: (e) ->
show: ->
div = @parentNode
table = div.nextSibling
replyHiding.show table
@ -987,7 +983,7 @@ options =
options.time.call time
options.backlink.call back
clearHidden: (e) ->
clearHidden: ->
#'hidden' might be misleading; it's the number of IDs we're *looking* for,
# not the number of posts actually hidden on the page.
$.delete "hiddenReplies/#{g.BOARD}/"
@ -1001,13 +997,13 @@ options =
@value = key
$.set @name, key
conf[@name] = key
time: (e) ->
time: ->
$.set 'time', @value
conf['time'] = @value
Time.foo()
Time.date = new Date()
$('#timePreview').textContent = Time.funk Time
backlink: (e) ->
backlink: ->
$.set 'backlink', @value
conf['backlink'] = @value
$('#backlinkPreview').textContent = conf['backlink'].replace /%id/, '123456789'
@ -1292,6 +1288,7 @@ QR =
if $('img.favicon', op).src is Favicon.empty
watcher.watch op, id
sys: ->
$.unbind d, 'DOMContentLoaded', QR.sys
if recaptcha = $ '#recaptcha_response_field' #post reporting
$.bind recaptcha, 'keydown', QR.keydown
return
@ -1349,7 +1346,7 @@ threading =
threadHiding =
init: ->
hiddenThreads = $.get "hiddenThreads/#{g.BOARD}/", {}
for thread in $$ 'div.thread'
for thread in $$ '.thread'
op = thread.firstChild
a = $.el 'a',
textContent: '[ - ]'
@ -1360,10 +1357,10 @@ threadHiding =
threadHiding.hideHide thread
cb:
hide: (e) ->
hide: ->
thread = @parentNode.parentNode
threadHiding.hide thread
show: (e) ->
show: ->
thread = @parentNode.parentNode
threadHiding.show thread
@ -1589,9 +1586,9 @@ watcher =
favicon.src = Favicon.empty
cb:
toggle: (e) ->
toggle: ->
watcher.toggle @parentNode
x: (e) ->
x: ->
[board, _, id] = @nextElementSibling
.getAttribute('href').substring(1).split('/')
watcher.unwatch board, id
@ -1659,16 +1656,21 @@ Time =
init: ->
Time.foo()
# GMT -8 is given as +480; would GMT +8 be -480 ?
chanOffset = 5 - new Date().getTimezoneOffset() / 60
# 4chan = EST = GMT -5
chanOffset-- if $.isDST()
@parse =
if Date.parse '10/11/11(Tue)18:53'
(node) -> new Date Date.parse(node.textContent) + g.chanOffset*HOUR
(node) -> new Date Date.parse(node.textContent) + chanOffset*HOUR
else # Firefox the Archaic cannot parse 4chan's time
(node) ->
[_, month, day, year, hour, min] =
node.textContent.match /(\d+)\/(\d+)\/(\d+)\(\w+\)(\d+):(\d+)/
year = "20#{year}"
month -= 1 #months start at 0
hour = g.chanOffset + Number hour
hour = chanOffset + Number hour
new Date year, month, day, hour, min
g.callbacks.push Time.node
@ -1964,7 +1966,7 @@ unread =
if unread.replies.length is 1
Favicon.update()
scroll: (e) ->
scroll: ->
updater.focus = true
height = d.body.clientHeight
for reply, i in unread.replies
@ -2036,7 +2038,7 @@ imgHover =
$.bind thumb, 'mouseover', imgHover.mouseover
$.bind thumb, 'mousemove', ui.hover
$.bind thumb, 'mouseout', ui.hoverend
mouseover: (e) ->
mouseover: ->
ui.el = $.el 'img'
id: 'iHover'
src: @parentNode.href
@ -2093,7 +2095,7 @@ imgExpand =
return if e.shiftKey or e.altKey or e.ctrlKey or e.button isnt 0
e.preventDefault()
imgExpand.toggle @
all: (e) ->
all: ->
imgExpand.on = @checked
if imgExpand.on #expand
for thumb in $$ 'img[md5]:not([hidden])'
@ -2101,7 +2103,7 @@ imgExpand =
else #contract
for thumb in $$ 'img[md5][hidden]'
imgExpand.contract thumb
typeChange: (e) ->
typeChange: ->
switch @value
when 'full'
klass = ''
@ -2136,13 +2138,13 @@ imgExpand =
thumb.hidden = true
$.add a, img
error: (e) ->
error: ->
thumb = @previousSibling
imgExpand.contract thumb
#navigator.online is not x-browser/os yet
if navigator.appName isnt 'Opera'
req = $.ajax @src, null, 'head'
req.onreadystatechange = (e) -> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404
req.onreadystatechange = -> setTimeout imgExpand.retry, 10000, thumb if @status isnt 404
else unless g.dead
setTimeout imgExpand.retry, 10000, thumb
retry: (thumb) ->
@ -2168,7 +2170,7 @@ imgExpand =
form = $ 'body > form'
$.prepend form, controls
resize: (e) ->
resize: ->
imgExpand.style.innerHTML = ".fitheight img[md5] + img {max-height:#{d.body.clientHeight}px;}"
firstRun =
@ -2253,28 +2255,30 @@ firstRun =
Main =
init: ->
$.unbind document, 'DOMContentLoaded', Main.init
if location.hostname is 'sys.4chan.org'
QR.sys()
return
if conf['404 Redirect'] and d.title is '4chan - 404' and /^\d+$/.test g.THREAD_ID
redirect()
return
if not $ '#navtopr'
if d.body
QR.sys()
else
$.bind d, 'DOMContentLoaded', QR.sys
return
$.bind window, 'message', Main.message
Favicon.init()
pathname = location.pathname.substring(1).split('/')
[g.BOARD, temp] = pathname
if temp is 'res'
g.REPLY = temp
g.THREAD_ID = pathname[2]
else
g.PAGENUM = parseInt(temp) or 0
g.hiddenReplies = $.get "hiddenReplies/#{g.BOARD}/", {}
tzOffset = (new Date()).getTimezoneOffset() / 60
# GMT -8 is given as +480; would GMT +8 be -480 ?
g.chanOffset = 5 - tzOffset
# 4chan = EST = GMT -5
g.chanOffset-- if $.isDST()
lastChecked = $.get 'lastChecked', 0
now = Date.now()
if lastChecked < now - 1*DAY
Main.reqUpdate = lastChecked < now - 1*DAY
if Main.reqUpdate
$.set 'lastChecked', now
cutoff = now - 7*DAY
@ -2291,22 +2295,16 @@ Main =
$.set "hiddenThreads/#{g.BOARD}/", hiddenThreads
$.set "hiddenReplies/#{g.BOARD}/", g.hiddenReplies
$.addStyle Main.css
#major features
threading.init()
if conf['Filter']
filter.init()
if conf['Reply Hiding']
replyHiding.init()
if conf['Image Expansion']
imgExpand.init()
if conf['Image Auto-Gif']
imgGif.init()
if conf['Anonymize']
anonymize.init()
if conf['Time Formatting']
Time.init()
@ -2314,18 +2312,12 @@ Main =
if conf['Sauce']
sauce.init()
if conf['Reveal Spoilers'] and $('.postarea label')
revealSpoilers.init()
if conf['Anonymize']
anonymize.init()
if conf['Image Auto-Gif']
imgGif.init()
if conf['Image Hover']
imgHover.init()
if conf['Quick Reply']
QR.init()
if conf['Report Button']
reportButton.init()
@ -2341,6 +2333,38 @@ Main =
if conf['Indicate OP quote']
quoteOP.init()
if g.REPLY
if conf['Image Preloading']
imgPreloading.init()
if d.body
Main.onLoad()
else
$.bind d, 'DOMContentLoaded', Main.onLoad
onLoad: ->
$.unbind d, 'DOMContentLoaded', Main.onLoad
if conf['404 Redirect'] and d.title is '4chan - 404' and /^\d+$/.test g.THREAD_ID
redirect()
return
if not $ '#navtopr'
return
$.addStyle Main.css
threading.init()
Favicon.init()
#major features
if conf['Image Expansion']
imgExpand.init()
if conf['Reveal Spoilers'] and $('.postarea label')
revealSpoilers.init()
if conf['Quick Reply']
QR.init()
if conf['Thread Watcher']
watcher.init()
@ -2351,34 +2375,32 @@ Main =
if conf['Thread Updater']
updater.init()
if conf['Image Preloading']
imgPreloading.init()
if conf['Post in Title']
titlePost.init()
if conf['Thread Stats']
threadStats.init()
if conf['Unread Count']
unread.init()
if conf['Reply Navigation']
nav.init()
if conf['Post in Title']
titlePost.init()
if conf['Unread Count']
unread.init()
else #not reply
if conf['Thread Hiding']
threadHiding.init()
if conf['Index Navigation']
nav.init()
if conf['Thread Expansion']
expandThread.init()
if conf['Comment Expansion']
expandComment.init()
if conf['Index Navigation']
nav.init()
nodes = $$ '.op, a + table'
g.callbacks.forEach (callback) ->
try
@ -2666,7 +2688,4 @@ Main =
}
'
if d.body
Main.init()
else
$.bind d, 'DOMContentLoaded', Main.init
Main.init()