2012-04-17 12:15:59 +00:00
|
|
|
openerp.web.list = function (instance) {
|
|
|
|
var _t = instance.web._t,
|
|
|
|
_lt = instance.web._lt;
|
|
|
|
var QWeb = instance.web.qweb;
|
|
|
|
instance.web.views.add('list', 'instance.web.ListView');
|
|
|
|
instance.web.ListView = instance.web.View.extend( /** @lends instance.web.ListView# */ {
|
2011-12-13 14:49:55 +00:00
|
|
|
_template: 'ListView',
|
2011-12-16 13:00:00 +00:00
|
|
|
display_name: _lt('List'),
|
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
|
2012-05-09 15:25:58 +00:00
|
|
|
'deletable': false,
|
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-12-16 13:00:00 +00:00
|
|
|
'addable': _lt("Create"),
|
2011-04-21 12:06:08 +00:00
|
|
|
// 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-12-14 17:29:52 +00:00
|
|
|
'reorderable': true,
|
2012-05-16 14:52:31 +00:00
|
|
|
'action_buttons': true,
|
2011-04-07 12:12:22 +00:00
|
|
|
},
|
2012-10-23 08:53:29 +00:00
|
|
|
view_type: 'tree',
|
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.
|
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @constructs instance.web.ListView
|
|
|
|
* @extends instance.web.View
|
2011-09-12 11:34:37 +00:00
|
|
|
*
|
2011-08-08 14:41:20 +00:00
|
|
|
* @param parent parent object
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {instance.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-04-07 12:12:22 +00:00
|
|
|
*/
|
2011-09-14 14:30:28 +00:00
|
|
|
init: function(parent, dataset, view_id, options) {
|
2011-08-19 10:39:55 +00:00
|
|
|
var self = this;
|
2011-09-14 14:30:28 +00:00
|
|
|
this._super(parent);
|
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-09-25 18:04:07 +00:00
|
|
|
this.colors = null;
|
2012-04-04 12:02:11 +00:00
|
|
|
this.fonts = 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();
|
|
|
|
|
2012-06-14 12:30:56 +00:00
|
|
|
this.set_groups(new (this.options.GroupsType)(this));
|
2011-07-18 12:54:22 +00:00
|
|
|
|
2012-04-17 12:15:59 +00:00
|
|
|
if (this.dataset instanceof instance.web.DataSetStatic) {
|
2012-10-17 10:52:13 +00:00
|
|
|
this.groups.datagroup = new StaticDataGroup(this.dataset);
|
2011-09-13 06:31:21 +00:00
|
|
|
} else {
|
2012-10-17 10:52:13 +00:00
|
|
|
this.groups.datagroup = new DataGroup(
|
2011-09-13 06:31:21 +00:00
|
|
|
this, this.model,
|
|
|
|
dataset.get_domain(),
|
2012-05-22 14:48:27 +00:00
|
|
|
dataset.get_context());
|
2011-09-13 06:31:21 +00:00
|
|
|
this.groups.datagroup.sort = this.dataset._sort;
|
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-09-25 18:04:07 +00:00
|
|
|
|
2011-12-14 14:43:17 +00:00
|
|
|
this.no_leaf = false;
|
2012-10-23 08:53:29 +00:00
|
|
|
this.grouped = false;
|
|
|
|
this.on('view_loaded', self, self.load_list);
|
2011-06-22 15:36:26 +00:00
|
|
|
},
|
2012-06-14 12:30:56 +00:00
|
|
|
set_default_options: function (options) {
|
|
|
|
this._super(options);
|
|
|
|
_.defaults(this.options, {
|
2012-06-25 13:52:15 +00:00
|
|
|
GroupsType: instance.web.ListView.Groups,
|
|
|
|
ListType: instance.web.ListView.List
|
2012-06-14 12:30:56 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
2012-02-21 15:30:23 +00:00
|
|
|
|| (this.getParent().action || {}).limit
|
2012-02-01 13:18:48 +00:00
|
|
|
|| 80);
|
2011-06-22 15:36:26 +00:00
|
|
|
}
|
|
|
|
return this._limit;
|
2011-05-27 07:13:09 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Set a custom Group construct as the root of the List View.
|
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {instance.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-12-14 17:29:52 +00:00
|
|
|
'row_link': function (e, id, dataset, view) {
|
|
|
|
self.do_activate_record(dataset.index, id, dataset, view);
|
2011-05-06 12:28:37 +00:00
|
|
|
}
|
|
|
|
});
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-04-21 11:59:24 +00:00
|
|
|
/**
|
2012-07-11 11:29:59 +00:00
|
|
|
* View startup method, the default behavior is to set the ``oe_list``
|
2011-04-21 11:59:24 +00:00
|
|
|
* 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() {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.addClass('oe_list');
|
2012-10-23 08:53:29 +00:00
|
|
|
return this._super();
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-09-25 18:04:07 +00:00
|
|
|
/**
|
2012-04-18 11:53:16 +00:00
|
|
|
* Returns the style for the provided record in the current view (from the
|
2012-04-19 11:45:57 +00:00
|
|
|
* ``@colors`` and ``@fonts`` attributes)
|
2011-09-25 18:04:07 +00:00
|
|
|
*
|
|
|
|
* @param {Record} record record for the current row
|
2012-04-18 11:53:16 +00:00
|
|
|
* @returns {String} CSS style declaration
|
2011-09-25 18:04:07 +00:00
|
|
|
*/
|
2012-04-18 11:53:16 +00:00
|
|
|
style_for: function (record) {
|
2012-04-18 09:22:54 +00:00
|
|
|
var style= '';
|
2012-04-04 12:02:11 +00:00
|
|
|
|
2011-09-29 14:24:10 +00:00
|
|
|
var context = _.extend({}, record.attributes, {
|
|
|
|
uid: this.session.uid,
|
|
|
|
current_date: new Date().toString('yyyy-MM-dd')
|
|
|
|
// TODO: time, datetime, relativedelta
|
|
|
|
});
|
2012-04-04 09:16:13 +00:00
|
|
|
|
2012-04-04 12:02:11 +00:00
|
|
|
if (this.fonts) {
|
2012-04-19 09:56:46 +00:00
|
|
|
for(var i=0, len=this.fonts.length; i<len; ++i) {
|
|
|
|
var pair = this.fonts[i],
|
|
|
|
font = pair[0],
|
|
|
|
expression = pair[1];
|
2012-04-04 12:02:11 +00:00
|
|
|
if (py.evaluate(expression, context).toJSON()) {
|
2012-04-18 09:22:54 +00:00
|
|
|
switch(font) {
|
|
|
|
case 'bold':
|
|
|
|
style += 'font-weight: bold;';
|
|
|
|
break;
|
|
|
|
case 'italic':
|
|
|
|
style += 'font-style: italic;';
|
|
|
|
break;
|
|
|
|
case 'underline':
|
|
|
|
style += 'text-decoration: underline;';
|
|
|
|
break;
|
|
|
|
}
|
2012-04-04 12:02:11 +00:00
|
|
|
}
|
2012-04-18 09:45:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-04 12:02:11 +00:00
|
|
|
|
|
|
|
if (!this.colors) { return style; }
|
2011-09-25 18:04:07 +00:00
|
|
|
for(var i=0, len=this.colors.length; i<len; ++i) {
|
|
|
|
var pair = this.colors[i],
|
|
|
|
color = pair[0],
|
|
|
|
expression = pair[1];
|
2012-02-27 07:37:21 +00:00
|
|
|
if (py.evaluate(expression, context).toJSON()) {
|
2012-04-18 09:22:54 +00:00
|
|
|
return style += 'color: ' + color + ';';
|
2011-09-25 18:04:07 +00:00
|
|
|
}
|
2011-09-30 13:29:41 +00:00
|
|
|
// TODO: handle evaluation errors
|
2011-09-25 18:04:07 +00:00
|
|
|
}
|
2012-04-04 07:30:16 +00:00
|
|
|
return style;
|
2011-09-25 18:04:07 +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
|
|
|
*/
|
2012-10-23 08:53:29 +00:00
|
|
|
load_list: function(data) {
|
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.name = "" + this.fields_view.arch.attrs.string;
|
|
|
|
|
2011-09-25 18:04:07 +00:00
|
|
|
if (this.fields_view.arch.attrs.colors) {
|
2011-09-30 13:29:41 +00:00
|
|
|
this.colors = _(this.fields_view.arch.attrs.colors.split(';')).chain()
|
|
|
|
.compact()
|
|
|
|
.map(function(color_pair) {
|
2011-09-25 18:04:07 +00:00
|
|
|
var pair = color_pair.split(':'),
|
|
|
|
color = pair[0],
|
|
|
|
expr = pair[1];
|
2011-09-30 13:29:41 +00:00
|
|
|
return [color, py.parse(py.tokenize(expr)), expr];
|
|
|
|
}).value();
|
2011-09-25 18:04:07 +00:00
|
|
|
}
|
|
|
|
|
2012-04-04 12:02:11 +00:00
|
|
|
if (this.fields_view.arch.attrs.fonts) {
|
2012-04-18 09:42:25 +00:00
|
|
|
this.fonts = _(this.fields_view.arch.attrs.fonts.split(';')).chain().compact()
|
2012-04-04 12:02:11 +00:00
|
|
|
.map(function(font_pair) {
|
|
|
|
var pair = font_pair.split(':'),
|
|
|
|
font = pair[0],
|
|
|
|
expr = pair[1];
|
|
|
|
return [font, py.parse(py.tokenize(expr)), expr];
|
2012-04-04 09:16:13 +00:00
|
|
|
}).value();
|
|
|
|
}
|
|
|
|
|
2012-10-23 08:53:29 +00:00
|
|
|
this.setup_columns(this.fields_view.fields, this.grouped);
|
2011-05-04 14:47:53 +00:00
|
|
|
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.html(QWeb.render(this._template, this));
|
|
|
|
this.$el.addClass(this.fields_view.arch.attrs['class']);
|
2012-09-04 07:41:45 +00:00
|
|
|
|
2011-04-07 15:35:27 +00:00
|
|
|
// Head hook
|
2012-04-09 19:35:09 +00:00
|
|
|
// Selecting records
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_list_record_selector').click(function(){
|
|
|
|
self.$el.find('.oe_list_record_selector input').prop('checked',
|
|
|
|
self.$el.find('.oe_list_record_selector').prop('checked') || false);
|
2011-10-25 10:59:25 +00:00
|
|
|
var selection = self.groups.get_selection();
|
|
|
|
$(self.groups).trigger(
|
|
|
|
'selected', [selection.ids, selection.records]);
|
2011-10-24 13:29:59 +00:00
|
|
|
});
|
|
|
|
|
2012-04-09 19:35:09 +00:00
|
|
|
// Sorting columns
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.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'));
|
2012-04-20 17:44:27 +00:00
|
|
|
if($this.hasClass("sortdown") || $this.hasClass("sortup")) {
|
|
|
|
$this.toggleClass("sortdown").toggleClass("sortup");
|
2011-07-13 11:18:15 +00:00
|
|
|
} else {
|
2012-04-20 17:44:27 +00:00
|
|
|
$this.toggleClass("sortdown");
|
2011-07-13 11:18:15 +00:00
|
|
|
}
|
2012-06-27 14:12:49 +00:00
|
|
|
$this.siblings('.oe_sortable').removeClass("sortup sortdown");
|
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
|
|
|
|
2012-09-11 06:36:07 +00:00
|
|
|
// Add button
|
2012-04-10 07:48:01 +00:00
|
|
|
if (!this.$buttons) {
|
2012-10-22 16:16:11 +00:00
|
|
|
this.$buttons = $(QWeb.render("ListView.buttons", {'widget':self}));
|
2012-04-10 07:48:01 +00:00
|
|
|
if (this.options.$buttons) {
|
|
|
|
this.$buttons.appendTo(this.options.$buttons);
|
|
|
|
} else {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_list_buttons').replaceWith(this.$buttons);
|
2012-04-10 07:48:01 +00:00
|
|
|
}
|
|
|
|
this.$buttons.find('.oe_list_add')
|
|
|
|
.click(this.proxy('do_add_record'))
|
2012-10-23 08:53:29 +00:00
|
|
|
.prop('disabled', this.grouped);
|
2012-04-10 07:48:01 +00:00
|
|
|
}
|
2012-04-09 19:35:09 +00:00
|
|
|
|
2012-04-09 19:43:33 +00:00
|
|
|
// Pager
|
2012-04-10 11:26:56 +00:00
|
|
|
if (!this.$pager) {
|
2012-10-22 16:16:11 +00:00
|
|
|
this.$pager = $(QWeb.render("ListView.pager", {'widget':self}));
|
2012-04-10 11:26:56 +00:00
|
|
|
if (this.options.$buttons) {
|
|
|
|
this.$pager.appendTo(this.options.$pager);
|
|
|
|
} else {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_list_pager').replaceWith(this.$pager);
|
2012-04-10 11:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$pager
|
|
|
|
.on('click', 'a[data-pager-action]', function () {
|
2011-06-23 13:49:20 +00:00
|
|
|
var $this = $(this);
|
2012-04-10 11:26:56 +00:00
|
|
|
var max_page = Math.floor(self.dataset.size() / self.limit());
|
|
|
|
switch ($this.data('pager-action')) {
|
|
|
|
case 'first':
|
|
|
|
self.page = 0; break;
|
|
|
|
case 'last':
|
|
|
|
self.page = max_page - 1;
|
|
|
|
break;
|
|
|
|
case 'next':
|
|
|
|
self.page += 1; break;
|
|
|
|
case 'previous':
|
|
|
|
self.page -= 1; break;
|
|
|
|
}
|
|
|
|
if (self.page < 0) {
|
|
|
|
self.page = max_page;
|
|
|
|
} else if (self.page > max_page) {
|
|
|
|
self.page = 0;
|
|
|
|
}
|
|
|
|
self.reload_content();
|
2012-06-27 14:12:49 +00:00
|
|
|
}).find('.oe_list_pager_state')
|
2012-04-10 11:26:56 +00:00
|
|
|
.click(function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
var $this = $(this);
|
2011-06-23 13:49:20 +00:00
|
|
|
|
2012-04-10 11:26:56 +00:00
|
|
|
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">' + _t("Unlimited") + '</option>')
|
|
|
|
.change(function () {
|
|
|
|
var val = parseInt($select.val(), 10);
|
|
|
|
self._limit = (isNaN(val) ? null : val);
|
|
|
|
self.page = 0;
|
|
|
|
self.reload_content();
|
2012-06-28 15:02:15 +00:00
|
|
|
}).blur(function() {
|
|
|
|
$(this).trigger('change');
|
2012-04-10 11:26:56 +00:00
|
|
|
})
|
|
|
|
.val(self._limit || 'NaN');
|
|
|
|
});
|
|
|
|
}
|
2012-04-09 19:35:09 +00:00
|
|
|
|
2012-04-09 19:43:33 +00:00
|
|
|
// Sidebar
|
2012-04-18 14:24:39 +00:00
|
|
|
if (!this.sidebar && this.options.$sidebar) {
|
2012-04-17 12:15:59 +00:00
|
|
|
this.sidebar = new instance.web.Sidebar(this);
|
2012-04-09 19:35:09 +00:00
|
|
|
this.sidebar.appendTo(this.options.$sidebar);
|
2012-09-04 15:18:24 +00:00
|
|
|
this.sidebar.add_items('other', _.compact([
|
2012-06-07 14:39:26 +00:00
|
|
|
{ label: _t("Export"), callback: this.on_sidebar_export },
|
2012-09-04 15:18:24 +00:00
|
|
|
self.is_action_enabled('delete') && { label: _t('Delete'), callback: this.do_delete_selected }
|
|
|
|
]));
|
2012-04-09 19:35:09 +00:00
|
|
|
this.sidebar.add_toolbar(this.fields_view.toolbar);
|
2012-08-24 18:27:07 +00:00
|
|
|
this.sidebar.$el.hide();
|
2011-07-26 21:00:05 +00:00
|
|
|
}
|
2012-10-23 08:53:29 +00:00
|
|
|
this.trigger('list_view_loaded', data, this.grouped);
|
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?
|
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {instance.web.DataSet} dataset
|
2011-06-23 12:46:41 +00:00
|
|
|
*/
|
|
|
|
configure_pager: function (dataset) {
|
|
|
|
this.dataset.ids = dataset.ids;
|
2012-02-28 10:12:00 +00:00
|
|
|
// Not exactly clean
|
|
|
|
if (dataset._length) {
|
|
|
|
this.dataset._length = dataset._length;
|
|
|
|
}
|
2011-06-23 12:46:41 +00:00
|
|
|
|
2012-05-09 14:21:34 +00:00
|
|
|
var total = dataset.size();
|
2012-06-28 09:47:45 +00:00
|
|
|
var limit = this.limit() || total;
|
2012-10-25 09:11:32 +00:00
|
|
|
if (total == 0)
|
|
|
|
this.$pager.hide();
|
|
|
|
else
|
|
|
|
this.$pager.css("display", "");
|
2012-06-28 09:47:45 +00:00
|
|
|
this.$pager.toggleClass('oe_list_pager_single_page', (total <= limit));
|
2012-05-09 14:21:34 +00:00
|
|
|
var spager = '-';
|
|
|
|
if (total) {
|
2012-06-28 09:47:45 +00:00
|
|
|
var range_start = this.page * limit + 1;
|
|
|
|
var range_stop = range_start - 1 + limit;
|
2012-05-09 14:21:34 +00:00
|
|
|
if (range_stop > total) {
|
|
|
|
range_stop = total;
|
|
|
|
}
|
|
|
|
spager = _.str.sprintf('%d-%d of %d', range_start, range_stop, total);
|
|
|
|
}
|
2012-05-03 13:55:20 +00:00
|
|
|
|
2012-06-27 14:12:49 +00:00
|
|
|
this.$pager.find('.oe_list_pager_state').text(spager);
|
2011-06-23 12:46:41 +00:00
|
|
|
},
|
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) {
|
2012-08-08 10:34:04 +00:00
|
|
|
var registry = instance.web.list.columns;
|
2011-05-17 14:03:58 +00:00
|
|
|
this.columns.splice(0, this.columns.length);
|
2012-08-08 10:34:04 +00:00
|
|
|
this.columns.push.apply(this.columns,
|
|
|
|
_(this.fields_view.arch.children).map(function (field) {
|
|
|
|
var id = field.attrs.name;
|
|
|
|
return registry.for_(id, fields[id], field);
|
|
|
|
}));
|
2011-05-27 07:13:09 +00:00
|
|
|
if (grouped) {
|
2012-08-08 10:34:04 +00:00
|
|
|
this.columns.unshift(
|
|
|
|
new instance.web.list.MetaColumn('_group', _t("Group")));
|
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) {
|
2012-10-17 10:26:03 +00:00
|
|
|
return column.invisible !== '1';
|
2011-05-17 14:03:58 +00:00
|
|
|
});
|
2012-10-17 10:26:03 +00:00
|
|
|
|
2012-08-08 10:34:04 +00:00
|
|
|
this.aggregate_columns = _(this.visible_columns).invoke('to_aggregate');
|
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
|
2012-04-17 12:15:59 +00:00
|
|
|
* :js:func:`~instance.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-12-07 10:45:03 +00:00
|
|
|
* @param {String} [view="page"] 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) {
|
2012-04-09 20:53:34 +00:00
|
|
|
view = view || index == null ? 'form' : 'form';
|
2011-04-12 13:02:29 +00:00
|
|
|
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 () {
|
2011-12-14 17:18:11 +00:00
|
|
|
this._super();
|
2012-04-09 20:53:34 +00:00
|
|
|
if (this.$buttons) {
|
2012-04-10 07:59:14 +00:00
|
|
|
this.$buttons.show();
|
2012-04-09 20:53:34 +00:00
|
|
|
}
|
2012-05-09 14:21:34 +00:00
|
|
|
if (this.$pager) {
|
|
|
|
this.$pager.show();
|
|
|
|
}
|
2011-04-04 15:34:26 +00:00
|
|
|
},
|
|
|
|
do_hide: function () {
|
2011-07-26 21:00:05 +00:00
|
|
|
if (this.sidebar) {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.sidebar.$el.hide();
|
2011-07-26 21:00:05 +00:00
|
|
|
}
|
2012-04-09 20:53:34 +00:00
|
|
|
if (this.$buttons) {
|
2012-04-10 07:59:14 +00:00
|
|
|
this.$buttons.hide();
|
2012-04-09 20:53:34 +00:00
|
|
|
}
|
|
|
|
if (this.$pager) {
|
2012-04-10 11:26:56 +00:00
|
|
|
this.$pager.hide();
|
2012-04-09 20:53:34 +00:00
|
|
|
}
|
|
|
|
this._super();
|
2011-04-12 14:08:29 +00:00
|
|
|
},
|
2012-10-22 16:16:11 +00:00
|
|
|
/**
|
|
|
|
* Reloads the list view based on the current settings (dataset & al)
|
|
|
|
*
|
2012-10-23 08:53:29 +00:00
|
|
|
* @deprecated
|
2012-10-22 16:16:11 +00:00
|
|
|
* @param {Boolean} [grouped] Should the list be displayed grouped
|
|
|
|
* @param {Object} [context] context to send the server while loading the view
|
|
|
|
*/
|
|
|
|
reload_view: function (grouped, context, initial) {
|
2012-10-23 08:53:29 +00:00
|
|
|
return this.load_view(context);
|
2012-10-22 16:16:11 +00:00
|
|
|
},
|
2011-05-27 15:10:00 +00:00
|
|
|
/**
|
|
|
|
* re-renders the content of the list view
|
2011-10-25 07:06:26 +00:00
|
|
|
*
|
|
|
|
* @returns {$.Deferred} promise to content reloading
|
2011-05-27 15:10:00 +00:00
|
|
|
*/
|
|
|
|
reload_content: function () {
|
2011-08-25 14:57:41 +00:00
|
|
|
var self = this;
|
2012-08-24 18:27:07 +00:00
|
|
|
self.$el.find('.oe_list_record_selector').prop('checked', false);
|
2011-08-18 12:21:06 +00:00
|
|
|
this.records.reset();
|
2011-10-25 07:06:26 +00:00
|
|
|
var reloaded = $.Deferred();
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_list_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-10-25 07:06:26 +00:00
|
|
|
reloaded.resolve();
|
2011-08-25 14:57:41 +00:00
|
|
|
}));
|
2011-12-15 15:31:34 +00:00
|
|
|
this.do_push_state({
|
|
|
|
page: this.page,
|
2011-12-20 18:35:21 +00:00
|
|
|
limit: this._limit
|
2011-12-15 15:31:34 +00:00
|
|
|
});
|
2011-10-25 07:06:26 +00:00
|
|
|
return reloaded.promise();
|
2011-05-27 15:10:00 +00:00
|
|
|
},
|
2011-12-21 14:19:53 +00:00
|
|
|
reload: function () {
|
|
|
|
return this.reload_content();
|
|
|
|
},
|
2012-06-22 13:50:15 +00:00
|
|
|
reload_record: function (record) {
|
|
|
|
return this.dataset.read_ids(
|
|
|
|
[record.get('id')],
|
|
|
|
_.pluck(_(this.columns).filter(function (r) {
|
|
|
|
return r.tag === 'field';
|
|
|
|
}), 'name')
|
2012-10-30 14:06:30 +00:00
|
|
|
).done(function (records) {
|
2012-06-22 13:50:15 +00:00
|
|
|
_(records[0]).each(function (value, key) {
|
|
|
|
record.set(key, value, {silent: true});
|
|
|
|
});
|
|
|
|
record.trigger('change', record);
|
|
|
|
});
|
|
|
|
},
|
2011-12-15 15:31:34 +00:00
|
|
|
|
2012-02-01 15:44:42 +00:00
|
|
|
do_load_state: function(state, warm) {
|
2011-12-15 15:31:34 +00:00
|
|
|
var reload = false;
|
|
|
|
if (state.page && this.page !== state.page) {
|
|
|
|
this.page = state.page;
|
|
|
|
reload = true;
|
|
|
|
}
|
|
|
|
if (state.limit) {
|
|
|
|
if (_.isString(state.limit)) {
|
|
|
|
state.limit = null;
|
|
|
|
}
|
|
|
|
if (state.limit !== this._limit) {
|
|
|
|
this._limit = state.limit;
|
|
|
|
reload = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (reload) {
|
|
|
|
this.reload_content();
|
|
|
|
}
|
|
|
|
},
|
2011-06-03 09:36:06 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-09-29 10:32:29 +00:00
|
|
|
do_search: function (domain, context, group_by) {
|
2011-12-01 10:42:33 +00:00
|
|
|
this.page = 0;
|
2012-10-17 10:52:13 +00:00
|
|
|
this.groups.datagroup = new DataGroup(
|
2011-09-29 10:32:29 +00:00
|
|
|
this, this.model, domain, context, 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-09-29 10:32:29 +00:00
|
|
|
if (_.isEmpty(group_by) && !context['group_by_no_leaf']) {
|
|
|
|
group_by = null;
|
2011-06-03 09:36:06 +00:00
|
|
|
}
|
2011-12-14 14:43:17 +00:00
|
|
|
this.no_leaf = !!context['group_by_no_leaf'];
|
2012-10-23 08:53:29 +00:00
|
|
|
this.grouped = !!group_by;
|
2011-05-31 11:23:39 +00:00
|
|
|
|
2012-10-30 14:06:30 +00:00
|
|
|
return this.load_view(context).then(
|
2012-10-22 16:16:11 +00:00
|
|
|
this.proxy('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) {
|
2011-11-10 10:36:50 +00:00
|
|
|
if (!(ids.length && confirm(_t("Do you really want to remove these records?")))) {
|
2011-05-06 14:07:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var self = this;
|
2012-10-30 14:06:30 +00:00
|
|
|
return $.when(this.dataset.unlink(ids)).done(function () {
|
2011-08-19 11:59:07 +00:00
|
|
|
_(ids).each(function (id) {
|
|
|
|
self.records.remove(self.records.get(id));
|
|
|
|
});
|
2011-10-04 07:19:45 +00:00
|
|
|
self.configure_pager(self.dataset);
|
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) {
|
2012-02-23 14:51:57 +00:00
|
|
|
if (!ids.length) {
|
|
|
|
this.dataset.index = 0;
|
2012-04-09 19:35:09 +00:00
|
|
|
if (this.sidebar) {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.sidebar.$el.hide();
|
2012-04-09 19:35:09 +00:00
|
|
|
}
|
2011-05-25 13:04:14 +00:00
|
|
|
this.compute_aggregates();
|
|
|
|
return;
|
|
|
|
}
|
2012-02-23 14:51:57 +00:00
|
|
|
|
|
|
|
this.dataset.index = _(this.dataset.ids).indexOf(ids[0]);
|
2012-04-09 19:35:09 +00:00
|
|
|
if (this.sidebar) {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.sidebar.$el.show();
|
2012-04-09 19:35:09 +00:00
|
|
|
}
|
2012-02-23 14:51:57 +00:00
|
|
|
|
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) {
|
2012-07-17 13:23:08 +00:00
|
|
|
this.handle_button(name, id, callback);
|
2012-07-10 07:39:28 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Base handling of buttons, can be called when overriding do_button_action
|
|
|
|
* in order to bypass parent overrides.
|
|
|
|
*
|
2012-08-07 09:23:17 +00:00
|
|
|
* The callback will be provided with the ``id`` as its parameter, in case
|
|
|
|
* handle_button's caller had to alter the ``id`` (or even create one)
|
|
|
|
* while not being ``callback``'s creator.
|
|
|
|
*
|
2012-07-10 07:39:28 +00:00
|
|
|
* This method should not be overridden.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2012-07-17 13:23:08 +00:00
|
|
|
handle_button: 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-12-13 08:10:38 +00:00
|
|
|
if ('confirm' in action && !window.confirm(action.confirm)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-28 08:50:51 +00:00
|
|
|
|
2012-04-17 12:15:59 +00:00
|
|
|
var c = new instance.web.CompoundContext();
|
2011-10-28 08:50:51 +00:00
|
|
|
c.set_eval_context(_.extend({
|
|
|
|
active_id: id,
|
|
|
|
active_ids: [id],
|
|
|
|
active_model: this.dataset.model
|
|
|
|
}, this.records.get(id).toContext()));
|
|
|
|
if (action.context) {
|
|
|
|
c.add(action.context);
|
|
|
|
}
|
|
|
|
action.context = c;
|
2012-08-07 09:23:17 +00:00
|
|
|
this.do_execute_action(
|
|
|
|
action, this.dataset, id, _.bind(callback, null, id));
|
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
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {instance.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
|
|
|
*/
|
2011-12-14 17:29:52 +00:00
|
|
|
do_activate_record: function (index, id, dataset, view) {
|
2011-10-04 10:21:15 +00:00
|
|
|
this.dataset.ids = dataset.ids;
|
2011-12-14 17:29:52 +00:00
|
|
|
this.select_record(index, view);
|
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 () {
|
2012-05-09 15:20:40 +00:00
|
|
|
var ids = this.groups.get_selection().ids;
|
|
|
|
if (ids.length) {
|
|
|
|
this.do_delete(this.groups.get_selection().ids);
|
|
|
|
} else {
|
|
|
|
this.do_warn(_t("Warning"), _t("You must select at least one record."));
|
|
|
|
}
|
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
|
2012-04-17 12:15:59 +00:00
|
|
|
* :js:class:`~instance.web.ListView.Group`, by calling
|
|
|
|
* :js:func:`~instance.web.ListView.group.get_records`.
|
2011-05-25 13:04:14 +00:00
|
|
|
*
|
|
|
|
* Then displays the aggregates in the table through
|
2012-04-17 12:15:59 +00:00
|
|
|
* :js:method:`~instance.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-09-21 07:19:25 +00:00
|
|
|
records = _(records).compact();
|
2011-05-25 13:04:14 +00:00
|
|
|
|
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) {
|
2012-01-10 15:39:05 +00:00
|
|
|
var self = this;
|
2012-08-24 18:27:07 +00:00
|
|
|
var $footer_cells = this.$el.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
|
|
|
|
2011-11-15 12:30:59 +00:00
|
|
|
$footer_cells.filter(_.str.sprintf('[data-field=%s]', column.id))
|
2012-08-08 10:34:04 +00:00
|
|
|
.html(column.format(aggregation, { process_modifiers: false }));
|
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
|
2011-11-23 12:29:57 +00:00
|
|
|
* @param {"before"|"after"} [options.position="after"] insertion position for the new columns
|
|
|
|
* @param {Object} [options.except] content row to not pad
|
2011-09-08 12:55:10 +00:00
|
|
|
*/
|
|
|
|
pad_columns: function (count, options) {
|
|
|
|
options = options || {};
|
|
|
|
// padding for action/pager header
|
2012-08-24 18:27:07 +00:00
|
|
|
var $first_header = this.$el.find('thead tr:first th');
|
2011-09-08 12:55:10 +00:00
|
|
|
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
|
2012-08-24 18:27:07 +00:00
|
|
|
var $rows = this.$el
|
2012-06-27 14:12:49 +00:00
|
|
|
.find('.oe_list_header_columns, tr:not(thead tr)')
|
2011-09-08 12:55:10 +00:00
|
|
|
.not(options['except']);
|
2012-06-27 14:12:49 +00:00
|
|
|
var newcols = new Array(count+1).join('<td class="oe_list_padding"></td>');
|
2011-09-08 12:55:10 +00:00
|
|
|
if (options.position === 'before') {
|
|
|
|
$rows.prepend(newcols);
|
|
|
|
} else {
|
|
|
|
$rows.append(newcols);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes all padding columns of the table
|
|
|
|
*/
|
|
|
|
unpad_columns: function () {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_list_padding').remove();
|
2011-09-08 12:55:10 +00:00
|
|
|
if (this.previous_colspan) {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el
|
2011-09-08 12:55:10 +00:00
|
|
|
.find('thead tr:first th')
|
|
|
|
.attr('colspan', this.previous_colspan);
|
|
|
|
this.previous_colspan = null;
|
|
|
|
}
|
2012-04-03 14:15:53 +00:00
|
|
|
},
|
|
|
|
no_result: function () {
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('.oe_view_nocontent').remove();
|
2012-04-03 14:17:26 +00:00
|
|
|
if (this.groups.group_by
|
|
|
|
|| !this.options.action
|
|
|
|
|| !this.options.action.help) {
|
2012-04-03 14:15:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-24 18:27:07 +00:00
|
|
|
this.$el.find('table:first').hide();
|
|
|
|
this.$el.prepend(
|
2012-08-04 15:10:59 +00:00
|
|
|
$('<div class="oe_view_nocontent">').html(this.options.action.help)
|
2012-05-10 09:26:21 +00:00
|
|
|
);
|
2012-09-12 10:05:52 +00:00
|
|
|
var create_nocontent = this.$buttons;
|
|
|
|
this.$el.find('.oe_view_nocontent').click(function() {
|
2012-09-14 11:47:47 +00:00
|
|
|
create_nocontent.effect('bounce', {distance: 18, times: 5}, 150);
|
2012-09-12 10:05:52 +00:00
|
|
|
});
|
2011-05-06 12:28:37 +00:00
|
|
|
}
|
|
|
|
});
|
2012-04-17 12:15:59 +00:00
|
|
|
instance.web.ListView.List = instance.web.Class.extend( /** @lends instance.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
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @constructs instance.web.ListView.List
|
|
|
|
* @extends instance.web.Class
|
2011-09-12 11:34:37 +00:00
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {Object} opts display options, identical to those of :js:class:`instance.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) {
|
2012-07-09 08:46:52 +00:00
|
|
|
var $row = self.$current.children(
|
|
|
|
'[data-id=' + record.get('id') + ']');
|
2011-08-19 15:15:10 +00:00
|
|
|
var index = $row.data('index');
|
|
|
|
$row.remove();
|
|
|
|
},
|
2012-01-12 08:49:10 +00:00
|
|
|
'reset': function () { return self.on_records_reset(); },
|
2012-07-02 11:10:26 +00:00
|
|
|
'change': function (event, record, attribute, value, old_value) {
|
|
|
|
var $row;
|
|
|
|
if (attribute === 'id') {
|
|
|
|
if (old_value) {
|
|
|
|
throw new Error("Setting 'id' attribute on existing record "
|
|
|
|
+ JSON.stringify(record.attributes));
|
|
|
|
}
|
2012-07-09 09:24:01 +00:00
|
|
|
if (!_.contains(self.dataset.ids, value)) {
|
|
|
|
// add record to dataset if not already in (added by
|
|
|
|
// the form view?)
|
|
|
|
self.dataset.ids.splice(
|
|
|
|
self.records.indexOf(record), 0, value);
|
|
|
|
}
|
2012-07-02 11:10:26 +00:00
|
|
|
// Set id on new record
|
2012-07-09 08:46:52 +00:00
|
|
|
$row = self.$current.children('[data-id=false]');
|
2012-07-02 11:10:26 +00:00
|
|
|
} else {
|
|
|
|
$row = self.$current.children(
|
|
|
|
'[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) {
|
2012-07-02 11:10:26 +00:00
|
|
|
var $new_row = $(self.render_record(record));
|
2011-08-24 12:22:51 +00:00
|
|
|
|
|
|
|
if (index === 0) {
|
|
|
|
$new_row.prependTo(self.$current);
|
|
|
|
} else {
|
|
|
|
var previous_record = records.at(index-1),
|
2012-07-02 11:10:26 +00:00
|
|
|
$previous_sibling = self.$current.children(
|
2011-08-24 12:22:51 +00:00
|
|
|
'[data-id=' + previous_record.get('id') + ']');
|
|
|
|
$new_row.insertAfter($previous_sibling);
|
|
|
|
}
|
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
|
|
|
|
2012-09-14 15:40:32 +00:00
|
|
|
this.$current = $('<tbody>')
|
2012-06-27 14:12:49 +00:00
|
|
|
.delegate('th.oe_list_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
|
|
|
})
|
2012-06-27 14:12:49 +00:00
|
|
|
.delegate('td.oe_list_record_delete button', 'click', function (e) {
|
2011-05-06 14:07:15 +00:00
|
|
|
e.stopPropagation();
|
2011-05-09 08:10:22 +00:00
|
|
|
var $row = $(e.target).closest('tr');
|
|
|
|
$(self).trigger('deleted', [[self.row_id($row)]]);
|
|
|
|
})
|
2012-06-27 14:12:49 +00:00
|
|
|
.delegate('td.oe_list_field_cell button', 'click', function (e) {
|
2011-05-09 08:10:22 +00:00
|
|
|
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-10-17 14:10:22 +00:00
|
|
|
// note: $.data converts data to number if it's composed only
|
|
|
|
// of digits, nice when storing actual numbers, not nice when
|
|
|
|
// storing strings composed only of digits. Force the action
|
|
|
|
// name to be a string
|
2012-08-07 09:23:17 +00:00
|
|
|
$(self).trigger('action', [field.toString(), record_id, function (id) {
|
|
|
|
return self.reload_record(self.records.get(id));
|
2011-06-03 14:37:23 +00:00
|
|
|
}]);
|
2011-05-09 08:22:12 +00:00
|
|
|
})
|
2012-01-10 15:39:05 +00:00
|
|
|
.delegate('a', 'click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
})
|
2011-05-09 08:22:12 +00:00
|
|
|
.delegate('tr', 'click', function (e) {
|
2011-10-17 09:52:16 +00:00
|
|
|
var row_id = self.row_id(e.currentTarget);
|
2012-07-11 13:18:04 +00:00
|
|
|
if (row_id) {
|
2012-07-11 11:10:27 +00:00
|
|
|
e.stopPropagation();
|
2011-10-20 16:22:55 +00:00
|
|
|
if (!self.dataset.select_id(row_id)) {
|
2012-07-11 13:18:04 +00:00
|
|
|
throw new Error("Could not find id in dataset");
|
2011-10-20 16:22:55 +00:00
|
|
|
}
|
2012-04-30 00:01:58 +00:00
|
|
|
self.row_clicked(e);
|
2011-10-17 09:52:16 +00:00
|
|
|
}
|
2011-05-06 12:28:37 +00:00
|
|
|
});
|
|
|
|
},
|
2011-12-14 17:29:52 +00:00
|
|
|
row_clicked: function (e, view) {
|
2011-06-03 09:36:06 +00:00
|
|
|
$(this).trigger(
|
|
|
|
'row_link',
|
2011-10-19 14:45:07 +00:00
|
|
|
[this.dataset.ids[this.dataset.index],
|
2011-12-14 17:29:52 +00:00
|
|
|
this.dataset, view]);
|
2011-06-03 09:36:06 +00:00
|
|
|
},
|
2011-10-14 08:43:10 +00:00
|
|
|
render_cell: function (record, column) {
|
2011-10-18 10:32:58 +00:00
|
|
|
var value;
|
2011-10-18 05:54:00 +00:00
|
|
|
if(column.type === 'reference') {
|
2011-10-18 10:32:58 +00:00
|
|
|
value = record.get(column.id);
|
2011-10-18 10:52:55 +00:00
|
|
|
var ref_match;
|
|
|
|
// Ensure that value is in a reference "shape", otherwise we're
|
|
|
|
// going to loop on performing name_get after we've resolved (and
|
|
|
|
// set) a human-readable version. m2o does not have this issue
|
|
|
|
// because the non-human-readable is just a number, where the
|
|
|
|
// human-readable version is a pair
|
|
|
|
if (value && (ref_match = /([\w\.]+),(\d+)/.exec(value))) {
|
2011-10-18 10:32:58 +00:00
|
|
|
// reference values are in the shape "$model,$id" (as a
|
|
|
|
// string), we need to split and name_get this pair in order
|
|
|
|
// to get a correctly displayable value in the field
|
2011-10-18 10:52:55 +00:00
|
|
|
var model = ref_match[1],
|
|
|
|
id = parseInt(ref_match[2], 10);
|
2012-10-30 14:06:30 +00:00
|
|
|
new instance.web.DataSet(this.view, model).name_get([id]).done(function(names) {
|
2011-10-18 10:32:58 +00:00
|
|
|
if (!names.length) { return; }
|
|
|
|
record.set(column.id, names[0][1]);
|
|
|
|
});
|
2011-10-18 05:54:00 +00:00
|
|
|
}
|
2011-10-18 10:34:43 +00:00
|
|
|
} else if (column.type === 'many2one') {
|
2011-10-18 10:32:58 +00:00
|
|
|
value = record.get(column.id);
|
2011-10-14 08:43:10 +00:00
|
|
|
// m2o values are usually name_get formatted, [Number, String]
|
|
|
|
// pairs, but in some cases only the id is provided. In these
|
|
|
|
// cases, we need to perform a name_get call to fetch the actual
|
|
|
|
// displayable value
|
|
|
|
if (typeof value === 'number' || value instanceof Number) {
|
|
|
|
// fetch the name, set it on the record (in the right field)
|
|
|
|
// and let the various registered events handle refreshing the
|
|
|
|
// row
|
2012-04-17 12:15:59 +00:00
|
|
|
new instance.web.DataSet(this.view, column.relation)
|
2012-10-30 14:06:30 +00:00
|
|
|
.name_get([value]).done(function (names) {
|
2011-10-14 08:43:10 +00:00
|
|
|
if (!names.length) { return; }
|
|
|
|
record.set(column.id, names[0]);
|
|
|
|
});
|
|
|
|
}
|
2012-09-28 11:35:24 +00:00
|
|
|
} else if (column.type === 'many2many') {
|
|
|
|
value = record.get(column.id);
|
|
|
|
// non-resolved (string) m2m values are arrays
|
|
|
|
if (value instanceof Array && !_.isEmpty(value)) {
|
|
|
|
var ids;
|
|
|
|
// they come in two shapes:
|
|
|
|
if (value[0] instanceof Array) {
|
|
|
|
var command = value[0];
|
|
|
|
// 1. an array of m2m commands (usually (6, false, ids))
|
|
|
|
if (command[0] !== 6) {
|
|
|
|
throw new Error(_t("Unknown m2m command ") + command[0]);
|
|
|
|
}
|
|
|
|
ids = command[2];
|
|
|
|
} else {
|
|
|
|
// 2. an array of ids
|
|
|
|
ids = value;
|
|
|
|
}
|
|
|
|
new instance.web.Model(column.relation)
|
2012-10-30 14:06:30 +00:00
|
|
|
.call('name_get', [ids]).done(function (names) {
|
2012-09-28 11:35:24 +00:00
|
|
|
record.set(column.id, _(names).pluck(1).join(', '));
|
|
|
|
})
|
|
|
|
}
|
2011-10-14 08:43:10 +00:00
|
|
|
}
|
2012-08-08 10:34:04 +00:00
|
|
|
return column.format(record.toForm().data, {
|
2012-01-10 15:39:05 +00:00
|
|
|
model: this.dataset.model,
|
|
|
|
id: record.get('id')
|
|
|
|
});
|
2011-10-14 08:43:10 +00:00
|
|
|
},
|
2011-05-06 12:28:37 +00:00
|
|
|
render: function () {
|
2011-07-08 14:06:55 +00:00
|
|
|
this.$current.empty().append(
|
|
|
|
QWeb.render('ListView.rows', _.extend({
|
2012-01-12 10:29:41 +00:00
|
|
|
render_cell: function () {
|
|
|
|
return self.render_cell.apply(self, arguments); }
|
2012-01-12 08:49:10 +00:00
|
|
|
}, this)));
|
2012-07-14 20:01:42 +00:00
|
|
|
this.pad_table_to(4);
|
2011-09-14 15:03:52 +00:00
|
|
|
},
|
|
|
|
pad_table_to: function (count) {
|
|
|
|
if (this.records.length >= count ||
|
|
|
|
_(this.columns).any(function(column) { return column.meta; })) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var cells = [];
|
|
|
|
if (this.options.selectable) {
|
2012-06-27 14:12:49 +00:00
|
|
|
cells.push('<th class="oe_list_record_selector"></td>');
|
2011-09-14 15:03:52 +00:00
|
|
|
}
|
|
|
|
_(this.columns).each(function(column) {
|
2011-11-14 09:27:21 +00:00
|
|
|
if (column.invisible === '1') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (column.tag === 'button') {
|
2012-06-27 14:12:49 +00:00
|
|
|
cells.push('<td class="oe_button" title="' + column.string + '"> </td>');
|
2011-11-14 09:27:21 +00:00
|
|
|
} else {
|
2011-09-14 15:03:52 +00:00
|
|
|
cells.push('<td title="' + column.string + '"> </td>');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (this.options.deletable) {
|
2012-06-27 14:12:49 +00:00
|
|
|
cells.push('<td class="oe_list_record_delete"><button type="button" style="visibility: hidden"> </button></td>');
|
2011-09-14 15:03:52 +00:00
|
|
|
}
|
|
|
|
cells.unshift('<tr>');
|
|
|
|
cells.push('</tr>');
|
|
|
|
|
|
|
|
var row = cells.join('');
|
2011-10-04 15:42:46 +00:00
|
|
|
this.$current
|
2011-10-04 15:59:18 +00:00
|
|
|
.children('tr:not([data-id])').remove().end()
|
2012-09-28 10:58:40 +00:00
|
|
|
.append(new Array(count - this.records.length + 1).join(row));
|
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 () {
|
2012-06-06 12:31:15 +00:00
|
|
|
var result = {ids: [], records: []};
|
2011-04-07 12:46:39 +00:00
|
|
|
if (!this.options.selectable) {
|
2012-06-06 12:31:15 +00:00
|
|
|
return result;
|
2011-04-07 12:46:39 +00:00
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
var records = this.records;
|
2012-06-27 14:12:49 +00:00
|
|
|
this.$current.find('th.oe_list_record_selector input:checked')
|
2011-05-25 09:28:13 +00:00
|
|
|
.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 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) {
|
2012-06-22 13:50:15 +00:00
|
|
|
return this.view.reload_record(record);
|
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) {
|
2012-06-28 14:14:03 +00:00
|
|
|
var self = this;
|
2011-08-24 08:53:03 +00:00
|
|
|
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-10-04 10:48:48 +00:00
|
|
|
view: this.view,
|
2012-01-12 10:29:41 +00:00
|
|
|
render_cell: function () {
|
2012-06-28 14:14:03 +00:00
|
|
|
return self.render_cell.apply(self, arguments); }
|
2011-06-03 13:24:38 +00:00
|
|
|
});
|
2011-03-30 14:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
2012-04-17 12:15:59 +00:00
|
|
|
instance.web.ListView.Groups = instance.web.Class.extend( /** @lends instance.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
|
2012-10-17 10:52:13 +00:00
|
|
|
* with the :js:class:`~DataGroup` bound to it.
|
2011-05-10 11:07:23 +00:00
|
|
|
*
|
|
|
|
* Provides events similar to those of
|
2012-04-17 12:15:59 +00:00
|
|
|
* :js:class:`~instance.web.ListView.List`
|
2011-08-17 13:16:35 +00:00
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @constructs instance.web.ListView.Groups
|
|
|
|
* @extends instance.web.Class
|
2011-09-12 11:34:37 +00:00
|
|
|
*
|
2012-04-17 12:15:59 +00:00
|
|
|
* @param {instance.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
|
|
|
|
2012-01-12 08:49:10 +00:00
|
|
|
var self = this;
|
|
|
|
this.records.bind('reset', function () {
|
|
|
|
return self.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()
|
2012-10-17 11:58:45 +00:00
|
|
|
.addClass('oe_list_group_pagination')
|
2011-06-24 13:07:13 +00:00
|
|
|
.append($prev)
|
2012-06-27 14:12:49 +00:00
|
|
|
.append('<span class="oe_list_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-12-21 09:29:21 +00:00
|
|
|
|
|
|
|
var no_subgroups = _(this.datagroup.group_by).isEmpty(),
|
|
|
|
records_terminated = !this.datagroup.context['group_by_no_leaf'];
|
|
|
|
if (no_subgroups && records_terminated) {
|
|
|
|
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];
|
|
|
|
}
|
2012-06-19 13:36:18 +00:00
|
|
|
var child = self.children[group.value] = new (self.view.options.GroupsType)(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;
|
|
|
|
|
2012-08-30 13:05:24 +00:00
|
|
|
var $row = child.$row = $('<tr class="oe_group_header">');
|
2011-11-15 11:29:06 +00:00
|
|
|
if (group.openable && group.length) {
|
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]);
|
|
|
|
|
2012-06-27 14:12:49 +00:00
|
|
|
var $group_column = $('<th class="oe_list_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; });
|
2012-07-03 15:55:40 +00:00
|
|
|
if (! group_column) {
|
|
|
|
throw new Error(_.str.sprintf(
|
|
|
|
_t("Grouping on field '%s' is not possible because that field does not appear in the list view."),
|
|
|
|
group.grouped_on));
|
|
|
|
}
|
2012-08-06 08:08:46 +00:00
|
|
|
var group_label;
|
2011-11-10 15:28:50 +00:00
|
|
|
try {
|
2012-08-08 10:34:04 +00:00
|
|
|
group_label = group_column.format(row_data, {
|
|
|
|
value_if_empty: _t("Undefined"),
|
|
|
|
process_modifiers: false
|
2012-08-06 08:08:46 +00:00
|
|
|
});
|
2011-11-10 15:28:50 +00:00
|
|
|
} catch (e) {
|
2012-08-06 08:08:46 +00:00
|
|
|
group_label = row_data[group_column.id].value;
|
2011-11-10 15:28:50 +00:00
|
|
|
}
|
2012-08-06 08:08:46 +00:00
|
|
|
$group_column.text(_.str.sprintf("%s (%d)",
|
|
|
|
group_label, group.length));
|
|
|
|
|
2011-12-13 15:39:31 +00:00
|
|
|
if (group.length && group.openable) {
|
|
|
|
// Make openable if not terminal group & group_by_no_leaf
|
|
|
|
$group_column.prepend('<span class="ui-icon ui-icon-triangle-1-e" style="float: left;">');
|
|
|
|
} else {
|
2011-11-15 11:29:06 +00:00
|
|
|
// Kinda-ugly hack: jquery-ui has no "empty" icon, so set
|
|
|
|
// wonky background position to ensure nothing is displayed
|
|
|
|
// there but the rest of the behavior is ui-icon's
|
2011-12-13 15:39:31 +00:00
|
|
|
$group_column.prepend('<span class="ui-icon" style="float: left; background-position: 150px 150px">');
|
2011-05-24 13:27:29 +00:00
|
|
|
}
|
2011-05-24 11:57:55 +00:00
|
|
|
}
|
|
|
|
self.indent($group_column, group.level);
|
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()
|
2012-08-08 10:34:04 +00:00
|
|
|
.filter(function (column) { return column.invisible !== '1'; })
|
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) {
|
2012-02-27 15:27:39 +00:00
|
|
|
var r = {};
|
|
|
|
r[column.id] = {value: group.aggregates[column.id]};
|
2012-05-30 21:26:51 +00:00
|
|
|
$('<td class="oe_number">')
|
2012-08-08 10:34:04 +00:00
|
|
|
.html(column.format(r, {process_modifiers: false}))
|
2011-05-20 13:00:38 +00:00
|
|
|
.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) {
|
2012-06-27 14:12:49 +00:00
|
|
|
$row.append('<td class="oe_list_group_pagination">');
|
2011-06-24 11:13:39 +00:00
|
|
|
}
|
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,
|
2012-06-14 12:30:56 +00:00
|
|
|
list = new (this.view.options.ListType)(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');
|
2012-01-10 14:35:56 +00:00
|
|
|
var options = { offset: page * limit, limit: limit, context: {bin_size: true} };
|
2011-10-10 14:36:34 +00:00
|
|
|
//TODO xmo: investigate why we need to put the setTimeout
|
2012-10-30 14:06:30 +00:00
|
|
|
$.async_when().done(function() {
|
|
|
|
dataset.read_slice(fields, options).done(function (records) {
|
|
|
|
// FIXME: ignominious hacks, parents (aka form view) should not send two ListView#reload_content concurrently
|
|
|
|
if (self.records.length) {
|
|
|
|
self.records.reset(null, {silent: true});
|
|
|
|
}
|
|
|
|
if (!self.datagroup.openable) {
|
|
|
|
view.configure_pager(dataset);
|
2012-01-17 12:43:12 +00:00
|
|
|
} else {
|
2012-10-30 14:06:30 +00:00
|
|
|
if (dataset.size() == records.length) {
|
|
|
|
// only one page
|
|
|
|
self.$row.find('td.oe_list_group_pagination').empty();
|
|
|
|
} else {
|
|
|
|
var pages = Math.ceil(dataset.size() / limit);
|
|
|
|
self.$row
|
|
|
|
.find('.oe_list_pager_state')
|
|
|
|
.text(_.str.sprintf(_t("%(page)d/%(page_count)d"), {
|
|
|
|
page: page + 1,
|
|
|
|
page_count: pages
|
|
|
|
}))
|
|
|
|
.end()
|
|
|
|
.find('button[data-pager-action=previous]')
|
|
|
|
.css('visibility',
|
|
|
|
page === 0 ? 'hidden' : '')
|
|
|
|
.end()
|
|
|
|
.find('button[data-pager-action=next]')
|
|
|
|
.css('visibility',
|
|
|
|
page === pages - 1 ? 'hidden' : '');
|
|
|
|
}
|
2012-01-17 12:43:12 +00:00
|
|
|
}
|
2011-06-23 12:46:41 +00:00
|
|
|
|
2012-10-30 14:06:30 +00:00
|
|
|
self.records.add(records, {silent: true});
|
|
|
|
list.render();
|
|
|
|
d.resolve(list);
|
|
|
|
if (_.isEmpty(records)) {
|
|
|
|
view.no_result();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
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
|
2012-08-07 13:43:43 +00:00
|
|
|
// visible column with @widget=handle or "sequence" column in the view.
|
2011-07-22 15:01:14 +00:00
|
|
|
if ((dataset.sort && dataset.sort())
|
|
|
|
|| !_(this.columns).any(function (column) {
|
2012-08-07 13:43:43 +00:00
|
|
|
return column.widget === 'handle'
|
|
|
|
|| column.name === 'sequence'; })) {
|
2011-07-22 15:01:14 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-08-07 13:43:43 +00:00
|
|
|
var sequence_field = _(this.columns).find(function (c) {
|
|
|
|
return c.widget === 'handle';
|
|
|
|
});
|
|
|
|
var seqname = sequence_field ? sequence_field.name : 'sequence';
|
|
|
|
|
2011-07-22 15:01:14 +00:00
|
|
|
// ondrop, move relevant record & fix sequences
|
|
|
|
list.$current.sortable({
|
2011-09-14 15:37:07 +00:00
|
|
|
axis: 'y',
|
|
|
|
items: '> tr[data-id]',
|
2012-08-07 13:43:43 +00:00
|
|
|
helper: 'clone'
|
|
|
|
});
|
|
|
|
if (sequence_field) {
|
|
|
|
list.$current.sortable('option', 'handle', '.oe_list_field_handle');
|
|
|
|
}
|
|
|
|
list.$current.sortable('option', {
|
|
|
|
start: function (e, ui) {
|
|
|
|
ui.placeholder.height(ui.item.height());
|
|
|
|
},
|
2011-07-22 15:01:14 +00:00
|
|
|
stop: function (event, ui) {
|
2011-08-24 08:53:03 +00:00
|
|
|
var to_move = list.records.get(ui.item.data('id')),
|
2011-12-06 12:37:35 +00:00
|
|
|
target_id = ui.item.prev().data('id'),
|
|
|
|
from_index = list.records.indexOf(to_move),
|
|
|
|
target = list.records.get(target_id);
|
|
|
|
if (list.records.at(from_index - 1) == target) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-22 15:01:14 +00:00
|
|
|
|
2011-08-24 08:53:03 +00:00
|
|
|
list.records.remove(to_move);
|
2011-12-06 12:37:35 +00:00
|
|
|
var to = target_id ? list.records.indexOf(target) + 1 : 0;
|
2011-08-24 08:53:03 +00:00
|
|
|
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)
|
2012-08-07 13:43:43 +00:00
|
|
|
seq = to ? list.records.at(to - 1).get(seqname) : 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-12-06 08:27:13 +00:00
|
|
|
// FIXME: breaks on o2ms (e.g. Accounting > Financial
|
|
|
|
// Accounting > Taxes > Taxes, child tax accounts)
|
|
|
|
// when synchronous (without setTimeout)
|
|
|
|
(function (dataset, id, seq) {
|
2012-10-30 14:06:30 +00:00
|
|
|
$.async_when().done(function () {
|
2012-08-07 13:43:43 +00:00
|
|
|
var attrs = {};
|
|
|
|
attrs[seqname] = seq;
|
|
|
|
dataset.write(id, attrs);
|
2011-12-19 16:28:05 +00:00
|
|
|
});
|
2011-12-06 08:27:13 +00:00
|
|
|
}(dataset, record.get('id'), seq));
|
2012-08-07 13:43:43 +00:00
|
|
|
record.set(seqname, seq);
|
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;
|
2012-08-24 18:27:07 +00:00
|
|
|
var $el = $('<tbody>');
|
|
|
|
this.elements = [$el[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) {
|
2012-08-24 18:27:07 +00:00
|
|
|
$el[0].appendChild(
|
2011-07-11 11:32:28 +00:00
|
|
|
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) {
|
2012-10-30 14:06:30 +00:00
|
|
|
self.render_dataset(dataset).done(function (list) {
|
2011-07-11 11:32:28 +00:00
|
|
|
self.children[null] = list;
|
|
|
|
self.elements =
|
2012-08-24 18:27:07 +00:00
|
|
|
[list.$current.replaceAll($el)[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
|
|
|
});
|
2012-08-24 18:27:07 +00:00
|
|
|
return $el;
|
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()) {
|
2011-09-21 07:19:25 +00:00
|
|
|
if (!this.datagroup.length) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-08 12:55:31 +00:00
|
|
|
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
|
|
|
|
2012-10-17 10:59:48 +00:00
|
|
|
var DataGroup = instance.web.CallbackEnabled.extend({
|
2012-10-17 10:52:13 +00:00
|
|
|
init: function(parent, model, domain, context, group_by, level) {
|
|
|
|
this._super(parent, null);
|
|
|
|
this.model = new instance.web.Model(model, context, domain);
|
|
|
|
this.group_by = group_by;
|
|
|
|
this.context = context;
|
|
|
|
this.domain = domain;
|
|
|
|
|
|
|
|
this.level = level || 0;
|
|
|
|
},
|
|
|
|
list: function (fields, ifGroups, ifRecords) {
|
|
|
|
var self = this;
|
|
|
|
var query = this.model.query(fields).order_by(this.sort).group_by(this.group_by);
|
2012-10-30 14:06:30 +00:00
|
|
|
$.when(query).done(function (querygroups) {
|
2012-10-17 10:52:13 +00:00
|
|
|
// leaf node
|
|
|
|
if (!querygroups) {
|
|
|
|
var ds = new instance.web.DataSetSearch(self, self.model.name, self.model.context(), self.model.domain());
|
|
|
|
ds._sort = self.sort;
|
|
|
|
ifRecords(ds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// internal node
|
|
|
|
var child_datagroups = _(querygroups).map(function (group) {
|
|
|
|
var child_context = _.extend(
|
|
|
|
{}, self.model.context(), group.model.context());
|
2012-10-17 10:52:57 +00:00
|
|
|
var child_dg = new DataGroup(
|
2012-10-17 10:52:13 +00:00
|
|
|
self, self.model.name, group.model.domain(),
|
|
|
|
child_context, group.model._context.group_by,
|
|
|
|
self.level + 1);
|
|
|
|
child_dg.sort = self.sort;
|
|
|
|
// copy querygroup properties
|
|
|
|
child_dg.__context = child_context;
|
|
|
|
child_dg.__domain = group.model.domain();
|
|
|
|
child_dg.folded = group.get('folded');
|
|
|
|
child_dg.grouped_on = group.get('grouped_on');
|
|
|
|
child_dg.length = group.get('length');
|
|
|
|
child_dg.value = group.get('value');
|
|
|
|
child_dg.openable = group.get('has_children');
|
|
|
|
child_dg.aggregates = group.get('aggregates');
|
|
|
|
return child_dg;
|
|
|
|
});
|
|
|
|
ifGroups(child_datagroups);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var StaticDataGroup = DataGroup.extend({
|
|
|
|
init: function (dataset) {
|
|
|
|
this.dataset = dataset;
|
|
|
|
},
|
|
|
|
list: function (fields, ifGroups, ifRecords) {
|
|
|
|
ifRecords(this.dataset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
};
|
2012-04-17 12:15:59 +00:00
|
|
|
var Record = instance.web.Class.extend(/** @lends Record# */{
|
2011-08-17 08:55:32 +00:00
|
|
|
/**
|
2011-09-12 11:34:37 +00:00
|
|
|
* @constructs Record
|
2012-04-17 12:15:59 +00:00
|
|
|
* @extends instance.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 () {
|
2011-09-29 14:50:31 +00:00
|
|
|
var form_data = {}, attrs = this.attributes;
|
|
|
|
for(var k in attrs) {
|
|
|
|
form_data[k] = {value: attrs[k]};
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
|
|
|
|
return {data: form_data};
|
2011-10-28 08:50:51 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Converts the current record to a format expected by context evaluations
|
|
|
|
* (identical to record.attributes, except m2o fields are their integer
|
|
|
|
* value rather than a pair)
|
|
|
|
*/
|
|
|
|
toContext: function () {
|
|
|
|
var output = {}, attrs = this.attributes;
|
|
|
|
for(var k in attrs) {
|
|
|
|
var val = attrs[k];
|
|
|
|
if (typeof val !== 'object') {
|
|
|
|
output[k] = val;
|
|
|
|
} else if (val instanceof Array) {
|
|
|
|
output[k] = val[0];
|
|
|
|
} else {
|
|
|
|
throw new Error("Can't convert value " + val + " to context");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
2011-08-17 08:55:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Record.include(Events);
|
2012-04-17 12:15:59 +00:00
|
|
|
var Collection = instance.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
|
2012-04-17 12:15:59 +00:00
|
|
|
* @extends instance.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) {
|
2012-04-17 11:43:58 +00:00
|
|
|
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) {
|
2012-04-17 11:43:58 +00:00
|
|
|
this.records.push(instance_);
|
2011-08-22 09:04:58 +00:00
|
|
|
if (!options.silent) {
|
2012-04-17 11:43:58 +00:00
|
|
|
this.trigger('add', this, instance_, this.records.length-1);
|
2011-08-22 09:04:58 +00:00
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
} else {
|
2011-08-22 09:04:58 +00:00
|
|
|
var insertion_index = options.at + i;
|
2012-04-17 11:43:58 +00:00
|
|
|
this.records.splice(insertion_index, 0, instance_);
|
2011-08-22 09:04:58 +00:00
|
|
|
if (!options.silent) {
|
2012-04-17 11:43:58 +00:00
|
|
|
this.trigger('add', this, instance_, insertion_index);
|
2011-08-22 09:04:58 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2011-10-25 08:51:12 +00:00
|
|
|
reset: function (records, options) {
|
|
|
|
options = options || {};
|
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 = {};
|
2012-01-12 17:09:17 +00:00
|
|
|
_(this.records).invoke('unbind', null, this._onRecordEvent);
|
2011-08-17 13:16:35 +00:00
|
|
|
this.length = 0;
|
|
|
|
this.records = [];
|
|
|
|
this._byId = {};
|
|
|
|
if (records) {
|
|
|
|
this.add(records);
|
|
|
|
}
|
2011-10-25 08:51:12 +00:00
|
|
|
if (!options.silent) {
|
|
|
|
this.trigger('reset', this);
|
|
|
|
}
|
2011-08-17 13:16:35 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Removes the provided record from the collection
|
|
|
|
*
|
|
|
|
* @param {Record} record
|
|
|
|
* @returns this
|
|
|
|
*/
|
|
|
|
remove: function (record) {
|
2012-07-10 12:37:18 +00:00
|
|
|
var index = this.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
|
|
|
|
2012-01-12 17:09:17 +00:00
|
|
|
record.unbind(null, this._onRecordEvent);
|
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
|
|
|
|
2012-07-02 12:29:02 +00:00
|
|
|
_onRecordEvent: function (event) {
|
|
|
|
switch(event) {
|
2011-08-19 10:12:08 +00:00
|
|
|
// don't propagate reset events
|
2012-07-02 12:29:02 +00:00
|
|
|
case 'reset': return;
|
|
|
|
case 'change:id':
|
|
|
|
var record = arguments[1];
|
|
|
|
var new_value = arguments[2];
|
|
|
|
var old_value = arguments[3];
|
|
|
|
// [change:id, record, new_value, old_value]
|
|
|
|
if (this._byId[old_value] === record) {
|
|
|
|
delete this._byId[old_value];
|
|
|
|
this._byId[new_value] = record;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-08-17 08:55:32 +00:00
|
|
|
this.trigger.apply(this, arguments);
|
2011-08-18 12:21:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// underscore-type methods
|
2012-06-28 14:14:03 +00:00
|
|
|
find: function (callback) {
|
2012-07-10 12:37:18 +00:00
|
|
|
var record;
|
2012-06-28 14:14:03 +00:00
|
|
|
for(var section in this._proxies) {
|
2012-07-10 12:37:18 +00:00
|
|
|
if (!this._proxies.hasOwnProperty(section)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ((record = this._proxies[section].find(callback))) {
|
|
|
|
return record;
|
2012-06-28 14:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for(var i=0; i<this.length; ++i) {
|
2012-07-02 11:05:04 +00:00
|
|
|
record = this.records[i];
|
|
|
|
if (callback(record)) {
|
|
|
|
return record;
|
|
|
|
}
|
2012-06-28 14:14:03 +00:00
|
|
|
}
|
|
|
|
},
|
2011-08-18 12:21:06 +00:00
|
|
|
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);
|
2012-07-10 12:37:18 +00:00
|
|
|
},
|
|
|
|
succ: function (record, options) {
|
|
|
|
options = options || {wraparound: false};
|
|
|
|
var result;
|
|
|
|
for(var section in this._proxies) {
|
|
|
|
if (!this._proxies.hasOwnProperty(section)) {
|
2012-07-10 13:12:05 +00:00
|
|
|
continue;
|
2012-07-10 12:37:18 +00:00
|
|
|
}
|
|
|
|
if ((result = this._proxies[section].succ(record, options))) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var index = this.indexOf(record);
|
|
|
|
if (index === -1) { return null; }
|
|
|
|
var next_index = index + 1;
|
|
|
|
if (options.wraparound && (next_index === this.length)) {
|
|
|
|
return this.at(0);
|
|
|
|
}
|
|
|
|
return this.at(next_index);
|
2012-07-10 13:12:05 +00:00
|
|
|
},
|
|
|
|
pred: function (record, options) {
|
|
|
|
options = options || {wraparound: false};
|
|
|
|
|
|
|
|
var result;
|
|
|
|
for (var section in this._proxies) {
|
|
|
|
if (!this._proxies.hasOwnProperty(section)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((result = this._proxies[section].pred(record, options))) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var index = this.indexOf(record);
|
|
|
|
if (index === -1) { return null; }
|
|
|
|
var next_index = index - 1;
|
|
|
|
if (options.wraparound && (next_index === -1)) {
|
|
|
|
return this.at(this.length - 1);
|
|
|
|
}
|
|
|
|
return this.at(next_index);
|
2011-08-17 08:55:32 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
Collection.include(Events);
|
2012-04-17 12:15:59 +00:00
|
|
|
instance.web.list = {
|
2011-08-17 08:55:32 +00:00
|
|
|
Events: Events,
|
|
|
|
Record: Record,
|
|
|
|
Collection: Collection
|
2012-08-08 10:34:04 +00:00
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Registry for column objects used to format table cells (and some other tasks
|
|
|
|
* e.g. aggregation computations).
|
|
|
|
*
|
|
|
|
* Maps a field or button to a Column type via its ``$tag.$widget``,
|
|
|
|
* ``$tag.$type`` or its ``$tag`` (alone).
|
|
|
|
*
|
|
|
|
* This specific registry has a dedicated utility method ``for_`` taking a
|
|
|
|
* field (from fields_get/fields_view_get.field) and a node (from a view) and
|
|
|
|
* returning the right object *already instantiated from the data provided*.
|
|
|
|
*
|
|
|
|
* @type {instance.web.Registry}
|
|
|
|
*/
|
|
|
|
instance.web.list.columns = new instance.web.Registry({
|
|
|
|
'field': 'instance.web.list.Column',
|
|
|
|
'field.boolean': 'instance.web.list.Boolean',
|
|
|
|
'field.binary': 'instance.web.list.Binary',
|
|
|
|
'field.progressbar': 'instance.web.list.ProgressBar',
|
|
|
|
'field.handle': 'instance.web.list.Handle',
|
|
|
|
'button': 'instance.web.list.Button',
|
|
|
|
});
|
|
|
|
instance.web.list.columns.for_ = function (id, field, node) {
|
|
|
|
var description = _.extend({tag: node.tag}, field, node.attrs);
|
|
|
|
var tag = description.tag;
|
|
|
|
var Type = this.get_any([
|
|
|
|
tag + '.' + description.widget,
|
|
|
|
tag + '.'+ description.type,
|
|
|
|
tag
|
|
|
|
]);
|
|
|
|
return new Type(id, node.tag, description)
|
|
|
|
};
|
|
|
|
|
|
|
|
instance.web.list.Column = instance.web.Class.extend({
|
|
|
|
init: function (id, tag, attrs) {
|
|
|
|
_.extend(attrs, {
|
|
|
|
id: id,
|
|
|
|
tag: tag
|
|
|
|
});
|
|
|
|
|
|
|
|
this.modifiers = attrs.modifiers ? JSON.parse(attrs.modifiers) : {};
|
|
|
|
delete attrs.modifiers;
|
|
|
|
_.extend(this, attrs);
|
|
|
|
|
|
|
|
if (this.modifiers['tree_invisible']) {
|
|
|
|
this.invisible = '1';
|
|
|
|
} else { delete this.invisible; }
|
|
|
|
},
|
|
|
|
modifiers_for: function (fields) {
|
|
|
|
var out = {};
|
|
|
|
var domain_computer = instance.web.form.compute_domain;
|
|
|
|
|
|
|
|
for (var attr in this.modifiers) {
|
|
|
|
if (!this.modifiers.hasOwnProperty(attr)) { continue; }
|
|
|
|
var modifier = this.modifiers[attr];
|
|
|
|
out[attr] = _.isBoolean(modifier)
|
|
|
|
? modifier
|
|
|
|
: domain_computer(modifier, fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
},
|
|
|
|
to_aggregate: function () {
|
|
|
|
if (this.type !== 'integer' && this.type !== 'float') {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var aggregation_func = this['group_operator'] || 'sum';
|
|
|
|
if (!(aggregation_func in this)) {
|
|
|
|
return {};
|
|
|
|
}
|
2012-09-04 11:04:22 +00:00
|
|
|
var C = function (fn, label) {
|
2012-08-08 10:34:04 +00:00
|
|
|
this['function'] = fn;
|
|
|
|
this.label = label;
|
|
|
|
};
|
|
|
|
C.prototype = this;
|
|
|
|
return new C(aggregation_func, this[aggregation_func]);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param row_data record whose values should be displayed in the cell
|
|
|
|
* @param {Object} [options]
|
|
|
|
* @param {String} [options.value_if_empty=''] what to display if the field's value is ``false``
|
|
|
|
* @param {Boolean} [options.process_modifiers=true] should the modifiers be computed ?
|
|
|
|
* @param {String} [options.model] current record's model
|
|
|
|
* @param {Number} [options.id] current record's id
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
format: function (row_data, options) {
|
|
|
|
options = options || {};
|
|
|
|
var attrs = {};
|
|
|
|
if (options.process_modifiers !== false) {
|
|
|
|
attrs = this.modifiers_for(row_data);
|
|
|
|
}
|
|
|
|
if (attrs.invisible) { return ''; }
|
|
|
|
|
|
|
|
if (!row_data[this.id]) {
|
|
|
|
return options.value_if_empty === undefined
|
|
|
|
? ''
|
|
|
|
: options.value_if_empty;
|
|
|
|
}
|
|
|
|
return this._format(row_data, options);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Method to override in order to provide alternative HTML content for the
|
|
|
|
* cell. Column._format will simply call ``instance.web.format_value`` and
|
|
|
|
* escape the output.
|
|
|
|
*
|
|
|
|
* The output of ``_format`` will *not* be escaped by ``format``, any
|
|
|
|
* escaping *must be done* by ``format``.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_format: function (row_data, options) {
|
|
|
|
return _.escape(instance.web.format_value(
|
|
|
|
row_data[this.id].value, this, options.value_if_empty));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.MetaColumn = instance.web.list.Column.extend({
|
|
|
|
meta: true,
|
|
|
|
init: function (id, string) {
|
|
|
|
this._super(id, '', {string: string});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.Button = instance.web.list.Column.extend({
|
|
|
|
/**
|
|
|
|
* Return an actual ``<button>`` tag
|
|
|
|
*/
|
|
|
|
format: function (row_data, options) {
|
2012-09-28 12:17:47 +00:00
|
|
|
options = options || {};
|
|
|
|
var attrs = {};
|
|
|
|
if (options.process_modifiers !== false) {
|
|
|
|
attrs = this.modifiers_for(row_data);
|
|
|
|
}
|
|
|
|
if (attrs.invisible) { return ''; }
|
|
|
|
|
|
|
|
return QWeb.render('ListView.row.button', {
|
|
|
|
widget: this,
|
|
|
|
prefix: instance.session.prefix,
|
|
|
|
disabled: attrs.readonly
|
|
|
|
|| isNaN(row_data.id.value)
|
|
|
|
|| instance.web.BufferedDataSet.virtual_id_regex.test(row_data.id.value)
|
|
|
|
});
|
2012-08-08 10:34:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.Boolean = instance.web.list.Column.extend({
|
|
|
|
/**
|
|
|
|
* Return a potentially disabled checkbox input
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_format: function (row_data, options) {
|
|
|
|
return _.str.sprintf('<input type="checkbox" %s disabled="disabled"/>',
|
|
|
|
row_data[this.id].value ? 'checked="checked"' : '');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.Binary = instance.web.list.Column.extend({
|
|
|
|
/**
|
|
|
|
* Return a link to the binary data as a file
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_format: function (row_data, options) {
|
|
|
|
var text = _t("Download");
|
|
|
|
var download_url = _.str.sprintf(
|
|
|
|
'/web/binary/saveas?session_id=%s&model=%s&field=%s&id=%d',
|
2012-08-14 15:29:00 +00:00
|
|
|
instance.session.session_id, options.model, this.id, options.id);
|
2012-08-08 10:34:04 +00:00
|
|
|
if (this.filename) {
|
|
|
|
download_url += '&filename_field=' + this.filename;
|
|
|
|
if (row_data[this.filename]) {
|
|
|
|
text = _.str.sprintf(_t("Download \"%s\""), instance.web.format_value(
|
|
|
|
row_data[this.filename].value, {type: 'char'}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _.template('<a href="<%-href%>"><%-text%></a> (%<-size%>)', {
|
|
|
|
text: text,
|
|
|
|
href: download_url,
|
|
|
|
size: row_data[this.id].value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.ProgressBar = instance.web.list.Column.extend({
|
|
|
|
/**
|
|
|
|
* Return a formatted progress bar display
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_format: function (row_data, options) {
|
|
|
|
return _.template(
|
|
|
|
'<progress value="<%-value%>" max="100"><%-value%>%</progress>', {
|
|
|
|
value: _.str.sprintf("%.0f", row_data[this.id].value || 0)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
instance.web.list.Handle = instance.web.list.Column.extend({
|
2012-09-04 11:56:27 +00:00
|
|
|
init: function () {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
// Handle overrides the field to not be form-editable.
|
|
|
|
this.modifiers.readonly = true;
|
|
|
|
},
|
2012-08-08 10:34:04 +00:00
|
|
|
/**
|
|
|
|
* Return styling hooks for a drag handle
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_format: function (row_data, options) {
|
|
|
|
return '<div class="oe_list_handle">';
|
|
|
|
}
|
|
|
|
});
|
2011-08-17 08:55:32 +00:00
|
|
|
};
|
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|