From bc3fedb125f9bf7197bae73aac79a39a033efdd8 Mon Sep 17 00:00:00 2001 From: rlx <0x0073@0x2620.org> Date: Tue, 20 Mar 2012 09:34:50 +0000 Subject: [PATCH] fix alphabetical order of annotations with tags, fixes #681 --- source/Ox.UI/js/Form/Ox.ArrayEditable.js | 10 +++++-- source/Ox.UI/js/Menu/Ox.Menu.js | 1 - source/Ox.UI/js/Video/Ox.AnnotationFolder.js | 7 +++-- source/Ox/js/Array.js | 29 ++++++++++++-------- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/source/Ox.UI/js/Form/Ox.ArrayEditable.js b/source/Ox.UI/js/Form/Ox.ArrayEditable.js index 8b6f9c03..1cbeee12 100644 --- a/source/Ox.UI/js/Form/Ox.ArrayEditable.js +++ b/source/Ox.UI/js/Form/Ox.ArrayEditable.js @@ -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(); } } diff --git a/source/Ox.UI/js/Menu/Ox.Menu.js b/source/Ox.UI/js/Menu/Ox.Menu.js index 6ab5da56..be73e602 100644 --- a/source/Ox.UI/js/Menu/Ox.Menu.js +++ b/source/Ox.UI/js/Menu/Ox.Menu.js @@ -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); diff --git a/source/Ox.UI/js/Video/Ox.AnnotationFolder.js b/source/Ox.UI/js/Video/Ox.AnnotationFolder.js index cb5fc9e3..71519443 100644 --- a/source/Ox.UI/js/Video/Ox.AnnotationFolder.js +++ b/source/Ox.UI/js/Video/Ox.AnnotationFolder.js @@ -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; diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index 4e2a8a04..a3faa4c0 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -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 Sorts an array of objects by given properties (arr, by) -> 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 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;