summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/data.js
diff options
context:
space:
mode:
authorFabien Meghazi <fme@openerp.com>2012-02-15 10:59:40 +0100
committerFabien Meghazi <fme@openerp.com>2012-02-15 10:59:40 +0100
commitbc233c30e5b9d28c9576b50c6f75dadbdf2e35ea (patch)
treeffadfea5a36040b12635e11acdc4237d5a953db8 /addons/web/static/src/js/data.js
parent5ab1b4f37ef93952567939444196cd9a91b92f02 (diff)
parent750c5db2a30f89593cbd64137e765b11d5226808 (diff)
[FIX] Merge last trunk
bzr revid: fme@openerp.com-20120215095940-2jv27t91i7u5mm0r
Diffstat (limited to 'addons/web/static/src/js/data.js')
-rw-r--r--addons/web/static/src/js/data.js96
1 files changed, 65 insertions, 31 deletions
diff --git a/addons/web/static/src/js/data.js b/addons/web/static/src/js/data.js
index be7d707c91c..636ad575f99 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,7 +497,38 @@ 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;
+ },
+ size: function () {
+ return this.ids.length;
+ },
+ alter_ids: function(n_ids) {
+ this.ids = n_ids;
+ },
});
openerp.web.DataSetStatic = openerp.web.DataSet.extend({
init: function(parent, model, context, ids) {
@@ -507,7 +539,6 @@ openerp.web.DataSetStatic = openerp.web.DataSet.extend({
read_slice: function (fields, options) {
options = options || {};
fields = fields || {};
- // TODO remove fields from options
var offset = options.offset || 0,
limit = options.limit || false;
var end_pos = limit && limit !== -1 ? offset + limit : this.ids.length;
@@ -542,8 +573,8 @@ 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;
+ this._length;
// subset records[offset:offset+limit]
// is it necessary ?
this.ids = [];
@@ -575,6 +606,7 @@ openerp.web.DataSetSearch = openerp.web.DataSet.extend(/** @lends openerp.web.D
}).pipe(function (result) {
self.ids = result.ids;
self.offset = offset;
+ self._length = result.length;
return result.records;
});
},
@@ -584,31 +616,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) {
@@ -620,6 +627,12 @@ openerp.web.DataSetSearch = openerp.web.DataSet.extend(/** @lends openerp.web.D
if (callback)
callback(result);
}, error_callback);
+ },
+ size: function () {
+ if (this._length !== undefined) {
+ return this._length;
+ }
+ return this._super();
}
});
openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({
@@ -641,7 +654,6 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({
defaults: this.last_default_get};
this.to_create.push(_.extend(_.clone(cached), {values: _.clone(cached.values)}));
this.cache.push(cached);
- this.on_change();
var prom = $.Deferred().then(callback);
prom.resolve({result: cached.id});
return prom.promise();
@@ -723,6 +735,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) {
@@ -734,7 +764,7 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({
self.cache.push({id: id, values: record});
} else {
// I assume cache value is prioritary
- _.defaults(cached.values, record);
+ cached.values = _.defaults(_.clone(cached.values), record);
}
});
return_records();
@@ -757,7 +787,11 @@ openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({
}
}
return this._super(method, args, callback, error_callback);
- }
+ },
+ alter_ids: function(n_ids) {
+ this._super(n_ids);
+ this.on_change();
+ },
});
openerp.web.BufferedDataSet.virtual_id_regex = /^one2many_v_id_.*$/;