better sorting of geonames

This commit is contained in:
rolux 2011-05-24 14:50:16 +02:00
parent 6ad45105af
commit 32b2da184a
4 changed files with 49 additions and 19 deletions

View file

@ -155,6 +155,18 @@ Ox.load.Geo = function(options, callback) {
return country; return country;
}; };
/*@
Ox.reverseGeoname <f> 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); callback(true);
}); });

View file

@ -1227,20 +1227,25 @@ Ox.List = function(options, self) {
setSelected(newSelectedIds); setSelected(newSelectedIds);
} }
function updateSort(map) { function updateSort() {
Ox.print('start sort') Ox.print('start sort')
var key = self.options.sort[0].key, 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 (self.listLength > 1) {
if (Ox.isArray(self.options.items)) { if (Ox.isArray(self.options.items)) {
self.options.items.sort(function(a, b) { self.options.items.sort(function(a, b) {
var aValue = map ? map(a[key]) : a[key], var aValue = sort[a.id],
bValue = map ? map(b[key]) : b[key], bValue = sort[b.id],
ret = 0 ret = 0
if (aValue < bValue) { if (aValue < bValue) {
return operator == '+' ? -1 : 1 ret = operator == '+' ? -1 : 1
} else if (aValue > bValue) { } else if (aValue > bValue) {
return operator == '+' ? 1 : -1; ret = operator == '+' ? 1 : -1;
} }
return ret; return ret;
}); });
@ -1266,6 +1271,9 @@ Ox.List = function(options, self) {
} else if (key == 'selected') { } else if (key == 'selected') {
Ox.print('setOption selected', value) Ox.print('setOption selected', value)
setSelected(value); setSelected(value);
} else if (key == 'sort') {
Ox.print('---sort---')
updateSort();
} }
}; };
@ -1490,11 +1498,13 @@ Ox.List = function(options, self) {
operator <s> +/- sort ascending or descending operator <s> +/- sort ascending or descending
map <f> function that maps values to sort values map <f> 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) { 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) { if (key != self.options.sort[0].key || operator != self.options.sort[0].operator) {
self.options.sort[0] = {key: key, operator: operator}; self.options.sort[0] = {key: key, operator: operator, map: map};
updateSort(map); updateSort();
that.triggerEvent('sort', self.options.sort[0]); that.triggerEvent('sort', self.options.sort[0]);
} }
return that; return that;

View file

@ -13,8 +13,8 @@ Ox.TextList <f:Ox.Element> TextList Object
format <f> format <f>
id <s> id <s>
removable <b> removable <b>
map <f> function that maps values to sort values
operator <s> default sort operator operator <s> default sort operator
sort <f> function that maps values to sort values
title <s> title <s>
unique <b> If true, this column acts as unique id unique <b> If true, this column acts as unique id
visible <b> visible <b>
@ -254,11 +254,15 @@ Ox.TextList = function(options, self) {
Ox.print('clickColumn', id); Ox.print('clickColumn', id);
var i = getColumnIndexById(id), var i = getColumnIndexById(id),
isSelected = self.options.sort[0].key == self.options.columns[i].id; isSelected = self.options.sort[0].key == self.options.columns[i].id;
that.sortList( that.$body.options({
self.options.columns[i].id, isSelected ? sort: [{
key: self.options.columns[i].id,
operator: isSelected ?
(self.options.sort[0].operator == '+' ? '-' : '+') : (self.options.sort[0].operator == '+' ? '-' : '+') :
self.options.columns[i].operator self.options.columns[i].operator,
); map: self.options.columns[i].map
}]
});
} }
function constructHead() { function constructHead() {
@ -729,7 +733,11 @@ Ox.TextList = function(options, self) {
that.sortList = function(key, operator) { that.sortList = function(key, operator) {
var isSelected = key == self.options.sort[0].key; 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 (self.options.columnsVisible) {
if (isSelected) { if (isSelected) {
updateOrder(self.options.columns[self.selectedColumn].id); updateOrder(self.options.columns[self.selectedColumn].id);
@ -745,7 +753,7 @@ Ox.TextList = function(options, self) {
that.$body.sortList( that.$body.sortList(
self.options.sort[0].key, self.options.sort[0].key,
self.options.sort[0].operator, self.options.sort[0].operator,
self.options.columns[self.selectedColumn].sort self.options.sort[0].map
); );
}, 10); }, 10);
return that; return that;

View file

@ -102,10 +102,10 @@ Ox.ListMap = function(options, self) {
editable: true, editable: true,
id: 'geoname', id: 'geoname',
removable: false, removable: false,
operator: '+', map: function(v) {
sort: function(v) {
return v.split(', ').reverse().join(', ') return v.split(', ').reverse().join(', ')
}, },
operator: '+',
title: 'Geoname', title: 'Geoname',
visible: true, visible: true,
width: 192 width: 192