2 openerp.web.data = function(openerp) {
5 * Serializes the sort criterion array of a dataset into a form which can be
6 * consumed by OpenERP's RPC APIs.
8 * @param {Array} criterion array of fields, from first to last criteria, prefixed with '-' for reverse sorting
9 * @returns {String} SQL-like sorting string (``ORDER BY``) clause
11 openerp.web.serialize_sort = function (criterion) {
12 return _.map(criterion,
14 if (criteria[0] === '-') {
15 return criteria.slice(1) + ' DESC';
17 return criteria + ' ASC';
21 openerp.web.DataGroup = openerp.web.Widget.extend( /** @lends openerp.web.DataGroup# */{
23 * Management interface between views and grouped collections of OpenERP
26 * The root DataGroup is instantiated with the relevant information
27 * (a session, a model, a domain, a context and a group_by sequence), the
28 * domain and context may be empty. It is then interacted with via
29 * :js:func:`~openerp.web.DataGroup.list`, which is used to read the
30 * content of the current grouping level.
32 * @constructs openerp.web.DataGroup
33 * @extends openerp.web.Widget
35 * @param {openerp.web.Session} session Current OpenERP session
36 * @param {String} model name of the model managed by this DataGroup
37 * @param {Array} domain search domain for this DataGroup
38 * @param {Object} context context of the DataGroup's searches
39 * @param {Array} group_by sequence of fields by which to group
40 * @param {Number} [level=0] nesting level of the group
42 init: function(parent, model, domain, context, group_by, level) {
43 this._super(parent, null);
45 if (group_by.length || context['group_by_no_leaf']) {
46 return new openerp.web.ContainerDataGroup( this, model, domain, context, group_by, level);
48 return new openerp.web.GrouplessDataGroup( this, model, domain, context, level);
53 this.context = context;
56 this.level = level || 0;
60 openerp.web.ContainerDataGroup = openerp.web.DataGroup.extend( /** @lends openerp.web.ContainerDataGroup# */ {
63 * @constructs openerp.web.ContainerDataGroup
64 * @extends openerp.web.DataGroup
73 init: function (parent, model, domain, context, group_by, level) {
74 this._super(parent, model, domain, context, null, level);
76 this.group_by = group_by;
79 * The format returned by ``read_group`` is absolutely dreadful:
81 * * A ``__context`` key provides future grouping levels
82 * * A ``__domain`` key provides the domain for the next search
83 * * The current grouping value is provided through the name of the
84 * current grouping name e.g. if currently grouping on ``user_id``, then
85 * the ``user_id`` value for this group will be provided through the
87 * * Similarly, the number of items in the group (not necessarily direct)
88 * is provided via ``${current_field}_count``
89 * * Other aggregate fields are just dumped there
91 * This function slightly improves the grouping records by:
93 * * Adding a ``grouped_on`` property providing the current grouping field
94 * * Adding a ``value`` and a ``length`` properties which replace the
95 * ``$current_field`` and ``${current_field}_count`` ones
96 * * Moving aggregate values into an ``aggregates`` property object
98 * Context and domain keys remain as-is, they should not be used externally
99 * but in case they're needed...
101 * @param {Object} group ``read_group`` record
103 transform_group: function (group) {
104 var field_name = this.group_by[0];
105 // In cases where group_by_no_leaf and no group_by, the result of
106 // read_group has aggregate fields but no __context or __domain.
107 // Create default (empty) values for those so that things don't break
108 var fixed_group = _.extend(
109 {__context: {group_by: []}, __domain: []},
113 _(fixed_group).each(function (value, key) {
114 if (key.indexOf('__') === 0
115 || key === field_name
116 || key === field_name + '_count') {
119 aggregates[key] = value || 0;
123 __context: fixed_group.__context,
124 __domain: fixed_group.__domain,
126 grouped_on: field_name,
127 // if terminal group (or no group) and group_by_no_leaf => use group.__count
128 length: fixed_group[field_name + '_count'] || fixed_group.__count,
129 value: fixed_group[field_name],
131 openable: !(this.context['group_by_no_leaf']
132 && fixed_group.__context.group_by.length === 0),
134 aggregates: aggregates
137 fetch: function (fields) {
139 var d = new $.Deferred();
142 this.rpc('/web/group/read', {
144 context: this.context,
146 fields: _.uniq(this.group_by.concat(fields)),
147 group_by_fields: this.group_by,
148 sort: openerp.web.serialize_sort(this.sort)
149 }, function () { }).then(function (response) {
150 var data_groups = _(response).map(
151 _.bind(self.transform_group, self));
152 self.groups = data_groups;
153 d.resolveWith(self, [data_groups]);
155 d.rejectWith.apply(d, [self, arguments]);
160 * The items of a list have the following properties:
163 * the number of records contained in the group (and all of its
164 * sub-groups). This does *not* provide the size of the "next level"
165 * of the group, unless the group is terminal (no more groups within
168 * the name of the field this level was grouped on, this is mostly
169 * used for display purposes, in order to know the name of the current
170 * level of grouping. The ``grouped_on`` should be the same for all
171 * objects of the list.
173 * the value which led to this group (this is the value all contained
174 * records have for the current ``grouped_on`` field name).
176 * a mapping of other aggregation fields provided by ``read_group``
178 * @param {Array} fields the list of fields to aggregate in each group, can be empty
179 * @param {Function} ifGroups function executed if any group is found (DataGroup.group_by is non-null and non-empty), called with a (potentially empty) list of groups as parameters.
180 * @param {Function} ifRecords function executed if there is no grouping left to perform, called with a DataSet instance as parameter
182 list: function (fields, ifGroups, ifRecords) {
184 this.fetch(fields).then(function (group_records) {
185 ifGroups(_(group_records).map(function (group) {
186 var child_context = _.extend({}, self.context, group.__context);
188 new openerp.web.DataGroup(
189 self, self.model, group.__domain,
190 child_context, child_context.group_by,
192 group, {sort: self.sort});
197 openerp.web.GrouplessDataGroup = openerp.web.DataGroup.extend( /** @lends openerp.web.GrouplessDataGroup# */ {
200 * @constructs openerp.web.GrouplessDataGroup
201 * @extends openerp.web.DataGroup
209 init: function (parent, model, domain, context, level) {
210 this._super(parent, model, domain, context, null, level);
212 list: function (fields, ifGroups, ifRecords) {
214 new openerp.web.DataSetSearch(this, this.model),
215 {domain: this.domain, context: this.context, _sort: this.sort}));
218 openerp.web.StaticDataGroup = openerp.web.GrouplessDataGroup.extend( /** @lends openerp.web.StaticDataGroup# */ {
220 * A specialization of groupless data groups, relying on a single static
221 * dataset as its records provider.
223 * @constructs openerp.web.StaticDataGroup
224 * @extends openerp.web.GrouplessDataGroup
225 * @param {openep.web.DataSetStatic} dataset a static dataset backing the groups
227 init: function (dataset) {
228 this.dataset = dataset;
230 list: function (fields, ifGroups, ifRecords) {
231 ifRecords(this.dataset);
235 openerp.web.DataSet = openerp.web.Widget.extend( /** @lends openerp.web.DataSet# */{
236 identifier_prefix: "dataset",
238 * DateaManagement interface between views and the collection of selected
239 * OpenERP records (represents the view's state?)
241 * @constructs openerp.web.DataSet
242 * @extends openerp.web.Widget
244 * @param {String} model the OpenERP model this dataset will manage
246 init: function(parent, model, context) {
249 this.context = context || {};
252 previous: function () {
254 if (this.index < 0) {
255 this.index = this.ids.length - 1;
261 if (this.index >= this.ids.length) {
269 * @param {Array} ids identifiers of the records to read
270 * @param {Array} fields fields to read and return, by default all fields are returned
271 * @param {Function} callback function called with read result
272 * @returns {$.Deferred}
274 read_ids: function (ids, fields, callback) {
275 return this.rpc('/web/dataset/get', {
279 context: this.get_context()
283 * Read a slice of the records represented by this DataSet, based on its
284 * domain and context.
286 * @param {Array} [fields] fields to read and return, by default all fields are returned
287 * @params {Object} options
288 * @param {Number} [options.offset=0] The index from which selected records should be returned
289 * @param {Number} [options.limit=null] The maximum number of records to return
290 * @param {Function} callback function called with read_slice result
291 * @returns {$.Deferred}
293 read_slice: function (fields, options, callback) {
297 * Reads the current dataset record (from its index)
299 * @params {Array} [fields] fields to read and return, by default all fields are returned
300 * @params {Function} callback function called with read_index result
301 * @returns {$.Deferred}
303 read_index: function (fields, callback) {
304 var def = $.Deferred().then(callback);
305 if (_.isEmpty(this.ids)) {
308 fields = fields || false;
309 return this.read_ids([this.ids[this.index]], fields).then(function(records) {
310 def.resolve(records[0]);
312 def.reject.apply(def, arguments);
315 return def.promise();
318 * Reads default values for the current model
320 * @param {Array} [fields] fields to get default values for, by default all defaults are read
321 * @param {Function} callback function called with default_get result
322 * @returns {$.Deferred}
324 default_get: function(fields, callback) {
325 return this.rpc('/web/dataset/default_get', {
328 context: this.get_context()
332 * Creates a new record in db
334 * @param {Object} data field values to set on the new record
335 * @param {Function} callback function called with operation result
336 * @param {Function} error_callback function called in case of creation error
337 * @returns {$.Deferred}
339 create: function(data, callback, error_callback) {
340 return this.rpc('/web/dataset/create', {
343 context: this.get_context()
344 }, callback, error_callback);
347 * Saves the provided data in an existing db record
349 * @param {Number|String} id identifier for the record to alter
350 * @param {Object} data field values to write into the record
351 * @param {Function} callback function called with operation result
352 * @param {Function} error_callback function called in case of write error
353 * @returns {$.Deferred}
355 write: function (id, data, options, callback, error_callback) {
356 options = options || {};
357 return this.rpc('/web/dataset/save', {
361 context: this.get_context(options.context)
362 }, callback, error_callback);
365 * Deletes an existing record from the database
367 * @param {Number|String} ids identifier of the record to delete
368 * @param {Function} callback function called with operation result
369 * @param {Function} error_callback function called in case of deletion error
371 unlink: function(ids, callback, error_callback) {
373 return this.call_and_eval("unlink", [ids, this.get_context()], null, 1,
374 callback, error_callback);
377 * Calls an arbitrary RPC method
379 * @param {String} method name of the method (on the current model) to call
380 * @param {Array} [args] arguments to pass to the method
381 * @param {Function} callback
382 * @param {Function} error_callback
383 * @returns {$.Deferred}
385 call: function (method, args, callback, error_callback) {
386 return this.rpc('/web/dataset/call', {
390 }, callback, error_callback);
393 * Calls an arbitrary method, with more crazy
395 * @param {String} method
396 * @param {Array} [args]
397 * @param {Number} [domain_index] index of a domain to evaluate in the args array
398 * @param {Number} [context_index] index of a context to evaluate in the args array
399 * @param {Function} callback
400 * @param {Function }error_callback
401 * @returns {$.Deferred}
403 call_and_eval: function (method, args, domain_index, context_index, callback, error_callback) {
404 return this.rpc('/web/dataset/call', {
407 domain_id: domain_index || null,
408 context_id: context_index || null,
410 }, callback, error_callback);
413 * Calls a button method, usually returning some sort of action
415 * @param {String} method
416 * @param {Array} [args]
417 * @param {Function} callback
418 * @param {Function} error_callback
419 * @returns {$.Deferred}
421 call_button: function (method, args, callback, error_callback) {
422 return this.rpc('/web/dataset/call_button', {
428 }, callback, error_callback);
431 * Fetches the "readable name" for records, based on intrinsic rules
434 * @param {Function} callback
435 * @returns {$.Deferred}
437 name_get: function(ids, callback) {
438 return this.call_and_eval('name_get', [ids, this.get_context()], null, 1, callback);
442 * @param {String} name name to perform a search for/on
443 * @param {Array} [domain=[]] filters for the objects returned, OpenERP domain
444 * @param {String} [operator='ilike'] matching operator to use with the provided name value
445 * @param {Number} [limit=100] maximum number of matches to return
446 * @param {Function} callback function to call with name_search result
447 * @returns {$.Deferred}
449 name_search: function (name, domain, operator, limit, callback) {
450 return this.call_and_eval('name_search',
451 [name || '', domain || false, operator || 'ilike', this.get_context(), limit || 100],
458 name_create: function(name, callback) {
459 return this.call_and_eval('name_create', [name, this.get_context()], null, 1, callback);
461 exec_workflow: function (id, signal, callback) {
462 return this.rpc('/web/dataset/exec_workflow', {
468 get_context: function(request_context) {
469 if (request_context) {
470 return new openerp.web.CompoundContext(this.context, request_context);
475 openerp.web.DataSetStatic = openerp.web.DataSet.extend({
476 init: function(parent, model, context, ids) {
477 this._super(parent, model, context);
479 this.ids = ids || [];
481 read_slice: function (fields, options, callback) {
482 // TODO remove fields from options
484 offset = options.offset || 0,
485 limit = options.limit || false,
486 fields = fields || false;
487 var end_pos = limit && limit !== -1 ? offset + limit : undefined;
488 return this.read_ids(this.ids.slice(offset, end_pos), fields, callback);
490 set_ids: function (ids) {
492 if (this.index !== null) {
493 this.index = this.index <= this.ids.length - 1 ?
494 this.index : (this.ids.length > 0 ? this.length - 1 : 0);
497 unlink: function(ids) {
499 return $.Deferred().resolve({result: true});
501 on_unlink: function(ids) {
502 this.set_ids(_.without.apply(null, [this.ids].concat(ids)));
505 openerp.web.DataSetSearch = openerp.web.DataSet.extend(/** @lends openerp.web.DataSetSearch */{
507 * @constructs openerp.web.DataSetSearch
508 * @extends openerp.web.DataSet
510 * @param {Object} parent
511 * @param {String} model
512 * @param {Object} context
513 * @param {Array} domain
515 init: function(parent, model, context, domain) {
516 this._super(parent, model, context);
517 this.domain = domain || [];
520 // subset records[offset:offset+limit]
525 * Read a slice of the records represented by this DataSet, based on its
526 * domain and context.
528 * @params {Object} options
529 * @param {Array} [options.fields] fields to read and return, by default all fields are returned
530 * @param {Object} [options.context] context data to add to the request payload, on top of the DataSet's own context
531 * @param {Array} [options.domain] domain data to add to the request payload, ANDed with the dataset's domain
532 * @param {Number} [options.offset=0] The index from which selected records should be returned
533 * @param {Number} [options.limit=null] The maximum number of records to return
534 * @param {Function} callback function called with read_slice result
535 * @returns {$.Deferred}
537 read_slice: function (fields, options, callback) {
539 var options = options || {};
540 var offset = options.offset || 0;
541 return this.rpc('/web/dataset/search_read', {
543 fields: fields || false,
544 domain: this.get_domain(options.domain),
545 context: this.get_context(options.context),
548 limit: options.limit || false
549 }).pipe(function (result) {
550 self.ids = result.ids;
551 self.offset = offset;
552 return result.records;
555 get_domain: function (other_domain) {
557 return new openerp.web.CompoundDomain(this.domain, other_domain);
562 * Reads or changes sort criteria on the dataset.
564 * If not provided with any argument, serializes the sort criteria to
565 * an SQL-like form usable by OpenERP's ORM.
567 * If given a field, will set that field as first sorting criteria or,
568 * if the field is already the first sorting criteria, will reverse it.
570 * @param {String} [field] field to sort on, reverses it (toggle from ASC to DESC) if already the main sort criteria
571 * @param {Boolean} [force_reverse=false] forces inserting the field as DESC
572 * @returns {String|undefined}
574 sort: function (field, force_reverse) {
576 return openerp.web.serialize_sort(this._sort);
578 var reverse = force_reverse || (this._sort[0] === field);
579 this._sort.splice.apply(
580 this._sort, [0, this._sort.length].concat(
581 _.without(this._sort, field, '-' + field)));
583 this._sort.unshift((reverse ? '-' : '') + field);
586 unlink: function(ids, callback, error_callback) {
588 return this._super(ids, function(result) {
589 self.ids = _.without.apply(_, [self.ids].concat(ids));
590 if (this.index !== null) {
591 self.index = self.index <= self.ids.length - 1 ?
592 self.index : (self.ids.length > 0 ? self.ids.length -1 : 0);
599 openerp.web.BufferedDataSet = openerp.web.DataSetStatic.extend({
600 virtual_id_prefix: "one2many_v_id_",
603 this._super.apply(this, arguments);
605 this.last_default_get = {};
607 default_get: function(fields, callback) {
608 return this._super(fields).then(this.on_default_get).then(callback);
610 on_default_get: function(res) {
611 this.last_default_get = res;
613 create: function(data, callback, error_callback) {
614 var cached = {id:_.uniqueId(this.virtual_id_prefix), values: data,
615 defaults: this.last_default_get};
616 this.to_create.push(_.extend(_.clone(cached), {values: _.clone(cached.values)}));
617 this.cache.push(cached);
619 var prom = $.Deferred().then(callback);
620 prom.resolve({result: cached.id});
621 return prom.promise();
623 write: function (id, data, options, callback) {
625 var record = _.detect(this.to_create, function(x) {return x.id === id;});
626 record = record || _.detect(this.to_write, function(x) {return x.id === id;});
629 for (var k in data) {
630 if (record.values[k] === undefined || record.values[k] !== data[k]) {
635 $.extend(record.values, data);
638 record = {id: id, values: data};
639 self.to_write.push(record);
641 var cached = _.detect(this.cache, function(x) {return x.id === id;});
642 $.extend(cached.values, record.values);
645 var to_return = $.Deferred().then(callback);
646 to_return.resolve({result: true});
647 return to_return.promise();
649 unlink: function(ids, callback, error_callback) {
651 _.each(ids, function(id) {
652 if (! _.detect(self.to_create, function(x) { return x.id === id; })) {
653 self.to_delete.push({id: id})
656 this.to_create = _.reject(this.to_create, function(x) { return _.include(ids, x.id);});
657 this.to_write = _.reject(this.to_write, function(x) { return _.include(ids, x.id);});
658 this.cache = _.reject(this.cache, function(x) { return _.include(ids, x.id);});
659 this.set_ids(_.without.apply(_, [this.ids].concat(ids)));
661 var to_return = $.Deferred().then(callback);
662 setTimeout(function () {to_return.resolve({result: true});}, 0);
663 return to_return.promise();
665 reset_ids: function(ids) {
671 this.delete_all = false;
673 on_change: function() {},
674 read_ids: function (ids, fields, callback) {
677 _.each(ids, function(id) {
678 var cached = _.detect(self.cache, function(x) {return x.id === id;});
679 var created = _.detect(self.to_create, function(x) {return x.id === id;});
681 _.each(fields, function(x) {if (cached.values[x] === undefined)
682 cached.values[x] = created.defaults[x] || false;});
684 if (!cached || !_.all(fields, function(x) {return cached.values[x] !== undefined}))
688 var completion = $.Deferred().then(callback);
689 var return_records = function() {
690 var records = _.map(ids, function(id) {
691 return _.extend({}, _.detect(self.cache, function(c) {return c.id === id;}).values, {"id": id});
693 if (self.debug_mode) {
694 if (_.include(records, undefined)) {
695 throw "Record not correctly loaded";
698 completion.resolve(records);
700 if(to_get.length > 0) {
701 var rpc_promise = this._super(to_get, fields, function(records) {
702 _.each(records, function(record, index) {
703 var id = to_get[index];
704 var cached = _.detect(self.cache, function(x) {return x.id === id;});
706 self.cache.push({id: id, values: record});
708 // I assume cache value is prioritary
709 _.defaults(cached.values, record);
714 $.when(rpc_promise).fail(function() {completion.reject();});
718 return completion.promise();
721 openerp.web.BufferedDataSet.virtual_id_regex = /^one2many_v_id_.*$/;
723 openerp.web.ProxyDataSet = openerp.web.DataSetSearch.extend({
725 this._super.apply(this, arguments);
726 this.create_function = null;
727 this.write_function = null;
728 this.read_function = null;
730 read_ids: function () {
731 if (this.read_function) {
732 return this.read_function.apply(null, arguments);
734 return this._super.apply(this, arguments);
737 default_get: function(fields, callback) {
738 return this._super(fields, callback).then(this.on_default_get);
740 on_default_get: function(result) {},
741 create: function(data, callback, error_callback) {
742 this.on_create(data);
743 if (this.create_function) {
744 return this.create_function(data, callback, error_callback);
746 console.warn("trying to create a record using default proxy dataset behavior");
747 var to_return = $.Deferred().then(callback);
748 setTimeout(function () {to_return.resolve({"result": undefined});}, 0);
749 return to_return.promise();
752 on_create: function(data) {},
753 write: function (id, data, options, callback) {
754 this.on_write(id, data);
755 if (this.write_function) {
756 return this.write_function(id, data, options, callback);
758 console.warn("trying to write a record using default proxy dataset behavior");
759 var to_return = $.Deferred().then(callback);
760 setTimeout(function () {to_return.resolve({"result": true});}, 0);
761 return to_return.promise();
764 on_write: function(id, data) {},
765 unlink: function(ids, callback, error_callback) {
767 console.warn("trying to unlink a record using default proxy dataset behavior");
768 var to_return = $.Deferred().then(callback);
769 setTimeout(function () {to_return.resolve({"result": true});}, 0);
770 return to_return.promise();
772 on_unlink: function(ids) {}
775 openerp.web.Model = openerp.web.SessionAware.extend({
776 init: function(session, model_name) {
777 this._super(session);
778 this.model_name = model_name;
780 get_func: function(method_name) {
783 if (method_name == "search_read")
784 return self._search_read.apply(self, arguments);
785 return self._call(method_name, _.toArray(arguments));
788 _call: function (method, args) {
789 return this.rpc('/web/dataset/call', {
790 model: this.model_name,
793 }).pipe(function(result) {
794 if (method == "read" && result instanceof Array && result.length > 0 && result[0]["id"]) {
796 _.each(_.range(result.length), function(i) {
797 index[result[i]["id"]] = result[i];
799 result = _.map(args[0], function(x) {return index[x];});
804 _search_read: function(domain, fields, offset, limit, order, context) {
805 return this.rpc('/web/dataset/search_read', {
806 model: this.model_name,
813 }).pipe(function(result) {
814 return result.records;
819 openerp.web.CompoundContext = openerp.web.Class.extend({
821 this.__ref = "compound_context";
822 this.__contexts = [];
823 this.__eval_context = null;
825 _.each(arguments, function(x) {
829 add: function (context) {
830 this.__contexts.push(context);
833 set_eval_context: function (eval_context) {
834 this.__eval_context = eval_context;
837 get_eval_context: function () {
838 return this.__eval_context;
842 openerp.web.CompoundDomain = openerp.web.Class.extend({
844 this.__ref = "compound_domain";
846 this.__eval_context = null;
848 _.each(arguments, function(x) {
852 add: function(domain) {
853 this.__domains.push(domain);
856 set_eval_context: function(eval_context) {
857 this.__eval_context = eval_context;
860 get_eval_context: function() {
861 return this.__eval_context;
866 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: