Refactor index generation/sorting.

This commit is contained in:
Mayhem 2013-11-01 03:46:21 +01:00
parent 092d9317e0
commit d2587e4bb2
2 changed files with 55 additions and 58 deletions

View File

@ -170,7 +170,7 @@ Main =
# Something might have gone wrong! # Something might have gone wrong!
Main.initStyle() Main.initStyle()
if board = $ '.board' if g.VIEW is 'thread' and board = $ '.board'
threads = [] threads = []
posts = [] posts = []

View File

@ -19,7 +19,7 @@ Index =
input = label.el.firstChild input = label.el.firstChild
input.checked = Conf['Index Mode'] is input.value input.checked = Conf['Index Mode'] is input.value
$.on input, 'change', $.cb.value $.on input, 'change', $.cb.value
$.on input, 'change', @update $.on input, 'change', @cb.mode
sortEntry = sortEntry =
el: $.el 'span', textContent: 'Sort by' el: $.el 'span', textContent: 'Sort by'
@ -31,7 +31,7 @@ Index =
input = label.el.firstChild input = label.el.firstChild
input.checked = Conf['Index Sort'] is input.value input.checked = Conf['Index Sort'] is input.value
$.on input, 'change', $.cb.value $.on input, 'change', $.cb.value
$.on input, 'change', @resort $.on input, 'change', @cb.sort
$.event 'AddMenuEntry', $.event 'AddMenuEntry',
type: 'header' type: 'header'
@ -45,11 +45,15 @@ Index =
initReady: -> initReady: ->
$.off d, '4chanXInitFinished', Index.initReady $.off d, '4chanXInitFinished', Index.initReady
Index.root = $ '.board' Index.root = $ '.board'
Index.liveThreads = $$('.board > .thread', Index.root).map Get.threadFromRoot
Index.resort()
return if Conf['Index Mode'] is 'paged'
Index.update() Index.update()
cb:
mode: ->
Index.buildIndex()
sort: ->
Index.sort()
Index.buildIndex()
update: -> update: ->
return unless navigator.onLine return unless navigator.onLine
Index.req?.abort() Index.req?.abort()
@ -86,27 +90,29 @@ Index =
Header.scrollTo Index.root if Index.root.getBoundingClientRect().top < 0 Header.scrollTo Index.root if Index.root.getBoundingClientRect().top < 0
parse: (pages) -> parse: (pages) ->
if Conf['Index Mode'] is 'paged' Index.parseThreadList pages
pageNum = +window.location.pathname.split('/')[2] Index.buildAll()
dataThr = pages[pageNum].threads Index.sort()
else Index.buildIndex()
dataThr = [] parseThreadList: (pages) ->
for page in pages Index.threadsNumPerPage = pages[0].threads.length
dataThr.push page.threads... Index.liveThreadData = pages.reduce ((arr, next) -> arr.concat next.threads), []
Index.liveThreadIDs = Index.liveThreadData.map (data) -> data.no
nodes = [] for threadID, thread of g.BOARD.threads when thread.ID not in Index.liveThreadIDs
threads = [] thread.collect()
posts = [] return
Index.liveThreads = [] buildAll: ->
for data in dataThr Index.nodes = []
threadRoot = Build.thread g.BOARD, data threads = []
nodes.push threadRoot, $.el 'hr' posts = []
unless thread = g.threads["#{g.BOARD}.#{data.no}"] for threadData in Index.liveThreadData
thread = new Thread data.no, g.BOARD threadRoot = Build.thread g.BOARD, threadData
Index.nodes.push threadRoot, $.el 'hr'
unless thread = g.BOARD.threads[threadData.no]
thread = new Thread threadData.no, g.BOARD
threads.push thread threads.push thread
Index.liveThreads.push thread postRoots = $$ '.thread > .postContainer', threadRoot
for postRoot in $$ '.thread > .postContainer', threadRoot for postRoot in postRoots when postRoot.id.match(/\d+/)[0] not of thread.posts
continue if thread.posts[postRoot.id.match /\d+/]
try try
posts.push new Post postRoot, thread, g.BOARD posts.push new Post postRoot, thread, g.BOARD
catch err catch err
@ -118,44 +124,35 @@ Index =
error: err error: err
Main.handleErrors errors if errors Main.handleErrors errors if errors
Index.collectDeadThreads()
# Add the threads and <hr>s in a container to make sure all features work. # Add the threads and <hr>s in a container to make sure all features work.
$.nodes nodes $.nodes Index.nodes
Main.callbackNodes Thread, threads Main.callbackNodes Thread, threads
Main.callbackNodes Post, posts Main.callbackNodes Post, posts
$.event 'IndexRefresh' sort: ->
Index.setIndex nodes
setIndex: (nodes) ->
$.rmAll Index.root
$.add Index.root, Index.sort nodes
$('.pagelist').hidden = Conf['Index Mode'] isnt 'paged'
sort: (unsortedNodes) ->
nodes = []
switch Conf['Index Sort'] switch Conf['Index Sort']
when 'bump' when 'bump'
for thread in Index.liveThreads sortedThreadIDs = Index.liveThreadIDs
i = unsortedNodes.indexOf thread.OP.nodes.root.parentNode
nodes.push unsortedNodes[i], unsortedNodes[i + 1]
when 'birth' when 'birth'
dates = [] sortedThreadIDs = [Index.liveThreadIDs...].sort (a, b) -> b - a
for threadRoot, i in unsortedNodes by 2 Index.sortedNodes = []
dates.push Get.threadFromRoot(threadRoot).OP.info.date for threadID in sortedThreadIDs
unsortedDates = [dates...] i = Index.liveThreadIDs.indexOf(threadID) * 2
for date in dates.sort((a, b) -> b - a) Index.sortedNodes.push Index.nodes[i], Index.nodes[i + 1]
i = unsortedDates.indexOf(date) * 2 return unless Conf['Filter']
nodes.push unsortedNodes[i], unsortedNodes[i + 1]
return nodes unless Conf['Filter']
# Put the highlighted thread & <hr> on top of the index # Put the highlighted thread & <hr> on top of the index
# while keeping the original order they appear in. # while keeping the original order they appear in.
offset = 0 offset = 0
for threadRoot, i in nodes by 2 when Get.threadFromRoot(threadRoot).isOnTop for threadRoot, i in Index.sortedNodes by 2 when Get.threadFromRoot(threadRoot).isOnTop
nodes.splice offset++ * 2, 0, nodes.splice(i, 2)... Index.sortedNodes.splice offset++ * 2, 0, Index.sortedNodes.splice(i, 2)...
nodes
resort: ->
Index.setIndex $$ '.board > .thread, .board > hr', Index.root
collectDeadThreads: (liveThreads) ->
for threadID, thread of g.threads when thread not in Index.liveThreads
thread.collect()
return return
buildIndex: ->
if Conf['Index Mode'] is 'paged'
pageNum = +window.location.pathname.split('/')[2]
nodesPerPage = Index.threadsNumPerPage * 2
nodes = Index.sortedNodes.slice nodesPerPage * pageNum, nodesPerPage * (pageNum + 1)
else
nodes = Index.sortedNodes
$.event 'IndexRefresh'
$.rmAll Index.root
$.add Index.root, nodes
$('.pagelist').hidden = Conf['Index Mode'] isnt 'paged'