1 openerp.web.list = function (openerp) {
2 var _t = openerp.web._t,
4 var QWeb = openerp.web.qweb;
5 openerp.web.views.add('list', 'openerp.web.ListView');
6 openerp.web.ListView = openerp.web.View.extend( /** @lends openerp.web.ListView# */ {
8 display_name: _lt('List'),
10 // records can be selected one by one
12 // list rows can be deleted
14 // whether the column headers should be displayed
16 // display addition button, with that label
17 'addable': _lt("Create"),
18 // whether the list view can be sorted, note that once a view has been
19 // sorted it can not be reordered anymore
21 // whether the view rows can be reordered (via vertical drag & drop)
23 // display an edit icon linking to form view
27 * Core class for list-type displays.
29 * As a view, needs a number of view-related parameters to be correctly
30 * instantiated, provides options and overridable methods for behavioral
33 * See constructor parameters and method documentations for information on
34 * the default behaviors and possible options for the list view.
36 * @constructs openerp.web.ListView
37 * @extends openerp.web.View
39 * @param parent parent object
40 * @param {openerp.web.DataSet} dataset the dataset the view should work with
41 * @param {String} view_id the listview's identifier, if any
42 * @param {Object} options A set of options used to configure the view
43 * @param {Boolean} [options.selectable=true] determines whether view rows are selectable (e.g. via a checkbox)
44 * @param {Boolean} [options.header=true] should the list's header be displayed
45 * @param {Boolean} [options.deletable=true] are the list rows deletable
46 * @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.
47 * @param {Boolean} [options.sortable=true] is it possible to sort the table by clicking on column headers
48 * @param {Boolean} [options.reorderable=true] is it possible to reorder list rows
50 init: function(parent, dataset, view_id, options) {
53 this.set_default_options(_.extend({}, this.defaults, options || {}));
54 this.dataset = dataset;
55 this.model = dataset.model;
56 this.view_id = view_id;
57 this.previous_colspan = null;
62 this.records = new Collection();
64 this.set_groups(new openerp.web.ListView.Groups(this));
66 if (this.dataset instanceof openerp.web.DataSetStatic) {
67 this.groups.datagroup = new openerp.web.StaticDataGroup(this.dataset);
69 this.groups.datagroup = new openerp.web.DataGroup(
72 dataset.get_context(),
74 this.groups.datagroup.sort = this.dataset._sort;
78 this.records.bind('change', function (event, record, key) {
79 if (!_(self.aggregate_columns).chain()
80 .pluck('name').contains(key).value()) {
83 self.compute_aggregates();
89 * Retrieves the view's number of records per page (|| section)
91 * options > defaults > parent.action.limit > indefinite
93 * @returns {Number|null}
96 if (this._limit === undefined) {
97 this._limit = (this.options.limit
98 || this.defaults.limit
99 || (this.widget_parent.action || {}).limit
105 * Set a custom Group construct as the root of the List View.
107 * @param {openerp.web.ListView.Groups} groups
109 set_groups: function (groups) {
112 $(this.groups).unbind("selected deleted action row_link");
116 this.groups = groups;
117 $(this.groups).bind({
118 'selected': function (e, ids, records) {
119 self.do_select(ids, records);
121 'deleted': function (e, ids) {
124 'action': function (e, action_name, id, callback) {
125 self.do_button_action(action_name, id, callback);
127 'row_link': function (e, id, dataset, view) {
128 self.do_activate_record(dataset.index, id, dataset, view);
133 * View startup method, the default behavior is to set the ``oe-listview``
134 * class on its root element and to perform an RPC load call.
136 * @returns {$.Deferred} loading promise
140 this.$element.addClass('oe-listview');
141 return this.reload_view(null, null, true);
144 * Returns the color for the provided record in the current view (from the
145 * ``@colors`` attribute)
147 * @param {Record} record record for the current row
148 * @returns {String} CSS color declaration
150 color_for: function (record) {
151 if (!this.colors) { return ''; }
152 var context = _.extend({}, record.attributes, {
153 uid: this.session.uid,
154 current_date: new Date().toString('yyyy-MM-dd')
155 // TODO: time, datetime, relativedelta
157 for(var i=0, len=this.colors.length; i<len; ++i) {
158 var pair = this.colors[i],
160 expression = pair[1];
161 if (py.evaluate(expression, context)) {
162 return 'color: ' + color + ';';
164 // TODO: handle evaluation errors
169 * Called after loading the list view's description, sets up such things
170 * as the view table's columns, renders the table itself and hooks up the
171 * various table-level and row-level DOM events (action buttons, deletion
172 * buttons, selection of records, [New] button, selection of a given
175 * Sets up the following:
177 * * Processes arch and fields to generate a complete field descriptor for each field
178 * * Create the table itself and allocate visible columns
179 * * Hook in the top-level (header) [New|Add] and [Delete] button
180 * * Sets up showing/hiding the top-level [Delete] button based on records being selected or not
181 * * Sets up event handlers for action buttons and per-row deletion button
182 * * Hooks global callback for clicking on a row
183 * * Sets up its sidebar, if any
185 * @param {Object} data wrapped fields_view_get result
186 * @param {Object} data.fields_view fields_view_get result (processed)
187 * @param {Object} data.fields_view.fields mapping of fields for the current model
188 * @param {Object} data.fields_view.arch current list view descriptor
189 * @param {Boolean} grouped Is the list view grouped
191 on_loaded: function(data, grouped) {
193 this.fields_view = data;
194 this.name = "" + this.fields_view.arch.attrs.string;
196 if (this.fields_view.arch.attrs.colors) {
197 this.colors = _(this.fields_view.arch.attrs.colors.split(';')).chain()
199 .map(function(color_pair) {
200 var pair = color_pair.split(':'),
203 return [color, py.parse(py.tokenize(expr)), expr];
207 this.setup_columns(this.fields_view.fields, grouped);
209 this.$element.html(QWeb.render(this._template, this));
211 this.$element.find('.all-record-selector').click(function(){
212 self.$element.find('.oe-record-selector input').prop('checked',
213 self.$element.find('.all-record-selector').prop('checked') || false);
214 var selection = self.groups.get_selection();
215 $(self.groups).trigger(
216 'selected', [selection.ids, selection.records]);
219 this.$element.find('.oe-list-add')
220 .click(this.proxy('do_add_record'))
221 .attr('disabled', grouped && this.options.editable);
222 this.$element.find('.oe-list-delete')
223 .attr('disabled', true)
224 .click(this.proxy('do_delete_selected'));
225 this.$element.find('thead').delegate('th.oe-sortable[data-id]', 'click', function (e) {
229 self.dataset.sort($this.data('id'));
230 if ($this.find('span').length) {
231 $this.find('span').toggleClass(
232 'ui-icon-triangle-1-s ui-icon-triangle-1-n');
234 $this.append('<span class="ui-icon ui-icon-triangle-1-s">')
235 .siblings('.oe-sortable').find('span').remove();
238 self.reload_content();
241 this.$element.find('.oe-list-pager')
242 .delegate('button', 'click', function () {
244 switch ($this.data('pager-action')) {
246 self.page = 0; break;
248 self.page = Math.floor(
249 self.dataset.ids.length / self.limit());
252 self.page += 1; break;
254 self.page -= 1; break;
256 self.reload_content();
257 }).find('.oe-pager-state')
258 .click(function (e) {
262 var $select = $('<select>')
263 .appendTo($this.empty())
264 .click(function (e) {e.stopPropagation();})
265 .append('<option value="80">80</option>' +
266 '<option value="100">100</option>' +
267 '<option value="200">200</option>' +
268 '<option value="500">500</option>' +
269 '<option value="NaN">' + _t("Unlimited") + '</option>')
270 .change(function () {
271 var val = parseInt($select.val(), 10);
272 self._limit = (isNaN(val) ? null : val);
274 self.reload_content();
276 .val(self._limit || 'NaN');
278 if (!this.sidebar && this.options.sidebar && this.options.sidebar_id) {
279 this.sidebar = new openerp.web.Sidebar(this, this.options.sidebar_id);
280 this.sidebar.start();
281 this.sidebar.add_toolbar(this.fields_view.toolbar);
282 this.set_common_sidebar_sections(this.sidebar);
286 * Configures the ListView pager based on the provided dataset's information
288 * Horrifying side-effect: sets the dataset's data on this.dataset?
290 * @param {openerp.web.DataSet} dataset
292 configure_pager: function (dataset) {
293 this.dataset.ids = dataset.ids;
295 var limit = this.limit(),
296 total = dataset.ids.length,
297 first = (this.page * limit),
299 if (!limit || (total - first) < limit) {
302 last = first + limit;
304 this.$element.find('span.oe-pager-state').empty().text(_.str.sprintf(
305 _t("[%(first_record)d to %(last_record)d] of %(records_count)d"), {
306 first_record: first + 1,
312 .find('button[data-pager-action=first], button[data-pager-action=previous]')
313 .attr('disabled', this.page === 0)
315 .find('button[data-pager-action=last], button[data-pager-action=next]')
316 .attr('disabled', last === total);
319 * Sets up the listview's columns: merges view and fields data, move
320 * grouped-by columns to the front of the columns list and make them all
323 * @param {Object} fields fields_view_get's fields section
324 * @param {Boolean} [grouped] Should the grouping columns (group and count) be displayed
326 setup_columns: function (fields, grouped) {
327 var domain_computer = openerp.web.form.compute_domain;
329 var noop = function () { return {}; };
330 var field_to_column = function (field) {
331 var name = field.attrs.name;
332 var column = _.extend({id: name, tag: field.tag},
333 fields[name], field.attrs);
334 // modifiers computer
335 if (column.modifiers) {
336 var modifiers = JSON.parse(column.modifiers);
337 column.modifiers_for = function (fields) {
338 if (!modifiers.invisible) {
342 'invisible': domain_computer(modifiers.invisible, fields)
345 if (modifiers['tree_invisible']) {
346 column.invisible = '1';
348 delete column.invisible;
351 column.modifiers_for = noop;
356 this.columns.splice(0, this.columns.length);
357 this.columns.push.apply(
359 _(this.fields_view.arch.children).map(field_to_column));
361 this.columns.unshift({
362 id: '_group', tag: '', string: "Group", meta: true,
363 modifiers_for: function () { return {}; }
365 id: '_count', tag: '', string: '#', meta: true,
366 modifiers_for: function () { return {}; }
370 this.visible_columns = _.filter(this.columns, function (column) {
371 return column.invisible !== '1';
374 this.aggregate_columns = _(this.visible_columns)
375 .map(function (column) {
376 if (column.type !== 'integer' && column.type !== 'float') {
379 var aggregation_func = column['group_operator'] || 'sum';
380 if (!(aggregation_func in column)) {
384 return _.extend({}, column, {
385 'function': aggregation_func,
386 label: column[aggregation_func]
391 * Used to handle a click on a table row, if no other handler caught the
394 * The default implementation asks the list view's view manager to switch
395 * to a different view (by calling
396 * :js:func:`~openerp.web.ViewManager.on_mode_switch`), using the
397 * provided record index (within the current list view's dataset).
399 * If the index is null, ``switch_to_record`` asks for the creation of a
402 * @param {Number|void} index the record index (in the current dataset) to switch to
403 * @param {String} [view="page"] the view type to switch to
405 select_record:function (index, view) {
406 view = view || index == null ? 'form' : 'page';
407 this.dataset.index = index;
408 _.delay(_.bind(function () {
409 this.do_switch_view(view);
412 do_show: function () {
415 this.sidebar.$element.show();
418 do_hide: function () {
421 this.sidebar.$element.hide();
425 * Reloads the list view based on the current settings (dataset & al)
427 * @param {Boolean} [grouped] Should the list be displayed grouped
428 * @param {Object} [context] context to send the server while loading the view
430 reload_view: function (grouped, context, initial) {
432 var callback = function (field_view_get) {
433 self.on_loaded(field_view_get, grouped);
435 if (this.embedded_view) {
436 return $.Deferred().then(callback).resolve(this.embedded_view);
438 return this.rpc('/web/listview/load', {
440 view_id: this.view_id,
442 context: this.dataset.get_context(context),
443 toolbar: this.options.sidebar
448 * re-renders the content of the list view
450 * @returns {$.Deferred} promise to content reloading
452 reload_content: function () {
454 this.records.reset();
455 var reloaded = $.Deferred();
456 this.$element.find('.oe-listview-content').append(
457 this.groups.render(function () {
458 if (self.dataset.index == null) {
460 self.records.each(function () { has_one = true; });
462 self.dataset.index = 0;
465 self.compute_aggregates();
472 return reloaded.promise();
474 reload: function () {
475 return this.reload_content();
478 do_load_state: function(state) {
480 if (state.page && this.page !== state.page) {
481 this.page = state.page;
485 if (_.isString(state.limit)) {
488 if (state.limit !== this._limit) {
489 this._limit = state.limit;
494 this.reload_content();
498 * Handler for the result of eval_domain_and_context, actually perform the
501 * @param {Object} results results of evaluating domain and process for a search
503 do_search: function (domain, context, group_by) {
505 this.groups.datagroup = new openerp.web.DataGroup(
506 this, this.model, domain, context, group_by);
507 this.groups.datagroup.sort = this.dataset._sort;
509 if (_.isEmpty(group_by) && !context['group_by_no_leaf']) {
512 this.no_leaf = !!context['group_by_no_leaf'];
514 this.reload_view(!!group_by, context).then(
515 this.proxy('reload_content'));
518 * Handles the signal to delete lines from the records list
520 * @param {Array} ids the ids of the records to delete
522 do_delete: function (ids) {
523 if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
527 return $.when(this.dataset.unlink(ids)).then(function () {
528 _(ids).each(function (id) {
529 self.records.remove(self.records.get(id));
531 self.configure_pager(self.dataset);
532 self.compute_aggregates();
536 * Handles the signal indicating that a new record has been selected
538 * @param {Array} ids selected record ids
539 * @param {Array} records selected record values
541 do_select: function (ids, records) {
542 this.$element.find('.oe-list-delete')
543 .attr('disabled', !ids.length);
546 this.sidebar.do_unfold();
548 this.sidebar.do_fold();
551 if (!records.length) {
552 this.compute_aggregates();
555 this.compute_aggregates(_(records).map(function (record) {
556 return {count: 1, values: record};
560 * Handles action button signals on a record
562 * @param {String} name action name
563 * @param {Object} id id of the record the action should be called on
564 * @param {Function} callback should be called after the action is executed, if non-null
566 do_button_action: function (name, id, callback) {
567 var action = _.detect(this.columns, function (field) {
568 return field.name === name;
570 if (!action) { return; }
571 if ('confirm' in action && !window.confirm(action.confirm)) {
575 var c = new openerp.web.CompoundContext();
576 c.set_eval_context(_.extend({
579 active_model: this.dataset.model
580 }, this.records.get(id).toContext()));
581 if (action.context) {
582 c.add(action.context);
585 this.do_execute_action(action, this.dataset, id, callback);
588 * Handles the activation of a record (clicking on it)
590 * @param {Number} index index of the record in the dataset
591 * @param {Object} id identifier of the activated record
592 * @param {openerp.web.DataSet} dataset dataset in which the record is available (may not be the listview's dataset in case of nested groups)
594 do_activate_record: function (index, id, dataset, view) {
595 this.dataset.ids = dataset.ids;
596 this.select_record(index, view);
599 * Handles signal for the addition of a new record (can be a creation,
600 * can be the addition from a remote source, ...)
602 * The default implementation is to switch to a new record on the form view
604 do_add_record: function () {
605 this.select_record(null);
608 * Handles deletion of all selected lines
610 do_delete_selected: function () {
611 this.do_delete(this.groups.get_selection().ids);
614 * Computes the aggregates for the current list view, either on the
615 * records provided or on the records of the internal
616 * :js:class:`~openerp.web.ListView.Group`, by calling
617 * :js:func:`~openerp.web.ListView.group.get_records`.
619 * Then displays the aggregates in the table through
620 * :js:method:`~openerp.web.ListView.display_aggregates`.
622 * @param {Array} [records]
624 compute_aggregates: function (records) {
625 var columns = _(this.aggregate_columns).filter(function (column) {
626 return column['function']; });
627 if (_.isEmpty(columns)) { return; }
629 if (_.isEmpty(records)) {
630 records = this.groups.get_records();
632 records = _(records).compact();
634 var count = 0, sums = {};
635 _(columns).each(function (column) {
636 switch (column['function']) {
638 sums[column.id] = -Infinity;
641 sums[column.id] = Infinity;
647 _(records).each(function (record) {
648 count += record.count || 1;
649 _(columns).each(function (column) {
650 var field = column.id,
651 value = record.values[field];
652 switch (column['function']) {
654 sums[field] += value;
657 sums[field] += record.count * value;
660 if (sums[field] > value) {
665 if (sums[field] < value) {
674 _(columns).each(function (column) {
675 var field = column.id;
676 switch (column['function']) {
678 aggregates[field] = {value: sums[field] / count};
681 aggregates[field] = {value: sums[field]};
685 this.display_aggregates(aggregates);
687 display_aggregates: function (aggregation) {
689 var $footer_cells = this.$element.find('.oe-list-footer');
690 _(this.aggregate_columns).each(function (column) {
691 if (!column['function']) {
695 $footer_cells.filter(_.str.sprintf('[data-field=%s]', column.id))
696 .html(openerp.web.format_cell(aggregation, column, {
697 process_modifiers: false
701 get_selected_ids: function() {
702 var ids = this.groups.get_selection().ids;
706 * Adds padding columns at the start or end of all table rows (including
709 * @param {Number} count number of columns to add
710 * @param {Object} options
711 * @param {"before"|"after"} [options.position="after"] insertion position for the new columns
712 * @param {Object} [options.except] content row to not pad
714 pad_columns: function (count, options) {
715 options = options || {};
716 // padding for action/pager header
717 var $first_header = this.$element.find('thead tr:first th');
718 var colspan = $first_header.attr('colspan');
720 if (!this.previous_colspan) {
721 this.previous_colspan = colspan;
723 $first_header.attr('colspan', parseInt(colspan, 10) + count);
725 // Padding for column titles, footer and data rows
726 var $rows = this.$element
727 .find('.oe-listview-header-columns, tr:not(thead tr)')
728 .not(options['except']);
729 var newcols = new Array(count+1).join('<td class="oe-listview-padding"></td>');
730 if (options.position === 'before') {
731 $rows.prepend(newcols);
733 $rows.append(newcols);
737 * Removes all padding columns of the table
739 unpad_columns: function () {
740 this.$element.find('.oe-listview-padding').remove();
741 if (this.previous_colspan) {
743 .find('thead tr:first th')
744 .attr('colspan', this.previous_colspan);
745 this.previous_colspan = null;
749 openerp.web.ListView.List = openerp.web.Class.extend( /** @lends openerp.web.ListView.List# */{
751 * List display for the ListView, handles basic DOM events and transforms
752 * them in the relevant higher-level events, to which the list view (or
753 * other consumers) can subscribe.
755 * Events on this object are registered via jQuery.
760 * Triggered when a row is selected (using check boxes), provides an
761 * array of ids of all the selected records.
763 * Triggered when deletion buttons are hit, provide an array of ids of
764 * all the records being marked for suppression.
766 * Triggered when an action button is clicked, provides two parameters:
768 * * The name of the action to execute (as a string)
769 * * The id of the record to execute the action on
771 * Triggered when a row of the table is clicked, provides the index (in
772 * the rows array) and id of the selected record to the handle function.
774 * @constructs openerp.web.ListView.List
775 * @extends openerp.web.Class
777 * @param {Object} opts display options, identical to those of :js:class:`openerp.web.ListView`
779 init: function (group, opts) {
782 this.view = group.view;
783 this.session = this.view.session;
785 this.options = opts.options;
786 this.columns = opts.columns;
787 this.dataset = opts.dataset;
788 this.records = opts.records;
790 this.record_callbacks = {
791 'remove': function (event, record) {
792 var $row = self.$current.find(
793 '[data-id=' + record.get('id') + ']');
794 var index = $row.data('index');
796 self.refresh_zebra(index);
798 'reset': function () { return self.on_records_reset(); },
799 'change': function (event, record) {
800 var $row = self.$current.find('[data-id=' + record.get('id') + ']');
801 $row.replaceWith(self.render_record(record));
803 'add': function (ev, records, record, index) {
804 var $new_row = $('<tr>').attr({
805 'data-id': record.get('id')
809 $new_row.prependTo(self.$current);
811 var previous_record = records.at(index-1),
812 $previous_sibling = self.$current.find(
813 '[data-id=' + previous_record.get('id') + ']');
814 $new_row.insertAfter($previous_sibling);
817 self.refresh_zebra(index, 1);
820 _(this.record_callbacks).each(function (callback, event) {
821 this.records.bind(event, callback);
824 this.$_element = $('<tbody class="ui-widget-content">')
825 .appendTo(document.body)
826 .delegate('th.oe-record-selector', 'click', function (e) {
828 var selection = self.get_selection();
830 'selected', [selection.ids, selection.records]);
832 .delegate('td.oe-record-delete button', 'click', function (e) {
834 var $row = $(e.target).closest('tr');
835 $(self).trigger('deleted', [[self.row_id($row)]]);
837 .delegate('td.oe-field-cell button', 'click', function (e) {
839 var $target = $(e.currentTarget),
840 field = $target.closest('td').data('field'),
841 $row = $target.closest('tr'),
842 record_id = self.row_id($row);
844 // note: $.data converts data to number if it's composed only
845 // of digits, nice when storing actual numbers, not nice when
846 // storing strings composed only of digits. Force the action
847 // name to be a string
848 $(self).trigger('action', [field.toString(), record_id, function () {
849 return self.reload_record(self.records.get(record_id));
852 .delegate('a', 'click', function (e) {
855 .delegate('tr', 'click', function (e) {
857 var row_id = self.row_id(e.currentTarget);
858 if (row_id !== undefined) {
859 if (!self.dataset.select_id(row_id)) {
860 throw "Could not find id in dataset"
863 if ($(e.target).is('.oe-record-edit-link-img')) {
866 self.row_clicked(e, view);
870 row_clicked: function (e, view) {
873 [this.dataset.ids[this.dataset.index],
874 this.dataset, view]);
876 render_cell: function (record, column) {
878 if(column.type === 'reference') {
879 value = record.get(column.id);
881 // Ensure that value is in a reference "shape", otherwise we're
882 // going to loop on performing name_get after we've resolved (and
883 // set) a human-readable version. m2o does not have this issue
884 // because the non-human-readable is just a number, where the
885 // human-readable version is a pair
886 if (value && (ref_match = /([\w\.]+),(\d+)/.exec(value))) {
887 // reference values are in the shape "$model,$id" (as a
888 // string), we need to split and name_get this pair in order
889 // to get a correctly displayable value in the field
890 var model = ref_match[1],
891 id = parseInt(ref_match[2], 10);
892 new openerp.web.DataSet(this.view, model).name_get([id], function(names) {
893 if (!names.length) { return; }
894 record.set(column.id, names[0][1]);
897 } else if (column.type === 'many2one') {
898 value = record.get(column.id);
899 // m2o values are usually name_get formatted, [Number, String]
900 // pairs, but in some cases only the id is provided. In these
901 // cases, we need to perform a name_get call to fetch the actual
903 if (typeof value === 'number' || value instanceof Number) {
904 // fetch the name, set it on the record (in the right field)
905 // and let the various registered events handle refreshing the
907 new openerp.web.DataSet(this.view, column.relation)
908 .name_get([value], function (names) {
909 if (!names.length) { return; }
910 record.set(column.id, names[0]);
914 return openerp.web.format_cell(record.toForm().data, column, {
915 model: this.dataset.model,
919 render: function () {
922 this.$current.remove();
924 this.$current = this.$_element.clone(true);
925 this.$current.empty().append(
926 QWeb.render('ListView.rows', _.extend({
927 render_cell: function () { return self.render_cell(); }
929 this.pad_table_to(5);
931 pad_table_to: function (count) {
932 if (this.records.length >= count ||
933 _(this.columns).any(function(column) { return column.meta; })) {
937 if (this.options.selectable) {
938 cells.push('<th class="oe-record-selector"></td>');
940 if (this.options.isClarkGable) {
941 cells.push('<th class="oe-record-edit-link"></td>');
943 _(this.columns).each(function(column) {
944 if (column.invisible === '1') {
947 if (column.tag === 'button') {
948 cells.push('<td class="oe-button" title="' + column.string + '"> </td>');
950 cells.push('<td title="' + column.string + '"> </td>');
953 if (this.options.deletable) {
954 cells.push('<td class="oe-record-delete"><button type="button" style="visibility: hidden"> </button></td>');
956 cells.unshift('<tr>');
959 var row = cells.join('');
961 .children('tr:not([data-id])').remove().end()
962 .append(new Array(count - this.records.length + 1).join(row));
963 this.refresh_zebra(this.records.length);
966 * Gets the ids of all currently selected records, if any
967 * @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
969 get_selection: function () {
970 if (!this.options.selectable) {
973 var records = this.records;
974 var result = {ids: [], records: []};
975 this.$current.find('th.oe-record-selector input:checked')
976 .closest('tr').each(function () {
977 var record = records.get($(this).data('id'));
978 result.ids.push(record.get('id'));
979 result.records.push(record.attributes);
984 * Returns the identifier of the object displayed in the provided table
987 * @param {Object} row the selected table row
988 * @returns {Number|String} the identifier of the row's object
990 row_id: function (row) {
991 return $(row).data('id');
994 * Death signal, cleans up list display
996 on_records_reset: function () {
997 _(this.record_callbacks).each(function (callback, event) {
998 this.records.unbind(event, callback);
1000 if (!this.$current) { return; }
1001 this.$current.remove();
1002 this.$current = null;
1003 this.$_element.remove();
1005 get_records: function () {
1006 return this.records.map(function (record) {
1007 return {count: 1, values: record.attributes};
1011 * Reloads the provided record by re-reading its content from the server.
1013 * @param {Record} record
1014 * @returns {$.Deferred} promise to the finalization of the reloading
1016 reload_record: function (record) {
1017 return this.dataset.read_ids(
1019 _.pluck(_(this.columns).filter(function (r) {
1020 return r.tag === 'field';
1022 ).then(function (records) {
1023 _(records[0]).each(function (value, key) {
1024 record.set(key, value, {silent: true});
1026 record.trigger('change', record);
1030 * Renders a list record to HTML
1032 * @param {Record} record index of the record to render in ``this.rows``
1033 * @returns {String} QWeb rendering of the selected record
1035 render_record: function (record) {
1036 var index = this.records.indexOf(record);
1037 return QWeb.render('ListView.row', {
1038 columns: this.columns,
1039 options: this.options,
1041 row_parity: (index % 2 === 0) ? 'even' : 'odd',
1043 render_cell: function () { return this.render_cell(); }
1047 * Fixes fixes the even/odd classes
1049 * @param {Number} [from_index] index from which to resequence
1050 * @param {Number} [offset = 0] selection offset for DOM, in case there are rows to ignore in the table
1052 refresh_zebra: function (from_index, offset) {
1053 offset = offset || 0;
1054 from_index = from_index || 0;
1055 var dom_offset = offset + from_index;
1056 var sel = dom_offset ? ':gt(' + (dom_offset - 1) + ')' : null;
1057 this.$current.children(sel).each(function (i, e) {
1058 var index = from_index + i;
1059 // reset record-index accelerators on rows and even/odd
1060 var even = index%2 === 0;
1061 $(e).toggleClass('even', even)
1062 .toggleClass('odd', !even);
1066 openerp.web.ListView.Groups = openerp.web.Class.extend( /** @lends openerp.web.ListView.Groups# */{
1067 passtrough_events: 'action deleted row_link',
1069 * Grouped display for the ListView. Handles basic DOM events and interacts
1070 * with the :js:class:`~openerp.web.DataGroup` bound to it.
1072 * Provides events similar to those of
1073 * :js:class:`~openerp.web.ListView.List`
1075 * @constructs openerp.web.ListView.Groups
1076 * @extends openerp.web.Class
1078 * @param {openerp.web.ListView} view
1079 * @param {Object} [options]
1080 * @param {Collection} [options.records]
1081 * @param {Object} [options.options]
1082 * @param {Array} [options.columns]
1084 init: function (view, options) {
1085 options = options || {};
1087 this.records = options.records || view.records;
1088 this.options = options.options || view.options;
1089 this.columns = options.columns || view.columns;
1090 this.datagroup = null;
1098 this.records.bind('reset', function () {
1099 return self.on_records_reset(); });
1101 make_fragment: function () {
1102 return document.createDocumentFragment();
1105 * Returns a DOM node after which a new tbody can be inserted, so that it
1106 * follows the provided row.
1108 * Necessary to insert the result of a new group or list view within an
1109 * existing groups render, without losing track of the groups's own
1112 * @param {HTMLTableRowElement} row the row after which the caller wants to insert a body
1113 * @returns {HTMLTableSectionElement} element after which a tbody can be inserted
1115 point_insertion: function (row) {
1117 var red_letter_tboday = $row.closest('tbody')[0];
1119 var $next_siblings = $row.nextAll();
1120 if ($next_siblings.length) {
1121 var $root_kanal = $('<tbody>').insertAfter(red_letter_tboday);
1123 $root_kanal.append($next_siblings);
1124 this.elements.splice(
1125 _.indexOf(this.elements, red_letter_tboday),
1129 return red_letter_tboday;
1131 make_paginator: function () {
1133 var $prev = $('<button type="button" data-pager-action="previous"><</button>')
1134 .click(function (e) {
1135 e.stopPropagation();
1138 self.$row.closest('tbody').next()
1139 .replaceWith(self.render());
1141 var $next = $('<button type="button" data-pager-action="next">></button>')
1142 .click(function (e) {
1143 e.stopPropagation();
1146 self.$row.closest('tbody').next()
1147 .replaceWith(self.render());
1149 this.$row.children().last()
1151 .append('<span class="oe-pager-state"></span>')
1154 open: function (point_insertion) {
1155 this.render().insertAfter(point_insertion);
1157 var no_subgroups = _(this.datagroup.group_by).isEmpty(),
1158 records_terminated = !this.datagroup.context['group_by_no_leaf'];
1159 if (no_subgroups && records_terminated) {
1160 this.make_paginator();
1163 close: function () {
1164 this.$row.children().last().empty();
1165 this.records.reset();
1168 * Prefixes ``$node`` with floated spaces in order to indent it relative
1169 * to its own left margin/baseline
1171 * @param {jQuery} $node jQuery object to indent
1172 * @param {Number} level current nesting level, >= 1
1173 * @returns {jQuery} the indentation node created
1175 indent: function ($node, level) {
1177 .css({'float': 'left', 'white-space': 'pre'})
1178 .text(new Array(level).join(' '))
1181 render_groups: function (datagroups) {
1183 var placeholder = this.make_fragment();
1184 _(datagroups).each(function (group) {
1185 if (self.children[group.value]) {
1186 self.records.proxy(group.value).reset();
1187 delete self.children[group.value];
1189 var child = self.children[group.value] = new openerp.web.ListView.Groups(self.view, {
1190 records: self.records.proxy(group.value),
1191 options: self.options,
1192 columns: self.columns
1194 self.bind_child_events(child);
1195 child.datagroup = group;
1197 var $row = child.$row = $('<tr>');
1198 if (group.openable && group.length) {
1199 $row.click(function (e) {
1200 if (!$row.data('open')) {
1201 $row.data('open', true)
1202 .find('span.ui-icon')
1203 .removeClass('ui-icon-triangle-1-e')
1204 .addClass('ui-icon-triangle-1-s');
1205 child.open(self.point_insertion(e.currentTarget));
1207 $row.removeData('open')
1208 .find('span.ui-icon')
1209 .removeClass('ui-icon-triangle-1-s')
1210 .addClass('ui-icon-triangle-1-e');
1215 placeholder.appendChild($row[0]);
1217 var $group_column = $('<th class="oe-group-name">').appendTo($row);
1218 // Don't fill this if group_by_no_leaf but no group_by
1219 if (group.grouped_on) {
1221 row_data[group.grouped_on] = group;
1222 var group_column = _(self.columns).detect(function (column) {
1223 return column.id === group.grouped_on; });
1225 $group_column.html(openerp.web.format_cell(
1226 row_data, group_column, {
1227 value_if_empty: _t("Undefined"),
1228 process_modifiers: false
1231 $group_column.html(row_data[group_column.id].value);
1233 if (group.length && group.openable) {
1234 // Make openable if not terminal group & group_by_no_leaf
1235 $group_column.prepend('<span class="ui-icon ui-icon-triangle-1-e" style="float: left;">');
1237 // Kinda-ugly hack: jquery-ui has no "empty" icon, so set
1238 // wonky background position to ensure nothing is displayed
1239 // there but the rest of the behavior is ui-icon's
1240 $group_column.prepend('<span class="ui-icon" style="float: left; background-position: 150px 150px">');
1243 self.indent($group_column, group.level);
1245 $('<td>').text(group.length).appendTo($row);
1247 if (self.options.selectable) {
1248 $row.append('<td>');
1250 if (self.options.isClarkGable) {
1251 $row.append('<td>');
1253 _(self.columns).chain()
1254 .filter(function (column) {return !column.invisible;})
1255 .each(function (column) {
1257 // do not do anything
1258 } else if (column.id in group.aggregates) {
1259 var value = group.aggregates[column.id];
1260 $('<td class="oe-number">')
1261 .html(openerp.web.format_value(value, column))
1264 $row.append('<td>');
1267 if (self.options.deletable) {
1268 $row.append('<td class="oe-group-pagination">');
1273 bind_child_events: function (child) {
1274 var $this = $(this),
1276 $(child).bind('selected', function (e) {
1277 // can have selections spanning multiple links
1278 var selection = self.get_selection();
1279 $this.trigger(e, [selection.ids, selection.records]);
1280 }).bind(this.passtrough_events, function (e) {
1281 // additional positional parameters are provided to trigger as an
1282 // Array, following the event type or event object, but are
1283 // provided to the .bind event handler as *args.
1284 // Convert our *args back into an Array in order to trigger them
1285 // on the group itself, so it can ultimately be forwarded wherever
1286 // it's supposed to go.
1287 var args = Array.prototype.slice.call(arguments, 1);
1288 $this.trigger.call($this, e, args);
1291 render_dataset: function (dataset) {
1293 list = new openerp.web.ListView.List(this, {
1294 options: this.options,
1295 columns: this.columns,
1297 records: this.records
1299 this.bind_child_events(list);
1301 var view = this.view,
1302 limit = view.limit(),
1303 d = new $.Deferred(),
1304 page = this.datagroup.openable ? this.page : view.page;
1306 var fields = _.pluck(_.select(this.columns, function(x) {return x.tag == "field"}), 'name');
1307 var options = { offset: page * limit, limit: limit, context: {bin_size: true} };
1308 //TODO xmo: investigate why we need to put the setTimeout
1309 $.async_when().then(function() {dataset.read_slice(fields, options).then(function (records) {
1310 // FIXME: ignominious hacks, parents (aka form view) should not send two ListView#reload_content concurrently
1311 if (self.records.length) {
1312 self.records.reset(null, {silent: true});
1314 if (!self.datagroup.openable) {
1315 view.configure_pager(dataset);
1317 var pages = Math.ceil(dataset.ids.length / limit);
1319 .find('.oe-pager-state')
1320 .text(_.str.sprintf(_t("%(page)d/%(page_count)d"), {
1325 .find('button[data-pager-action=previous]')
1326 .attr('disabled', page === 0)
1328 .find('button[data-pager-action=next]')
1329 .attr('disabled', page === pages - 1);
1332 self.records.add(records, {silent: true});
1338 setup_resequence_rows: function (list, dataset) {
1339 // drag and drop enabled if list is not sorted and there is a
1340 // "sequence" column in the view.
1341 if ((dataset.sort && dataset.sort())
1342 || !_(this.columns).any(function (column) {
1343 return column.name === 'sequence'; })) {
1346 // ondrop, move relevant record & fix sequences
1347 list.$current.sortable({
1349 items: '> tr[data-id]',
1350 containment: 'parent',
1351 stop: function (event, ui) {
1352 var to_move = list.records.get(ui.item.data('id')),
1353 target_id = ui.item.prev().data('id'),
1354 from_index = list.records.indexOf(to_move),
1355 target = list.records.get(target_id);
1356 if (list.records.at(from_index - 1) == target) {
1360 list.records.remove(to_move);
1361 var to = target_id ? list.records.indexOf(target) + 1 : 0;
1362 list.records.add(to_move, { at: to });
1364 // resequencing time!
1365 var record, index = to,
1366 // if drag to 1st row (to = 0), start sequencing from 0
1367 // (exclusive lower bound)
1368 seq = to ? list.records.at(to - 1).get('sequence') : 0;
1369 while (++seq, record = list.records.at(index++)) {
1370 // write are independent from one another, so we can just
1371 // launch them all at the same time and we don't really
1372 // give a fig about when they're done
1373 // FIXME: breaks on o2ms (e.g. Accounting > Financial
1374 // Accounting > Taxes > Taxes, child tax accounts)
1375 // when synchronous (without setTimeout)
1376 (function (dataset, id, seq) {
1377 $.async_when().then(function () {
1378 dataset.write(id, {sequence: seq});
1380 }(dataset, record.get('id'), seq));
1381 record.set('sequence', seq);
1384 list.refresh_zebra();
1388 render: function (post_render) {
1390 var $element = $('<tbody>');
1391 this.elements = [$element[0]];
1393 this.datagroup.list(
1394 _(this.view.visible_columns).chain()
1395 .filter(function (column) { return column.tag === 'field' })
1396 .pluck('name').value(),
1398 $element[0].appendChild(
1399 self.render_groups(groups));
1400 if (post_render) { post_render(); }
1401 }, function (dataset) {
1402 self.render_dataset(dataset).then(function (list) {
1403 self.children[null] = list;
1405 [list.$current.replaceAll($element)[0]];
1406 self.setup_resequence_rows(list, dataset);
1407 if (post_render) { post_render(); }
1413 * Returns the ids of all selected records for this group, and the records
1416 get_selection: function () {
1417 var ids = [], records = [];
1420 .each(function (child) {
1421 var selection = child.get_selection();
1422 ids.push.apply(ids, selection.ids);
1423 records.push.apply(records, selection.records);
1426 return {ids: ids, records: records};
1428 on_records_reset: function () {
1430 $(this.elements).remove();
1432 get_records: function () {
1433 if (_(this.children).isEmpty()) {
1434 if (!this.datagroup.length) {
1438 count: this.datagroup.length,
1439 values: this.datagroup.aggregates
1442 return _(this.children).chain()
1443 .map(function (child) {
1444 return child.get_records();
1445 }).flatten().value();
1452 var Events = /** @lends Events# */{
1454 * @param {String} event event to listen to on the current object, null for all events
1455 * @param {Function} handler event handler to bind to the relevant event
1458 bind: function (event, handler) {
1459 var calls = this['_callbacks'] || (this._callbacks = {});
1461 if (event in calls) {
1462 calls[event].push(handler);
1464 calls[event] = [handler];
1469 * @param {String} event event to unbind on the current object
1470 * @param {function} [handler] specific event handler to remove (otherwise unbind all handlers for the event)
1473 unbind: function (event, handler) {
1474 var calls = this._callbacks || {};
1475 if (!(event in calls)) { return this; }
1477 delete calls[event];
1479 var handlers = calls[event];
1481 _(handlers).indexOf(handler),
1487 * @param {String} event
1490 trigger: function (event) {
1492 if (!(calls = this._callbacks)) { return this; }
1493 var callbacks = (calls[event] || []).concat(calls[null] || []);
1494 for(var i=0, length=callbacks.length; i<length; ++i) {
1495 callbacks[i].apply(this, arguments);
1500 var Record = openerp.web.Class.extend(/** @lends Record# */{
1502 * @constructs Record
1503 * @extends openerp.web.Class
1506 * @param {Object} [data]
1508 init: function (data) {
1509 this.attributes = data || {};
1512 * @param {String} key
1515 get: function (key) {
1516 return this.attributes[key];
1521 * @param {Object} [options]
1522 * @param {Boolean} [options.silent=false]
1525 set: function (key, value, options) {
1526 options = options || {};
1527 var old_value = this.attributes[key];
1528 if (old_value === value) {
1531 this.attributes[key] = value;
1532 if (!options.silent) {
1533 this.trigger('change:' + key, this, value, old_value);
1534 this.trigger('change', this, key, value, old_value);
1539 * Converts the current record to the format expected by form views:
1541 * .. code-block:: javascript
1550 * @returns {Object} record displayable in a form view
1552 toForm: function () {
1553 var form_data = {}, attrs = this.attributes;
1554 for(var k in attrs) {
1555 form_data[k] = {value: attrs[k]};
1558 return {data: form_data};
1561 * Converts the current record to a format expected by context evaluations
1562 * (identical to record.attributes, except m2o fields are their integer
1563 * value rather than a pair)
1565 toContext: function () {
1566 var output = {}, attrs = this.attributes;
1567 for(var k in attrs) {
1569 if (typeof val !== 'object') {
1571 } else if (val instanceof Array) {
1574 throw new Error("Can't convert value " + val + " to context");
1580 Record.include(Events);
1581 var Collection = openerp.web.Class.extend(/** @lends Collection# */{
1583 * Smarter collections, with events, very strongly inspired by Backbone's.
1585 * Using a "dumb" array of records makes synchronization between the
1588 * @constructs Collection
1589 * @extends openerp.web.Class
1592 * @param {Array} [records] records to initialize the collection with
1593 * @param {Object} [options]
1595 init: function (records, options) {
1596 options = options || {};
1597 _.bindAll(this, '_onRecordEvent');
1602 this._key = options.key;
1603 this._parent = options.parent;
1610 * @param {Object|Array} record
1611 * @param {Object} [options]
1612 * @param {Number} [options.at]
1613 * @param {Boolean} [options.silent=false]
1616 add: function (record, options) {
1617 options = options || {};
1618 var records = record instanceof Array ? record : [record];
1620 for(var i=0, length=records.length; i<length; ++i) {
1621 var instance = (records[i] instanceof Record) ? records[i] : new Record(records[i]);
1622 instance.bind(null, this._onRecordEvent);
1623 this._byId[instance.get('id')] = instance;
1624 if (options.at == undefined) {
1625 this.records.push(instance);
1626 if (!options.silent) {
1627 this.trigger('add', this, instance, this.records.length-1);
1630 var insertion_index = options.at + i;
1631 this.records.splice(insertion_index, 0, instance);
1632 if (!options.silent) {
1633 this.trigger('add', this, instance, insertion_index);
1642 * Get a record by its index in the collection, can also take a group if
1643 * the collection is not degenerate
1645 * @param {Number} index
1646 * @param {String} [group]
1647 * @returns {Record|undefined}
1649 at: function (index, group) {
1651 var groups = group.split('.');
1652 return this._proxies[groups[0]].at(index, groups.join('.'));
1654 return this.records[index];
1657 * Get a record by its database id
1659 * @param {Number} id
1660 * @returns {Record|undefined}
1662 get: function (id) {
1663 if (!_(this._proxies).isEmpty()) {
1665 _(this._proxies).detect(function (proxy) {
1666 return record = proxy.get(id);
1670 return this._byId[id];
1673 * Builds a proxy (insert/retrieve) to a subtree of the collection, by
1674 * the subtree's group
1676 * @param {String} section group path section
1677 * @returns {Collection}
1679 proxy: function (section) {
1680 return this._proxies[section] = new Collection(null, {
1683 }).bind(null, this._onRecordEvent);
1686 * @param {Array} [records]
1689 reset: function (records, options) {
1690 options = options || {};
1691 _(this._proxies).each(function (proxy) {
1701 if (!options.silent) {
1702 this.trigger('reset', this);
1707 * Removes the provided record from the collection
1709 * @param {Record} record
1712 remove: function (record) {
1714 var index = _(this.records).indexOf(record);
1716 _(this._proxies).each(function (proxy) {
1717 proxy.remove(record);
1722 this.records.splice(index, 1);
1723 delete this._byId[record.get('id')];
1725 this.trigger('remove', record, this);
1729 _onRecordEvent: function (event, record, options) {
1730 // don't propagate reset events
1731 if (event === 'reset') { return; }
1732 this.trigger.apply(this, arguments);
1735 // underscore-type methods
1736 each: function (callback) {
1737 for(var section in this._proxies) {
1738 if (this._proxies.hasOwnProperty(section)) {
1739 this._proxies[section].each(callback);
1742 for(var i=0; i<this.length; ++i) {
1743 callback(this.records[i]);
1746 map: function (callback) {
1748 this.each(function (record) {
1749 results.push(callback(record));
1753 pluck: function (fieldname) {
1754 return this.map(function (record) {
1755 return record.get(fieldname);
1758 indexOf: function (record) {
1759 return _(this.records).indexOf(record);
1762 Collection.include(Events);
1763 openerp.web.list = {
1766 Collection: Collection
1769 // vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax: