fix alphabetical order of annotations with tags, fixes #681

This commit is contained in:
rlx 2012-03-20 09:34:50 +00:00
parent c4de1a3a40
commit bc3fedb125
4 changed files with 30 additions and 17 deletions

View file

@ -11,6 +11,7 @@ Ox.ArrayEditable = function(options, self) {
.defaults({
clickLink: null,
editable: true,
getSortValue: null,
highlight: '',
itemName: 'item',
items: [],
@ -208,7 +209,6 @@ Ox.ArrayEditable = function(options, self) {
self.editing = false;
that.blurItem();
}
Ox.print('SELECT ITEM', self.options.selected, self.selected);
that.find('.OxSelected').removeClass('OxSelected');
self.selected > -1 && self.$items[self.selected].addClass('OxSelected');
triggerSelectEvent();
@ -279,7 +279,13 @@ Ox.ArrayEditable = function(options, self) {
function sortItems() {
if (!Ox.isEmpty(self.options.sort)) {
self.options.items = Ox.sortBy(self.options.items, self.options.sort);
self.options.items = Ox.sortBy(
self.options.items,
self.options.sort,
self.options.getSortValue
? {value: self.options.getSortValue}
: {}
);
self.selected = getSelectedPosition();
}
}

View file

@ -600,7 +600,6 @@ Ox.Menu = function(options, self) {
if (ids.length == 1) {
item = that.getItem(id);
group = item.options('group');
Ox.Log('Menu', 'checkItem', id, item, that.submenus)
if (group) {
offset = self.optionGroupOffset[group];
position = getItemPositionById(id);

View file

@ -199,6 +199,11 @@ Ox.AnnotationFolder = function(options, self) {
self.$annotations = Ox.ArrayEditable({
clickLink: self.options.clickLink,
editable: self.options.editable,
getSortValue: self.options.type == 'text'
? function(value) {
return Ox.stripTags(value);
}
: null,
highlight: self.options.highlight,
items: self.annotations,
placeholder: 'No ' + self.options.title,
@ -540,7 +545,6 @@ Ox.AnnotationFolder = function(options, self) {
if (value === '') {
self.editing = false;
}
value && Ox.print('----------------- select item in folder', value, self.options.collapsed)
if (value && self.options.collapsed) {
self.$panel.options({animate: false});
self.$panel.options({collapsed: false});
@ -620,7 +624,6 @@ Ox.AnnotationFolder = function(options, self) {
};
that.updateItem = function(id, data) {
Ox.print('-- UPDATE ITEM', id, data);
var item = Ox.getObjectById(self.options.items, id);
Ox.forEach(data, function(value, key) {
item[key] = value;

View file

@ -85,24 +85,27 @@ Ox.range = function() {
(function() {
function getSortValues(arr) {
var len, matches = {}, sort = {};
function getSortValues(arr, fn) {
var arr_ = fn ? arr.map(fn) : arr,
len, matches = {}, sort = {};
// find leading numbers
arr.forEach(function(val) {
var match = /^\d+/.exec(val);
arr.forEach(function(val, i) {
var match = /^\d+/.exec(arr_[i]);
matches[val] = match ? match[0] : '';
});
// get length of longest leading number
len = Ox.max(Ox.map(matches, function(val) {
return val.length;
}));
// pad leading numbers and make lowercase
arr.forEach(function(val) {
// pad leading numbers, make lowercase,
// and remove leading non-word characters
arr.forEach(function(val, i) {
sort[val] = (
matches[val]
? Ox.pad(matches[val], len)
+ val.toString().substr(matches[val].length)
: val
? arr_[i].toString().replace(
matches[val], Ox.pad(matches[val], len)
)
: arr_[i]
).toLowerCase().replace(/^\W+/, '');
});
return sort;
@ -138,13 +141,14 @@ Ox.range = function() {
Ox.sortBy <f> Sorts an array of objects by given properties
(arr, by) -> <a> Sorted array
arr <[o]> Array of objects
by <[s]> Array of object properties (asc: 'foo' or '+foo', desc: '-foo')
by <[s]> Array of object keys (asc: 'foo' or '+foo', desc: '-foo')
fn <o> Optional functions, per key, that return the sort value
> Ox.sortBy([{x: 1, y: 1}, {x: 1, y: 2}, {x: 2, y: 2}], ['+x', '-y'])
[{x: 1, y: 2}, {x: 1, y: 1}, {x: 2, y: 2}]
> Ox.sortBy([{id: 0, name: '80 Days'}, {id: 1, name: '8 Women'}], ['name'])
[{id: 1, name: '8 Women'}, {id: 0, name: '80 Days'}]
@*/
Ox.sortBy = function(arr, by) {
Ox.sortBy = function(arr, by, fn) {
var length = by.length, values = {};
by = by.map(function(v) {
return {
@ -152,12 +156,13 @@ Ox.range = function() {
operator: v[0] == '-' ? '-' : '+'
};
});
fn = fn || {};
by.map(function(v) {
return v.key;
}).forEach(function(key) {
values[key] = getSortValues(arr.map(function(v) {
return v[key];
}));
}), fn[key]);
});
return arr.sort(function(a, b) {
var aValue, bValue, index = 0, key, ret = 0;