From ae273f2414e64f78c7e9adc70d18e1d8abe9b561 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 6 Feb 2012 10:46:48 +0100 Subject: [IMP] move DataSetSearch#sort to DataSet#sort bzr revid: xmo@openerp.com-20120206094648-1pq0y6oczk8uzzw6 --- addons/web/static/src/js/data.js | 52 ++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'addons/web/static/src/js/data.js') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index b596b1eb250..6cfe20984be 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -248,6 +248,7 @@ openerp.web.DataSet = openerp.web.OldWidget.extend( /** @lends openerp.web.Data this.model = model; this.context = context || {}; this.index = null; + this._sort = []; }, previous: function () { this.index -= 1; @@ -496,6 +497,31 @@ openerp.web.DataSet = openerp.web.OldWidget.extend( /** @lends openerp.web.Data return new openerp.web.CompoundContext(this.context, request_context); } return this.context; + }, + /** + * Reads or changes sort criteria on the dataset. + * + * If not provided with any argument, serializes the sort criteria to + * an SQL-like form usable by OpenERP's ORM. + * + * If given a field, will set that field as first sorting criteria or, + * if the field is already the first sorting criteria, will reverse it. + * + * @param {String} [field] field to sort on, reverses it (toggle from ASC to DESC) if already the main sort criteria + * @param {Boolean} [force_reverse=false] forces inserting the field as DESC + * @returns {String|undefined} + */ + sort: function (field, force_reverse) { + if (!field) { + return openerp.web.serialize_sort(this._sort); + } + var reverse = force_reverse || (this._sort[0] === field); + this._sort.splice.apply( + this._sort, [0, this._sort.length].concat( + _.without(this._sort, field, '-' + field))); + + this._sort.unshift((reverse ? '-' : '') + field); + return undefined; } }); openerp.web.DataSetStatic = openerp.web.DataSet.extend({ @@ -541,7 +567,6 @@ openerp.web.DataSetSearch = openerp.web.DataSet.extend(/** @lends openerp.web.D init: function(parent, model, context, domain) { this._super(parent, model, context); this.domain = domain || []; - this._sort = []; this.offset = 0; // subset records[offset:offset+limit] // is it necessary ? @@ -583,31 +608,6 @@ openerp.web.DataSetSearch = openerp.web.DataSet.extend(/** @lends openerp.web.D } return this.domain; }, - /** - * Reads or changes sort criteria on the dataset. - * - * If not provided with any argument, serializes the sort criteria to - * an SQL-like form usable by OpenERP's ORM. - * - * If given a field, will set that field as first sorting criteria or, - * if the field is already the first sorting criteria, will reverse it. - * - * @param {String} [field] field to sort on, reverses it (toggle from ASC to DESC) if already the main sort criteria - * @param {Boolean} [force_reverse=false] forces inserting the field as DESC - * @returns {String|undefined} - */ - sort: function (field, force_reverse) { - if (!field) { - return openerp.web.serialize_sort(this._sort); - } - var reverse = force_reverse || (this._sort[0] === field); - this._sort.splice.apply( - this._sort, [0, this._sort.length].concat( - _.without(this._sort, field, '-' + field))); - - this._sort.unshift((reverse ? '-' : '') + field); - return undefined; - }, unlink: function(ids, callback, error_callback) { var self = this; return this._super(ids, function(result) { -- cgit v1.2.3 From 7b25f9d4c7495ed3fe2041b1ae8c4801d3c294dd Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 6 Feb 2012 11:12:15 +0100 Subject: [UP] update underscore bzr revid: xmo@openerp.com-20120206101215-yutyc4dv2p1qgxtu --- addons/web/static/src/js/data.js | 1 + 1 file changed, 1 insertion(+) (limited to 'addons/web/static/src/js/data.js') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 6cfe20984be..046930e4baf 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -722,6 +722,7 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({ throw "Record not correctly loaded"; } } + completion.resolve(records); }; if(to_get.length > 0) { -- cgit v1.2.3 From 6ebf8face0c4a53ecdb33b08b7072e083fdcdcbb Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 6 Feb 2012 11:32:59 +0100 Subject: [ADD] sorting support in buffered datasets bzr revid: xmo@openerp.com-20120206103259-nt3wjmt9w9k7kegy --- addons/web/static/src/js/data.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'addons/web/static/src/js/data.js') diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js index 046930e4baf..01962f829e6 100644 --- a/addons/web/static/src/js/data.js +++ b/addons/web/static/src/js/data.js @@ -722,7 +722,24 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({ throw "Record not correctly loaded"; } } + var sort_fields = self._sort, + compare = function (v1, v2) { + return (v1 < v2) ? -1 + : (v1 > v2) ? 1 + : 0; + }; + records.sort(function (a, b) { + return _.reduce(sort_fields, function (acc, field) { + if (acc) { return acc; } + var sign = 1; + if (field[0] === '-') { + sign = -1; + field = field.slice(1); + } + return sign * compare(a[field], b[field]); + }, 0); + }); completion.resolve(records); }; if(to_get.length > 0) { -- cgit v1.2.3