summaryrefslogtreecommitdiff
path: root/addons/base/static/src/js/list.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/base/static/src/js/list.js')
-rw-r--r--addons/base/static/src/js/list.js363
1 files changed, 267 insertions, 96 deletions
diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js
index 77a66731f34..3a482164bb6 100644
--- a/addons/base/static/src/js/list.js
+++ b/addons/base/static/src/js/list.js
@@ -44,7 +44,6 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
* @borrows openerp.base.ActionExecutor#execute_action as #execute_action
*/
init: function(view_manager, session, element_id, dataset, view_id, options) {
- var self = this;
this._super(session, element_id);
this.view_manager = view_manager || new openerp.base.NullViewManager();
this.dataset = dataset;
@@ -56,41 +55,35 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
this.options = _.extend({}, this.defaults, options || {});
this.flags = this.view_manager.action.flags;
- this.groups = new openerp.base.ListView.Groups(this, {
- options: this.options,
- columns: this.columns
- });
+ this.set_groups(new openerp.base.ListView.Groups(this));
+ },
+ /**
+ * Set a custom Group construct as the root of the List View.
+ *
+ * @param {openerp.base.ListView.Groups} groups
+ */
+ set_groups: function (groups) {
+ var self = this;
+ if (this.groups) {
+ $(this.groups).unbind("selected deleted action row_link");
+ delete this.groups;
+ }
+
+ this.groups = groups;
$(this.groups).bind({
- 'selected': function (e, selection) {
- self.$element.find('#oe-list-delete')
- .toggle(!!selection.length);
+ 'selected': function (e, ids, records) {
+ self.do_select(ids, records);
},
'deleted': function (e, ids) {
self.do_delete(ids);
},
'action': function (e, action_name, id, callback) {
- var action = _.detect(self.columns, function (field) {
- return field.name === action_name;
- });
- if (!action) { return; }
- self.execute_action(
- action, self.dataset, self.session.action_manager,
- id, function () {
- if (callback) {
- callback();
- }
- });
+ self.do_action(action_name, id, callback);
},
'row_link': function (e, index, id, dataset) {
- _.extend(self.dataset, {
- domain: dataset.domain,
- context: dataset.context
- }).read_slice([], null, null, function () {
- self.select_record(index);
- });
+ self.do_activate_record(index, id, dataset);
}
});
-
},
/**
* View startup method, the default behavior is to set the ``oe-listview``
@@ -100,11 +93,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
*/
start: function() {
this.$element.addClass('oe-listview');
- return this.rpc("/base/listview/load", {
- model: this.model,
- view_id: this.view_id,
- toolbar: !!this.flags.sidebar
- }, this.on_loaded);
+ return this.reload_view();
},
/**
* Called after loading the list view's description, sets up such things
@@ -127,15 +116,15 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
* @param {Object} data.fields_view fields_view_get result (processed)
* @param {Object} data.fields_view.fields mapping of fields for the current model
* @param {Object} data.fields_view.arch current list view descriptor
- * @param {Array} columns columns to move to the front (and make visible)
+ * @param {Boolean} grouped Is the list view grouped
*/
- on_loaded: function(data, columns) {
+ on_loaded: function(data, grouped) {
var self = this;
this.fields_view = data.fields_view;
//this.log(this.fields_view);
this.name = "" + this.fields_view.arch.attrs.string;
- this.setup_columns(this.fields_view.fields, columns);
+ this.setup_columns(this.fields_view.fields, grouped);
if (!this.fields_view.sorted) { this.fields_view.sorted = {}; }
@@ -152,7 +141,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
self.dataset.sort($(this).data('id'));
// TODO: should only reload content (and set the right column to a sorted display state)
- self.do_reload();
+ self.reload_view();
});
this.view_manager.sidebar.set_toolbar(data.fields_view.toolbar);
@@ -163,9 +152,9 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
* visible.
*
* @param {Object} fields fields_view_get's fields section
- * @param {Array} groupby_columns columns the ListView is grouped by
+ * @param {Boolean} [grouped] Should the grouping columns (group and count) be displayed
*/
- setup_columns: function (fields, groupby_columns) {
+ setup_columns: function (fields, grouped) {
var domain_computer = openerp.base.form.compute_domain;
var noop = function () { return {}; };
@@ -193,7 +182,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
this.columns.push.apply(
this.columns,
_(this.fields_view.arch.children).map(field_to_column));
- if (groupby_columns) {
+ if (grouped) {
this.columns.unshift({
id: '_group', tag: '', string: "Group", meta: true,
attrs_for: function () { return {}; }
@@ -206,6 +195,19 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
this.visible_columns = _.filter(this.columns, function (column) {
return column.invisible !== '1';
});
+
+ this.aggregate_columns = _(this.columns).chain()
+ .filter(function (column) {
+ return column['sum'] || column['avg'];})
+ .map(function (column) {
+ var func = column['sum'] ? 'sum' : 'avg';
+ return {
+ field: column.id,
+ type: column.type,
+ 'function': func,
+ label: column[func]
+ };
+ }).value();
},
/**
* Used to handle a click on a table row, if no other handler caught the
@@ -245,26 +247,20 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
this.hidden = true;
},
/**
- * Reloads the search view based on the current settings (dataset & al)
+ * Reloads the list view based on the current settings (dataset & al)
*
- * @param {Array} [primary_columns] columns to bring to the front of the
- * sequence
+ * @param {Boolean} [grouped] Should the list be displayed grouped
*/
- do_reload: function (primary_columns) {
- // TODO: should just fields_view_get I think
+ reload_view: function (grouped) {
var self = this;
this.dataset.offset = 0;
this.dataset.limit = false;
- return this.rpc('/base/listview/fill', {
- 'model': this.dataset.model,
- 'id': this.view_id,
- 'context': this.dataset.context,
- 'domain': this.dataset.domain,
- 'sort': this.dataset.sort && this.dataset.sort()
- }, function (result) {
- if (result.view) {
- self.on_loaded({fields_view: result.view}, primary_columns);
- }
+ return this.rpc('/base/listview/load', {
+ model: this.model,
+ view_id: this.view_id,
+ toolbar: !!this.flags.sidebar
+ }, function (field_view_get) {
+ self.on_loaded(field_view_get, grouped);
});
},
/**
@@ -286,16 +282,17 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
self.dataset.context = results.context;
self.dataset.domain = results.domain;
self.groups.datagroup = new openerp.base.DataGroup(
- self.session, self.dataset.model,
+ self.session, self.model,
results.domain, results.context,
results.group_by);
if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
results.group_by = null;
}
- self.do_reload(results.group_by).then(function () {
- self.$element.find('table').append(self.groups.render());
- });
+ self.reload_view(!!results.group_by).then(function () {
+ self.$element.find('table').append(
+ self.groups.render(function () {
+ self.compute_aggregates();}));});
});
},
/**
@@ -331,20 +328,154 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
});
},
/**
+ * Handles the signal indicating that a new record has been selected
+ *
+ * @param {Array} ids selected record ids
+ * @param {Array} records selected record values
+ */
+ do_select: function (ids, records) {
+ this.$element.find('#oe-list-delete')
+ .toggle(!!ids.length);
+
+ if (!records.length) {
+ this.compute_aggregates();
+ return;
+ }
+ this.compute_aggregates(records);
+ },
+ /**
+ * Handles action button signals on a record
+ *
+ * @param {String} name action name
+ * @param {Object} id id of the record the action should be called on
+ * @param {Function} callback should be called after the action is executed, if non-null
+ */
+ do_action: function (name, id, callback) {
+ var action = _.detect(this.columns, function (field) {
+ return field.name === name;
+ });
+ if (!action) { return; }
+ this.execute_action(
+ action, this.dataset, this.session.action_manager,
+ id, function () {
+ if (callback) {
+ callback();
+ }
+ });
+ },
+ /**
+ * Handles the activation of a record (clicking on it)
+ *
+ * @param {Number} index index of the record in the dataset
+ * @param {Object} id identifier of the activated record
+ * @param {openobject.base.DataSet} dataset dataset in which the record is available (may not be the listview's dataset in case of nested groups)
+ */
+ do_activate_record: function (index, id, dataset) {
+ var self = this;
+ _.extend(this.dataset, {
+ domain: dataset.domain,
+ context: dataset.context
+ }).read_slice([], 0, false, function () {
+ self.select_record(index);
+ });
+ },
+ /**
* Handles signal for the addition of a new record (can be a creation,
* can be the addition from a remote source, ...)
*
* The default implementation is to switch to a new record on the form view
*/
do_add_record: function () {
- this.notification.notify('Add', "New record");
this.select_record(null);
},
/**
* Handles deletion of all selected lines
*/
do_delete_selected: function () {
- this.do_delete(this.groups.get_selection());
+ this.do_delete(this.groups.get_selection().ids);
+ },
+ /**
+ * Computes the aggregates for the current list view, either on the
+ * records provided or on the records of the internal
+ * :js:class:`~openerp.base.ListView.Group`, by calling
+ * :js:func:`~openerp.base.ListView.group.get_records`.
+ *
+ * Then displays the aggregates in the table through
+ * :js:method:`~openerp.base.ListView.display_aggregates`.
+ *
+ * @param {Array} [records]
+ */
+ compute_aggregates: function (records) {
+ if (_.isEmpty(this.aggregate_columns)) {
+ return;
+ }
+ if (_.isEmpty(records)) {
+ records = this.groups.get_records();
+ }
+
+ var aggregator = this.build_aggregator(this.aggregate_columns);
+ this.display_aggregates(
+ _(records).reduce(aggregator, aggregator).value());
+ },
+ /**
+ * Creates a stateful callable aggregator object, which can be reduced over
+ * a collection of records in order to build the aggregations described
+ * by the parameter
+ *
+ * @param {Array} aggregation_descriptors
+ */
+ build_aggregator: function (aggregation_descriptors) {
+ var values = {};
+ var descriptors = {};
+ _(aggregation_descriptors).each(function (descriptor) {
+ values[descriptor.field] = [];
+ descriptors[descriptor.field] = descriptor;
+ });
+
+ var aggregator = function (_i, record) {
+ _(values).each(function (collection, key) {
+ collection.push(record[key]);
+ });
+
+ return aggregator;
+ };
+ aggregator.value = function () {
+ var result = {};
+
+ _(values).each(function (collection, key) {
+ var value;
+ switch(descriptors[key]['function']) {
+ case 'avg':
+ value = (_(collection).chain()
+ .filter(function (item) {
+ return !_.isUndefined(item); })
+ .reduce(function (total, item) {
+ return total + item; }, 0).value()
+ / collection.length);
+ break;
+ case 'sum':
+ value = (_(collection).chain()
+ .filter(function (item) {
+ return !_.isUndefined(item); })
+ .reduce(function (total, item) {
+ return total + item; }, 0).value());
+ break;
+ }
+ result[key] = value;
+ });
+
+ return result;
+ };
+ return aggregator;
+ },
+ display_aggregates: function (aggregation) {
+ var $footer = this.$element.find('.oe-list-footer').empty();
+ _(this.aggregate_columns).each(function (column) {
+ $(_.sprintf(
+ "<span>%s: %.2f</span>",
+ column.label, aggregation[column.field]))
+ .appendTo($footer);
+ });
}
// TODO: implement reorder (drag and drop rows)
});
@@ -378,7 +509,6 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
*/
init: function (opts) {
var self = this;
- // columns, rows, options
this.options = opts.options;
this.columns = opts.columns;
@@ -389,7 +519,9 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
.appendTo(document.body)
.delegate('th.oe-record-selector', 'click', function (e) {
e.stopPropagation();
- $(self).trigger('selected', [self.get_selection()]);
+ var selection = self.get_selection();
+ $(self).trigger(
+ 'selected', [selection.ids, selection.records]);
})
.delegate('td.oe-record-delete button', 'click', function (e) {
e.stopPropagation();
@@ -422,17 +554,24 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
},
/**
* Gets the ids of all currently selected records, if any
- * @returns {Array} empty if no record is selected (or the list view is not selectable)
+ * @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
*/
get_selection: function () {
if (!this.options.selectable) {
return [];
}
var rows = this.rows;
- return this.$current.find('th.oe-record-selector input:checked')
- .closest('tr').map(function () {
- return rows[$(this).prevAll().length].data.id.value;
- }).get();
+ var result = {ids: [], records: []};
+ this.$current.find('th.oe-record-selector input:checked')
+ .closest('tr').each(function () {
+ var record = {};
+ _(rows[$(this).prevAll().length].data).each(function (obj, key) {
+ record[key] = obj.value;
+ });
+ result.ids.push(record.id);
+ result.records.push(record);
+ });
+ return result;
},
/**
* Returns the index of the row in the list of rows.
@@ -460,6 +599,16 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
if (!this.$current) { return; }
this.$current.remove();
this.$current = null;
+ this.$_element.remove();
+ },
+ get_records: function () {
+ return _(this.rows).map(function (row) {
+ var record = {};
+ _(row.data).each(function (obj, key) {
+ record[key] = obj.value;
+ });
+ return record;
+ });
}
// drag and drop
// editable?
@@ -472,11 +621,11 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
* Provides events similar to those of
* :js:class:`~openerp.base.ListView.List`
*/
- init: function (view, opts) {
+ init: function (view) {
this.view = view;
- this.options = opts.options;
- this.columns = opts.columns;
- this.datagroup = {};
+ this.options = view.options;
+ this.columns = view.columns;
+ this.datagroup = null;
this.sections = [];
this.children = {};
@@ -502,19 +651,19 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
*/
point_insertion: function (row) {
var $row = $(row);
- var red_letter_tbody = $row.closest('tbody')[0];
+ var red_letter_tboday = $row.closest('tbody')[0];
var $next_siblings = $row.nextAll();
if ($next_siblings.length) {
- var $root_kanal = $('<tbody>').insertAfter(red_letter_tbody);
+ var $root_kanal = $('<tbody>').insertAfter(red_letter_tboday);
$root_kanal.append($next_siblings);
this.elements.splice(
- _.indexOf(this.elements, red_letter_tbody),
+ _.indexOf(this.elements, red_letter_tboday),
0,
$root_kanal[0]);
}
- return red_letter_tbody;
+ return red_letter_tboday;
},
open_group: function (e, group) {
var row = e.currentTarget;
@@ -570,7 +719,6 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
}
placeholder.appendChild($row[0]);
-
var $group_column = $('<th>').appendTo($row);
if (group.grouped_on) {
// Don't fill this if group_by_no_leaf but no group_by
@@ -615,7 +763,8 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
self = this;
$(child).bind('selected', function (e) {
// can have selections spanning multiple links
- $this.trigger(e, [self.get_selection()]);
+ var selection = self.get_selection();
+ $this.trigger(e, [selection.ids, selection.records]);
}).bind('action', function (e, name, id, callback) {
if (!callback) {
callback = function () {
@@ -652,46 +801,62 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
this.bind_child_events(list);
var d = new $.Deferred();
- this.view.rpc('/base/listview/fill', {
- model: dataset.model,
- id: this.view.view_id,
- context: dataset.context,
- domain: dataset.domain,
- sort: dataset.sort && dataset.sort()
- }, function (result) {
- rows.splice(0, rows.length);
- rows.push.apply(rows, result.records);
- list.render();
- d.resolve(list);
- });
+ dataset.read_slice(
+ _.filter(_.pluck(this.columns, 'name'), _.identity),
+ 0, false,
+ function (records) {
+ var form_records = _(records).map(function (record) {
+ // TODO: colors handling
+ var form_data = {},
+ form_record = {data: form_data};
+
+ _(record).each(function (value, key) {
+ form_data[key] = {value: value};
+ });
+
+ return form_record;
+ });
+
+ rows.splice(0, rows.length);
+ rows.push.apply(rows, form_records);
+ list.render();
+ d.resolve(list);
+ });
return d.promise();
},
- render: function () {
+ render: function (post_render) {
var self = this;
var $element = $('<tbody>');
this.elements = [$element[0]];
this.datagroup.list(function (groups) {
$element[0].appendChild(
self.render_groups(groups));
+ if (post_render) { post_render(); }
}, function (dataset) {
self.render_dataset(dataset).then(function (list) {
self.children[null] = list;
self.elements =
[list.$current.replaceAll($element)[0]];
+ if (post_render) { post_render(); }
});
});
return $element;
},
/**
- * Returns the ids of all selected records for this group
+ * Returns the ids of all selected records for this group, and the records
+ * themselves
*/
get_selection: function () {
- return _(this.children).chain()
- .map(function (child) {
- return child.get_selection();
- })
- .flatten()
- .value();
+ var ids = [], records = [];
+
+ _(this.children)
+ .each(function (child) {
+ var selection = child.get_selection();
+ ids.push.apply(ids, selection.ids);
+ records.push.apply(records, selection.records);
+ });
+
+ return {ids: ids, records: records};
},
apoptosis: function () {
_(this.children).each(function (child) {
@@ -699,6 +864,12 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
});
$(this.elements).remove();
return this;
+ },
+ get_records: function () {
+ return _(this.children).chain()
+ .map(function (child) {
+ return child.get_records();
+ }).flatten().value();
}
});
};