From f865484182f80b777cf5b3c4c9acaacb71ccda67 Mon Sep 17 00:00:00 2001 From: Kate von Roeder Date: Wed, 20 Nov 2013 05:47:36 -0800 Subject: [PATCH] Add Array.stableSort from mootools forge. Change calls to Array.sort to use new Array.stableSort. Fixes sorting problems on Chrome --- couchpotato/core/_base/clientscript/main.py | 1 + .../scripts/library/Array.stableSort.js | 56 +++++++++++++++++++ couchpotato/static/scripts/page/settings.js | 14 ++--- 3 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 couchpotato/static/scripts/library/Array.stableSort.js diff --git a/couchpotato/core/_base/clientscript/main.py b/couchpotato/core/_base/clientscript/main.py index 1b7f1636..a0be0d02 100644 --- a/couchpotato/core/_base/clientscript/main.py +++ b/couchpotato/core/_base/clientscript/main.py @@ -34,6 +34,7 @@ class ClientScript(Plugin): 'scripts/library/question.js', 'scripts/library/scrollspy.js', 'scripts/library/spin.js', + 'scripts/library/Array.stableSort.js', 'scripts/couchpotato.js', 'scripts/api.js', 'scripts/library/history.js', diff --git a/couchpotato/static/scripts/library/Array.stableSort.js b/couchpotato/static/scripts/library/Array.stableSort.js new file mode 100644 index 00000000..062c7566 --- /dev/null +++ b/couchpotato/static/scripts/library/Array.stableSort.js @@ -0,0 +1,56 @@ +/* +--- + +script: Array.stableSort.js + +description: Add a stable sort algorithm for all browsers + +license: MIT-style license. + +authors: + - Yorick Sijsling + +requires: + core/1.3: '*' + +provides: + - [Array.stableSort, Array.mergeSort] + +... +*/ + +(function() { + + var defaultSortFunction = function(a, b) { + return a > b ? 1 : (a < b ? -1 : 0); + } + + Array.implement({ + + stableSort: function(compare) { + // I would love some real feature recognition. Problem is that an unstable algorithm sometimes/often gives the same result as an unstable algorithm. + return (Browser.chrome || Browser.firefox2 || Browser.opera9) ? this.mergeSort(compare) : this.sort(compare); + }, + + mergeSort: function(compare, token) { + compare = compare || defaultSortFunction; + if (this.length > 1) { + // Split and sort both parts + var right = this.splice(Math.floor(this.length / 2)).mergeSort(compare); + var left = this.splice(0).mergeSort(compare); // 'this' is now empty. + + // Merge parts together + while (left.length > 0 || right.length > 0) { + this.push( + right.length === 0 ? left.shift() + : left.length === 0 ? right.shift() + : compare(left[0], right[0]) > 0 ? right.shift() + : left.shift()); + } + } + return this; + } + + }); +})(); + diff --git a/couchpotato/static/scripts/page/settings.js b/couchpotato/static/scripts/page/settings.js index 68b41d0a..213c0d96 100644 --- a/couchpotato/static/scripts/page/settings.js +++ b/couchpotato/static/scripts/page/settings.js @@ -111,6 +111,10 @@ Page.Settings = new Class({ Cookie.write('advanced_toggle_checked', +self.advanced_toggle.checked, {'duration': 365}); }, + sortByOrder: function(a, b){ + return (a.order || 100) - (b.order || 100) + }, + create: function(json){ var self = this; @@ -141,13 +145,11 @@ Page.Settings = new Class({ options.include(section); }); - options.sort(function(a, b){ - return (a.order || 100) - (b.order || 100) - }).each(function(section){ + options.stableSort(self.sortByOrder).each(function(section){ var section_name = section.section_name; // Add groups to content - section.groups.sortBy('order').each(function(group){ + section.groups.stableSort(self.sortByOrder).each(function(group){ if(group.hidden) return; if(self.wizard_only && !group.wizard) @@ -184,9 +186,7 @@ Page.Settings = new Class({ } // Add options to group - group.options.sort(function(a, b){ - return (a.order || 100) - (b.order || 100) - }).each(function(option){ + group.options.stableSort(self.sortByOrder).each(function(option){ if(option.hidden) return; var class_name = (option.type || 'string').capitalize(); var input = new Option[class_name](section_name, option.name, self.getValue(section_name, option.name), option);