From 092d9317e0f58057d6045eafa523a2e7f14b9e39 Mon Sep 17 00:00:00 2001 From: Mayhem Date: Thu, 31 Oct 2013 19:10:24 +0100 Subject: [PATCH] Add sorting options by bump order and creation date. --- CHANGELOG.md | 4 ++ lib/$.coffee | 2 +- src/General/Config.coffee | 4 +- src/Miscellaneous/Index.coffee | 67 +++++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 352ef32a5..c075422c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Index navigation improvements: - You can now refresh the index page you are on with the icon in the header bar or the same keybind for refreshing threads. - You can now switch between single-page and all-pages navigation via the "Index Navigation" header sub-menu. + - Threads in the index can now be sorted by: + - bump order + - creation date + Added a keybind to open the catalog search field on index pages. ### 3.11.5 - *2013-10-03* diff --git a/lib/$.coffee b/lib/$.coffee index 8b2202304..711159e6c 100644 --- a/lib/$.coffee +++ b/lib/$.coffee @@ -104,7 +104,7 @@ $.rm = do -> (el) -> el.parentNode?.removeChild el $.rmAll = (root) -> # jsperf.com/emptify-element - while node = root.firstChild + for node in [root.childNodes...] # HTMLSelectElement.remove !== Element.remove root.removeChild node return diff --git a/src/General/Config.coffee b/src/General/Config.coffee index dd3220383..95f1f2a48 100644 --- a/src/General/Config.coffee +++ b/src/General/Config.coffee @@ -139,8 +139,10 @@ Config = #//archive.foolz.us/%board/search/image/%MD5/;text:View same on foolz /%board/ #//archive.installgentoo.net/%board/image/%MD5;text:View same on installgentoo /%board/ """ - 'Index Mode': 'paged' 'Custom CSS': false + Index: + 'Index Mode': 'paged' + 'Index Sort': 'bump' Header: 'Header auto-hide': false 'Bottom header': false diff --git a/src/Miscellaneous/Index.coffee b/src/Miscellaneous/Index.coffee index 8de6fa1de..51aadffc3 100644 --- a/src/Miscellaneous/Index.coffee +++ b/src/Miscellaneous/Index.coffee @@ -9,34 +9,44 @@ Index = $.on button, 'click', Index.update Header.addShortcut button, 1 - subEntries = [] - - subEntry = - el: $.el 'span', textContent: 'Index Mode' + modeEntry = + el: $.el 'span', textContent: 'Index mode' subEntries: [ { el: $.el 'label', innerHTML: ' Paged' } { el: $.el 'label', innerHTML: ' All threads' } ] - for label in subEntry.subEntries + for label in modeEntry.subEntries input = label.el.firstChild input.checked = Conf['Index Mode'] is input.value $.on input, 'change', $.cb.value $.on input, 'change', @update - subEntries.push subEntry + + sortEntry = + el: $.el 'span', textContent: 'Sort by' + subEntries: [ + { el: $.el 'label', innerHTML: ' Bump order' } + { el: $.el 'label', innerHTML: ' Creation date' } + ] + for label in sortEntry.subEntries + input = label.el.firstChild + input.checked = Conf['Index Sort'] is input.value + $.on input, 'change', $.cb.value + $.on input, 'change', @resort $.event 'AddMenuEntry', type: 'header' el: $.el 'span', textContent: 'Index Navigation' order: 90 - subEntries: subEntries + subEntries: [modeEntry, sortEntry] $.on d, '4chanXInitFinished', @initReady initReady: -> $.off d, '4chanXInitFinished', Index.initReady Index.root = $ '.board' - Index.setIndex $$ '.board > .thread, .board > hr', Index.root + Index.liveThreads = $$('.board > .thread', Index.root).map Get.threadFromRoot + Index.resort() return if Conf['Index Mode'] is 'paged' Index.update() @@ -84,17 +94,17 @@ Index = for page in pages dataThr.push page.threads... - nodes = [] - threads = [] - liveThreads = [] - posts = [] + nodes = [] + threads = [] + posts = [] + Index.liveThreads = [] for data in dataThr threadRoot = Build.thread g.BOARD, data nodes.push threadRoot, $.el 'hr' unless thread = g.threads["#{g.BOARD}.#{data.no}"] thread = new Thread data.no, g.BOARD threads.push thread - liveThreads.push thread + Index.liveThreads.push thread for postRoot in $$ '.thread > .postContainer', threadRoot continue if thread.posts[postRoot.id.match /\d+/] try @@ -108,7 +118,7 @@ Index = error: err Main.handleErrors errors if errors - Index.collectDeadThreads liveThreads + Index.collectDeadThreads() # Add the threads and
s in a container to make sure all features work. $.nodes nodes Main.callbackNodes Thread, threads @@ -120,19 +130,32 @@ Index = $.rmAll Index.root $.add Index.root, Index.sort nodes $('.pagelist').hidden = Conf['Index Mode'] isnt 'paged' - sort: (nodes) -> + sort: (unsortedNodes) -> + nodes = [] + switch Conf['Index Sort'] + when 'bump' + for thread in Index.liveThreads + i = unsortedNodes.indexOf thread.OP.nodes.root.parentNode + nodes.push unsortedNodes[i], unsortedNodes[i + 1] + when 'birth' + dates = [] + for threadRoot, i in unsortedNodes by 2 + dates.push Get.threadFromRoot(threadRoot).OP.info.date + unsortedDates = [dates...] + for date in dates.sort((a, b) -> b - a) + i = unsortedDates.indexOf(date) * 2 + nodes.push unsortedNodes[i], unsortedNodes[i + 1] return nodes unless Conf['Filter'] # Put the highlighted thread &
on top of the index # while keeping the original order they appear in. - i = offset = 0 - while threadRoot = nodes[i] - if Get.threadFromRoot(threadRoot).isOnTop - nodes.splice offset, 0, nodes.splice(i, 2)... - offset += 2 - i += 2 + offset = 0 + for threadRoot, i in nodes by 2 when Get.threadFromRoot(threadRoot).isOnTop + nodes.splice offset++ * 2, 0, nodes.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 liveThreads + for threadID, thread of g.threads when thread not in Index.liveThreads thread.collect() return