Fix/Refactor ExpandThread.coffee

This commit is contained in:
Mayhem 2013-11-02 15:23:32 +01:00
parent a0d3694b61
commit 61cd553a64

View File

@ -1,25 +1,29 @@
ExpandThread = ExpandThread =
init: -> init: ->
return if g.VIEW isnt 'index' or !Conf['Thread Expansion'] return if g.VIEW isnt 'index' or !Conf['Thread Expansion']
@statuses = {}
$.on d, 'IndexRefresh', @onrefresh $.on d, 'IndexRefresh', @onIndexRefresh
Thread.callbacks.push Thread.callbacks.push
name: 'Thread Expansion' name: 'Thread Expansion'
cb: @node cb: @node
node: -> node: ->
return unless span = $.x 'following-sibling::span[contains(@class,"summary")][1]', @OP.nodes.root ExpandThread.setButton @
[posts, files] = span.textContent.match /\d+/g
setButton: (thread) ->
return unless span = $.x 'following-sibling::span[contains(@class,"summary")][1]', thread.OP.nodes.root
a = $.el 'a', a = $.el 'a',
textContent: ExpandThread.text '+', posts, files textContent: ExpandThread.text '+', span.textContent.match(/\d+/g)...
className: 'summary' className: 'summary'
href: 'javascript:;' href: 'javascript:;'
$.on a, 'click', ExpandThread.cbToggle $.on a, 'click', ExpandThread.cbToggle
$.replace span, a $.replace span, a
onrefresh: -> onIndexRefresh: ->
for threadID, status of ExpandThread.statuses
status.req?.abort()
delete ExpandThread.statuses[threadID]
for threadID, thread of g.BOARD.threads for threadID, thread of g.BOARD.threads
ExpandThread.node.call thread ExpandThread.setButton thread
return return
text: (status, posts, files) -> text: (status, posts, files) ->
@ -35,89 +39,76 @@ ExpandThread =
toggle: (thread) -> toggle: (thread) ->
threadRoot = thread.OP.nodes.root.parentNode threadRoot = thread.OP.nodes.root.parentNode
a = $ '.summary', threadRoot a = $ '.summary', threadRoot
if thread.ID of ExpandThread.statuses
switch thread.isExpanded ExpandThread.contract thread, a, threadRoot
when false, undefined else
for post in $$ '.thread > .postContainer', threadRoot ExpandThread.expand thread, a, threadRoot
ExpandComment.expand Get.postFromRoot post expand: (thread, a, threadRoot) ->
unless a ExpandThread.statuses[thread] = status = {}
thread.isExpanded = true for post in $$ '.thread > .postContainer', threadRoot
return ExpandComment.expand Get.postFromRoot post
thread.isExpanded = 'loading' return unless a
[posts, files] = a.textContent.match /\d+/g a.textContent = ExpandThread.text '...', a.textContent.match(/\d+/g)...
a.textContent = ExpandThread.text '...', posts, files status.req = $.cache "//api.4chan.org/#{thread.board}/res/#{thread}.json", ->
$.cache "//api.4chan.org/#{thread.board}/res/#{thread}.json", -> delete status.req
ExpandThread.parse @, thread, a ExpandThread.parse @, thread, a
contract: (thread, a, threadRoot) ->
when 'loading' status = ExpandThread.statuses[thread]
thread.isExpanded = false delete ExpandThread.statuses[thread]
return unless a if status.req
[posts, files] = a.textContent.match /\d+/g status.req.abort()
a.textContent = ExpandThread.text '+', posts, files a.textContent = ExpandThread.text '+', a.textContent.match(/\d+/g)... if a
when true
thread.isExpanded = false
#goddamit moot
num = if thread.isSticky
1
else switch g.BOARD.ID
# XXX boards config
when 'b', 'vg' then 3
when 't' then 1
else 5
posts = $$ ".thread > .replyContainer", threadRoot
for post in [thread.OP.nodes.root].concat posts[-num..]
ExpandComment.contract Get.postFromRoot post
return unless a
postsCount = 0
filesCount = 0
for reply in posts[...-num]
if Conf['Quote Inlining']
# rm clones
inlined.click() while inlined = $ '.inlined', reply
postsCount++
filesCount++ if 'file' of Get.postFromRoot reply
$.rm reply
a.textContent = ExpandThread.text '+', postsCount, filesCount
return
parse: (req, thread, a) ->
return if a.textContent[0] is '+'
if req.status not in [200, 304]
a.textContent = "Error #{req.statusText} (#{req.status})"
$.off a, 'click', ExpandThread.cbToggle
return return
thread.isExpanded = true num = if thread.isSticky
1
else switch g.BOARD.ID
# XXX boards config
when 'b', 'vg' then 3
when 't' then 1
else 5
posts = $$ '.thread > .replyContainer', threadRoot
for post in [thread.OP.nodes.root].concat posts[-num..]
ExpandComment.contract Get.postFromRoot post
return unless a
postsCount = 0
filesCount = 0
for reply in posts[...-num]
# rm clones
inlined.click() while inlined = $ '.inlined', reply if Conf['Quote Inlining']
postsCount++
filesCount++ if 'file' of Get.postFromRoot reply
$.rm reply
a.textContent = ExpandThread.text '+', postsCount, filesCount
parse: (req, thread, a) ->
if req.status not in [200, 304]
a.textContent = "Error #{req.statusText} (#{req.status})"
return
{posts} = JSON.parse req.response data = JSON.parse(req.response).posts
if spoilerRange = posts.shift().custom_spoiler Build.spoilerRange[thread.board] = data.shift().custom_spoiler
Build.spoilerRange[thread.board] = spoilerRange
postsObj = [] posts = []
postsRoot = [] postsRoot = []
filesCount = 0 filesCount = 0
for reply in posts for postData in data
if post = thread.posts[reply.no] if post = thread.posts[postData.no]
filesCount++ if 'file' of post filesCount++ if 'file' of post
postsRoot.push post.nodes.root postsRoot.push post.nodes.root
continue continue
root = Build.postFromObject reply, thread.board.ID root = Build.postFromObject postData, thread.board.ID
post = new Post root, thread, thread.board post = new Post root, thread, thread.board
link = $ 'a[title="Highlight this post"]', root
link.href = "res/#{thread}#p#{post}"
link.nextSibling.href = "res/#{thread}#q#{post}"
filesCount++ if 'file' of post filesCount++ if 'file' of post
postsObj.push post posts.push post
postsRoot.push root postsRoot.push root
Main.callbackNodes Post, postsObj Main.callbackNodes Post, posts
$.after a, postsRoot $.after a, postsRoot
postsCount = postsRoot.length postsCount = postsRoot.length
a.textContent = ExpandThread.text '-', postsCount, filesCount a.textContent = ExpandThread.text '-', postsCount, filesCount
# Enable 4chan features. # Enable 4chan features.
if Conf['Enable 4chan\'s Extension'] if Conf['Enable 4chan\'s Extension']
$.globalEval "Parser.parseThread(#{thread.ID}, 1, #{postsCount})" $.globalEval "Parser.parseThread(#{thread}, 1, #{postsCount})"
else else
Fourchan.parseThread thread.ID, 1, postsCount Fourchan.parseThread thread.ID, 1, postsCount