From 32b2da184a5127d24b8e682b16b9beea19bd442c Mon Sep 17 00:00:00 2001 From: rolux Date: Tue, 24 May 2011 14:50:16 +0200 Subject: [PATCH] better sorting of geonames --- source/Ox.Geo/Ox.Geo.js | 12 ++++++++++++ source/Ox.UI/js/List/Ox.List.js | 28 +++++++++++++++++++--------- source/Ox.UI/js/List/Ox.TextList.js | 24 ++++++++++++++++-------- source/Ox.UI/js/Map/Ox.ListMap.js | 4 ++-- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/source/Ox.Geo/Ox.Geo.js b/source/Ox.Geo/Ox.Geo.js index 137e2766..9b2a1a92 100644 --- a/source/Ox.Geo/Ox.Geo.js +++ b/source/Ox.Geo/Ox.Geo.js @@ -155,6 +155,18 @@ Ox.load.Geo = function(options, callback) { return country; }; + /*@ + Ox.reverseGeoname Reverses a geoname + @*/ + Ox.reverseGeoname = function(geoname) { + return geoname + .replace('Bonaire, Saint Eustatius and Saba', 'Bonaire Saint Eustatius and Saba') + .split(', ') + .reverse() + .join(', ') + .replace('Bonaire Saint Eustatius and Saba', 'Bonaire, Saint Eustatius and Saba'); + }; + callback(true); }); diff --git a/source/Ox.UI/js/List/Ox.List.js b/source/Ox.UI/js/List/Ox.List.js index 1bbbc717..0d39f180 100644 --- a/source/Ox.UI/js/List/Ox.List.js +++ b/source/Ox.UI/js/List/Ox.List.js @@ -1227,20 +1227,25 @@ Ox.List = function(options, self) { setSelected(newSelectedIds); } - function updateSort(map) { + function updateSort() { Ox.print('start sort') var key = self.options.sort[0].key, - operator = self.options.sort[0].operator; + operator = self.options.sort[0].operator, + map = self.options.sort[0].map, + sort = {}; + self.options.items.forEach(function(item) { + sort[item.id] = map ? map(item[key]) : item[key]; + }); if (self.listLength > 1) { if (Ox.isArray(self.options.items)) { self.options.items.sort(function(a, b) { - var aValue = map ? map(a[key]) : a[key], - bValue = map ? map(b[key]) : b[key], + var aValue = sort[a.id], + bValue = sort[b.id], ret = 0 if (aValue < bValue) { - return operator == '+' ? -1 : 1 + ret = operator == '+' ? -1 : 1 } else if (aValue > bValue) { - return operator == '+' ? 1 : -1; + ret = operator == '+' ? 1 : -1; } return ret; }); @@ -1266,6 +1271,9 @@ Ox.List = function(options, self) { } else if (key == 'selected') { Ox.print('setOption selected', value) setSelected(value); + } else if (key == 'sort') { + Ox.print('---sort---') + updateSort(); } }; @@ -1490,11 +1498,13 @@ Ox.List = function(options, self) { operator +/- sort ascending or descending map function that maps values to sort values @*/ + // fixme: this (and others) should be deprecated, + // one should set options instead that.sortList = function(key, operator, map) { - Ox.print('sortList', key, operator) + Ox.print('sortList', key, operator, map) if (key != self.options.sort[0].key || operator != self.options.sort[0].operator) { - self.options.sort[0] = {key: key, operator: operator}; - updateSort(map); + self.options.sort[0] = {key: key, operator: operator, map: map}; + updateSort(); that.triggerEvent('sort', self.options.sort[0]); } return that; diff --git a/source/Ox.UI/js/List/Ox.TextList.js b/source/Ox.UI/js/List/Ox.TextList.js index 35c8776b..33e3cb81 100644 --- a/source/Ox.UI/js/List/Ox.TextList.js +++ b/source/Ox.UI/js/List/Ox.TextList.js @@ -13,8 +13,8 @@ Ox.TextList TextList Object format id removable + map function that maps values to sort values operator default sort operator - sort function that maps values to sort values title unique If true, this column acts as unique id visible @@ -254,11 +254,15 @@ Ox.TextList = function(options, self) { Ox.print('clickColumn', id); var i = getColumnIndexById(id), isSelected = self.options.sort[0].key == self.options.columns[i].id; - that.sortList( - self.options.columns[i].id, isSelected ? - (self.options.sort[0].operator == '+' ? '-' : '+') : - self.options.columns[i].operator - ); + that.$body.options({ + sort: [{ + key: self.options.columns[i].id, + operator: isSelected ? + (self.options.sort[0].operator == '+' ? '-' : '+') : + self.options.columns[i].operator, + map: self.options.columns[i].map + }] + }); } function constructHead() { @@ -729,7 +733,11 @@ Ox.TextList = function(options, self) { that.sortList = function(key, operator) { var isSelected = key == self.options.sort[0].key; - self.options.sort = [{key: key, operator: operator}]; + self.options.sort = [{ + key: key, + operator: operator, + map: self.options.columns[self.selectedColumn].sort + }]; if (self.options.columnsVisible) { if (isSelected) { updateOrder(self.options.columns[self.selectedColumn].id); @@ -745,7 +753,7 @@ Ox.TextList = function(options, self) { that.$body.sortList( self.options.sort[0].key, self.options.sort[0].operator, - self.options.columns[self.selectedColumn].sort + self.options.sort[0].map ); }, 10); return that; diff --git a/source/Ox.UI/js/Map/Ox.ListMap.js b/source/Ox.UI/js/Map/Ox.ListMap.js index 98368dfd..3a05a38d 100644 --- a/source/Ox.UI/js/Map/Ox.ListMap.js +++ b/source/Ox.UI/js/Map/Ox.ListMap.js @@ -102,10 +102,10 @@ Ox.ListMap = function(options, self) { editable: true, id: 'geoname', removable: false, - operator: '+', - sort: function(v) { + map: function(v) { return v.split(', ').reverse().join(', ') }, + operator: '+', title: 'Geoname', visible: true, width: 192