diff --git a/builds/4chan-X.user.js b/builds/4chan-X.user.js index ba25d1e63..535756d8d 100644 --- a/builds/4chan-X.user.js +++ b/builds/4chan-X.user.js @@ -2718,43 +2718,34 @@ return Main.callbackNodes(Post, posts); }, sort: function() { - var i, sortedThreadIDs, threadID, _i, _len; - switch (Conf['Index Sort']) { - case 'bump': - sortedThreadIDs = Index.liveThreadIDs; - break; - case 'lastreply': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - if ('last_replies' in a) { - a = a.last_replies[a.last_replies.length - 1]; - } - if ('last_replies' in b) { - b = b.last_replies[b.last_replies.length - 1]; - } - return b.no - a.no; - }).map(function(data) { - return data.no; - }); - break; - case 'birth': - sortedThreadIDs = __slice.call(Index.liveThreadIDs).sort(function(a, b) { - return b - a; - }); - break; - case 'replycount': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - return b.replies - a.replies; - }).map(function(data) { - return data.no; - }); - break; - case 'filecount': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - return b.images - a.images; - }).map(function(data) { - return data.no; - }); - } + var cnd, fn, i, item, items, sortOnTop, sortedThreadIDs, threadID, _i, _len; + sortedThreadIDs = { + lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) { + if ('last_replies' in a) { + a = a.last_replies[a.last_replies.length - 1]; + } + if ('last_replies' in b) { + b = b.last_replies[b.last_replies.length - 1]; + } + return b.no - a.no; + }).map(function(data) { + return data.no; + }), + bump: Index.liveThreadIDs, + birth: __slice.call(Index.liveThreadIDs).sort(function(a, b) { + return b - a; + }), + replycount: __slice.call(Index.liveThreadData).sort(function(a, b) { + return b.replies - a.replies; + }).map(function(data) { + return data.no; + }), + filecount: __slice.call(Index.liveThreadData).sort(function(a, b) { + return b.images - a.images; + }).map(function(data) { + return data.no; + }) + }[Conf['Index Sort']]; Index.sortedNodes = []; for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) { threadID = sortedThreadIDs[_i]; @@ -2764,18 +2755,31 @@ if (Index.isSearching) { Index.sortedNodes = Index.querySearch(Index.searchInput.value) || Index.sortedNodes; } - Index.sortOnTop(function(thread) { - return thread.isSticky; - }); - if (Conf['Filter']) { - Index.sortOnTop(function(thread) { - return thread.isOnTop; - }); - } - if (Conf['Anchor Hidden Threads']) { - return Index.sortOnTop(function(thread) { - return !thread.isHidden; - }); + sortOnTop = Index.sortOnTop; + items = [ + { + fn: function(thread) { + return thread.isSticky; + }, + cnd: true + }, { + fn: function(thread) { + return thread.isOnTop; + }, + cnd: Conf['Filter'] + }, { + fn: function(thread) { + return !thread.isHidden; + }, + cnd: Conf['Anchor Hidden Threads'] + } + ]; + i = 0; + while (item = items[i++]) { + fn = item.fn, cnd = item.cnd; + if (fn) { + sortOnTop(fn); + } } }, sortOnTop: function(match) { diff --git a/builds/crx/script.js b/builds/crx/script.js index c68e169e8..819f7145e 100644 --- a/builds/crx/script.js +++ b/builds/crx/script.js @@ -2728,43 +2728,34 @@ return Main.callbackNodes(Post, posts); }, sort: function() { - var i, sortedThreadIDs, threadID, _i, _len; - switch (Conf['Index Sort']) { - case 'bump': - sortedThreadIDs = Index.liveThreadIDs; - break; - case 'lastreply': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - if ('last_replies' in a) { - a = a.last_replies[a.last_replies.length - 1]; - } - if ('last_replies' in b) { - b = b.last_replies[b.last_replies.length - 1]; - } - return b.no - a.no; - }).map(function(data) { - return data.no; - }); - break; - case 'birth': - sortedThreadIDs = __slice.call(Index.liveThreadIDs).sort(function(a, b) { - return b - a; - }); - break; - case 'replycount': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - return b.replies - a.replies; - }).map(function(data) { - return data.no; - }); - break; - case 'filecount': - sortedThreadIDs = __slice.call(Index.liveThreadData).sort(function(a, b) { - return b.images - a.images; - }).map(function(data) { - return data.no; - }); - } + var cnd, fn, i, item, items, sortOnTop, sortedThreadIDs, threadID, _i, _len; + sortedThreadIDs = { + lastreply: __slice.call(Index.liveThreadData).sort(function(a, b) { + if ('last_replies' in a) { + a = a.last_replies[a.last_replies.length - 1]; + } + if ('last_replies' in b) { + b = b.last_replies[b.last_replies.length - 1]; + } + return b.no - a.no; + }).map(function(data) { + return data.no; + }), + bump: Index.liveThreadIDs, + birth: __slice.call(Index.liveThreadIDs).sort(function(a, b) { + return b - a; + }), + replycount: __slice.call(Index.liveThreadData).sort(function(a, b) { + return b.replies - a.replies; + }).map(function(data) { + return data.no; + }), + filecount: __slice.call(Index.liveThreadData).sort(function(a, b) { + return b.images - a.images; + }).map(function(data) { + return data.no; + }) + }[Conf['Index Sort']]; Index.sortedNodes = []; for (_i = 0, _len = sortedThreadIDs.length; _i < _len; _i++) { threadID = sortedThreadIDs[_i]; @@ -2774,18 +2765,31 @@ if (Index.isSearching) { Index.sortedNodes = Index.querySearch(Index.searchInput.value) || Index.sortedNodes; } - Index.sortOnTop(function(thread) { - return thread.isSticky; - }); - if (Conf['Filter']) { - Index.sortOnTop(function(thread) { - return thread.isOnTop; - }); - } - if (Conf['Anchor Hidden Threads']) { - return Index.sortOnTop(function(thread) { - return !thread.isHidden; - }); + sortOnTop = Index.sortOnTop; + items = [ + { + fn: function(thread) { + return thread.isSticky; + }, + cnd: true + }, { + fn: function(thread) { + return thread.isOnTop; + }, + cnd: Conf['Filter'] + }, { + fn: function(thread) { + return !thread.isHidden; + }, + cnd: Conf['Anchor Hidden Threads'] + } + ]; + i = 0; + while (item = items[i++]) { + fn = item.fn, cnd = item.cnd; + if (fn) { + sortOnTop(fn); + } } }, sortOnTop: function(match) { diff --git a/src/General/Index.coffee b/src/General/Index.coffee index f01dc7dc2..3e2cb30c6 100644 --- a/src/General/Index.coffee +++ b/src/General/Index.coffee @@ -374,21 +374,18 @@ Index = Main.callbackNodes Post, posts sort: -> - switch Conf['Index Sort'] - when 'bump' - sortedThreadIDs = Index.liveThreadIDs - when 'lastreply' - sortedThreadIDs = [Index.liveThreadData...].sort((a, b) -> + sortedThreadIDs = { + lastreply: + [Index.liveThreadData...].sort((a, b) -> a = a.last_replies[a.last_replies.length - 1] if 'last_replies' of a b = b.last_replies[b.last_replies.length - 1] if 'last_replies' of b b.no - a.no ).map (data) -> data.no - when 'birth' - sortedThreadIDs = [Index.liveThreadIDs...].sort (a, b) -> b - a - when 'replycount' - sortedThreadIDs = [Index.liveThreadData...].sort((a, b) -> b.replies - a.replies).map (data) -> data.no - when 'filecount' - sortedThreadIDs = [Index.liveThreadData...].sort((a, b) -> b.images - a.images).map (data) -> data.no + bump: Index.liveThreadIDs + birth: [Index.liveThreadIDs...].sort (a, b) -> b - a + replycount: [Index.liveThreadData...].sort((a, b) -> b.replies - a.replies).map (data) -> data.no + filecount: [Index.liveThreadData...].sort((a, b) -> b.images - a.images).map (data) -> data.no + }[Conf['Index Sort']] Index.sortedNodes = [] for threadID in sortedThreadIDs i = Index.liveThreadIDs.indexOf(threadID) * 2 @@ -396,11 +393,22 @@ Index = if Index.isSearching Index.sortedNodes = Index.querySearch(Index.searchInput.value) or Index.sortedNodes # Sticky threads - Index.sortOnTop (thread) -> thread.isSticky - # Highlighted threads - Index.sortOnTop((thread) -> thread.isOnTop) if Conf['Filter'] - # Non-hidden threads - Index.sortOnTop((thread) -> !thread.isHidden) if Conf['Anchor Hidden Threads'] + {sortOnTop} = Index + items = [ + fn: (thread) -> thread.isSticky + cnd: true + , # Highlighted threads + fn: (thread) -> thread.isOnTop + cnd: Conf['Filter'] + , # Non-hidden threads + fn: (thread) -> !thread.isHidden + cnd: Conf['Anchor Hidden Threads'] + ] + i = 0 + while item = items[i++] + {fn, cnd} = item + sortOnTop fn if fn + return sortOnTop: (match) -> offset = 0