From 8fc1f8668982d61382ca4067163b298f3cb15f32 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 30 Aug 2012 05:23:32 +0200 Subject: [PATCH] Add Main.callbackNodes. --- 4chan_x.user.js | 44 ++++++++++++++++++++++++++++++++------------ script.coffee | 33 +++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 20 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 12e18cb98..70878f088 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -728,26 +728,46 @@ return $.ready(Main.initFeaturesReady); }, initFeaturesReady: function() { - var board, child, thread, _i, _j, _len, _len1, _ref, _ref1; + var child, posts, thread, threads, _i, _j, _len, _len1, _ref, _ref1; if (!$.id('navtopr')) { return; } - board = $('.board'); - _ref = board.children; + threads = []; + posts = []; + _ref = $('.board').children; for (_i = 0, _len = _ref.length; _i < _len; _i++) { child = _ref[_i]; - if (child.className === 'thread') { - thread = new Thread(child, g.BOARD); - _ref1 = thread.root.children; - for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { - child = _ref1[_j]; - if ($.hasClass(child, 'postContainer')) { - new Post(child, thread, g.BOARD); - } + if (child.className !== 'thread') { + continue; + } + thread = new Thread(child, g.BOARD); + threads.push(thread); + _ref1 = thread.root.children; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + child = _ref1[_j]; + if (!$.hasClass(child, 'postContainer')) { + continue; } + posts.push(new Post(child, thread, g.BOARD)); + } + } + Main.callbackNodes(Thread, threads, true); + return Main.callbackNodes(Post, posts, true); + }, + callbackNodes: function(klass, nodes, notify) { + var callback, i, len, _i, _j, _len, _ref; + len = nodes.length; + _ref = klass.prototype.callbacks; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + try { + for (i = _j = 0; 0 <= len ? _j < len : _j > len; i = 0 <= len ? ++_j : --_j) { + callback.cb.call(nodes[i]); + } + } catch (err) { + } } - return $.log(g); }, settings: function() { return alert('Here be settings'); diff --git a/script.coffee b/script.coffee index d62464fda..8f7205a2d 100644 --- a/script.coffee +++ b/script.coffee @@ -566,14 +566,31 @@ Main = $.ready Main.initFeaturesReady initFeaturesReady: -> return unless $.id 'navtopr' - board = $ '.board' - for child in board.children - if child.className is 'thread' - thread = new Thread child, g.BOARD - for child in thread.root.children - if $.hasClass child, 'postContainer' - new Post child, thread, g.BOARD - $.log g + + threads = [] + posts = [] + + for child in $('.board').children + continue unless child.className is 'thread' + thread = new Thread child, g.BOARD + threads.push thread + for child in thread.root.children + continue unless $.hasClass child, 'postContainer' + posts.push new Post child, thread, g.BOARD + + Main.callbackNodes Thread, threads, true + Main.callbackNodes Post, posts, true + + callbackNodes: (klass, nodes, notify) -> + # get the nodes' length only once + len = nodes.length + for callback in klass::callbacks + try + for i in [0...len] + callback.cb.call nodes[i] + catch err + # handle error if notify + return settings: -> alert 'Here be settings'