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.
30 * @param parent parent object
31 * @param element_id the id of the DOM elements this view should link itself to
32 * @param {openerp.base.DataSet} dataset the dataset the view should work with
33 * @param {String} view_id the listview's identifier, if any
34 * @param {Object} options A set of options used to configure the view
35 * @param {Boolean} [options.selectable=true] determines whether view rows are selectable (e.g. via a checkbox)
36 * @param {Boolean} [options.header=true] should the list's header be displayed
37 * @param {Boolean} [options.deletable=true] are the list rows deletable
38 * @param {void|String} [options.addable="New"] should the new-record button be displayed, and what should its label be. Use ``null`` to hide the button.
39 * @param {Boolean} [options.sortable=true] is it possible to sort the table by clicking on column headers
40 * @param {Boolean} [options.reorderable=true] is it possible to reorder list rows
42 * @borrows openerp.base.ActionExecutor#execute_action as #execute_action
44 init: function(parent, element_id, dataset, view_id, options) {
46 this._super(parent, element_id);
47 this.set_default_options(_.extend({}, this.defaults, options || {}));
48 this.dataset = dataset;
49 this.model = dataset.model;
50 this.view_id = view_id;
54 this.records = new Collection();
56 this.set_groups(new openerp.base.ListView.Groups(this));
58 if (this.dataset instanceof openerp.base.DataSetStatic) {
59 this.groups.datagroup = new openerp.base.StaticDataGroup(this.dataset);
63 this.records.bind('change', function (event, record, key) {
64 if (!_(self.aggregate_columns).chain()
65 .pluck('name').contains(key).value()) {
68 self.compute_aggregates();
72 * Retrieves the view's number of records per page (|| section)
74 * options > defaults > parent.action.limit > indefinite
76 * @returns {Number|null}
79 if (this._limit === undefined) {
80 this._limit = (this.options.limit
81 || this.defaults.limit
82 || (this.widget_parent.action || {}).limit
88 * Set a custom Group construct as the root of the List View.
90 * @param {openerp.base.ListView.Groups} groups
92 set_groups: function (groups) {
95 $(this.groups).unbind("selected deleted action row_link");
100 $(this.groups).bind({
101 'selected': function (e, ids, records) {
102 self.do_select(ids, records);
104 'deleted': function (e, ids) {
107 'action': function (e, action_name, id, callback) {
108 self.do_button_action(action_name, id, callback);
110 'row_link': function (e, id, dataset) {
111 self.do_activate_record(dataset.index, id, dataset);
116 * View startup method, the default behavior is to set the ``oe-listview``
117 * class on its root element and to perform an RPC load call.
119 * @returns {$.Deferred} loading promise
122 this.$element.addClass('oe-listview');
123 return this.reload_view(null, null, true);
126 * Called after loading the list view's description, sets up such things
127 * as the view table's columns, renders the table itself and hooks up the
128 * various table-level and row-level DOM events (action buttons, deletion
129 * buttons, selection of records, [New] button, selection of a given
132 * Sets up the following:
134 * * Processes arch and fields to generate a complete field descriptor for each field
135 * * Create the table itself and allocate visible columns
136 * * Hook in the top-level (header) [New|Add] and [Delete] button
137 * * Sets up showing/hiding the top-level [Delete] button based on records being selected or not
138 * * Sets up event handlers for action buttons and per-row deletion button
139 * * Hooks global callback for clicking on a row
140 * * Sets up its sidebar, if any
142 * @param {Object} data wrapped fields_view_get result
143 * @param {Object} data.fields_view fields_view_get result (processed)
144 * @param {Object} data.fields_view.fields mapping of fields for the current model
145 * @param {Object} data.fields_view.arch current list view descriptor
146 * @param {Boolean} grouped Is the list view grouped
148 on_loaded: function(data, grouped) {
150 this.fields_view = data.fields_view;
151 //this.log(this.fields_view);
152 this.name = "" + this.fields_view.arch.attrs.string;
154 this.setup_columns(this.fields_view.fields, grouped);
156 this.$element.html(QWeb.render("ListView", this));
159 this.$element.find('.oe-list-add')
160 .click(this.do_add_record)
161 .attr('disabled', grouped && this.options.editable);
162 this.$element.find('.oe-list-delete')
163 .attr('disabled', true)
164 .click(this.do_delete_selected);
165 this.$element.find('thead').delegate('th.oe-sortable[data-id]', 'click', function (e) {
169 self.dataset.sort($this.data('id'));
170 if ($this.find('span').length) {
171 $this.find('span').toggleClass(
172 'ui-icon-triangle-1-s ui-icon-triangle-1-n');
174 $this.append('<span class="ui-icon ui-icon-triangle-1-s">')
175 .siblings('.oe-sortable').find('span').remove();
178 self.reload_content();
181 this.$element.find('.oe-list-pager')
182 .delegate('button', 'click', function () {
184 switch ($this.data('pager-action')) {
186 self.page = 0; break;
188 self.page = Math.floor(
189 self.dataset.ids.length / self.limit());
192 self.page += 1; break;
194 self.page -= 1; break;
196 self.reload_content();
197 }).find('.oe-pager-state')
198 .click(function (e) {
202 var $select = $('<select>')
203 .appendTo($this.empty())
204 .click(function (e) {e.stopPropagation();})
205 .append('<option value="80">80</option>' +
206 '<option value="100">100</option>' +
207 '<option value="200">200</option>' +
208 '<option value="500">500</option>' +
209 '<option value="NaN">Unlimited</option>')
210 .change(function () {
211 var val = parseInt($select.val(), 10);
212 self._limit = (isNaN(val) ? null : val);
214 self.reload_content();
216 .val(self._limit || 'NaN');
218 if (!this.sidebar && this.options.sidebar && this.options.sidebar_id) {
219 this.sidebar = new openerp.base.Sidebar(this, this.options.sidebar_id);
220 this.sidebar.start();
221 this.sidebar.add_toolbar(data.fields_view.toolbar);
222 this.set_common_sidebar_sections(this.sidebar);
226 * Configures the ListView pager based on the provided dataset's information
228 * Horrifying side-effect: sets the dataset's data on this.dataset?
230 * @param {openerp.base.DataSet} dataset
232 configure_pager: function (dataset) {
233 this.dataset.ids = dataset.ids;
235 var limit = this.limit(),
236 total = dataset.ids.length,
237 first = (this.page * limit),
239 if (!limit || (total - first) < limit) {
242 last = first + limit;
244 this.$element.find('span.oe-pager-state').empty().text(_.sprintf(
245 "[%d to %d] of %d", first + 1, last, total));
248 .find('button[data-pager-action=first], button[data-pager-action=previous]')
249 .attr('disabled', this.page === 0)
251 .find('button[data-pager-action=last], button[data-pager-action=next]')
252 .attr('disabled', last === total);
255 * Sets up the listview's columns: merges view and fields data, move
256 * grouped-by columns to the front of the columns list and make them all
259 * @param {Object} fields fields_view_get's fields section
260 * @param {Boolean} [grouped] Should the grouping columns (group and count) be displayed
262 setup_columns: function (fields, grouped) {
263 var domain_computer = openerp.base.form.compute_domain;
265 var noop = function () { return {}; };
266 var field_to_column = function (field) {
267 var name = field.attrs.name;
268 var column = _.extend({id: name, tag: field.tag},
269 field.attrs, fields[name]);
270 // modifiers computer
271 if (column.modifiers) {
272 var modifiers = JSON.parse(column.modifiers);
273 column.modifiers_for = function (fields) {
274 if (!modifiers.invisible) {
278 'invisible': domain_computer(modifiers.invisible, fields)
281 if (modifiers['tree_invisible']) {
282 column.invisible = '1';
285 column.modifiers_for = noop;
290 this.columns.splice(0, this.columns.length);
291 this.columns.push.apply(
293 _(this.fields_view.arch.children).map(field_to_column));
295 this.columns.unshift({
296 id: '_group', tag: '', string: "Group", meta: true,
297 modifiers_for: function () { return {}; }
299 id: '_count', tag: '', string: '#', meta: true,
300 modifiers_for: function () { return {}; }
304 this.visible_columns = _.filter(this.columns, function (column) {
305 return column.invisible !== '1';
308 this.aggregate_columns = _(this.visible_columns)
309 .map(function (column) {
310 if (column.type !== 'integer' && column.type !== 'float') {
313 var aggregation_func = column['group_operator'] || 'sum';
315 return _.extend({}, column, {
316 'function': aggregation_func,
317 label: column[aggregation_func]
322 * Used to handle a click on a table row, if no other handler caught the
325 * The default implementation asks the list view's view manager to switch
326 * to a different view (by calling
327 * :js:func:`~openerp.base.ViewManager.on_mode_switch`), using the
328 * provided record index (within the current list view's dataset).
330 * If the index is null, ``switch_to_record`` asks for the creation of a
333 * @param {Number|void} index the record index (in the current dataset) to switch to
334 * @param {String} [view="form"] the view type to switch to
336 select_record:function (index, view) {
337 view = view || 'form';
338 this.dataset.index = index;
339 _.delay(_.bind(function () {
340 this.do_switch_view(view);
343 do_show: function () {
344 this.$element.show();
346 this.sidebar.$element.show();
349 this.reload_content();
352 do_hide: function () {
353 this.$element.hide();
355 this.sidebar.$element.hide();
360 * Reloads the list view based on the current settings (dataset & al)
362 * @param {Boolean} [grouped] Should the list be displayed grouped
363 * @param {Object} [context] context to send the server while loading the view
365 reload_view: function (grouped, context, initial) {
367 var callback = function (field_view_get) {
368 self.on_loaded(field_view_get, grouped);
370 if (this.embedded_view) {
371 return $.Deferred().then(callback).resolve({fields_view: this.embedded_view});
373 return this.rpc('/base/listview/load', {
375 view_id: this.view_id,
376 context: this.dataset.get_context(context),
377 toolbar: this.options.sidebar
382 * re-renders the content of the list view
384 reload_content: function () {
385 this.records.reset();
386 this.$element.find('.oe-listview-content').append(
388 $.proxy(this, 'compute_aggregates')));
391 * Event handler for a search, asks for the computation/folding of domains
392 * and contexts (and group-by), then reloads the view's content.
394 * @param {Array} domains a sequence of literal and non-literal domains
395 * @param {Array} contexts a sequence of literal and non-literal contexts
396 * @param {Array} groupbys a sequence of literal and non-literal group-by contexts
397 * @returns {$.Deferred} fold request evaluation promise
399 do_search: function (domains, contexts, groupbys) {
400 return this.rpc('/base/session/eval_domain_and_context', {
403 group_by_seq: groupbys
404 }, $.proxy(this, 'do_actual_search'));
407 * Handler for the result of eval_domain_and_context, actually perform the
410 * @param {Object} results results of evaluating domain and process for a search
412 do_actual_search: function (results) {
413 this.groups.datagroup = new openerp.base.DataGroup(
418 this.groups.datagroup.sort = this.dataset._sort;
420 if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
421 results.group_by = null;
424 this.reload_view(!!results.group_by, results.context).then(
425 $.proxy(this, 'reload_content'));
428 * Handles the signal to delete lines from the records list
430 * @param {Array} ids the ids of the records to delete
432 do_delete: function (ids) {
437 return $.when(this.dataset.unlink(ids)).then(function () {
438 _(ids).each(function (id) {
439 self.records.remove(self.records.get(id));
441 self.compute_aggregates();
445 * Handles the signal indicating that a new record has been selected
447 * @param {Array} ids selected record ids
448 * @param {Array} records selected record values
450 do_select: function (ids, records) {
451 this.$element.find('.oe-list-delete')
452 .attr('disabled', !ids.length);
454 if (!records.length) {
455 this.compute_aggregates();
458 this.compute_aggregates(_(records).map(function (record) {
459 return {count: 1, values: record};
463 * Handles action button signals on a record
465 * @param {String} name action name
466 * @param {Object} id id of the record the action should be called on
467 * @param {Function} callback should be called after the action is executed, if non-null
469 do_button_action: function (name, id, callback) {
470 var action = _.detect(this.columns, function (field) {
471 return field.name === name;
473 if (!action) { return; }
474 this.execute_action(action, this.dataset, id, callback);
477 * Handles the activation of a record (clicking on it)
479 * @param {Number} index index of the record in the dataset
480 * @param {Object} id identifier of the activated record
481 * @param {openerp.base.DataSet} dataset dataset in which the record is available (may not be the listview's dataset in case of nested groups)
483 do_activate_record: function (index, id, dataset) {
485 // TODO is it needed ?
486 this.dataset.read_slice([],{
487 context: dataset.get_context(),
488 domain: dataset.get_domain()
490 self.select_record(index);
494 * Handles signal for the addition of a new record (can be a creation,
495 * can be the addition from a remote source, ...)
497 * The default implementation is to switch to a new record on the form view
499 do_add_record: function () {
500 this.select_record(null);
503 * Handles deletion of all selected lines
505 do_delete_selected: function () {
506 this.do_delete(this.groups.get_selection().ids);
509 * Computes the aggregates for the current list view, either on the
510 * records provided or on the records of the internal
511 * :js:class:`~openerp.base.ListView.Group`, by calling
512 * :js:func:`~openerp.base.ListView.group.get_records`.
514 * Then displays the aggregates in the table through
515 * :js:method:`~openerp.base.ListView.display_aggregates`.
517 * @param {Array} [records]
519 compute_aggregates: function (records) {
520 var columns = _(this.aggregate_columns).filter(function (column) {
521 return column['function']; });
522 if (_.isEmpty(columns)) { return; }
524 if (_.isEmpty(records)) {
525 records = this.groups.get_records();
528 var count = 0, sums = {};
529 _(columns).each(function (column) {
530 switch (column['function']) {
532 sums[column.id] = -Infinity;
535 sums[column.id] = Infinity;
541 _(records).each(function (record) {
542 count += record.count || 1;
543 _(columns).each(function (column) {
544 var field = column.id,
545 value = record.values[field];
546 switch (column['function']) {
548 sums[field] += value;
551 sums[field] += record.count * value;
554 if (sums[field] > value) {
559 if (sums[field] < value) {
568 _(columns).each(function (column) {
569 var field = column.id;
570 switch (column['function']) {
572 aggregates[field] = {value: sums[field] / count};
575 aggregates[field] = {value: sums[field]};
579 this.display_aggregates(aggregates);
581 display_aggregates: function (aggregation) {
582 var $footer_cells = this.$element.find('.oe-list-footer');
583 _(this.aggregate_columns).each(function (column) {
584 if (!column['function']) {
588 $footer_cells.filter(_.sprintf('[data-field=%s]', column.id))
589 .html(openerp.base.format_cell(aggregation, column));
592 // TODO: implement reorder (drag and drop rows)
594 openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.ListView.List# */{
596 * List display for the ListView, handles basic DOM events and transforms
597 * them in the relevant higher-level events, to which the list view (or
598 * other consumers) can subscribe.
600 * Events on this object are registered via jQuery.
605 * Triggered when a row is selected (using check boxes), provides an
606 * array of ids of all the selected records.
608 * Triggered when deletion buttons are hit, provide an array of ids of
609 * all the records being marked for suppression.
611 * Triggered when an action button is clicked, provides two parameters:
613 * * The name of the action to execute (as a string)
614 * * The id of the record to execute the action on
616 * Triggered when a row of the table is clicked, provides the index (in
617 * the rows array) and id of the selected record to the handle function.
620 * @param {Object} opts display options, identical to those of :js:class:`openerp.base.ListView`
622 init: function (group, opts) {
625 this.view = group.view;
626 this.session = this.view.session;
628 this.options = opts.options;
629 this.columns = opts.columns;
630 this.dataset = opts.dataset;
631 this.records = opts.records;
633 this.record_callbacks = {
634 'remove': function (event, record) {
635 var $row = self.$current.find(
636 '[data-id=' + record.get('id') + ']');
637 var index = $row.data('index');
639 self.refresh_zebra(index);
641 'reset': $.proxy(this, 'on_records_reset'),
642 'change': function (event, record) {
643 var $row = self.$current.find('[data-id=' + record.get('id') + ']');
644 $row.replaceWith(self.render_record(record));
646 'add': function (ev, records, record, index) {
647 var $new_row = $('<tr>').attr({
648 'data-id': record.get('id')
652 $new_row.prependTo(self.$current);
654 var previous_record = records.at(index-1),
655 $previous_sibling = self.$current.find(
656 '[data-id=' + previous_record.get('id') + ']');
657 $new_row.insertAfter($previous_sibling);
660 self.refresh_zebra(index, 1);
663 _(this.record_callbacks).each(function (callback, event) {
664 this.records.bind(event, callback);
667 this.$_element = $('<tbody class="ui-widget-content">')
668 .appendTo(document.body)
669 .delegate('th.oe-record-selector', 'click', function (e) {
671 var selection = self.get_selection();
673 'selected', [selection.ids, selection.records]);
675 .delegate('td.oe-record-delete button', 'click', function (e) {
677 var $row = $(e.target).closest('tr');
678 $(self).trigger('deleted', [[self.row_id($row)]]);
680 .delegate('td.oe-field-cell button', 'click', function (e) {
682 var $target = $(e.currentTarget),
683 field = $target.closest('td').data('field'),
684 $row = $target.closest('tr'),
685 record_id = self.row_id($row);
687 $(self).trigger('action', [field, record_id, function () {
688 return self.reload_record(self.records.get(record_id));
691 .delegate('tr', 'click', function (e) {
693 self.dataset.index = self.records.indexOf(
695 self.row_id(e.currentTarget)));
699 row_clicked: function () {
702 [this.records.at(this.dataset.index).get('id'),
705 render: function () {
707 this.$current.remove();
709 this.$current = this.$_element.clone(true);
710 this.$current.empty().append(
711 QWeb.render('ListView.rows', _.extend({
712 render_cell: openerp.base.format_cell}, this)));
715 * Gets the ids of all currently selected records, if any
716 * @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
718 get_selection: function () {
719 if (!this.options.selectable) {
722 var records = this.records;
723 var result = {ids: [], records: []};
724 this.$current.find('th.oe-record-selector input:checked')
725 .closest('tr').each(function () {
726 var record = records.get($(this).data('id'));
727 result.ids.push(record.get('id'));
728 result.records.push(record.attributes);
733 * Returns the identifier of the object displayed in the provided table
736 * @param {Object} row the selected table row
737 * @returns {Number|String} the identifier of the row's object
739 row_id: function (row) {
740 return $(row).data('id');
743 * Death signal, cleans up list display
745 on_records_reset: function () {
746 _(this.record_callbacks).each(function (callback, event) {
747 this.records.unbind(event, callback);
749 if (!this.$current) { return; }
750 this.$current.remove();
751 this.$current = null;
752 this.$_element.remove();
754 get_records: function () {
755 return this.records.map(function (record) {
756 return {count: 1, values: record.attributes};
760 * Reloads the provided record by re-reading its content from the server.
762 * @param {Record} record
763 * @returns {$.Deferred} promise to the finalization of the reloading
765 reload_record: function (record) {
766 return this.dataset.read_ids(
768 _.pluck(_(this.columns).filter(function (r) {
769 return r.tag === 'field';
772 _(records[0]).each(function (value, key) {
773 record.set(key, value, {silent: true});
775 record.trigger('change', record);
780 * Renders a list record to HTML
782 * @param {Record} record index of the record to render in ``this.rows``
783 * @returns {String} QWeb rendering of the selected record
785 render_record: function (record) {
786 var index = this.records.indexOf(record);
787 return QWeb.render('ListView.row', {
788 columns: this.columns,
789 options: this.options,
791 row_parity: (index % 2 === 0) ? 'even' : 'odd',
792 render_cell: openerp.base.format_cell
796 * Fixes fixes the even/odd classes
798 * @param {Number} [from_index] index from which to resequence
799 * @param {Number} [offset = 0] selection offset for DOM, in case there are rows to ignore in the table
801 refresh_zebra: function (from_index, offset) {
802 offset = offset || 0;
803 from_index = from_index || 0;
804 var dom_offset = offset + from_index;
805 var sel = dom_offset ? ':gt(' + (dom_offset - 1) + ')' : null;
806 this.$current.children(sel).each(function (i, e) {
807 var index = from_index + i;
808 // reset record-index accelerators on rows and even/odd
809 var even = index%2 === 0;
810 $(e).toggleClass('even', even)
811 .toggleClass('odd', !even);
815 openerp.base.ListView.Groups = openerp.base.Class.extend( /** @lends openerp.base.ListView.Groups# */{
816 passtrough_events: 'action deleted row_link',
818 * Grouped display for the ListView. Handles basic DOM events and interacts
819 * with the :js:class:`~openerp.base.DataGroup` bound to it.
821 * Provides events similar to those of
822 * :js:class:`~openerp.base.ListView.List`
825 * @param {openerp.base.ListView} view
826 * @param {Object} [options]
827 * @param {Collection} [options.records]
828 * @param {Object} [options.options]
829 * @param {Array} [options.columns]
831 init: function (view, options) {
832 options = options || {};
834 this.records = options.records || view.records;
835 this.options = options.options || view.options;
836 this.columns = options.columns || view.columns;
837 this.datagroup = null;
844 this.records.bind('reset', $.proxy(this, 'on_records_reset'));
846 make_fragment: function () {
847 return document.createDocumentFragment();
850 * Returns a DOM node after which a new tbody can be inserted, so that it
851 * follows the provided row.
853 * Necessary to insert the result of a new group or list view within an
854 * existing groups render, without losing track of the groups's own
857 * @param {HTMLTableRowElement} row the row after which the caller wants to insert a body
858 * @returns {HTMLTableSectionElement} element after which a tbody can be inserted
860 point_insertion: function (row) {
862 var red_letter_tboday = $row.closest('tbody')[0];
864 var $next_siblings = $row.nextAll();
865 if ($next_siblings.length) {
866 var $root_kanal = $('<tbody>').insertAfter(red_letter_tboday);
868 $root_kanal.append($next_siblings);
869 this.elements.splice(
870 _.indexOf(this.elements, red_letter_tboday),
874 return red_letter_tboday;
876 make_paginator: function () {
878 var $prev = $('<button type="button" data-pager-action="previous"><</button>')
879 .click(function (e) {
883 self.$row.closest('tbody').next()
884 .replaceWith(self.render());
886 var $next = $('<button type="button" data-pager-action="next">></button>')
887 .click(function (e) {
891 self.$row.closest('tbody').next()
892 .replaceWith(self.render());
894 this.$row.children().last()
896 .append('<span class="oe-pager-state"></span>')
899 open: function (point_insertion) {
900 this.render().insertAfter(point_insertion);
901 this.make_paginator();
904 this.$row.children().last().empty();
905 this.records.reset();
908 * Prefixes ``$node`` with floated spaces in order to indent it relative
909 * to its own left margin/baseline
911 * @param {jQuery} $node jQuery object to indent
912 * @param {Number} level current nesting level, >= 1
913 * @returns {jQuery} the indentation node created
915 indent: function ($node, level) {
917 .css({'float': 'left', 'white-space': 'pre'})
918 .text(new Array(level).join(' '))
921 render_groups: function (datagroups) {
923 var placeholder = this.make_fragment();
924 _(datagroups).each(function (group) {
925 if (self.children[group.value]) {
926 self.records.proxy(group.value).reset();
927 delete self.children[group.value];
929 var child = self.children[group.value] = new openerp.base.ListView.Groups(self.view, {
930 records: self.records.proxy(group.value),
931 options: self.options,
932 columns: self.columns
934 self.bind_child_events(child);
935 child.datagroup = group;
937 var $row = child.$row = $('<tr>');
938 if (group.openable) {
939 $row.click(function (e) {
940 if (!$row.data('open')) {
941 $row.data('open', true)
942 .find('span.ui-icon')
943 .removeClass('ui-icon-triangle-1-e')
944 .addClass('ui-icon-triangle-1-s');
945 child.open(self.point_insertion(e.currentTarget));
947 $row.removeData('open')
948 .find('span.ui-icon')
949 .removeClass('ui-icon-triangle-1-s')
950 .addClass('ui-icon-triangle-1-e');
955 placeholder.appendChild($row[0]);
957 var $group_column = $('<th class="oe-group-name">').appendTo($row);
958 // Don't fill this if group_by_no_leaf but no group_by
959 if (group.grouped_on) {
961 row_data[group.grouped_on] = group;
962 var group_column = _(self.columns).detect(function (column) {
963 return column.id === group.grouped_on; });
964 $group_column.html(openerp.base.format_cell(
965 row_data, group_column, "Undefined"
967 if (group.openable) {
968 // Make openable if not terminal group & group_by_no_leaf
970 .prepend('<span class="ui-icon ui-icon-triangle-1-e" style="float: left;">');
973 self.indent($group_column, group.level);
975 $('<td>').text(group.length).appendTo($row);
977 if (self.options.selectable) {
980 _(self.columns).chain()
981 .filter(function (column) {return !column.invisible;})
982 .each(function (column) {
984 // do not do anything
985 } else if (column.id in group.aggregates) {
986 var value = group.aggregates[column.id];
988 if (column.type === 'integer') {
990 } else if (column.type === 'float') {
994 .text(_.sprintf(format, value))
1000 if (self.options.deletable) {
1001 $row.append('<td class="oe-group-pagination">');
1006 bind_child_events: function (child) {
1007 var $this = $(this),
1009 $(child).bind('selected', function (e) {
1010 // can have selections spanning multiple links
1011 var selection = self.get_selection();
1012 $this.trigger(e, [selection.ids, selection.records]);
1013 }).bind(this.passtrough_events, function (e) {
1014 // additional positional parameters are provided to trigger as an
1015 // Array, following the event type or event object, but are
1016 // provided to the .bind event handler as *args.
1017 // Convert our *args back into an Array in order to trigger them
1018 // on the group itself, so it can ultimately be forwarded wherever
1019 // it's supposed to go.
1020 var args = Array.prototype.slice.call(arguments, 1);
1021 $this.trigger.call($this, e, args);
1024 render_dataset: function (dataset) {
1026 list = new openerp.base.ListView.List(this, {
1027 options: this.options,
1028 columns: this.columns,
1030 records: this.records
1032 this.bind_child_events(list);
1034 var view = this.view,
1035 limit = view.limit(),
1036 d = new $.Deferred(),
1037 page = this.datagroup.openable ? this.page : view.page;
1039 var fields = _.pluck(_.select(this.columns, function(x) {return x.tag == "field"}), 'name');
1040 var options = { offset: page * limit, limit: limit };
1041 dataset.read_slice(fields, options , function (records) {
1042 if (!self.datagroup.openable) {
1043 view.configure_pager(dataset);
1045 var pages = Math.ceil(dataset.ids.length / limit);
1047 .find('.oe-pager-state')
1048 .text(_.sprintf('%d/%d', page + 1, pages))
1050 .find('button[data-pager-action=previous]')
1051 .attr('disabled', page === 0)
1053 .find('button[data-pager-action=next]')
1054 .attr('disabled', page === pages - 1);
1057 self.records.add(records, {silent: true});
1063 setup_resequence_rows: function (list, dataset) {
1064 // drag and drop enabled if list is not sorted and there is a
1065 // "sequence" column in the view.
1066 if ((dataset.sort && dataset.sort())
1067 || !_(this.columns).any(function (column) {
1068 return column.name === 'sequence'; })) {
1071 // ondrop, move relevant record & fix sequences
1072 list.$current.sortable({
1073 stop: function (event, ui) {
1074 var to_move = list.records.get(ui.item.data('id')),
1075 target_id = ui.item.prev().data('id');
1077 list.records.remove(to_move);
1078 var to = target_id ? list.records.indexOf(list.records.get(target_id)) : 0;
1079 list.records.add(to_move, { at: to });
1081 // resequencing time!
1082 var record, index = to,
1083 // if drag to 1st row (to = 0), start sequencing from 0
1084 // (exclusive lower bound)
1085 seq = to ? list.records.at(to - 1).get('sequence') : 0;
1086 while (++seq, record = list.records.at(index++)) {
1087 // write are independent from one another, so we can just
1088 // launch them all at the same time and we don't really
1089 // give a fig about when they're done
1090 dataset.write(record.get('id'), {sequence: seq});
1091 record.set('sequence', seq);
1094 list.refresh_zebra();
1098 render: function (post_render) {
1100 var $element = $('<tbody>');
1101 this.elements = [$element[0]];
1103 this.datagroup.list(
1104 _(this.view.visible_columns).chain()
1105 .filter(function (column) { return column.tag === 'field' })
1106 .pluck('name').value(),
1108 $element[0].appendChild(
1109 self.render_groups(groups));
1110 if (post_render) { post_render(); }
1111 }, function (dataset) {
1112 self.render_dataset(dataset).then(function (list) {
1113 self.children[null] = list;
1115 [list.$current.replaceAll($element)[0]];
1116 self.setup_resequence_rows(list, dataset);
1117 if (post_render) { post_render(); }
1123 * Returns the ids of all selected records for this group, and the records
1126 get_selection: function () {
1127 var ids = [], records = [];
1130 .each(function (child) {
1131 var selection = child.get_selection();
1132 ids.push.apply(ids, selection.ids);
1133 records.push.apply(records, selection.records);
1136 return {ids: ids, records: records};
1138 on_records_reset: function () {
1140 $(this.elements).remove();
1142 get_records: function () {
1143 if (_(this.children).isEmpty()) {
1145 count: this.datagroup.length,
1146 values: this.datagroup.aggregates
1149 return _(this.children).chain()
1150 .map(function (child) {
1151 return child.get_records();
1152 }).flatten().value();
1158 * @extends openerp.base.Class
1162 * @param {String} event event to listen to on the current object, null for all events
1163 * @param {Function} handler event handler to bind to the relevant event
1166 bind: function (event, handler) {
1167 var calls = this['_callbacks'] || (this._callbacks = {});
1169 if (event in calls) {
1170 calls[event].push(handler);
1172 calls[event] = [handler];
1177 * @param {String} event event to unbind on the current object
1178 * @param {function} [handler] specific event handler to remove (otherwise unbind all handlers for the event)
1181 unbind: function (event, handler) {
1182 var calls = this._callbacks || {};
1183 if (!(event in calls)) { return this; }
1185 delete calls[event];
1187 var handlers = calls[event];
1189 _(handlers).indexOf(handler),
1195 * @param {String} event
1198 trigger: function (event) {
1200 if (!(calls = this._callbacks)) { return this; }
1201 var callbacks = (calls[event] || []).concat(calls[null] || []);
1202 for(var i=0, length=callbacks.length; i<length; ++i) {
1203 callbacks[i].apply(this, arguments);
1208 var Record = openerp.base.Class.extend(/** @lends Record# */{
1211 * @extends openerp.base.Class
1212 * @borrows Events#bind as this.bind
1213 * @borrows Events#trigger as this.trigger
1214 * @param {Object} [data]
1216 init: function (data) {
1217 this.attributes = data || {};
1220 * @param {String} key
1223 get: function (key) {
1224 return this.attributes[key];
1229 * @param {Object} [options]
1230 * @param {Boolean} [options.silent=false]
1233 set: function (key, value, options) {
1234 options = options || {};
1235 var old_value = this.attributes[key];
1236 if (old_value === value) {
1239 this.attributes[key] = value;
1240 if (!options.silent) {
1241 this.trigger('change:' + key, this, value, old_value);
1242 this.trigger('change', this, key, value, old_value);
1247 * Converts the current record to the format expected by form views:
1249 * .. code-block:: javascript
1258 * @returns {Object} record displayable in a form view
1260 toForm: function () {
1262 _(this.attributes).each(function (value, key) {
1263 form_data[key] = {value: value};
1266 return {data: form_data};
1269 Record.include(Events);
1270 var Collection = openerp.base.Class.extend(/** @lends Collection# */{
1272 * Smarter collections, with events, very strongly inspired by Backbone's.
1274 * Using a "dumb" array of records makes synchronization between the
1278 * @extends openerp.base.Class
1279 * @borrows Events#bind as this.bind
1280 * @borrows Events#trigger as this.trigger
1281 * @param {Array} [records] records to initialize the collection with
1282 * @param {Object} [options]
1284 init: function (records, options) {
1285 options = options || {};
1286 _.bindAll(this, '_onRecordEvent');
1291 this._key = options.key;
1292 this._parent = options.parent;
1299 * @param {Object|Array} record
1300 * @param {Object} [options]
1301 * @param {Number} [options.at]
1302 * @param {Boolean} [options.silent=false]
1305 add: function (record, options) {
1306 options = options || {};
1307 var records = record instanceof Array ? record : [record];
1309 for(var i=0, length=records.length; i<length; ++i) {
1310 var instance = (records[i] instanceof Record) ? records[i] : new Record(records[i]);
1311 instance.bind(null, this._onRecordEvent);
1312 this._byId[instance.get('id')] = instance;
1313 if (options.at == undefined) {
1314 this.records.push(instance);
1315 if (!options.silent) {
1316 this.trigger('add', this, instance, this.records.length-1);
1319 var insertion_index = options.at + i;
1320 this.records.splice(insertion_index, 0, instance);
1321 if (!options.silent) {
1322 this.trigger('add', this, instance, insertion_index);
1331 * Get a record by its index in the collection, can also take a group if
1332 * the collection is not degenerate
1334 * @param {Number} index
1335 * @param {String} [group]
1336 * @returns {Record|undefined}
1338 at: function (index, group) {
1340 var groups = group.split('.');
1341 return this._proxies[groups[0]].at(index, groups.join('.'));
1343 return this.records[index];
1346 * Get a record by its database id
1348 * @param {Number} id
1349 * @returns {Record|undefined}
1351 get: function (id) {
1352 if (!_(this._proxies).isEmpty()) {
1354 _(this._proxies).detect(function (proxy) {
1355 return record = proxy.get(id);
1359 return this._byId[id];
1362 * Builds a proxy (insert/retrieve) to a subtree of the collection, by
1363 * the subtree's group
1365 * @param {String} section group path section
1366 * @returns {Collection}
1368 proxy: function (section) {
1369 return this._proxies[section] = new Collection(null, {
1372 }).bind(null, this._onRecordEvent);
1375 * @param {Array} [records]
1378 reset: function (records) {
1379 _(this._proxies).each(function (proxy) {
1389 this.trigger('reset', this);
1393 * Removes the provided record from the collection
1395 * @param {Record} record
1398 remove: function (record) {
1400 var index = _(this.records).indexOf(record);
1402 _(this._proxies).each(function (proxy) {
1403 proxy.remove(record);
1408 this.records.splice(index, 1);
1409 delete this._byId[record.get('id')];
1411 this.trigger('remove', record, this);
1415 _onRecordEvent: function (event, record, options) {
1416 // don't propagate reset events
1417 if (event === 'reset') { return; }
1418 this.trigger.apply(this, arguments);
1421 // underscore-type methods
1422 each: function (callback) {
1423 for(var section in this._proxies) {
1424 if (this._proxies.hasOwnProperty(section)) {
1425 this._proxies[section].each(callback);
1428 for(var i=0; i<this.length; ++i) {
1429 callback(this.records[i]);
1432 map: function (callback) {
1434 this.each(function (record) {
1435 results.push(callback(record));
1439 pluck: function (fieldname) {
1440 return this.map(function (record) {
1441 return record.get(fieldname);
1444 indexOf: function (record) {
1445 return _(this.records).indexOf(record);
1448 Collection.include(Events);
1449 openerp.base.list = {
1452 Collection: Collection
1455 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: