From 65a2e2a39bff8e3455ef8296f16eb77fabbfa705 Mon Sep 17 00:00:00 2001 From: Christopher Ghormley Date: Wed, 22 Jun 2011 18:25:49 +0000 Subject: [PATCH 1/4] Fix for secondary sort column(s) using 'prepare' flag. This is an attempt to fix #573. Secondary sort column is provided for in the documentation but didn't work. Keep track of sort direction for any sorted column. Need to add a test for this feature. No doubt class state and method arguments could be cleaned up. --- Source/Interface/HtmlTable.Sort.js | 63 ++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index 91941fe0..68dfcc1c 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -50,7 +50,7 @@ HtmlTable = Class.refactor(HtmlTable, { initialize: function (){ this.previous.apply(this, arguments); if (this.occluded) return this.occluded; - this.sorted = {index: null, dir: 1}; + this.sorted = {index: null, reverse: [], order: []}; if (!this.bound) this.bound = {}; this.bound.headClick = this.headClick.bind(this); this.sortSpans = new Elements(); @@ -126,6 +126,7 @@ HtmlTable = Class.refactor(HtmlTable, { previousSerialization.sortIndex = this.sorted.index; previousSerialization.sortReverse = this.sorted.reverse; } + previousSerialization.sortOrder = this.sorted.lastSortOrder; return previousSerialization; }, @@ -136,12 +137,14 @@ HtmlTable = Class.refactor(HtmlTable, { this.previous.apply(this, arguments); }, - setSortedState: function(index, reverse){ - if (reverse != null) this.sorted.reverse = reverse; - else if (this.sorted.index == index) this.sorted.reverse = !this.sorted.reverse; - else this.sorted.reverse = this.sorted.index == null; + setSortedState: function(index, reverse, pre){ + if (index != null) { + if (reverse != null) this.sorted.reverse[index] = reverse; + else if (this.sorted.reverse[index] != null) this.sorted.reverse[index] = !this.sorted.reverse[index]; + else this.sorted.reverse[index] = false; - if (index != null) this.sorted.index = index; + if (!pre) this.sorted.index = index; + } }, setHeadSort: function(sorted){ @@ -151,7 +154,7 @@ HtmlTable = Class.refactor(HtmlTable, { if (!head.length) return; if (sorted){ head.addClass(this.options.classHeadSort); - if (this.sorted.reverse) head.addClass(this.options.classHeadSortRev); + if (this.sorted.reverse[this.sorted.index]) head.addClass(this.options.classHeadSortRev); else head.removeClass(this.options.classHeadSortRev); } else { head.removeClass(this.options.classHeadSort).removeClass(this.options.classHeadSortRev); @@ -193,21 +196,25 @@ HtmlTable = Class.refactor(HtmlTable, { return item.value; }, - getParser: function(){ - var parser = this.parsers[this.sorted.index]; + getParser: function(index){ + var parser; + + if (index) parser = this.parsers[index]; + else parser = this.parsers[this.sorted.index]; + return typeOf(parser) == 'string' ? HtmlTable.Parsers[parser] : parser; }, sort: function(index, reverse, pre){ if (!this.head) return; - if (!pre){ - this.clearSort(); - this.setSortedState(index, reverse); - this.setHeadSort(true); - } + this.sorted.order.push(index); + + if (!pre) this.clearSort(); + this.setSortedState(index, reverse, pre); + if (!pre) this.setHeadSort(true); - var parser = this.getParser(); + var parser = this.getParser(index); if (!parser) return; var rel; @@ -216,22 +223,23 @@ HtmlTable = Class.refactor(HtmlTable, { this.body.dispose(); } - var data = this.parseData(parser).sort(function(a, b){ + var data = this.parseData(parser, index).sort(function(a, b){ if (a.value === b.value) return 0; return a.value > b.value ? 1 : -1; }); - if (this.sorted.reverse == (parser == HtmlTable.Parsers['input-checked'])) data.reverse(true); + if (this.sorted.reverse[index] == (parser == HtmlTable.Parsers['input-checked'])) data.reverse(true); + this.setRowSort(data, pre); if (rel) rel.grab(this.body); this.fireEvent('stateChanged'); - return this.fireEvent('sort', [this.body, this.sorted.index]); + return this.fireEvent('sort', [this.body, index]); }, - parseData: function(parser){ + parseData: function(parser, index){ return Array.map(this.body.rows, function(row, i){ - var value = parser.convert.call(document.id(row.cells[this.sorted.index])); + var value = parser.convert.call(document.id(row.cells[index])); return { position: i, value: value @@ -242,10 +250,23 @@ HtmlTable = Class.refactor(HtmlTable, { clearSort: function(){ this.setHeadSort(false); this.body.getElements('td').removeClass(this.options.classCellSort); + + this.sorted.lastSortOrder = this.sorted.order; + this.sorted.order = []; }, reSort: function(){ - if (this.sortEnabled) this.sort.call(this, this.sorted.index, this.sorted.reverse); + + var colIndex; + var reverse = this.sorted.reverse; + var lastSort = this.sorted.lastSortOrder; + + if (this.sortEnabled) { + for (var colIndex=0; colIndex Date: Thu, 7 Jul 2011 17:23:01 +0000 Subject: [PATCH 2/4] Initialize lastSortOrder. --- Source/Interface/HtmlTable.Sort.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index 68dfcc1c..d924a90a 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -50,7 +50,10 @@ HtmlTable = Class.refactor(HtmlTable, { initialize: function (){ this.previous.apply(this, arguments); if (this.occluded) return this.occluded; - this.sorted = {index: null, reverse: [], order: []}; + this.sorted = {index: null, + reverse: [], + order: [], + lastSortOrder: []}; if (!this.bound) this.bound = {}; this.bound.headClick = this.headClick.bind(this); this.sortSpans = new Elements(); From 10503b34c5d7be0ac21c00709184eab65e7eb768 Mon Sep 17 00:00:00 2001 From: Christopher Ghormley Date: Thu, 7 Jul 2011 17:37:43 +0000 Subject: [PATCH 3/4] Whitespace. --- Source/Interface/HtmlTable.Sort.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index d924a90a..82f998a9 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -50,10 +50,7 @@ HtmlTable = Class.refactor(HtmlTable, { initialize: function (){ this.previous.apply(this, arguments); if (this.occluded) return this.occluded; - this.sorted = {index: null, - reverse: [], - order: [], - lastSortOrder: []}; + this.sorted = {index: null, reverse: [], order: [], lastSortOrder: []}; if (!this.bound) this.bound = {}; this.bound.headClick = this.headClick.bind(this); this.sortSpans = new Elements(); From 69dafbe94648591627f92d84bbf22fd542c6a8c2 Mon Sep 17 00:00:00 2001 From: Christopher Ghormley Date: Thu, 21 Jul 2011 00:42:12 +0000 Subject: [PATCH 4/4] restore: Restore the sort state, then call reSort. --- Source/Interface/HtmlTable.Sort.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index 82f998a9..d8bdbcb8 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -132,7 +132,10 @@ HtmlTable = Class.refactor(HtmlTable, { restore: function(tableState){ if(this.options.sortable && tableState.sortIndex){ - this.sort(tableState.sortIndex, tableState.sortReverse); + this.sorted.lastSortOrder = tableState.sortOrder; + this.sorted.reverse = tableState.sortReverse; + this.sorted.index = tableState.sortIndex; + this.reSort(); } this.previous.apply(this, arguments); },