From 149ea4ccc2fe730a30963537390bd1863e11fbc2 Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Sun, 17 Feb 2013 18:47:41 +0100 Subject: [PATCH] Expand OP comments when expanding a thread. Close #764. --- 4chan_x.user.js | 49 +++++++++++++++++++++++++++++++++------------ src/features.coffee | 37 +++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 3369fc20b..bf3c12f87 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -3912,21 +3912,42 @@ node: function() { var a; if (a = $('.abbr > a', this.nodes.comment)) { - return $.on(a, 'click', ExpandComment.expand); + return $.on(a, 'click', ExpandComment.cb); } }, - expand: function(e) { - var a, post; + cb: function(e) { + var post; e.preventDefault(); post = Get.postFromNode(this); - this.textContent = "Post No." + post + " Loading..."; - a = this; - return $.cache("//api.4chan.org" + this.pathname + ".json", function() { + return ExpandComment.expand(post); + }, + expand: function(post) { + var a; + if (post.nodes.longComment) { + $.replace(post.nodes.shortComment, post.nodes.longComment); + post.nodes.comment = post.nodes.longComment; + return; + } + if (!(a = $('.abbr > a', post.nodes.comment))) { + return; + } + a.textContent = "Post No." + post + " Loading..."; + return $.cache("//api.4chan.org" + a.pathname + ".json", function() { return ExpandComment.parse(this, a, post); }); }, + contract: function(post) { + var a; + if (!post.nodes.shortComment) { + return; + } + a = a = $('.abbr > a', post.nodes.shortComment); + a.textContent = 'here'; + $.replace(post.nodes.longComment, post.nodes.shortComment); + return post.nodes.comment = post.nodes.shortComment; + }, parse: function(req, a, post) { - var comment, href, postObj, posts, prev, quote, spoilerRange, _i, _j, _len, _len1, _ref; + var clone, href, postObj, posts, quote, spoilerRange, _i, _j, _len, _len1, _ref; if (req.status !== 200) { a.textContent = "Error " + req.statusText + " (" + req.status + ")"; return; @@ -3945,9 +3966,9 @@ a.textContent = "Post No." + post + " not found."; return; } - comment = post.nodes.comment; - comment.innerHTML = postObj.com; - _ref = $$('.quotelink', comment); + clone = post.nodes.comment.cloneNode(false); + clone.innerHTML = postObj.com; + _ref = $$('.quotelink', clone); for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { quote = _ref[_j]; href = quote.getAttribute('href'); @@ -3973,9 +3994,9 @@ if (Conf['Mark Cross-thread Quotes']) { QuoteCT.node.call(post); } - prev = comment.previousSibling; - $.rm(comment); - return $.after(prev, comment); + post.nodes.shortComment = post.nodes.comment; + $.replace(post.nodes.comment, clone); + return post.nodes.comment = post.nodes.longComment = clone; } }; @@ -4020,12 +4041,14 @@ $.cache(url, function() { return ExpandThread.parse(this, thread, a); }); + ExpandComment.expand(thread.posts[thread]); break; case '×': a.textContent = text.replace('× Loading...', '+'); break; case '-': a.textContent = text.replace('-', '+'); + ExpandComment.contract(thread.posts[thread]); num = (function() { switch (g.BOARD) { case 'b': diff --git a/src/features.coffee b/src/features.coffee index 6ddf28429..3df47c2e9 100644 --- a/src/features.coffee +++ b/src/features.coffee @@ -2428,13 +2428,25 @@ ExpandComment = cb: @node node: -> if a = $ '.abbr > a', @nodes.comment - $.on a, 'click', ExpandComment.expand - expand: (e) -> + $.on a, 'click', ExpandComment.cb + cb: (e) -> e.preventDefault() post = Get.postFromNode @ - @textContent = "Post No.#{post} Loading..." - a = @ - $.cache "//api.4chan.org#{@pathname}.json", -> ExpandComment.parse @, a, post + ExpandComment.expand post + expand: (post) -> + if post.nodes.longComment + $.replace post.nodes.shortComment, post.nodes.longComment + post.nodes.comment = post.nodes.longComment + return + return unless a = $ '.abbr > a', post.nodes.comment + a.textContent = "Post No.#{post} Loading..." + $.cache "//api.4chan.org#{a.pathname}.json", -> ExpandComment.parse @, a, post + contract: (post) -> + return unless post.nodes.shortComment + a = a = $ '.abbr > a', post.nodes.shortComment + a.textContent = 'here' + $.replace post.nodes.longComment, post.nodes.shortComment + post.nodes.comment = post.nodes.shortComment parse: (req, a, post) -> if req.status isnt 200 a.textContent = "Error #{req.statusText} (#{req.status})" @@ -2450,9 +2462,9 @@ ExpandComment = a.textContent = "Post No.#{post} not found." return - {comment} = post.nodes - comment.innerHTML = postObj.com - for quote in $$ '.quotelink', comment + clone = post.nodes.comment.cloneNode false + clone.innerHTML = postObj.com + for quote in $$ '.quotelink', clone href = quote.getAttribute 'href' continue if href[0] is '/' # Cross-board quote, or board link quote.href = "/#{post.board}/res/#{href}" # Fix pathnames @@ -2470,10 +2482,9 @@ ExpandComment = QuoteCT.node.call post # XXX g code # XXX sci math - # Fix linkifiers: - prev = comment.previousSibling - $.rm comment - $.after prev, comment + post.nodes.shortComment = post.nodes.comment + $.replace post.nodes.comment, clone + post.nodes.comment = post.nodes.longComment = clone ExpandThread = init: -> @@ -2506,12 +2517,14 @@ ExpandThread = when '+' a.textContent = text.replace '+', '× Loading...' $.cache url, -> ExpandThread.parse @, thread, a + ExpandComment.expand thread.posts[thread] when '×' a.textContent = text.replace '× Loading...', '+' when '-' a.textContent = text.replace '-', '+' + ExpandComment.contract thread.posts[thread] #goddamit moot num = switch g.BOARD # XXX boards config