2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.list = function (openerp) {
|
2011-09-07 09:37:43 +00:00
|
|
|
var QWeb = openerp.web.qweb;
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.views.add('list', 'openerp.web.ListView');
|
|
|
|
openerp.web.ListView = openerp.web.View.extend( /** @lends openerp.web.ListView# */ {
|
2011-04-07 12:12:22 +00:00
|
|
|
defaults: {
|
|
|
|
// records can be selected one by one
|
2011-04-07 13:27:26 +00:00
|
|
|
'selectable': true,
|
2011-04-07 15:13:19 +00:00
|
|
|
// list rows can be deleted
|
|
|
|
'deletable': true,
|
2011-04-07 13:27:26 +00:00
|
|
|
// whether the column headers should be displayed
|
2011-04-12 13:02:29 +00:00
|
|
|
'header': true,
|
|
|
|
// display addition button, with that label
|
2011-04-21 12:06:08 +00:00
|
|
|
'addable': "New",
|
|
|
|
// whether the list view can be sorted, note that once a view has been
|
|
|
|
// sorted it can not be reordered anymore
|
|
|
|
'sortable': true,
|
|
|
|
// whether the view rows can be reordered (via vertical drag & drop)
|
2011-06-03 09:36:06 +00:00
|
|
|
'reorderable': true
|
2011-04-07 12:12:22 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-04-21 11:59:24 +00:00
|
|
|
* Core class for list-type displays.
|
|
|
|
*
|
|
|
|
* As a view, needs a number of view-related parameters to be correctly
|
|
|
|
* instantiated, provides options and overridable methods for behavioral
|
|
|
|
* customization.
|
|
|
|
*
|
|
|
|
* See constructor parameters and method documentations for information on
|
|
|
|
* the default behaviors and possible options for the list view.
|
|
|
|
*
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs openerp.web.ListView
|
|
|
|
* @extends openerp.web.View
|
|
|
|
*
|
2011-08-08 14:41:20 +00:00
|
|
|
* @param parent parent object
|
2011-04-07 12:12:22 +00:00
|
|
|
* @param element_id the id of the DOM elements this view should link itself to
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.DataSet} dataset the dataset the view should work with
|
2011-04-07 12:12:22 +00:00
|
|
|
* @param {String} view_id the listview's identifier, if any
|
|
|
|
* @param {Object} options A set of options used to configure the view
|
|
|
|
* @param {Boolean} [options.selectable=true] determines whether view rows are selectable (e.g. via a checkbox)
|
2011-04-07 13:27:26 +00:00
|
|
|
* @param {Boolean} [options.header=true] should the list's header be displayed
|
2011-04-07 15:13:19 +00:00
|
|
|
* @param {Boolean} [options.deletable=true] are the list rows deletable
|
2011-06-29 13:58:49 +00:00
|
|
|
* @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.
|
2011-04-21 12:06:08 +00:00
|
|
|
* @param {Boolean} [options.sortable=true] is it possible to sort the table by clicking on column headers
|
|
|
|
* @param {Boolean} [options.reorderable=true] is it possible to reorder list rows
|
2011-05-04 09:23:51 +00:00
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @borrows openerp.web.ActionExecutor#execute_action as #execute_action
|
2011-04-07 12:12:22 +00:00
|
|
|
*/
|
2011-07-18 12:54:22 +00:00
|
|
|
init: function(parent, element_id, dataset, view_id, options) {
|
2011-08-19 10:39:55 +00:00
|
|
|
var self = this;
|
2011-07-18 12:54:22 +00:00
|
|
|
this._super(parent, element_id);
|
2011-08-08 17:32:30 +00:00
|
|
|
this.set_default_options(_.extend({}, this.defaults, options || {}));
|
2011-03-30 14:00:48 +00:00
|
|
|
this.dataset = dataset;
|
|
|
|
this.model = dataset.model;
|
|
|
|
this.view_id = view_id;
|
2011-09-08 12:55:10 +00:00
|
|
|
this.previous_colspan = null;
|
2011-03-30 14:00:48 +00:00
|
|
|
|
2011-04-06 13:58:37 +00:00
|
|
|
this.columns = [];
|
2011-03-30 14:00:48 +00:00
|
|
|
|
2011-08-17 13:16:35 +00:00
|
|
|
this.records = new Collection();
|
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
this.set_groups(new openerp.web.ListView.Groups(this));
|
2011-07-18 12:54:22 +00:00
|
|
|
|
2011-09-05 11:03:09 +00:00
|
|
|
if (this.dataset instanceof openerp.web.DataSetStatic) {
|
|
|
|
this.groups.datagroup = new openerp.web.StaticDataGroup(this.dataset);
|
2011-05-31 13:38:17 +00:00
|
|
|
}
|
2011-06-22 15:36:26 +00:00
|
|
|
|
|
|
|
this.page = 0;
|
2011-08-19 10:39:55 +00:00
|
|
|
this.records.bind('change', function (event, record, key) {
|
|
|
|
if (!_(self.aggregate_columns).chain()
|
|
|
|
.pluck('name').contains(key).value()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.compute_aggregates();
|
|
|
|
});
|
2011-06-22 15:36:26 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Retrieves the view's number of records per page (|| section)
|
|
|
|
*
|
2011-08-11 08:43:01 +00:00
|
|
|
* options > defaults > parent.action.limit > indefinite
|
2011-06-22 15:36:26 +00:00
|
|
|
*
|
|
|
|
* @returns {Number|null}
|
|
|
|
*/
|
|
|
|
limit: function () {
|
2011-06-23 13:49:20 +00:00
|
|
|
if (this._limit === undefined) {
|
2011-06-22 15:36:26 +00:00
|
|
|
this._limit = (this.options.limit
|
|
|
|
|| this.defaults.limit
|
2011-08-11 08:43:01 +00:00
|
|
|
|| (this.widget_parent.action || {}).limit
|
2011-06-22 15:36:26 +00:00
|
|
|
|| null);
|
|
|
|
}
|
|
|
|
return this._limit;
|
2011-05-27 07:13:09 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set a custom Group construct as the root of the List View.
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.ListView.Groups} groups
|
2011-05-27 07:13:09 +00:00
|
|
|
*/
|
|
|
|
set_groups: function (groups) {
|
|
|
|
var self = this;
|
|
|
|
if (this.groups) {
|
|
|
|
$(this.groups).unbind("selected deleted action row_link");
|
|
|
|
delete this.groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.groups = groups;
|
2011-05-17 14:03:58 +00:00
|
|
|
$(this.groups).bind({
|
2011-05-25 09:28:13 +00:00
|
|
|
'selected': function (e, ids, records) {
|
|
|
|
self.do_select(ids, records);
|
2011-05-06 14:07:15 +00:00
|
|
|
},
|
|
|
|
'deleted': function (e, ids) {
|
|
|
|
self.do_delete(ids);
|
2011-05-09 08:10:22 +00:00
|
|
|
},
|
2011-05-19 11:13:09 +00:00
|
|
|
'action': function (e, action_name, id, callback) {
|
2011-08-11 02:00:14 +00:00
|
|
|
self.do_button_action(action_name, id, callback);
|
2011-05-09 08:22:12 +00:00
|
|
|
},
|
2011-06-06 11:42:04 +00:00
|
|
|
'row_link': function (e, id, dataset) {
|
|
|
|
self.do_activate_record(dataset.index, id, dataset);
|
2011-05-06 12:28:37 +00:00
|
|
|
}
|
|
|
|
});
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-04-21 11:59:24 +00:00
|
|
|
/**
|
|
|
|
* View startup method, the default behavior is to set the ``oe-listview``
|
|
|
|
* class on its root element and to perform an RPC load call.
|
|
|
|
*
|
|
|
|
* @returns {$.Deferred} loading promise
|
|
|
|
*/
|
2011-03-30 14:00:48 +00:00
|
|
|
start: function() {
|
2011-04-12 09:22:22 +00:00
|
|
|
this.$element.addClass('oe-listview');
|
2011-08-25 08:18:33 +00:00
|
|
|
return this.reload_view(null, null, true);
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-04-21 11:59:24 +00:00
|
|
|
/**
|
|
|
|
* Called after loading the list view's description, sets up such things
|
|
|
|
* as the view table's columns, renders the table itself and hooks up the
|
|
|
|
* various table-level and row-level DOM events (action buttons, deletion
|
|
|
|
* buttons, selection of records, [New] button, selection of a given
|
|
|
|
* record, ...)
|
|
|
|
*
|
|
|
|
* Sets up the following:
|
|
|
|
*
|
|
|
|
* * Processes arch and fields to generate a complete field descriptor for each field
|
|
|
|
* * Create the table itself and allocate visible columns
|
|
|
|
* * Hook in the top-level (header) [New|Add] and [Delete] button
|
|
|
|
* * Sets up showing/hiding the top-level [Delete] button based on records being selected or not
|
|
|
|
* * Sets up event handlers for action buttons and per-row deletion button
|
|
|
|
* * Hooks global callback for clicking on a row
|
|
|
|
* * Sets up its sidebar, if any
|
|
|
|
*
|
|
|
|
* @param {Object} data wrapped fields_view_get result
|
|
|
|
* @param {Object} data.fields_view fields_view_get result (processed)
|
|
|
|
* @param {Object} data.fields_view.fields mapping of fields for the current model
|
|
|
|
* @param {Object} data.fields_view.arch current list view descriptor
|
2011-05-27 07:13:09 +00:00
|
|
|
* @param {Boolean} grouped Is the list view grouped
|
2011-04-21 11:59:24 +00:00
|
|
|
*/
|
2011-05-27 07:13:09 +00:00
|
|
|
on_loaded: function(data, grouped) {
|
2011-04-12 14:55:16 +00:00
|
|
|
var self = this;
|
2011-09-06 20:54:38 +00:00
|
|
|
this.fields_view = data;
|
2011-03-30 14:00:48 +00:00
|
|
|
//this.log(this.fields_view);
|
|
|
|
this.name = "" + this.fields_view.arch.attrs.string;
|
|
|
|
|
2011-05-27 07:13:09 +00:00
|
|
|
this.setup_columns(this.fields_view.fields, grouped);
|
2011-05-04 14:47:53 +00:00
|
|
|
|
2011-04-06 13:58:37 +00:00
|
|
|
this.$element.html(QWeb.render("ListView", this));
|
2011-04-07 15:35:27 +00:00
|
|
|
|
|
|
|
// Head hook
|
2011-06-23 10:54:04 +00:00
|
|
|
this.$element.find('.oe-list-add')
|
2011-06-07 08:57:17 +00:00
|
|
|
.click(this.do_add_record)
|
2011-06-07 14:02:48 +00:00
|
|
|
.attr('disabled', grouped && this.options.editable);
|
2011-06-23 10:54:04 +00:00
|
|
|
this.$element.find('.oe-list-delete')
|
2011-06-07 14:02:48 +00:00
|
|
|
.attr('disabled', true)
|
2011-04-12 12:24:02 +00:00
|
|
|
.click(this.do_delete_selected);
|
2011-07-13 11:18:15 +00:00
|
|
|
this.$element.find('thead').delegate('th.oe-sortable[data-id]', 'click', function (e) {
|
2011-05-04 14:05:35 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
|
2011-07-13 11:18:15 +00:00
|
|
|
var $this = $(this);
|
|
|
|
self.dataset.sort($this.data('id'));
|
|
|
|
if ($this.find('span').length) {
|
|
|
|
$this.find('span').toggleClass(
|
|
|
|
'ui-icon-triangle-1-s ui-icon-triangle-1-n');
|
|
|
|
} else {
|
|
|
|
$this.append('<span class="ui-icon ui-icon-triangle-1-s">')
|
|
|
|
.siblings('.oe-sortable').find('span').remove();
|
|
|
|
}
|
2011-05-04 14:05:35 +00:00
|
|
|
|
2011-07-13 10:37:25 +00:00
|
|
|
self.reload_content();
|
2011-05-04 14:05:35 +00:00
|
|
|
});
|
2011-04-07 15:35:27 +00:00
|
|
|
|
2011-06-23 12:46:41 +00:00
|
|
|
this.$element.find('.oe-list-pager')
|
|
|
|
.delegate('button', 'click', function () {
|
|
|
|
var $this = $(this);
|
|
|
|
switch ($this.data('pager-action')) {
|
|
|
|
case 'first':
|
|
|
|
self.page = 0; break;
|
|
|
|
case 'last':
|
|
|
|
self.page = Math.floor(
|
|
|
|
self.dataset.ids.length / self.limit());
|
|
|
|
break;
|
|
|
|
case 'next':
|
|
|
|
self.page += 1; break;
|
|
|
|
case 'previous':
|
|
|
|
self.page -= 1; break;
|
|
|
|
}
|
|
|
|
self.reload_content();
|
|
|
|
}).find('.oe-pager-state')
|
|
|
|
.click(function (e) {
|
|
|
|
e.stopPropagation();
|
2011-06-23 13:49:20 +00:00
|
|
|
var $this = $(this);
|
|
|
|
|
|
|
|
var $select = $('<select>')
|
|
|
|
.appendTo($this.empty())
|
|
|
|
.click(function (e) {e.stopPropagation();})
|
|
|
|
.append('<option value="80">80</option>' +
|
|
|
|
'<option value="100">100</option>' +
|
|
|
|
'<option value="200">200</option>' +
|
|
|
|
'<option value="500">500</option>' +
|
|
|
|
'<option value="NaN">Unlimited</option>')
|
|
|
|
.change(function () {
|
|
|
|
var val = parseInt($select.val(), 10);
|
|
|
|
self._limit = (isNaN(val) ? null : val);
|
|
|
|
self.page = 0;
|
|
|
|
self.reload_content();
|
|
|
|
})
|
|
|
|
.val(self._limit || 'NaN');
|
2011-06-23 12:46:41 +00:00
|
|
|
});
|
2011-08-08 16:39:00 +00:00
|
|
|
if (!this.sidebar && this.options.sidebar && this.options.sidebar_id) {
|
2011-09-05 11:03:09 +00:00
|
|
|
this.sidebar = new openerp.web.Sidebar(this, this.options.sidebar_id);
|
2011-07-26 21:00:05 +00:00
|
|
|
this.sidebar.start();
|
2011-09-06 20:54:38 +00:00
|
|
|
this.sidebar.add_toolbar(this.fields_view.toolbar);
|
2011-07-28 15:15:45 +00:00
|
|
|
this.set_common_sidebar_sections(this.sidebar);
|
2011-07-26 21:00:05 +00:00
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-06-23 12:46:41 +00:00
|
|
|
/**
|
|
|
|
* Configures the ListView pager based on the provided dataset's information
|
|
|
|
*
|
|
|
|
* Horrifying side-effect: sets the dataset's data on this.dataset?
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.DataSet} dataset
|
2011-06-23 12:46:41 +00:00
|
|
|
*/
|
|
|
|
configure_pager: function (dataset) {
|
|
|
|
this.dataset.ids = dataset.ids;
|
|
|
|
|
|
|
|
var limit = this.limit(),
|
|
|
|
total = dataset.ids.length,
|
|
|
|
first = (this.page * limit),
|
2011-06-23 13:49:20 +00:00
|
|
|
last;
|
|
|
|
if (!limit || (total - first) < limit) {
|
|
|
|
last = total;
|
|
|
|
} else {
|
|
|
|
last = first + limit;
|
|
|
|
}
|
|
|
|
this.$element.find('span.oe-pager-state').empty().text(_.sprintf(
|
2011-06-23 12:46:41 +00:00
|
|
|
"[%d to %d] of %d", first + 1, last, total));
|
|
|
|
|
|
|
|
this.$element
|
|
|
|
.find('button[data-pager-action=first], button[data-pager-action=previous]')
|
|
|
|
.attr('disabled', this.page === 0)
|
|
|
|
.end()
|
|
|
|
.find('button[data-pager-action=last], button[data-pager-action=next]')
|
|
|
|
.attr('disabled', last === total);
|
|
|
|
},
|
2011-04-07 12:12:22 +00:00
|
|
|
/**
|
2011-05-17 14:07:26 +00:00
|
|
|
* Sets up the listview's columns: merges view and fields data, move
|
|
|
|
* grouped-by columns to the front of the columns list and make them all
|
|
|
|
* visible.
|
2011-04-07 12:12:22 +00:00
|
|
|
*
|
2011-05-17 14:07:26 +00:00
|
|
|
* @param {Object} fields fields_view_get's fields section
|
2011-05-27 07:13:09 +00:00
|
|
|
* @param {Boolean} [grouped] Should the grouping columns (group and count) be displayed
|
2011-04-07 12:12:22 +00:00
|
|
|
*/
|
2011-05-27 07:13:09 +00:00
|
|
|
setup_columns: function (fields, grouped) {
|
2011-09-05 11:03:09 +00:00
|
|
|
var domain_computer = openerp.web.form.compute_domain;
|
2011-04-12 14:08:29 +00:00
|
|
|
|
2011-05-17 14:07:26 +00:00
|
|
|
var noop = function () { return {}; };
|
2011-05-17 14:03:58 +00:00
|
|
|
var field_to_column = function (field) {
|
|
|
|
var name = field.attrs.name;
|
|
|
|
var column = _.extend({id: name, tag: field.tag},
|
|
|
|
field.attrs, fields[name]);
|
2011-07-07 10:37:40 +00:00
|
|
|
// modifiers computer
|
|
|
|
if (column.modifiers) {
|
|
|
|
var modifiers = JSON.parse(column.modifiers);
|
|
|
|
column.modifiers_for = function (fields) {
|
2011-07-12 09:54:59 +00:00
|
|
|
if (!modifiers.invisible) {
|
|
|
|
return {};
|
2011-05-17 14:03:58 +00:00
|
|
|
}
|
2011-07-12 09:54:59 +00:00
|
|
|
return {
|
|
|
|
'invisible': domain_computer(modifiers.invisible, fields)
|
|
|
|
};
|
2011-05-17 14:03:58 +00:00
|
|
|
};
|
2011-07-12 14:34:08 +00:00
|
|
|
if (modifiers['tree_invisible']) {
|
|
|
|
column.invisible = '1';
|
|
|
|
}
|
2011-05-17 14:03:58 +00:00
|
|
|
} else {
|
2011-07-07 10:37:40 +00:00
|
|
|
column.modifiers_for = noop;
|
2011-05-17 14:03:58 +00:00
|
|
|
}
|
|
|
|
return column;
|
|
|
|
};
|
2011-06-22 15:36:26 +00:00
|
|
|
|
2011-05-17 14:03:58 +00:00
|
|
|
this.columns.splice(0, this.columns.length);
|
|
|
|
this.columns.push.apply(
|
|
|
|
this.columns,
|
|
|
|
_(this.fields_view.arch.children).map(field_to_column));
|
2011-05-27 07:13:09 +00:00
|
|
|
if (grouped) {
|
2011-05-24 06:52:05 +00:00
|
|
|
this.columns.unshift({
|
|
|
|
id: '_group', tag: '', string: "Group", meta: true,
|
2011-07-07 10:37:40 +00:00
|
|
|
modifiers_for: function () { return {}; }
|
2011-05-24 06:52:05 +00:00
|
|
|
}, {
|
|
|
|
id: '_count', tag: '', string: '#', meta: true,
|
2011-07-07 10:37:40 +00:00
|
|
|
modifiers_for: function () { return {}; }
|
2011-05-24 06:52:05 +00:00
|
|
|
});
|
|
|
|
}
|
2011-04-07 12:12:22 +00:00
|
|
|
|
2011-05-17 14:03:58 +00:00
|
|
|
this.visible_columns = _.filter(this.columns, function (column) {
|
|
|
|
return column.invisible !== '1';
|
|
|
|
});
|
2011-05-25 13:04:14 +00:00
|
|
|
|
2011-06-07 11:36:29 +00:00
|
|
|
this.aggregate_columns = _(this.visible_columns)
|
2011-05-25 13:04:14 +00:00
|
|
|
.map(function (column) {
|
2011-06-07 14:42:15 +00:00
|
|
|
if (column.type !== 'integer' && column.type !== 'float') {
|
2011-06-07 11:36:29 +00:00
|
|
|
return {};
|
|
|
|
}
|
2011-06-07 14:42:15 +00:00
|
|
|
var aggregation_func = column['group_operator'] || 'sum';
|
|
|
|
|
2011-07-25 10:29:49 +00:00
|
|
|
return _.extend({}, column, {
|
2011-06-07 14:42:15 +00:00
|
|
|
'function': aggregation_func,
|
|
|
|
label: column[aggregation_func]
|
2011-07-25 10:29:49 +00:00
|
|
|
});
|
2011-06-07 11:36:29 +00:00
|
|
|
});
|
2011-03-31 14:44:22 +00:00
|
|
|
},
|
2011-04-12 13:02:29 +00:00
|
|
|
/**
|
2011-04-21 11:59:24 +00:00
|
|
|
* Used to handle a click on a table row, if no other handler caught the
|
|
|
|
* event.
|
|
|
|
*
|
|
|
|
* The default implementation asks the list view's view manager to switch
|
|
|
|
* to a different view (by calling
|
2011-09-05 11:03:09 +00:00
|
|
|
* :js:func:`~openerp.web.ViewManager.on_mode_switch`), using the
|
2011-04-21 11:59:24 +00:00
|
|
|
* provided record index (within the current list view's dataset).
|
2011-04-12 13:02:29 +00:00
|
|
|
*
|
|
|
|
* If the index is null, ``switch_to_record`` asks for the creation of a
|
|
|
|
* new record.
|
|
|
|
*
|
2011-06-29 13:58:49 +00:00
|
|
|
* @param {Number|void} index the record index (in the current dataset) to switch to
|
2011-04-21 11:59:24 +00:00
|
|
|
* @param {String} [view="form"] the view type to switch to
|
2011-04-12 13:02:29 +00:00
|
|
|
*/
|
2011-05-12 09:57:11 +00:00
|
|
|
select_record:function (index, view) {
|
2011-04-12 13:02:29 +00:00
|
|
|
view = view || 'form';
|
|
|
|
this.dataset.index = index;
|
|
|
|
_.delay(_.bind(function () {
|
2011-08-08 14:41:20 +00:00
|
|
|
this.do_switch_view(view);
|
2011-04-12 13:02:29 +00:00
|
|
|
}, this));
|
|
|
|
},
|
2011-04-04 15:34:26 +00:00
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
2011-07-26 21:00:05 +00:00
|
|
|
if (this.sidebar) {
|
|
|
|
this.sidebar.$element.show();
|
|
|
|
}
|
2011-09-12 13:31:15 +00:00
|
|
|
if (!_(this.dataset.ids).isEmpty()) {
|
2011-08-19 10:12:08 +00:00
|
|
|
this.reload_content();
|
2011-04-12 14:08:29 +00:00
|
|
|
}
|
2011-04-04 15:34:26 +00:00
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
2011-07-26 21:00:05 +00:00
|
|
|
if (this.sidebar) {
|
|
|
|
this.sidebar.$element.hide();
|
|
|
|
}
|
2011-04-12 14:08:29 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-05-27 07:13:09 +00:00
|
|
|
* Reloads the list view based on the current settings (dataset & al)
|
2011-05-17 14:03:58 +00:00
|
|
|
*
|
2011-05-27 07:13:09 +00:00
|
|
|
* @param {Boolean} [grouped] Should the list be displayed grouped
|
2011-08-11 14:44:07 +00:00
|
|
|
* @param {Object} [context] context to send the server while loading the view
|
2011-04-12 14:08:29 +00:00
|
|
|
*/
|
2011-08-25 08:18:33 +00:00
|
|
|
reload_view: function (grouped, context, initial) {
|
|
|
|
var self = this;
|
|
|
|
var callback = function (field_view_get) {
|
2011-06-23 12:46:41 +00:00
|
|
|
self.on_loaded(field_view_get, grouped);
|
2011-08-25 08:18:33 +00:00
|
|
|
};
|
2011-06-06 07:04:51 +00:00
|
|
|
if (this.embedded_view) {
|
2011-09-07 06:44:24 +00:00
|
|
|
return $.Deferred().then(callback).resolve(this.embedded_view);
|
2011-06-01 08:23:58 +00:00
|
|
|
} else {
|
2011-09-05 11:03:09 +00:00
|
|
|
return this.rpc('/web/listview/load', {
|
2011-06-01 08:23:58 +00:00
|
|
|
model: this.model,
|
|
|
|
view_id: this.view_id,
|
2011-09-07 06:34:57 +00:00
|
|
|
view_type: "tree",
|
2011-08-11 14:44:07 +00:00
|
|
|
context: this.dataset.get_context(context),
|
2011-07-26 21:00:05 +00:00
|
|
|
toolbar: this.options.sidebar
|
2011-08-25 08:18:33 +00:00
|
|
|
}, callback);
|
2011-06-01 08:23:58 +00:00
|
|
|
}
|
2011-04-04 15:34:26 +00:00
|
|
|
},
|
2011-05-27 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* re-renders the content of the list view
|
|
|
|
*/
|
|
|
|
reload_content: function () {
|
2011-08-25 14:57:41 +00:00
|
|
|
var self = this;
|
2011-08-18 12:21:06 +00:00
|
|
|
this.records.reset();
|
2011-07-15 12:50:44 +00:00
|
|
|
this.$element.find('.oe-listview-content').append(
|
2011-08-25 14:57:41 +00:00
|
|
|
this.groups.render(function () {
|
|
|
|
if (self.dataset.index == null) {
|
|
|
|
var has_one = false;
|
|
|
|
self.records.each(function () { has_one = true; });
|
|
|
|
if (has_one) {
|
|
|
|
self.dataset.index = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.compute_aggregates();
|
|
|
|
}));
|
2011-05-27 15:10:00 +00:00
|
|
|
},
|
2011-04-21 11:59:24 +00:00
|
|
|
/**
|
|
|
|
* Event handler for a search, asks for the computation/folding of domains
|
|
|
|
* and contexts (and group-by), then reloads the view's content.
|
|
|
|
*
|
|
|
|
* @param {Array} domains a sequence of literal and non-literal domains
|
|
|
|
* @param {Array} contexts a sequence of literal and non-literal contexts
|
|
|
|
* @param {Array} groupbys a sequence of literal and non-literal group-by contexts
|
|
|
|
* @returns {$.Deferred} fold request evaluation promise
|
|
|
|
*/
|
2011-03-31 14:44:22 +00:00
|
|
|
do_search: function (domains, contexts, groupbys) {
|
2011-09-05 11:03:09 +00:00
|
|
|
return this.rpc('/web/session/eval_domain_and_context', {
|
2011-08-25 13:24:56 +00:00
|
|
|
domains: [this.dataset.get_domain()].concat(domains),
|
|
|
|
contexts: [this.dataset.get_context()].concat(contexts),
|
2011-03-31 14:44:22 +00:00
|
|
|
group_by_seq: groupbys
|
2011-06-03 09:36:06 +00:00
|
|
|
}, $.proxy(this, 'do_actual_search'));
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Handler for the result of eval_domain_and_context, actually perform the
|
|
|
|
* searching
|
|
|
|
*
|
|
|
|
* @param {Object} results results of evaluating domain and process for a search
|
|
|
|
*/
|
|
|
|
do_actual_search: function (results) {
|
2011-09-05 11:03:09 +00:00
|
|
|
this.groups.datagroup = new openerp.web.DataGroup(
|
2011-07-18 12:54:22 +00:00
|
|
|
this, this.model,
|
2011-08-22 14:33:58 +00:00
|
|
|
results.domain,
|
|
|
|
results.context,
|
2011-06-03 09:36:06 +00:00
|
|
|
results.group_by);
|
2011-07-13 10:37:25 +00:00
|
|
|
this.groups.datagroup.sort = this.dataset._sort;
|
2011-05-24 13:27:29 +00:00
|
|
|
|
2011-06-03 09:36:06 +00:00
|
|
|
if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
|
|
|
|
results.group_by = null;
|
|
|
|
}
|
2011-05-31 11:23:39 +00:00
|
|
|
|
2011-08-11 14:44:07 +00:00
|
|
|
this.reload_view(!!results.group_by, results.context).then(
|
2011-06-03 09:36:06 +00:00
|
|
|
$.proxy(this, 'reload_content'));
|
2011-04-05 10:38:35 +00:00
|
|
|
},
|
2011-04-07 15:13:19 +00:00
|
|
|
/**
|
2011-07-07 13:48:43 +00:00
|
|
|
* Handles the signal to delete lines from the records list
|
2011-04-07 15:13:19 +00:00
|
|
|
*
|
2011-07-07 13:48:43 +00:00
|
|
|
* @param {Array} ids the ids of the records to delete
|
2011-04-07 15:13:19 +00:00
|
|
|
*/
|
2011-05-06 14:07:15 +00:00
|
|
|
do_delete: function (ids) {
|
|
|
|
if (!ids.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
return $.when(this.dataset.unlink(ids)).then(function () {
|
2011-08-19 11:59:07 +00:00
|
|
|
_(ids).each(function (id) {
|
|
|
|
self.records.remove(self.records.get(id));
|
|
|
|
});
|
2011-07-25 09:15:06 +00:00
|
|
|
self.compute_aggregates();
|
2011-05-06 14:07:15 +00:00
|
|
|
});
|
2011-04-07 15:13:19 +00:00
|
|
|
},
|
2011-05-25 09:28:13 +00:00
|
|
|
/**
|
|
|
|
* Handles the signal indicating that a new record has been selected
|
|
|
|
*
|
|
|
|
* @param {Array} ids selected record ids
|
|
|
|
* @param {Array} records selected record values
|
|
|
|
*/
|
|
|
|
do_select: function (ids, records) {
|
2011-06-23 10:54:04 +00:00
|
|
|
this.$element.find('.oe-list-delete')
|
2011-06-07 14:02:48 +00:00
|
|
|
.attr('disabled', !ids.length);
|
2011-05-25 13:04:14 +00:00
|
|
|
|
|
|
|
if (!records.length) {
|
|
|
|
this.compute_aggregates();
|
|
|
|
return;
|
|
|
|
}
|
2011-06-08 12:55:31 +00:00
|
|
|
this.compute_aggregates(_(records).map(function (record) {
|
|
|
|
return {count: 1, values: record};
|
|
|
|
}));
|
2011-05-25 09:28:13 +00:00
|
|
|
},
|
2011-05-27 07:44:42 +00:00
|
|
|
/**
|
|
|
|
* Handles action button signals on a record
|
|
|
|
*
|
|
|
|
* @param {String} name action name
|
|
|
|
* @param {Object} id id of the record the action should be called on
|
|
|
|
* @param {Function} callback should be called after the action is executed, if non-null
|
|
|
|
*/
|
2011-08-11 02:00:14 +00:00
|
|
|
do_button_action: function (name, id, callback) {
|
2011-08-19 10:39:55 +00:00
|
|
|
var action = _.detect(this.columns, function (field) {
|
2011-05-27 07:44:42 +00:00
|
|
|
return field.name === name;
|
|
|
|
});
|
|
|
|
if (!action) { return; }
|
2011-08-19 10:39:55 +00:00
|
|
|
this.execute_action(action, this.dataset, id, callback);
|
2011-05-27 07:44:42 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Handles the activation of a record (clicking on it)
|
|
|
|
*
|
|
|
|
* @param {Number} index index of the record in the dataset
|
|
|
|
* @param {Object} id identifier of the activated record
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.DataSet} dataset dataset in which the record is available (may not be the listview's dataset in case of nested groups)
|
2011-05-27 07:44:42 +00:00
|
|
|
*/
|
|
|
|
do_activate_record: function (index, id, dataset) {
|
|
|
|
var self = this;
|
2011-08-18 22:57:21 +00:00
|
|
|
// TODO is it needed ?
|
|
|
|
this.dataset.read_slice([],{
|
2011-08-11 08:28:18 +00:00
|
|
|
context: dataset.get_context(),
|
|
|
|
domain: dataset.get_domain()
|
|
|
|
}, function () {
|
|
|
|
self.select_record(index);
|
2011-05-27 07:44:42 +00:00
|
|
|
});
|
|
|
|
},
|
2011-04-12 13:02:29 +00:00
|
|
|
/**
|
|
|
|
* Handles signal for the addition of a new record (can be a creation,
|
|
|
|
* can be the addition from a remote source, ...)
|
|
|
|
*
|
|
|
|
* The default implementation is to switch to a new record on the form view
|
|
|
|
*/
|
|
|
|
do_add_record: function () {
|
2011-05-12 09:57:11 +00:00
|
|
|
this.select_record(null);
|
2011-04-12 13:02:29 +00:00
|
|
|
},
|
2011-04-07 15:35:27 +00:00
|
|
|
/**
|
|
|
|
* Handles deletion of all selected lines
|
|
|
|
*/
|
|
|
|
do_delete_selected: function () {
|
2011-05-25 09:28:13 +00:00
|
|
|
this.do_delete(this.groups.get_selection().ids);
|
2011-05-25 13:04:14 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Computes the aggregates for the current list view, either on the
|
|
|
|
* records provided or on the records of the internal
|
2011-09-05 11:03:09 +00:00
|
|
|
* :js:class:`~openerp.web.ListView.Group`, by calling
|
|
|
|
* :js:func:`~openerp.web.ListView.group.get_records`.
|
2011-05-25 13:04:14 +00:00
|
|
|
*
|
|
|
|
* Then displays the aggregates in the table through
|
2011-09-05 11:03:09 +00:00
|
|
|
* :js:method:`~openerp.web.ListView.display_aggregates`.
|
2011-05-25 13:04:14 +00:00
|
|
|
*
|
|
|
|
* @param {Array} [records]
|
|
|
|
*/
|
|
|
|
compute_aggregates: function (records) {
|
2011-06-08 12:15:45 +00:00
|
|
|
var columns = _(this.aggregate_columns).filter(function (column) {
|
|
|
|
return column['function']; });
|
|
|
|
if (_.isEmpty(columns)) { return; }
|
|
|
|
|
2011-05-25 13:04:14 +00:00
|
|
|
if (_.isEmpty(records)) {
|
|
|
|
records = this.groups.get_records();
|
|
|
|
}
|
|
|
|
|
2011-06-08 12:15:45 +00:00
|
|
|
var count = 0, sums = {};
|
2011-07-25 10:46:30 +00:00
|
|
|
_(columns).each(function (column) {
|
|
|
|
switch (column['function']) {
|
|
|
|
case 'max':
|
|
|
|
sums[column.id] = -Infinity;
|
|
|
|
break;
|
|
|
|
case 'min':
|
|
|
|
sums[column.id] = Infinity;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sums[column.id] = 0;
|
|
|
|
}
|
|
|
|
});
|
2011-06-08 12:15:45 +00:00
|
|
|
_(records).each(function (record) {
|
2011-06-08 12:55:31 +00:00
|
|
|
count += record.count || 1;
|
2011-06-08 12:15:45 +00:00
|
|
|
_(columns).each(function (column) {
|
2011-07-25 10:46:30 +00:00
|
|
|
var field = column.id,
|
2011-08-18 12:21:06 +00:00
|
|
|
value = record.values[field];
|
2011-06-08 12:55:31 +00:00
|
|
|
switch (column['function']) {
|
|
|
|
case 'sum':
|
2011-07-25 10:46:30 +00:00
|
|
|
sums[field] += value;
|
2011-06-08 12:55:31 +00:00
|
|
|
break;
|
|
|
|
case 'avg':
|
2011-07-25 10:46:30 +00:00
|
|
|
sums[field] += record.count * value;
|
|
|
|
break;
|
|
|
|
case 'min':
|
|
|
|
if (sums[field] > value) {
|
|
|
|
sums[field] = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'max':
|
|
|
|
if (sums[field] < value) {
|
|
|
|
sums[field] = value;
|
|
|
|
}
|
2011-06-08 12:55:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-05-25 13:04:14 +00:00
|
|
|
});
|
2011-06-08 12:15:45 +00:00
|
|
|
});
|
2011-05-25 13:04:14 +00:00
|
|
|
|
2011-06-08 12:15:45 +00:00
|
|
|
var aggregates = {};
|
|
|
|
_(columns).each(function (column) {
|
2011-07-25 10:29:49 +00:00
|
|
|
var field = column.id;
|
2011-06-08 12:15:45 +00:00
|
|
|
switch (column['function']) {
|
|
|
|
case 'avg':
|
2011-07-25 10:29:49 +00:00
|
|
|
aggregates[field] = {value: sums[field] / count};
|
2011-06-08 12:15:45 +00:00
|
|
|
break;
|
2011-07-25 10:46:30 +00:00
|
|
|
default:
|
|
|
|
aggregates[field] = {value: sums[field]};
|
2011-06-08 12:15:45 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-25 13:04:14 +00:00
|
|
|
|
2011-06-08 12:15:45 +00:00
|
|
|
this.display_aggregates(aggregates);
|
2011-05-25 13:04:14 +00:00
|
|
|
},
|
|
|
|
display_aggregates: function (aggregation) {
|
2011-06-07 11:36:29 +00:00
|
|
|
var $footer_cells = this.$element.find('.oe-list-footer');
|
2011-05-25 14:35:16 +00:00
|
|
|
_(this.aggregate_columns).each(function (column) {
|
2011-06-07 11:36:29 +00:00
|
|
|
if (!column['function']) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-25 10:29:49 +00:00
|
|
|
|
|
|
|
$footer_cells.filter(_.sprintf('[data-field=%s]', column.id))
|
2011-09-05 11:03:09 +00:00
|
|
|
.html(openerp.web.format_cell(aggregation, column));
|
2011-05-25 14:35:16 +00:00
|
|
|
});
|
2011-08-26 13:03:35 +00:00
|
|
|
},
|
|
|
|
get_selected_ids: function() {
|
2011-08-26 13:28:35 +00:00
|
|
|
var ids = this.groups.get_selection().ids;
|
|
|
|
return ids;
|
2011-09-08 12:55:10 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Adds padding columns at the start or end of all table rows (including
|
|
|
|
* field names row)
|
|
|
|
*
|
|
|
|
* @param {Number} count number of columns to add
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {"before"|"after"} [position="after"] insertion position for the new columns
|
|
|
|
* @param {Object} [except] content row to not pad
|
|
|
|
*/
|
|
|
|
pad_columns: function (count, options) {
|
|
|
|
options = options || {};
|
|
|
|
// padding for action/pager header
|
|
|
|
var $first_header = this.$element.find('thead tr:first th');
|
|
|
|
var colspan = $first_header.attr('colspan');
|
|
|
|
if (colspan) {
|
|
|
|
if (!this.previous_colspan) {
|
|
|
|
this.previous_colspan = colspan;
|
|
|
|
}
|
|
|
|
$first_header.attr('colspan', parseInt(colspan, 10) + count);
|
|
|
|
}
|
|
|
|
// Padding for column titles, footer and data rows
|
|
|
|
var $rows = this.$element
|
|
|
|
.find('.oe-listview-header-columns, tr:not(thead tr)')
|
|
|
|
.not(options['except']);
|
|
|
|
var newcols = new Array(count+1).join('<td class="oe-listview-padding"></td>');
|
|
|
|
if (options.position === 'before') {
|
|
|
|
$rows.prepend(newcols);
|
|
|
|
} else {
|
|
|
|
$rows.append(newcols);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes all padding columns of the table
|
|
|
|
*/
|
|
|
|
unpad_columns: function () {
|
|
|
|
this.$element.find('.oe-listview-padding').remove();
|
|
|
|
if (this.previous_colspan) {
|
|
|
|
this.$element
|
|
|
|
.find('thead tr:first th')
|
|
|
|
.attr('colspan', this.previous_colspan);
|
|
|
|
this.previous_colspan = null;
|
|
|
|
}
|
2011-05-06 12:28:37 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.ListView.List = openerp.web.Class.extend( /** @lends openerp.web.ListView.List# */{
|
2011-05-09 06:54:57 +00:00
|
|
|
/**
|
|
|
|
* List display for the ListView, handles basic DOM events and transforms
|
|
|
|
* them in the relevant higher-level events, to which the list view (or
|
|
|
|
* other consumers) can subscribe.
|
|
|
|
*
|
|
|
|
* Events on this object are registered via jQuery.
|
|
|
|
*
|
|
|
|
* Available events:
|
|
|
|
*
|
|
|
|
* `selected`
|
|
|
|
* Triggered when a row is selected (using check boxes), provides an
|
|
|
|
* array of ids of all the selected records.
|
|
|
|
* `deleted`
|
|
|
|
* Triggered when deletion buttons are hit, provide an array of ids of
|
|
|
|
* all the records being marked for suppression.
|
2011-05-09 08:10:22 +00:00
|
|
|
* `action`
|
|
|
|
* Triggered when an action button is clicked, provides two parameters:
|
|
|
|
*
|
|
|
|
* * The name of the action to execute (as a string)
|
|
|
|
* * The id of the record to execute the action on
|
2011-05-09 08:22:12 +00:00
|
|
|
* `row_link`
|
|
|
|
* Triggered when a row of the table is clicked, provides the index (in
|
|
|
|
* the rows array) and id of the selected record to the handle function.
|
2011-05-09 06:54:57 +00:00
|
|
|
*
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs openerp.web.ListView.List
|
|
|
|
* @extends openerp.web.Class
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {Object} opts display options, identical to those of :js:class:`openerp.web.ListView`
|
2011-05-09 06:54:57 +00:00
|
|
|
*/
|
2011-06-01 12:49:54 +00:00
|
|
|
init: function (group, opts) {
|
2011-05-06 12:28:37 +00:00
|
|
|
var self = this;
|
2011-06-01 12:49:54 +00:00
|
|
|
this.group = group;
|
2011-06-22 15:36:26 +00:00
|
|
|
this.view = group.view;
|
2011-08-03 12:06:07 +00:00
|
|
|
this.session = this.view.session;
|
2011-05-06 12:28:37 +00:00
|
|
|
|
|
|
|
this.options = opts.options;
|
|
|
|
this.columns = opts.columns;
|
2011-05-20 14:20:41 +00:00
|
|
|
this.dataset = opts.dataset;
|
2011-08-17 13:16:35 +00:00
|
|
|
this.records = opts.records;
|
2011-05-06 12:28:37 +00:00
|
|
|
|
2011-08-19 15:15:10 +00:00
|
|
|
this.record_callbacks = {
|
|
|
|
'remove': function (event, record) {
|
2011-08-22 10:50:28 +00:00
|
|
|
var $row = self.$current.find(
|
|
|
|
'[data-id=' + record.get('id') + ']');
|
2011-08-19 15:15:10 +00:00
|
|
|
var index = $row.data('index');
|
|
|
|
$row.remove();
|
2011-08-22 10:50:28 +00:00
|
|
|
self.refresh_zebra(index);
|
2011-08-19 15:15:10 +00:00
|
|
|
},
|
|
|
|
'reset': $.proxy(this, 'on_records_reset'),
|
|
|
|
'change': function (event, record) {
|
|
|
|
var $row = self.$current.find('[data-id=' + record.get('id') + ']');
|
2011-08-24 08:53:03 +00:00
|
|
|
$row.replaceWith(self.render_record(record));
|
2011-08-22 09:04:58 +00:00
|
|
|
},
|
|
|
|
'add': function (ev, records, record, index) {
|
2011-08-24 12:22:51 +00:00
|
|
|
var $new_row = $('<tr>').attr({
|
2011-08-22 10:50:28 +00:00
|
|
|
'data-id': record.get('id')
|
2011-08-24 12:22:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
$new_row.prependTo(self.$current);
|
|
|
|
} else {
|
|
|
|
var previous_record = records.at(index-1),
|
|
|
|
$previous_sibling = self.$current.find(
|
|
|
|
'[data-id=' + previous_record.get('id') + ']');
|
|
|
|
$new_row.insertAfter($previous_sibling);
|
|
|
|
}
|
|
|
|
|
2011-08-22 10:50:28 +00:00
|
|
|
self.refresh_zebra(index, 1);
|
2011-08-19 15:15:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
_(this.record_callbacks).each(function (callback, event) {
|
|
|
|
this.records.bind(event, callback);
|
|
|
|
}, this);
|
2011-08-19 10:12:08 +00:00
|
|
|
|
2011-05-06 12:28:37 +00:00
|
|
|
this.$_element = $('<tbody class="ui-widget-content">')
|
|
|
|
.appendTo(document.body)
|
|
|
|
.delegate('th.oe-record-selector', 'click', function (e) {
|
2011-05-06 14:07:15 +00:00
|
|
|
e.stopPropagation();
|
2011-05-25 09:28:13 +00:00
|
|
|
var selection = self.get_selection();
|
2011-05-27 09:28:11 +00:00
|
|
|
$(self).trigger(
|
|
|
|
'selected', [selection.ids, selection.records]);
|
2011-05-06 14:07:15 +00:00
|
|
|
})
|
|
|
|
.delegate('td.oe-record-delete button', 'click', function (e) {
|
|
|
|
e.stopPropagation();
|
2011-05-09 08:10:22 +00:00
|
|
|
var $row = $(e.target).closest('tr');
|
|
|
|
$(self).trigger('deleted', [[self.row_id($row)]]);
|
|
|
|
})
|
|
|
|
.delegate('td.oe-field-cell button', 'click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
var $target = $(e.currentTarget),
|
|
|
|
field = $target.closest('td').data('field'),
|
2011-06-03 14:37:23 +00:00
|
|
|
$row = $target.closest('tr'),
|
2011-08-23 14:13:00 +00:00
|
|
|
record_id = self.row_id($row);
|
2011-05-09 08:10:22 +00:00
|
|
|
|
2011-06-03 14:37:23 +00:00
|
|
|
$(self).trigger('action', [field, record_id, function () {
|
2011-08-23 14:13:00 +00:00
|
|
|
return self.reload_record(self.records.get(record_id));
|
2011-06-03 14:37:23 +00:00
|
|
|
}]);
|
2011-05-09 08:22:12 +00:00
|
|
|
})
|
|
|
|
.delegate('tr', 'click', function (e) {
|
|
|
|
e.stopPropagation();
|
2011-08-24 08:53:03 +00:00
|
|
|
self.dataset.index = self.records.indexOf(
|
|
|
|
self.records.get(
|
|
|
|
self.row_id(e.currentTarget)));
|
2011-06-06 11:42:04 +00:00
|
|
|
self.row_clicked(e);
|
2011-05-06 12:28:37 +00:00
|
|
|
});
|
|
|
|
},
|
2011-06-06 11:42:04 +00:00
|
|
|
row_clicked: function () {
|
2011-06-03 09:36:06 +00:00
|
|
|
$(this).trigger(
|
|
|
|
'row_link',
|
2011-08-17 13:16:35 +00:00
|
|
|
[this.records.at(this.dataset.index).get('id'),
|
2011-06-03 09:36:06 +00:00
|
|
|
this.dataset]);
|
|
|
|
},
|
2011-05-06 12:28:37 +00:00
|
|
|
render: function () {
|
2011-05-17 16:09:48 +00:00
|
|
|
if (this.$current) {
|
|
|
|
this.$current.remove();
|
|
|
|
}
|
|
|
|
this.$current = this.$_element.clone(true);
|
2011-07-08 14:06:55 +00:00
|
|
|
this.$current.empty().append(
|
|
|
|
QWeb.render('ListView.rows', _.extend({
|
2011-09-05 11:03:09 +00:00
|
|
|
render_cell: openerp.web.format_cell}, this)));
|
2011-04-07 15:35:27 +00:00
|
|
|
},
|
2011-04-07 12:46:39 +00:00
|
|
|
/**
|
|
|
|
* Gets the ids of all currently selected records, if any
|
2011-05-25 09:28:13 +00:00
|
|
|
* @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
|
2011-04-07 12:46:39 +00:00
|
|
|
*/
|
|
|
|
get_selection: function () {
|
|
|
|
if (!this.options.selectable) {
|
|
|
|
return [];
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
var records = this.records;
|
2011-05-25 09:28:13 +00:00
|
|
|
var result = {ids: [], records: []};
|
|
|
|
this.$current.find('th.oe-record-selector input:checked')
|
|
|
|
.closest('tr').each(function () {
|
2011-08-19 13:21:12 +00:00
|
|
|
var record = records.get($(this).data('id'));
|
2011-08-17 13:16:35 +00:00
|
|
|
result.ids.push(record.get('id'));
|
2011-08-18 12:21:06 +00:00
|
|
|
result.records.push(record.attributes);
|
2011-05-25 09:28:13 +00:00
|
|
|
});
|
|
|
|
return result;
|
2011-05-06 14:07:15 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Returns the identifier of the object displayed in the provided table
|
|
|
|
* row
|
|
|
|
*
|
|
|
|
* @param {Object} row the selected table row
|
|
|
|
* @returns {Number|String} the identifier of the row's object
|
|
|
|
*/
|
|
|
|
row_id: function (row) {
|
2011-08-19 10:39:55 +00:00
|
|
|
return $(row).data('id');
|
2011-05-19 09:22:22 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-08-19 10:12:08 +00:00
|
|
|
* Death signal, cleans up list display
|
2011-05-19 09:22:22 +00:00
|
|
|
*/
|
2011-08-19 15:15:10 +00:00
|
|
|
on_records_reset: function () {
|
|
|
|
_(this.record_callbacks).each(function (callback, event) {
|
|
|
|
this.records.unbind(event, callback);
|
|
|
|
}, this);
|
2011-05-20 13:30:45 +00:00
|
|
|
if (!this.$current) { return; }
|
2011-05-19 09:22:22 +00:00
|
|
|
this.$current.remove();
|
|
|
|
this.$current = null;
|
2011-05-25 15:02:40 +00:00
|
|
|
this.$_element.remove();
|
2011-05-25 13:04:14 +00:00
|
|
|
},
|
|
|
|
get_records: function () {
|
2011-08-18 12:21:06 +00:00
|
|
|
return this.records.map(function (record) {
|
|
|
|
return {count: 1, values: record.attributes};
|
|
|
|
});
|
2011-06-03 13:24:38 +00:00
|
|
|
},
|
2011-08-23 14:13:00 +00:00
|
|
|
/**
|
|
|
|
* Reloads the provided record by re-reading its content from the server.
|
|
|
|
*
|
|
|
|
* @param {Record} record
|
|
|
|
* @returns {$.Deferred} promise to the finalization of the reloading
|
|
|
|
*/
|
|
|
|
reload_record: function (record) {
|
2011-08-19 10:39:55 +00:00
|
|
|
return this.dataset.read_ids(
|
2011-08-23 14:13:00 +00:00
|
|
|
[record.get('id')],
|
2011-08-19 10:39:55 +00:00
|
|
|
_.pluck(_(this.columns).filter(function (r) {
|
|
|
|
return r.tag === 'field';
|
|
|
|
}), 'name'),
|
2011-08-23 14:13:00 +00:00
|
|
|
function (records) {
|
|
|
|
_(records[0]).each(function (value, key) {
|
|
|
|
record.set(key, value, {silent: true});
|
2011-08-19 10:39:55 +00:00
|
|
|
});
|
2011-08-23 14:13:00 +00:00
|
|
|
record.trigger('change', record);
|
2011-08-19 10:39:55 +00:00
|
|
|
}
|
|
|
|
);
|
2011-06-03 13:24:38 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Renders a list record to HTML
|
|
|
|
*
|
2011-08-24 08:53:03 +00:00
|
|
|
* @param {Record} record index of the record to render in ``this.rows``
|
2011-06-03 13:24:38 +00:00
|
|
|
* @returns {String} QWeb rendering of the selected record
|
|
|
|
*/
|
2011-08-24 08:53:03 +00:00
|
|
|
render_record: function (record) {
|
|
|
|
var index = this.records.indexOf(record);
|
2011-06-03 13:24:38 +00:00
|
|
|
return QWeb.render('ListView.row', {
|
|
|
|
columns: this.columns,
|
|
|
|
options: this.options,
|
2011-08-24 08:53:03 +00:00
|
|
|
record: record,
|
|
|
|
row_parity: (index % 2 === 0) ? 'even' : 'odd',
|
2011-09-05 11:03:09 +00:00
|
|
|
render_cell: openerp.web.format_cell
|
2011-06-03 13:24:38 +00:00
|
|
|
});
|
2011-08-22 10:50:28 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-08-24 08:53:03 +00:00
|
|
|
* Fixes fixes the even/odd classes
|
2011-08-22 10:50:28 +00:00
|
|
|
*
|
|
|
|
* @param {Number} [from_index] index from which to resequence
|
|
|
|
* @param {Number} [offset = 0] selection offset for DOM, in case there are rows to ignore in the table
|
|
|
|
*/
|
|
|
|
refresh_zebra: function (from_index, offset) {
|
|
|
|
offset = offset || 0;
|
|
|
|
from_index = from_index || 0;
|
|
|
|
var dom_offset = offset + from_index;
|
|
|
|
var sel = dom_offset ? ':gt(' + (dom_offset - 1) + ')' : null;
|
|
|
|
this.$current.children(sel).each(function (i, e) {
|
|
|
|
var index = from_index + i;
|
|
|
|
// reset record-index accelerators on rows and even/odd
|
|
|
|
var even = index%2 === 0;
|
2011-08-24 08:53:03 +00:00
|
|
|
$(e).toggleClass('even', even)
|
2011-08-22 10:50:28 +00:00
|
|
|
.toggleClass('odd', !even);
|
|
|
|
});
|
2011-03-30 14:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.ListView.Groups = openerp.web.Class.extend( /** @lends openerp.web.ListView.Groups# */{
|
2011-06-07 10:30:24 +00:00
|
|
|
passtrough_events: 'action deleted row_link',
|
2011-05-10 11:07:23 +00:00
|
|
|
/**
|
|
|
|
* Grouped display for the ListView. Handles basic DOM events and interacts
|
2011-09-05 11:03:09 +00:00
|
|
|
* with the :js:class:`~openerp.web.DataGroup` bound to it.
|
2011-05-10 11:07:23 +00:00
|
|
|
*
|
|
|
|
* Provides events similar to those of
|
2011-09-05 11:03:09 +00:00
|
|
|
* :js:class:`~openerp.web.ListView.List`
|
2011-08-17 13:16:35 +00:00
|
|
|
*
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs openerp.web.ListView.Groups
|
|
|
|
* @extends openerp.web.Class
|
|
|
|
*
|
2011-09-05 11:03:09 +00:00
|
|
|
* @param {openerp.web.ListView} view
|
2011-08-17 13:16:35 +00:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Collection} [options.records]
|
|
|
|
* @param {Object} [options.options]
|
|
|
|
* @param {Array} [options.columns]
|
2011-05-10 11:07:23 +00:00
|
|
|
*/
|
2011-08-17 13:16:35 +00:00
|
|
|
init: function (view, options) {
|
|
|
|
options = options || {};
|
2011-05-18 09:06:30 +00:00
|
|
|
this.view = view;
|
2011-08-17 13:16:35 +00:00
|
|
|
this.records = options.records || view.records;
|
|
|
|
this.options = options.options || view.options;
|
|
|
|
this.columns = options.columns || view.columns;
|
2011-05-27 07:13:09 +00:00
|
|
|
this.datagroup = null;
|
2011-05-18 14:35:17 +00:00
|
|
|
|
2011-06-24 11:13:39 +00:00
|
|
|
this.$row = null;
|
2011-05-19 09:22:22 +00:00
|
|
|
this.children = {};
|
2011-06-24 13:07:13 +00:00
|
|
|
|
|
|
|
this.page = 0;
|
2011-08-19 10:12:08 +00:00
|
|
|
|
|
|
|
this.records.bind('reset', $.proxy(this, 'on_records_reset'));
|
2011-05-17 16:09:48 +00:00
|
|
|
},
|
|
|
|
make_fragment: function () {
|
2011-05-19 09:22:22 +00:00
|
|
|
return document.createDocumentFragment();
|
2011-05-17 16:09:48 +00:00
|
|
|
},
|
2011-05-19 09:22:22 +00:00
|
|
|
/**
|
|
|
|
* Returns a DOM node after which a new tbody can be inserted, so that it
|
|
|
|
* follows the provided row.
|
|
|
|
*
|
|
|
|
* Necessary to insert the result of a new group or list view within an
|
|
|
|
* existing groups render, without losing track of the groups's own
|
|
|
|
* elements
|
|
|
|
*
|
|
|
|
* @param {HTMLTableRowElement} row the row after which the caller wants to insert a body
|
|
|
|
* @returns {HTMLTableSectionElement} element after which a tbody can be inserted
|
|
|
|
*/
|
|
|
|
point_insertion: function (row) {
|
|
|
|
var $row = $(row);
|
2011-05-27 07:27:51 +00:00
|
|
|
var red_letter_tboday = $row.closest('tbody')[0];
|
2011-05-18 13:00:09 +00:00
|
|
|
|
2011-05-19 09:22:22 +00:00
|
|
|
var $next_siblings = $row.nextAll();
|
|
|
|
if ($next_siblings.length) {
|
2011-05-27 07:27:51 +00:00
|
|
|
var $root_kanal = $('<tbody>').insertAfter(red_letter_tboday);
|
2011-05-18 13:00:09 +00:00
|
|
|
|
2011-05-19 09:22:22 +00:00
|
|
|
$root_kanal.append($next_siblings);
|
|
|
|
this.elements.splice(
|
2011-05-27 07:27:51 +00:00
|
|
|
_.indexOf(this.elements, red_letter_tboday),
|
2011-05-19 09:22:22 +00:00
|
|
|
0,
|
|
|
|
$root_kanal[0]);
|
|
|
|
}
|
2011-05-27 07:27:51 +00:00
|
|
|
return red_letter_tboday;
|
2011-05-19 09:22:22 +00:00
|
|
|
},
|
2011-06-24 13:07:13 +00:00
|
|
|
make_paginator: function () {
|
|
|
|
var self = this;
|
|
|
|
var $prev = $('<button type="button" data-pager-action="previous"><</button>')
|
|
|
|
.click(function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
self.page -= 1;
|
|
|
|
|
|
|
|
self.$row.closest('tbody').next()
|
|
|
|
.replaceWith(self.render());
|
|
|
|
});
|
|
|
|
var $next = $('<button type="button" data-pager-action="next">></button>')
|
|
|
|
.click(function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
self.page += 1;
|
|
|
|
|
|
|
|
self.$row.closest('tbody').next()
|
|
|
|
.replaceWith(self.render());
|
|
|
|
});
|
2011-06-24 11:13:39 +00:00
|
|
|
this.$row.children().last()
|
2011-06-24 13:07:13 +00:00
|
|
|
.append($prev)
|
2011-06-24 11:13:39 +00:00
|
|
|
.append('<span class="oe-pager-state"></span>')
|
2011-06-24 13:07:13 +00:00
|
|
|
.append($next);
|
|
|
|
},
|
2011-06-08 12:15:45 +00:00
|
|
|
open: function (point_insertion) {
|
|
|
|
this.render().insertAfter(point_insertion);
|
2011-06-24 13:07:13 +00:00
|
|
|
this.make_paginator();
|
2011-06-08 12:15:45 +00:00
|
|
|
},
|
|
|
|
close: function () {
|
2011-06-24 11:13:39 +00:00
|
|
|
this.$row.children().last().empty();
|
2011-08-19 10:12:08 +00:00
|
|
|
this.records.reset();
|
2011-05-18 13:00:09 +00:00
|
|
|
},
|
2011-05-24 06:52:05 +00:00
|
|
|
/**
|
|
|
|
* Prefixes ``$node`` with floated spaces in order to indent it relative
|
|
|
|
* to its own left margin/baseline
|
|
|
|
*
|
|
|
|
* @param {jQuery} $node jQuery object to indent
|
|
|
|
* @param {Number} level current nesting level, >= 1
|
|
|
|
* @returns {jQuery} the indentation node created
|
|
|
|
*/
|
|
|
|
indent: function ($node, level) {
|
|
|
|
return $('<span>')
|
|
|
|
.css({'float': 'left', 'white-space': 'pre'})
|
|
|
|
.text(new Array(level).join(' '))
|
|
|
|
.prependTo($node);
|
|
|
|
},
|
2011-05-17 16:09:48 +00:00
|
|
|
render_groups: function (datagroups) {
|
|
|
|
var self = this;
|
|
|
|
var placeholder = this.make_fragment();
|
|
|
|
_(datagroups).each(function (group) {
|
2011-06-08 12:15:45 +00:00
|
|
|
if (self.children[group.value]) {
|
2011-08-19 10:12:08 +00:00
|
|
|
self.records.proxy(group.value).reset();
|
2011-06-08 12:15:45 +00:00
|
|
|
delete self.children[group.value];
|
|
|
|
}
|
2011-09-05 11:03:09 +00:00
|
|
|
var child = self.children[group.value] = new openerp.web.ListView.Groups(self.view, {
|
2011-08-17 13:16:35 +00:00
|
|
|
records: self.records.proxy(group.value),
|
2011-06-08 12:15:45 +00:00
|
|
|
options: self.options,
|
|
|
|
columns: self.columns
|
|
|
|
});
|
|
|
|
self.bind_child_events(child);
|
|
|
|
child.datagroup = group;
|
|
|
|
|
2011-06-24 11:13:39 +00:00
|
|
|
var $row = child.$row = $('<tr>');
|
2011-05-24 10:26:54 +00:00
|
|
|
if (group.openable) {
|
2011-05-24 09:11:25 +00:00
|
|
|
$row.click(function (e) {
|
|
|
|
if (!$row.data('open')) {
|
2011-06-08 12:15:45 +00:00
|
|
|
$row.data('open', true)
|
|
|
|
.find('span.ui-icon')
|
|
|
|
.removeClass('ui-icon-triangle-1-e')
|
|
|
|
.addClass('ui-icon-triangle-1-s');
|
|
|
|
child.open(self.point_insertion(e.currentTarget));
|
2011-05-24 09:11:25 +00:00
|
|
|
} else {
|
|
|
|
$row.removeData('open')
|
|
|
|
.find('span.ui-icon')
|
|
|
|
.removeClass('ui-icon-triangle-1-s')
|
|
|
|
.addClass('ui-icon-triangle-1-e');
|
2011-06-08 12:15:45 +00:00
|
|
|
child.close();
|
2011-05-24 09:11:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-05-17 16:09:48 +00:00
|
|
|
placeholder.appendChild($row[0]);
|
|
|
|
|
2011-06-24 11:13:39 +00:00
|
|
|
var $group_column = $('<th class="oe-group-name">').appendTo($row);
|
2011-07-12 14:19:34 +00:00
|
|
|
// Don't fill this if group_by_no_leaf but no group_by
|
2011-05-24 13:27:29 +00:00
|
|
|
if (group.grouped_on) {
|
2011-07-12 14:19:34 +00:00
|
|
|
var row_data = {};
|
|
|
|
row_data[group.grouped_on] = group;
|
|
|
|
var group_column = _(self.columns).detect(function (column) {
|
|
|
|
return column.id === group.grouped_on; });
|
2011-09-05 11:03:09 +00:00
|
|
|
$group_column.html(openerp.web.format_cell(
|
2011-07-25 10:29:49 +00:00
|
|
|
row_data, group_column, "Undefined"
|
2011-07-12 14:19:34 +00:00
|
|
|
));
|
2011-05-24 13:27:29 +00:00
|
|
|
if (group.openable) {
|
|
|
|
// Make openable if not terminal group & group_by_no_leaf
|
|
|
|
$group_column
|
2011-05-24 14:00:55 +00:00
|
|
|
.prepend('<span class="ui-icon ui-icon-triangle-1-e" style="float: left;">');
|
2011-05-24 13:27:29 +00:00
|
|
|
}
|
2011-05-24 11:57:55 +00:00
|
|
|
}
|
|
|
|
self.indent($group_column, group.level);
|
|
|
|
// count column
|
|
|
|
$('<td>').text(group.length).appendTo($row);
|
2011-06-22 15:36:26 +00:00
|
|
|
|
2011-06-24 11:13:39 +00:00
|
|
|
if (self.options.selectable) {
|
|
|
|
$row.append('<td>');
|
|
|
|
}
|
2011-05-20 13:00:38 +00:00
|
|
|
_(self.columns).chain()
|
2011-05-17 16:09:48 +00:00
|
|
|
.filter(function (column) {return !column.invisible;})
|
2011-05-20 13:00:38 +00:00
|
|
|
.each(function (column) {
|
2011-05-24 11:57:55 +00:00
|
|
|
if (column.meta) {
|
|
|
|
// do not do anything
|
2011-05-20 13:00:38 +00:00
|
|
|
} else if (column.id in group.aggregates) {
|
|
|
|
var value = group.aggregates[column.id];
|
|
|
|
var format;
|
|
|
|
if (column.type === 'integer') {
|
|
|
|
format = "%.0f";
|
|
|
|
} else if (column.type === 'float') {
|
|
|
|
format = "%.2f";
|
|
|
|
}
|
|
|
|
$('<td>')
|
|
|
|
.text(_.sprintf(format, value))
|
|
|
|
.appendTo($row);
|
|
|
|
} else {
|
|
|
|
$row.append('<td>');
|
|
|
|
}
|
2011-05-10 11:07:23 +00:00
|
|
|
});
|
2011-06-24 11:13:39 +00:00
|
|
|
if (self.options.deletable) {
|
|
|
|
$row.append('<td class="oe-group-pagination">');
|
|
|
|
}
|
2011-05-10 11:07:23 +00:00
|
|
|
});
|
2011-05-17 16:09:48 +00:00
|
|
|
return placeholder;
|
|
|
|
},
|
2011-05-19 09:22:22 +00:00
|
|
|
bind_child_events: function (child) {
|
2011-05-18 14:35:17 +00:00
|
|
|
var $this = $(this),
|
|
|
|
self = this;
|
2011-05-19 09:22:22 +00:00
|
|
|
$(child).bind('selected', function (e) {
|
2011-05-18 14:35:17 +00:00
|
|
|
// can have selections spanning multiple links
|
2011-05-25 09:28:13 +00:00
|
|
|
var selection = self.get_selection();
|
|
|
|
$this.trigger(e, [selection.ids, selection.records]);
|
2011-06-07 10:30:24 +00:00
|
|
|
}).bind(this.passtrough_events, function (e) {
|
2011-05-18 13:00:09 +00:00
|
|
|
// additional positional parameters are provided to trigger as an
|
|
|
|
// Array, following the event type or event object, but are
|
|
|
|
// provided to the .bind event handler as *args.
|
|
|
|
// Convert our *args back into an Array in order to trigger them
|
|
|
|
// on the group itself, so it can ultimately be forwarded wherever
|
|
|
|
// it's supposed to go.
|
|
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
2011-05-18 14:35:17 +00:00
|
|
|
$this.trigger.call($this, e, args);
|
2011-05-18 13:00:09 +00:00
|
|
|
});
|
2011-05-18 14:35:17 +00:00
|
|
|
},
|
|
|
|
render_dataset: function (dataset) {
|
2011-08-17 13:16:35 +00:00
|
|
|
var self = this,
|
2011-09-05 11:03:09 +00:00
|
|
|
list = new openerp.web.ListView.List(this, {
|
2011-05-18 14:35:17 +00:00
|
|
|
options: this.options,
|
|
|
|
columns: this.columns,
|
2011-05-20 14:20:41 +00:00
|
|
|
dataset: dataset,
|
2011-08-17 13:16:35 +00:00
|
|
|
records: this.records
|
2011-05-10 11:07:23 +00:00
|
|
|
});
|
2011-05-19 09:22:22 +00:00
|
|
|
this.bind_child_events(list);
|
2011-05-18 09:06:30 +00:00
|
|
|
|
2011-06-23 12:46:41 +00:00
|
|
|
var view = this.view,
|
|
|
|
limit = view.limit(),
|
2011-06-24 13:07:13 +00:00
|
|
|
d = new $.Deferred(),
|
|
|
|
page = this.datagroup.openable ? this.page : view.page;
|
|
|
|
|
2011-08-18 22:57:21 +00:00
|
|
|
var fields = _.pluck(_.select(this.columns, function(x) {return x.tag == "field"}), 'name');
|
|
|
|
var options = { offset: page * limit, limit: limit };
|
|
|
|
dataset.read_slice(fields, options , function (records) {
|
2011-08-25 14:57:41 +00:00
|
|
|
if (!self.datagroup.openable) {
|
|
|
|
view.configure_pager(dataset);
|
|
|
|
} else {
|
|
|
|
var pages = Math.ceil(dataset.ids.length / limit);
|
|
|
|
self.$row
|
|
|
|
.find('.oe-pager-state')
|
|
|
|
.text(_.sprintf('%d/%d', page + 1, pages))
|
|
|
|
.end()
|
|
|
|
.find('button[data-pager-action=previous]')
|
|
|
|
.attr('disabled', page === 0)
|
|
|
|
.end()
|
|
|
|
.find('button[data-pager-action=next]')
|
|
|
|
.attr('disabled', page === pages - 1);
|
|
|
|
}
|
2011-06-23 12:46:41 +00:00
|
|
|
|
2011-08-25 14:57:41 +00:00
|
|
|
self.records.add(records, {silent: true});
|
|
|
|
list.render();
|
|
|
|
d.resolve(list);
|
|
|
|
});
|
2011-05-18 09:06:30 +00:00
|
|
|
return d.promise();
|
2011-05-10 11:07:23 +00:00
|
|
|
},
|
2011-07-22 15:01:14 +00:00
|
|
|
setup_resequence_rows: function (list, dataset) {
|
|
|
|
// drag and drop enabled if list is not sorted and there is a
|
|
|
|
// "sequence" column in the view.
|
|
|
|
if ((dataset.sort && dataset.sort())
|
|
|
|
|| !_(this.columns).any(function (column) {
|
|
|
|
return column.name === 'sequence'; })) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// ondrop, move relevant record & fix sequences
|
|
|
|
list.$current.sortable({
|
|
|
|
stop: function (event, ui) {
|
2011-08-24 08:53:03 +00:00
|
|
|
var to_move = list.records.get(ui.item.data('id')),
|
|
|
|
target_id = ui.item.prev().data('id');
|
2011-07-22 15:01:14 +00:00
|
|
|
|
2011-08-24 08:53:03 +00:00
|
|
|
list.records.remove(to_move);
|
|
|
|
var to = target_id ? list.records.indexOf(list.records.get(target_id)) : 0;
|
|
|
|
list.records.add(to_move, { at: to });
|
2011-07-22 15:01:14 +00:00
|
|
|
|
|
|
|
// resequencing time!
|
2011-08-17 13:16:35 +00:00
|
|
|
var record, index = to,
|
2011-07-22 15:01:14 +00:00
|
|
|
// if drag to 1st row (to = 0), start sequencing from 0
|
|
|
|
// (exclusive lower bound)
|
2011-08-17 13:16:35 +00:00
|
|
|
seq = to ? list.records.at(to - 1).get('sequence') : 0;
|
2011-08-22 10:13:08 +00:00
|
|
|
while (++seq, record = list.records.at(index++)) {
|
2011-07-22 15:01:14 +00:00
|
|
|
// write are independent from one another, so we can just
|
|
|
|
// launch them all at the same time and we don't really
|
|
|
|
// give a fig about when they're done
|
2011-08-17 13:16:35 +00:00
|
|
|
dataset.write(record.get('id'), {sequence: seq});
|
2011-08-19 10:39:55 +00:00
|
|
|
record.set('sequence', seq);
|
2011-07-22 15:01:14 +00:00
|
|
|
}
|
2011-08-24 08:53:03 +00:00
|
|
|
|
|
|
|
list.refresh_zebra();
|
2011-07-22 15:01:14 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2011-05-25 14:04:56 +00:00
|
|
|
render: function (post_render) {
|
2011-05-17 16:09:48 +00:00
|
|
|
var self = this;
|
2011-05-19 09:22:22 +00:00
|
|
|
var $element = $('<tbody>');
|
|
|
|
this.elements = [$element[0]];
|
2011-07-11 11:32:28 +00:00
|
|
|
|
|
|
|
this.datagroup.list(
|
|
|
|
_(this.view.visible_columns).chain()
|
|
|
|
.filter(function (column) { return column.tag === 'field' })
|
|
|
|
.pluck('name').value(),
|
|
|
|
function (groups) {
|
|
|
|
$element[0].appendChild(
|
|
|
|
self.render_groups(groups));
|
2011-05-25 14:04:56 +00:00
|
|
|
if (post_render) { post_render(); }
|
2011-07-11 11:32:28 +00:00
|
|
|
}, function (dataset) {
|
|
|
|
self.render_dataset(dataset).then(function (list) {
|
|
|
|
self.children[null] = list;
|
|
|
|
self.elements =
|
|
|
|
[list.$current.replaceAll($element)[0]];
|
2011-07-22 15:01:14 +00:00
|
|
|
self.setup_resequence_rows(list, dataset);
|
2011-07-11 11:32:28 +00:00
|
|
|
if (post_render) { post_render(); }
|
|
|
|
});
|
2011-05-18 09:06:30 +00:00
|
|
|
});
|
2011-05-19 09:22:22 +00:00
|
|
|
return $element;
|
2011-05-18 14:35:17 +00:00
|
|
|
},
|
|
|
|
/**
|
2011-05-25 09:28:13 +00:00
|
|
|
* Returns the ids of all selected records for this group, and the records
|
|
|
|
* themselves
|
2011-05-18 14:35:17 +00:00
|
|
|
*/
|
|
|
|
get_selection: function () {
|
2011-05-25 09:28:13 +00:00
|
|
|
var ids = [], records = [];
|
|
|
|
|
|
|
|
_(this.children)
|
|
|
|
.each(function (child) {
|
|
|
|
var selection = child.get_selection();
|
|
|
|
ids.push.apply(ids, selection.ids);
|
|
|
|
records.push.apply(records, selection.records);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {ids: ids, records: records};
|
2011-05-19 09:22:22 +00:00
|
|
|
},
|
2011-08-19 10:12:08 +00:00
|
|
|
on_records_reset: function () {
|
2011-05-31 06:33:13 +00:00
|
|
|
this.children = {};
|
2011-05-20 13:30:45 +00:00
|
|
|
$(this.elements).remove();
|
2011-05-25 13:04:14 +00:00
|
|
|
},
|
|
|
|
get_records: function () {
|
2011-06-08 12:55:31 +00:00
|
|
|
if (_(this.children).isEmpty()) {
|
|
|
|
return {
|
|
|
|
count: this.datagroup.length,
|
|
|
|
values: this.datagroup.aggregates
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 13:04:14 +00:00
|
|
|
return _(this.children).chain()
|
|
|
|
.map(function (child) {
|
|
|
|
return child.get_records();
|
|
|
|
}).flatten().value();
|
2011-08-19 15:15:10 +00:00
|
|
|
}
|
2011-05-10 11:07:23 +00:00
|
|
|
});
|
2011-08-17 08:55:32 +00:00
|
|
|
|
|
|
|
/**
|
2011-09-12 12:38:52 +00:00
|
|
|
* @mixin Events
|
2011-08-17 08:55:32 +00:00
|
|
|
*/
|
2011-09-12 12:38:52 +00:00
|
|
|
var Events = /** @lends Events# */{
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
|
|
|
* @param {String} event event to listen to on the current object, null for all events
|
|
|
|
* @param {Function} handler event handler to bind to the relevant event
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
bind: function (event, handler) {
|
|
|
|
var calls = this['_callbacks'] || (this._callbacks = {});
|
|
|
|
|
|
|
|
if (event in calls) {
|
|
|
|
calls[event].push(handler);
|
|
|
|
} else {
|
|
|
|
calls[event] = [handler];
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2011-08-19 15:15:10 +00:00
|
|
|
/**
|
|
|
|
* @param {String} event event to unbind on the current object
|
|
|
|
* @param {function} [handler] specific event handler to remove (otherwise unbind all handlers for the event)
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
unbind: function (event, handler) {
|
|
|
|
var calls = this._callbacks || {};
|
|
|
|
if (!(event in calls)) { return this; }
|
|
|
|
if (!handler) {
|
|
|
|
delete calls[event];
|
|
|
|
} else {
|
|
|
|
var handlers = calls[event];
|
|
|
|
handlers.splice(
|
|
|
|
_(handlers).indexOf(handler),
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
|
|
|
* @param {String} event
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
trigger: function (event) {
|
|
|
|
var calls;
|
|
|
|
if (!(calls = this._callbacks)) { return this; }
|
|
|
|
var callbacks = (calls[event] || []).concat(calls[null] || []);
|
|
|
|
for(var i=0, length=callbacks.length; i<length; ++i) {
|
|
|
|
callbacks[i].apply(this, arguments);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
2011-03-30 14:00:48 +00:00
|
|
|
};
|
2011-09-05 11:03:09 +00:00
|
|
|
var Record = openerp.web.Class.extend(/** @lends Record# */{
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs Record
|
2011-09-05 11:03:09 +00:00
|
|
|
* @extends openerp.web.Class
|
2011-09-12 11:34:37 +00:00
|
|
|
*
|
2011-09-12 12:38:52 +00:00
|
|
|
* @mixes Events
|
2011-08-17 08:55:32 +00:00
|
|
|
* @param {Object} [data]
|
|
|
|
*/
|
|
|
|
init: function (data) {
|
2011-08-17 13:16:35 +00:00
|
|
|
this.attributes = data || {};
|
2011-08-17 08:55:32 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {String} key
|
|
|
|
* @returns {Object}
|
|
|
|
*/
|
|
|
|
get: function (key) {
|
2011-08-17 13:16:35 +00:00
|
|
|
return this.attributes[key];
|
2011-08-17 08:55:32 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param key
|
|
|
|
* @param value
|
2011-08-22 09:04:58 +00:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Boolean} [options.silent=false]
|
2011-08-17 08:55:32 +00:00
|
|
|
* @returns {Record}
|
|
|
|
*/
|
2011-08-22 09:04:58 +00:00
|
|
|
set: function (key, value, options) {
|
|
|
|
options = options || {};
|
2011-08-19 10:17:23 +00:00
|
|
|
var old_value = this.attributes[key];
|
|
|
|
if (old_value === value) {
|
2011-08-19 10:16:44 +00:00
|
|
|
return this;
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
this.attributes[key] = value;
|
2011-08-22 09:04:58 +00:00
|
|
|
if (!options.silent) {
|
|
|
|
this.trigger('change:' + key, this, value, old_value);
|
|
|
|
this.trigger('change', this, key, value, old_value);
|
|
|
|
}
|
2011-08-17 08:55:32 +00:00
|
|
|
return this;
|
2011-08-17 13:16:35 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Converts the current record to the format expected by form views:
|
|
|
|
*
|
|
|
|
* .. code-block:: javascript
|
|
|
|
*
|
|
|
|
* data: {
|
|
|
|
* $fieldname: {
|
|
|
|
* value: $value
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @returns {Object} record displayable in a form view
|
|
|
|
*/
|
|
|
|
toForm: function () {
|
|
|
|
var form_data = {};
|
|
|
|
_(this.attributes).each(function (value, key) {
|
|
|
|
form_data[key] = {value: value};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {data: form_data};
|
2011-08-17 08:55:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Record.include(Events);
|
2011-09-05 11:03:09 +00:00
|
|
|
var Collection = openerp.web.Class.extend(/** @lends Collection# */{
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
2011-08-17 13:36:00 +00:00
|
|
|
* Smarter collections, with events, very strongly inspired by Backbone's.
|
|
|
|
*
|
|
|
|
* Using a "dumb" array of records makes synchronization between the
|
|
|
|
* various serious
|
|
|
|
*
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs Collection
|
2011-09-05 11:03:09 +00:00
|
|
|
* @extends openerp.web.Class
|
2011-09-12 11:34:37 +00:00
|
|
|
*
|
2011-09-12 12:38:52 +00:00
|
|
|
* @mixes Events
|
2011-08-17 08:55:32 +00:00
|
|
|
* @param {Array} [records] records to initialize the collection with
|
|
|
|
* @param {Object} [options]
|
|
|
|
*/
|
|
|
|
init: function (records, options) {
|
|
|
|
options = options || {};
|
|
|
|
_.bindAll(this, '_onRecordEvent');
|
2011-08-17 13:16:35 +00:00
|
|
|
this.length = 0;
|
2011-08-17 08:55:32 +00:00
|
|
|
this.records = [];
|
|
|
|
this._byId = {};
|
|
|
|
this._proxies = {};
|
|
|
|
this._key = options.key;
|
|
|
|
this._parent = options.parent;
|
2011-03-30 14:00:48 +00:00
|
|
|
|
2011-08-17 08:55:32 +00:00
|
|
|
if (records) {
|
|
|
|
this.add(records);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @param {Object|Array} record
|
2011-08-17 13:16:35 +00:00
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {Number} [options.at]
|
2011-08-22 09:04:58 +00:00
|
|
|
* @param {Boolean} [options.silent=false]
|
2011-08-17 08:55:32 +00:00
|
|
|
* @returns this
|
|
|
|
*/
|
2011-08-17 13:16:35 +00:00
|
|
|
add: function (record, options) {
|
|
|
|
options = options || {};
|
2011-08-17 08:55:32 +00:00
|
|
|
var records = record instanceof Array ? record : [record];
|
|
|
|
|
|
|
|
for(var i=0, length=records.length; i<length; ++i) {
|
|
|
|
var instance = (records[i] instanceof Record) ? records[i] : new Record(records[i]);
|
|
|
|
instance.bind(null, this._onRecordEvent);
|
|
|
|
this._byId[instance.get('id')] = instance;
|
2011-08-22 10:56:56 +00:00
|
|
|
if (options.at == undefined) {
|
2011-08-17 13:16:35 +00:00
|
|
|
this.records.push(instance);
|
2011-08-22 09:04:58 +00:00
|
|
|
if (!options.silent) {
|
|
|
|
this.trigger('add', this, instance, this.records.length-1);
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
} else {
|
2011-08-22 09:04:58 +00:00
|
|
|
var insertion_index = options.at + i;
|
|
|
|
this.records.splice(insertion_index, 0, instance);
|
|
|
|
if (!options.silent) {
|
|
|
|
this.trigger('add', this, instance, insertion_index);
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
}
|
|
|
|
this.length++;
|
2011-08-17 08:55:32 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a record by its index in the collection, can also take a group if
|
|
|
|
* the collection is not degenerate
|
|
|
|
*
|
|
|
|
* @param {Number} index
|
|
|
|
* @param {String} [group]
|
|
|
|
* @returns {Record|undefined}
|
|
|
|
*/
|
|
|
|
at: function (index, group) {
|
|
|
|
if (group) {
|
|
|
|
var groups = group.split('.');
|
|
|
|
return this._proxies[groups[0]].at(index, groups.join('.'));
|
|
|
|
}
|
|
|
|
return this.records[index];
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Get a record by its database id
|
|
|
|
*
|
|
|
|
* @param {Number} id
|
|
|
|
* @returns {Record|undefined}
|
|
|
|
*/
|
|
|
|
get: function (id) {
|
|
|
|
if (!_(this._proxies).isEmpty()) {
|
|
|
|
var record = null;
|
|
|
|
_(this._proxies).detect(function (proxy) {
|
|
|
|
return record = proxy.get(id);
|
|
|
|
});
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
return this._byId[id];
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Builds a proxy (insert/retrieve) to a subtree of the collection, by
|
|
|
|
* the subtree's group
|
|
|
|
*
|
|
|
|
* @param {String} section group path section
|
|
|
|
* @returns {Collection}
|
|
|
|
*/
|
|
|
|
proxy: function (section) {
|
|
|
|
return this._proxies[section] = new Collection(null, {
|
|
|
|
parent: this,
|
|
|
|
key: section
|
|
|
|
}).bind(null, this._onRecordEvent);
|
|
|
|
},
|
2011-08-17 13:16:35 +00:00
|
|
|
/**
|
|
|
|
* @param {Array} [records]
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
reset: function (records) {
|
2011-08-18 12:21:06 +00:00
|
|
|
_(this._proxies).each(function (proxy) {
|
|
|
|
proxy.reset();
|
|
|
|
});
|
2011-08-18 13:27:47 +00:00
|
|
|
this._proxies = {};
|
2011-08-17 13:16:35 +00:00
|
|
|
this.length = 0;
|
|
|
|
this.records = [];
|
|
|
|
this._byId = {};
|
|
|
|
if (records) {
|
|
|
|
this.add(records);
|
|
|
|
}
|
|
|
|
this.trigger('reset', this);
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes the provided record from the collection
|
|
|
|
*
|
|
|
|
* @param {Record} record
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
remove: function (record) {
|
2011-08-19 11:59:07 +00:00
|
|
|
var self = this;
|
2011-08-17 13:16:35 +00:00
|
|
|
var index = _(this.records).indexOf(record);
|
2011-08-19 11:59:07 +00:00
|
|
|
if (index === -1) {
|
|
|
|
_(this._proxies).each(function (proxy) {
|
|
|
|
proxy.remove(record);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
|
|
|
|
this.records.splice(index, 1);
|
|
|
|
delete this._byId[record.get('id')];
|
|
|
|
this.length--;
|
|
|
|
this.trigger('remove', record, this);
|
|
|
|
return this;
|
|
|
|
},
|
2011-05-27 15:10:00 +00:00
|
|
|
|
2011-08-17 08:55:32 +00:00
|
|
|
_onRecordEvent: function (event, record, options) {
|
2011-08-19 10:12:08 +00:00
|
|
|
// don't propagate reset events
|
|
|
|
if (event === 'reset') { return; }
|
2011-08-17 08:55:32 +00:00
|
|
|
this.trigger.apply(this, arguments);
|
2011-08-18 12:21:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// underscore-type methods
|
|
|
|
each: function (callback) {
|
|
|
|
for(var section in this._proxies) {
|
|
|
|
if (this._proxies.hasOwnProperty(section)) {
|
|
|
|
this._proxies[section].each(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var i=0; i<this.length; ++i) {
|
|
|
|
callback(this.records[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
map: function (callback) {
|
|
|
|
var results = [];
|
|
|
|
this.each(function (record) {
|
|
|
|
results.push(callback(record));
|
|
|
|
});
|
|
|
|
return results;
|
2011-08-19 13:21:12 +00:00
|
|
|
},
|
2011-08-24 12:21:24 +00:00
|
|
|
pluck: function (fieldname) {
|
|
|
|
return this.map(function (record) {
|
|
|
|
return record.get(fieldname);
|
|
|
|
});
|
|
|
|
},
|
2011-08-19 13:21:12 +00:00
|
|
|
indexOf: function (record) {
|
|
|
|
return _(this.records).indexOf(record);
|
2011-08-17 08:55:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Collection.include(Events);
|
2011-09-05 11:03:09 +00:00
|
|
|
openerp.web.list = {
|
2011-08-17 08:55:32 +00:00
|
|
|
Events: Events,
|
|
|
|
Record: Record,
|
|
|
|
Collection: Collection
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|