1 openerp.base.list = function (openerp) {
2 openerp.base.views.add('list', 'openerp.base.ListView');
3 openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListView# */ {
5 // records can be selected one by one
7 // list rows can be deleted
9 // whether the column headers should be displayed
11 // display addition button, with that label
13 // whether the list view can be sorted, note that once a view has been
14 // sorted it can not be reordered anymore
16 // whether the view rows can be reordered (via vertical drag & drop)
20 * Core class for list-type displays.
22 * As a view, needs a number of view-related parameters to be correctly
23 * instantiated, provides options and overridable methods for behavioral
26 * See constructor parameters and method documentations for information on
27 * the default behaviors and possible options for the list view.
31 * @param session An OpenERP session object
32 * @param element_id the id of the DOM elements this view should link itself to
33 * @param {openerp.base.DataSet} dataset the dataset the view should work with
34 * @param {String} view_id the listview's identifier, if any
35 * @param {Object} options A set of options used to configure the view
36 * @param {Boolean} [options.selectable=true] determines whether view rows are selectable (e.g. via a checkbox)
37 * @param {Boolean} [options.header=true] should the list's header be displayed
38 * @param {Boolean} [options.deletable=true] are the list rows deletable
39 * @param {null|String} [options.addable="New"] should the new-record button be displayed, and what should its label be. Use ``null`` to hide the button.
40 * @param {Boolean} [options.sortable=true] is it possible to sort the table by clicking on column headers
41 * @param {Boolean} [options.reorderable=true] is it possible to reorder list rows
43 * @borrows openerp.base.ActionExecutor#execute_action as #execute_action
45 init: function(view_manager, session, element_id, dataset, view_id, options) {
46 this._super(session, element_id);
47 this.view_manager = view_manager || new openerp.base.NullViewManager();
48 this.dataset = dataset;
49 this.model = dataset.model;
50 this.view_id = view_id;
54 this.options = _.extend({}, this.defaults, options || {});
55 this.flags = this.view_manager.flags || {};
57 this.set_groups(new openerp.base.ListView.Groups(this));
59 if (this.dataset instanceof openerp.base.DataSetStatic) {
60 this.groups.datagroup = new openerp.base.StaticDataGroup(this.dataset);
64 * Set a custom Group construct as the root of the List View.
66 * @param {openerp.base.ListView.Groups} groups
68 set_groups: function (groups) {
71 $(this.groups).unbind("selected deleted action row_link");
77 'selected': function (e, ids, records) {
78 self.do_select(ids, records);
80 'deleted': function (e, ids) {
83 'action': function (e, action_name, id, callback) {
84 self.do_action(action_name, id, callback);
86 'row_link': function (e, id, dataset) {
87 self.do_activate_record(dataset.index, id, dataset);
92 * View startup method, the default behavior is to set the ``oe-listview``
93 * class on its root element and to perform an RPC load call.
95 * @returns {$.Deferred} loading promise
98 this.$element.addClass('oe-listview');
99 return this.reload_view();
102 * Called after loading the list view's description, sets up such things
103 * as the view table's columns, renders the table itself and hooks up the
104 * various table-level and row-level DOM events (action buttons, deletion
105 * buttons, selection of records, [New] button, selection of a given
108 * Sets up the following:
110 * * Processes arch and fields to generate a complete field descriptor for each field
111 * * Create the table itself and allocate visible columns
112 * * Hook in the top-level (header) [New|Add] and [Delete] button
113 * * Sets up showing/hiding the top-level [Delete] button based on records being selected or not
114 * * Sets up event handlers for action buttons and per-row deletion button
115 * * Hooks global callback for clicking on a row
116 * * Sets up its sidebar, if any
118 * @param {Object} data wrapped fields_view_get result
119 * @param {Object} data.fields_view fields_view_get result (processed)
120 * @param {Object} data.fields_view.fields mapping of fields for the current model
121 * @param {Object} data.fields_view.arch current list view descriptor
122 * @param {Boolean} grouped Is the list view grouped
124 on_loaded: function(data, grouped) {
126 this.fields_view = data.fields_view;
127 //this.log(this.fields_view);
128 this.name = "" + this.fields_view.arch.attrs.string;
130 this.setup_columns(this.fields_view.fields, grouped);
132 if (!this.fields_view.sorted) { this.fields_view.sorted = {}; }
134 this.$element.html(QWeb.render("ListView", this));
137 this.$element.find('#oe-list-add').click(this.do_add_record);
138 this.$element.find('#oe-list-delete')
140 .click(this.do_delete_selected);
141 this.$element.find('thead').delegate('th[data-id]', 'click', function (e) {
144 self.dataset.sort($(this).data('id'));
146 // TODO: should only reload content (and set the right column to a sorted display state)
150 this.view_manager.sidebar.set_toolbar(data.fields_view.toolbar);
153 * Sets up the listview's columns: merges view and fields data, move
154 * grouped-by columns to the front of the columns list and make them all
157 * @param {Object} fields fields_view_get's fields section
158 * @param {Boolean} [grouped] Should the grouping columns (group and count) be displayed
160 setup_columns: function (fields, grouped) {
161 var domain_computer = openerp.base.form.compute_domain;
163 var noop = function () { return {}; };
164 var field_to_column = function (field) {
165 var name = field.attrs.name;
166 var column = _.extend({id: name, tag: field.tag},
167 field.attrs, fields[name]);
170 var attrs = JSON.parse(column.attrs);
171 column.attrs_for = function (fields) {
173 for (var attr in attrs) {
174 result[attr] = domain_computer(attrs[attr], fields);
179 column.attrs_for = noop;
184 this.columns.splice(0, this.columns.length);
185 this.columns.push.apply(
187 _(this.fields_view.arch.children).map(field_to_column));
189 this.columns.unshift({
190 id: '_group', tag: '', string: "Group", meta: true,
191 attrs_for: function () { return {}; }
193 id: '_count', tag: '', string: '#', meta: true,
194 attrs_for: function () { return {}; }
198 this.visible_columns = _.filter(this.columns, function (column) {
199 return column.invisible !== '1';
202 this.aggregate_columns = _(this.columns).chain()
203 .filter(function (column) {
204 return column['sum'] || column['avg'];})
205 .map(function (column) {
206 var func = column['sum'] ? 'sum' : 'avg';
216 * Used to handle a click on a table row, if no other handler caught the
219 * The default implementation asks the list view's view manager to switch
220 * to a different view (by calling
221 * :js:func:`~openerp.base.ViewManager.on_mode_switch`), using the
222 * provided record index (within the current list view's dataset).
224 * If the index is null, ``switch_to_record`` asks for the creation of a
227 * @param {Number|null} index the record index (in the current dataset) to switch to
228 * @param {String} [view="form"] the view type to switch to
230 select_record:function (index, view) {
231 view = view || 'form';
232 this.dataset.index = index;
233 _.delay(_.bind(function () {
234 if(this.view_manager) {
235 this.view_manager.on_mode_switch(view);
239 do_show: function () {
240 this.$element.show();
242 this.$element.find('table').append(
243 this.groups.apoptosis().render());
246 this.view_manager.sidebar.do_refresh(true);
248 do_hide: function () {
249 this.$element.hide();
253 * Reloads the list view based on the current settings (dataset & al)
255 * @param {Boolean} [grouped] Should the list be displayed grouped
257 reload_view: function (grouped) {
259 this.dataset.offset = 0;
260 this.dataset.limit = false;
261 var callback = function (field_view_get) {
262 self.on_loaded(field_view_get, grouped);
264 if (this.embedded_view) {
265 return $.Deferred().then(callback).resolve({fields_view: this.embedded_view});
267 return this.rpc('/base/listview/load', {
269 view_id: this.view_id,
270 context: this.dataset.context,
271 toolbar: !!this.flags.sidebar
276 * re-renders the content of the list view
278 reload_content: function () {
279 this.$element.find('table').append(
280 this.groups.apoptosis().render(
281 $.proxy(this, 'compute_aggregates')));
284 * Event handler for a search, asks for the computation/folding of domains
285 * and contexts (and group-by), then reloads the view's content.
287 * @param {Array} domains a sequence of literal and non-literal domains
288 * @param {Array} contexts a sequence of literal and non-literal contexts
289 * @param {Array} groupbys a sequence of literal and non-literal group-by contexts
290 * @returns {$.Deferred} fold request evaluation promise
292 do_search: function (domains, contexts, groupbys) {
293 return this.rpc('/base/session/eval_domain_and_context', {
296 group_by_seq: groupbys
297 }, $.proxy(this, 'do_actual_search'));
300 * Handler for the result of eval_domain_and_context, actually perform the
303 * @param {Object} results results of evaluating domain and process for a search
305 do_actual_search: function (results) {
306 this.dataset.context = results.context;
307 this.dataset.domain = results.domain;
308 this.groups.datagroup = new openerp.base.DataGroup(
309 this.session, this.model,
310 results.domain, results.context,
313 if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
314 results.group_by = null;
317 this.reload_view(!!results.group_by).then(
318 $.proxy(this, 'reload_content'));
321 * Handles the signal to delete a line from the DOM
323 * @param {Array} ids the id of the object to delete
325 do_delete: function (ids) {
330 return $.when(this.dataset.unlink(ids)).then(function () {
332 .map(function (row, index) {
335 id: row.data.id.value
337 .filter(function (record) {
338 return _.contains(ids, record.id);
340 .sort(function (a, b) {
341 // sort in reverse index order, so we delete from the end
342 // and don't blow up the following indexes (leading to
343 // removing the wrong records from the visible list)
344 return b.index - a.index;
346 .each(function (record) {
347 self.rows.splice(record.index, 1);
349 // TODO only refresh modified rows
353 * Handles the signal indicating that a new record has been selected
355 * @param {Array} ids selected record ids
356 * @param {Array} records selected record values
358 do_select: function (ids, records) {
359 this.$element.find('#oe-list-delete')
360 .toggle(!!ids.length);
362 if (!records.length) {
363 this.compute_aggregates();
366 this.compute_aggregates(records);
369 * Handles action button signals on a record
371 * @param {String} name action name
372 * @param {Object} id id of the record the action should be called on
373 * @param {Function} callback should be called after the action is executed, if non-null
375 do_action: function (name, id, callback) {
376 var action = _.detect(this.columns, function (field) {
377 return field.name === name;
379 if (!action) { return; }
381 action, this.dataset, this.session.action_manager,
389 * Handles the activation of a record (clicking on it)
391 * @param {Number} index index of the record in the dataset
392 * @param {Object} id identifier of the activated record
393 * @param {openobject.base.DataSet} dataset dataset in which the record is available (may not be the listview's dataset in case of nested groups)
395 do_activate_record: function (index, id, dataset) {
397 _.extend(this.dataset, {
398 domain: dataset.domain,
399 context: dataset.context
400 }).read_slice([], 0, false, function () {
401 self.select_record(index);
405 * Handles signal for the addition of a new record (can be a creation,
406 * can be the addition from a remote source, ...)
408 * The default implementation is to switch to a new record on the form view
410 do_add_record: function () {
411 this.select_record(null);
414 * Handles deletion of all selected lines
416 do_delete_selected: function () {
417 this.do_delete(this.groups.get_selection().ids);
420 * Computes the aggregates for the current list view, either on the
421 * records provided or on the records of the internal
422 * :js:class:`~openerp.base.ListView.Group`, by calling
423 * :js:func:`~openerp.base.ListView.group.get_records`.
425 * Then displays the aggregates in the table through
426 * :js:method:`~openerp.base.ListView.display_aggregates`.
428 * @param {Array} [records]
430 compute_aggregates: function (records) {
431 if (_.isEmpty(this.aggregate_columns)) {
434 if (_.isEmpty(records)) {
435 records = this.groups.get_records();
438 var aggregator = this.build_aggregator(this.aggregate_columns);
439 this.display_aggregates(
440 _(records).reduce(aggregator, aggregator).value());
443 * Creates a stateful callable aggregator object, which can be reduced over
444 * a collection of records in order to build the aggregations described
447 * @param {Array} aggregation_descriptors
449 build_aggregator: function (aggregation_descriptors) {
451 var descriptors = {};
452 _(aggregation_descriptors).each(function (descriptor) {
453 values[descriptor.field] = [];
454 descriptors[descriptor.field] = descriptor;
457 var aggregator = function (_i, record) {
458 _(values).each(function (collection, key) {
459 collection.push(record[key]);
464 aggregator.value = function () {
467 _(values).each(function (collection, key) {
469 switch(descriptors[key]['function']) {
471 value = (_(collection).chain()
472 .filter(function (item) {
473 return !_.isUndefined(item); })
474 .reduce(function (total, item) {
475 return total + item; }, 0).value()
476 / collection.length);
479 value = (_(collection).chain()
480 .filter(function (item) {
481 return !_.isUndefined(item); })
482 .reduce(function (total, item) {
483 return total + item; }, 0).value());
493 display_aggregates: function (aggregation) {
494 var $footer = this.$element.find('.oe-list-footer').empty();
495 _(this.aggregate_columns).each(function (column) {
497 "<span>%s: %.2f</span>",
498 column.label, aggregation[column.field]))
502 // TODO: implement reorder (drag and drop rows)
504 openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List# */{
506 * List display for the ListView, handles basic DOM events and transforms
507 * them in the relevant higher-level events, to which the list view (or
508 * other consumers) can subscribe.
510 * Events on this object are registered via jQuery.
515 * Triggered when a row is selected (using check boxes), provides an
516 * array of ids of all the selected records.
518 * Triggered when deletion buttons are hit, provide an array of ids of
519 * all the records being marked for suppression.
521 * Triggered when an action button is clicked, provides two parameters:
523 * * The name of the action to execute (as a string)
524 * * The id of the record to execute the action on
526 * Triggered when a row of the table is clicked, provides the index (in
527 * the rows array) and id of the selected record to the handle function.
530 * @param {Object} opts display options, identical to those of :js:class:`openerp.base.ListView`
532 init: function (group, opts) {
536 this.options = opts.options;
537 this.columns = opts.columns;
538 this.dataset = opts.dataset;
539 this.rows = opts.rows;
541 this.$_element = $('<tbody class="ui-widget-content">')
542 .appendTo(document.body)
543 .delegate('th.oe-record-selector', 'click', function (e) {
545 var selection = self.get_selection();
547 'selected', [selection.ids, selection.records]);
549 .delegate('td.oe-record-delete button', 'click', function (e) {
551 var $row = $(e.target).closest('tr');
552 $(self).trigger('deleted', [[self.row_id($row)]]);
554 .delegate('td.oe-field-cell button', 'click', function (e) {
556 var $target = $(e.currentTarget),
557 field = $target.closest('td').data('field'),
558 $row = $target.closest('tr'),
559 record_id = self.row_id($row),
560 index = self.row_position($row);
562 $(self).trigger('action', [field, record_id, function () {
563 self.reload_record(index, true);
566 .delegate('tr', 'click', function (e) {
568 self.dataset.index = self.row_position(e.currentTarget);
572 row_clicked: function () {
575 [this.rows[this.dataset.index].data.id.value,
578 render: function () {
580 this.$current.remove();
582 this.$current = this.$_element.clone(true);
583 this.$current.empty().append($(QWeb.render('ListView.rows', this)));
585 get_fields_view: function () {
587 var view = $.extend(true, {}, this.group.view.fields_view);
588 _(view.arch.children).each(function (widget) {
589 widget.attrs.nolabel = true;
590 if (widget.tag === 'button') {
591 delete widget.attrs.string;
594 view.arch.attrs.col = 2 * view.arch.children.length;
598 * Gets the ids of all currently selected records, if any
599 * @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
601 get_selection: function () {
602 if (!this.options.selectable) {
605 var rows = this.rows;
606 var result = {ids: [], records: []};
607 this.$current.find('th.oe-record-selector input:checked')
608 .closest('tr').each(function () {
610 _(rows[$(this).data('index')].data).each(function (obj, key) {
611 record[key] = obj.value;
613 result.ids.push(record.id);
614 result.records.push(record);
619 * Returns the index of the row in the list of rows.
621 * @param {Object} row the selected row
622 * @returns {Number} the position of the row in this.rows
624 row_position: function (row) {
625 return $(row).data('index');
628 * Returns the identifier of the object displayed in the provided table
631 * @param {Object} row the selected table row
632 * @returns {Number|String} the identifier of the row's object
634 row_id: function (row) {
635 return this.rows[this.row_position(row)].data.id.value;
638 * Death signal, cleans up list
640 apoptosis: function () {
641 if (!this.$current) { return; }
642 this.$current.remove();
643 this.$current = null;
644 this.$_element.remove();
646 get_records: function () {
647 return _(this.rows).map(function (row) {
649 _(row.data).each(function (obj, key) {
650 record[key] = obj.value;
656 * Transforms a record from what is returned by a dataset read (a simple
657 * mapping of ``$fieldname: $value``) to the format expected by list rows
666 * This format allows for the insertion of a bunch of metadata (names,
669 * @param {Object} record original record, in dataset format
670 * @returns {Object} record displayable in a form or list view
672 transform_record: function (record) {
673 // TODO: colors handling
675 form_record = {data: form_data};
677 _(record).each(function (value, key) {
678 form_data[key] = {value: value};
684 * Reloads the record at index ``row_index`` in the list's rows.
686 * By default, simply re-renders the record. If the ``fetch`` parameter is
687 * provided and ``true``, will first fetch the record anew.
689 * @param {Number} record_index index of the record to reload
690 * @param {Boolean} fetch fetches the record from remote before reloading it
692 reload_record: function (record_index, fetch) {
696 // save index to restore it later, if already set
697 var old_index = this.dataset.index;
698 this.dataset.index = record_index;
699 read_p = this.dataset.read_index(
700 _.filter(_.pluck(this.columns, 'name'), _.identity),
702 var form_record = self.transform_record(record);
703 self.rows.splice(record_index, 1, form_record);
704 self.dataset.index = old_index;
709 return $.when(read_p).then(function () {
710 self.$current.children().eq(record_index)
711 .replaceWith(self.render_record(record_index)); })
714 * Renders a list record to HTML
716 * @param {Number} record_index index of the record to render in ``this.rows``
717 * @returns {String} QWeb rendering of the selected record
719 render_record: function (record_index) {
720 return QWeb.render('ListView.row', {
721 columns: this.columns,
722 options: this.options,
723 row: this.rows[record_index],
724 row_parity: (record_index % 2 === 0) ? 'even' : 'odd',
725 row_index: record_index
730 openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Groups# */{
732 * Grouped display for the ListView. Handles basic DOM events and interacts
733 * with the :js:class:`~openerp.base.DataGroup` bound to it.
735 * Provides events similar to those of
736 * :js:class:`~openerp.base.ListView.List`
738 init: function (view) {
740 this.options = view.options;
741 this.columns = view.columns;
742 this.datagroup = null;
747 pad: function ($row) {
748 if (this.options.selectable) {
752 make_fragment: function () {
753 return document.createDocumentFragment();
756 * Returns a DOM node after which a new tbody can be inserted, so that it
757 * follows the provided row.
759 * Necessary to insert the result of a new group or list view within an
760 * existing groups render, without losing track of the groups's own
763 * @param {HTMLTableRowElement} row the row after which the caller wants to insert a body
764 * @returns {HTMLTableSectionElement} element after which a tbody can be inserted
766 point_insertion: function (row) {
768 var red_letter_tboday = $row.closest('tbody')[0];
770 var $next_siblings = $row.nextAll();
771 if ($next_siblings.length) {
772 var $root_kanal = $('<tbody>').insertAfter(red_letter_tboday);
774 $root_kanal.append($next_siblings);
775 this.elements.splice(
776 _.indexOf(this.elements, red_letter_tboday),
780 return red_letter_tboday;
782 open_group: function (e, group) {
783 var row = e.currentTarget;
785 if (this.children[group.value]) {
786 this.children[group.value].apoptosis();
787 delete this.children[group.value];
789 var prospekt = this.children[group.value] = new openerp.base.ListView.Groups(this.view, {
790 options: this.options,
791 columns: this.columns
793 this.bind_child_events(prospekt);
794 prospekt.datagroup = group;
795 prospekt.render().insertAfter(
796 this.point_insertion(row));
797 $(row).find('span.ui-icon')
798 .removeClass('ui-icon-triangle-1-e')
799 .addClass('ui-icon-triangle-1-s');
802 * Prefixes ``$node`` with floated spaces in order to indent it relative
803 * to its own left margin/baseline
805 * @param {jQuery} $node jQuery object to indent
806 * @param {Number} level current nesting level, >= 1
807 * @returns {jQuery} the indentation node created
809 indent: function ($node, level) {
811 .css({'float': 'left', 'white-space': 'pre'})
812 .text(new Array(level).join(' '))
815 render_groups: function (datagroups) {
817 var placeholder = this.make_fragment();
818 _(datagroups).each(function (group) {
819 var $row = $('<tr>');
820 if (group.openable) {
821 $row.click(function (e) {
822 if (!$row.data('open')) {
823 $row.data('open', true);
824 self.open_group(e, group);
826 $row.removeData('open')
827 .find('span.ui-icon')
828 .removeClass('ui-icon-triangle-1-s')
829 .addClass('ui-icon-triangle-1-e');
830 _(self.children).each(function (child) {child.apoptosis();});
834 placeholder.appendChild($row[0]);
836 var $group_column = $('<th>').appendTo($row);
837 if (group.grouped_on) {
838 // Don't fill this if group_by_no_leaf but no group_by
840 .text((group.value instanceof Array ? group.value[1] : group.value));
841 if (group.openable) {
842 // Make openable if not terminal group & group_by_no_leaf
844 .prepend('<span class="ui-icon ui-icon-triangle-1-e" style="float: left;">');
847 self.indent($group_column, group.level);
849 $('<td>').text(group.length).appendTo($row);
852 _(self.columns).chain()
853 .filter(function (column) {return !column.invisible;})
854 .each(function (column) {
856 // do not do anything
857 } else if (column.id in group.aggregates) {
858 var value = group.aggregates[column.id];
860 if (column.type === 'integer') {
862 } else if (column.type === 'float') {
866 .text(_.sprintf(format, value))
875 bind_child_events: function (child) {
878 $(child).bind('selected', function (e) {
879 // can have selections spanning multiple links
880 var selection = self.get_selection();
881 $this.trigger(e, [selection.ids, selection.records]);
882 }).bind('action deleted row_link', function (e) {
883 // additional positional parameters are provided to trigger as an
884 // Array, following the event type or event object, but are
885 // provided to the .bind event handler as *args.
886 // Convert our *args back into an Array in order to trigger them
887 // on the group itself, so it can ultimately be forwarded wherever
888 // it's supposed to go.
889 var args = Array.prototype.slice.call(arguments, 1);
890 $this.trigger.call($this, e, args);
893 render_dataset: function (dataset) {
895 list = new openerp.base.ListView.List(this, {
896 options: this.options,
897 columns: this.columns,
901 this.bind_child_events(list);
903 var d = new $.Deferred();
905 _.filter(_.pluck(this.columns, 'name'), _.identity),
908 var form_records = _(records).map(
909 $.proxy(list, 'transform_record'));
911 rows.splice(0, rows.length);
912 rows.push.apply(rows, form_records);
918 render: function (post_render) {
920 var $element = $('<tbody>');
921 this.elements = [$element[0]];
922 this.datagroup.list(function (groups) {
923 $element[0].appendChild(
924 self.render_groups(groups));
925 if (post_render) { post_render(); }
926 }, function (dataset) {
927 self.render_dataset(dataset).then(function (list) {
928 self.children[null] = list;
930 [list.$current.replaceAll($element)[0]];
931 if (post_render) { post_render(); }
937 * Returns the ids of all selected records for this group, and the records
940 get_selection: function () {
941 var ids = [], records = [];
944 .each(function (child) {
945 var selection = child.get_selection();
946 ids.push.apply(ids, selection.ids);
947 records.push.apply(records, selection.records);
950 return {ids: ids, records: records};
952 apoptosis: function () {
953 _(this.children).each(function (child) {
957 $(this.elements).remove();
960 get_records: function () {
961 return _(this.children).chain()
962 .map(function (child) {
963 return child.get_records();
964 }).flatten().value();
969 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: