2011-03-30 14:35:56 +00:00
|
|
|
openerp.base.list = function (openerp) {
|
2011-03-31 15:25:31 +00:00
|
|
|
openerp.base.views.add('list', 'openerp.base.ListView');
|
2011-04-07 12:12:22 +00:00
|
|
|
openerp.base.ListView = openerp.base.Controller.extend(
|
|
|
|
/** @lends openerp.base.ListView# */ {
|
|
|
|
defaults: {
|
|
|
|
// records can be selected one by one
|
2011-04-07 13:27:26 +00:00
|
|
|
'selectable': true,
|
|
|
|
// whether the column headers should be displayed
|
|
|
|
'header': true
|
2011-04-07 12:12:22 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @constructs
|
|
|
|
* @param view_manager
|
|
|
|
* @param session An OpenERP session object
|
|
|
|
* @param element_id the id of the DOM elements this view should link itself to
|
|
|
|
* @param {openerp.base.DataSet} dataset the dataset the view should work with
|
|
|
|
* @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 12:12:22 +00:00
|
|
|
*/
|
|
|
|
init: function(view_manager, session, element_id, dataset, view_id, options) {
|
2011-03-30 14:00:48 +00:00
|
|
|
this._super(session, element_id);
|
2011-04-04 13:47:05 +00:00
|
|
|
this.view_manager = view_manager;
|
2011-03-30 14:00:48 +00:00
|
|
|
this.dataset = dataset;
|
|
|
|
this.model = dataset.model;
|
|
|
|
this.view_id = view_id;
|
|
|
|
|
2011-04-06 13:58:37 +00:00
|
|
|
this.columns = [];
|
|
|
|
this.rows = [];
|
2011-04-07 12:12:22 +00:00
|
|
|
|
|
|
|
this.options = _.extend({}, this.defaults, options || {});
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
|
|
|
start: function() {
|
|
|
|
//this.log('Starting ListView '+this.model+this.view_id)
|
2011-04-01 11:50:05 +00:00
|
|
|
return this.rpc("/base/listview/load", {"model": this.model, "view_id":this.view_id}, this.on_loaded);
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
|
|
|
on_loaded: function(data) {
|
|
|
|
this.fields_view = data.fields_view;
|
|
|
|
//this.log(this.fields_view);
|
|
|
|
this.name = "" + this.fields_view.arch.attrs.string;
|
2011-04-04 15:06:19 +00:00
|
|
|
|
2011-04-06 13:58:37 +00:00
|
|
|
var fields = this.fields_view.fields;
|
|
|
|
this.columns = _(this.fields_view.arch.children).chain()
|
|
|
|
.map(function (field) {
|
|
|
|
var name = field.attrs.name;
|
|
|
|
return _.extend({id: name, tag: field.tag}, field.attrs, fields[name]);
|
|
|
|
}).value();
|
|
|
|
|
|
|
|
this.$element.html(QWeb.render("ListView", this));
|
2011-04-07 12:12:22 +00:00
|
|
|
this.$element.find('table').delegate(
|
|
|
|
'th.oe-record-selector', 'click', function (e) {
|
|
|
|
// A click in the selection cell should not activate the
|
|
|
|
// linking feature
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
});
|
2011-04-07 06:07:20 +00:00
|
|
|
this.$element.find('table').delegate(
|
|
|
|
'tr', 'click', this.on_select_row);
|
2011-03-30 14:00:48 +00:00
|
|
|
|
2011-04-04 13:47:05 +00:00
|
|
|
// sidebar stuff
|
2011-04-04 16:08:13 +00:00
|
|
|
if (this.view_manager.sidebar)
|
|
|
|
this.view_manager.sidebar.load_multi_actions();
|
2011-03-30 14:00:48 +00:00
|
|
|
},
|
2011-04-07 12:12:22 +00:00
|
|
|
/**
|
|
|
|
* Fills the table with the provided records after emptying it
|
|
|
|
*
|
|
|
|
* @param {Array} records the records to fill the list view with
|
|
|
|
* @returns {Promise} promise to the end of view rendering (list views are asynchronously filled for improved responsiveness)
|
|
|
|
*/
|
2011-03-30 14:00:48 +00:00
|
|
|
do_fill_table: function(records) {
|
2011-04-06 13:58:37 +00:00
|
|
|
this.rows = records;
|
|
|
|
|
2011-04-07 08:36:24 +00:00
|
|
|
var $table = this.$element.find('table');
|
2011-04-07 06:07:20 +00:00
|
|
|
// remove all data lines
|
2011-04-07 08:36:24 +00:00
|
|
|
$table.find('tbody').remove();
|
|
|
|
|
2011-04-07 06:07:20 +00:00
|
|
|
// add new content
|
2011-04-07 12:12:22 +00:00
|
|
|
var columns = this.columns,
|
|
|
|
rows = this.rows,
|
|
|
|
options = this.options;
|
|
|
|
|
2011-04-07 08:36:24 +00:00
|
|
|
// Paginate by groups of 50 for rendering
|
2011-04-07 12:12:22 +00:00
|
|
|
var PAGE_SIZE = 50,
|
|
|
|
bodies_count = Math.ceil(this.rows.length / PAGE_SIZE),
|
|
|
|
body = 0,
|
|
|
|
$body = $('<tbody>').appendTo($table);
|
|
|
|
|
2011-04-07 08:36:24 +00:00
|
|
|
var render_body = function () {
|
2011-04-07 12:12:22 +00:00
|
|
|
var rendered = $.Deferred();
|
2011-04-07 08:36:24 +00:00
|
|
|
setTimeout(function () {
|
|
|
|
$body.append(
|
|
|
|
QWeb.render("ListView.rows", {
|
|
|
|
columns: columns,
|
2011-04-07 12:12:22 +00:00
|
|
|
rows: rows.slice(body*PAGE_SIZE, (body+1)*PAGE_SIZE),
|
|
|
|
options: options
|
2011-04-07 08:36:24 +00:00
|
|
|
}));
|
|
|
|
++body;
|
|
|
|
if (body < bodies_count) {
|
|
|
|
render_body();
|
2011-04-07 12:12:22 +00:00
|
|
|
} else {
|
|
|
|
rendered.resolve();
|
2011-04-07 08:36:24 +00:00
|
|
|
}
|
|
|
|
}, 0);
|
2011-04-07 12:12:22 +00:00
|
|
|
return rendered.promise();
|
2011-04-07 08:36:24 +00:00
|
|
|
};
|
2011-04-07 12:12:22 +00:00
|
|
|
return render_body();
|
2011-03-31 14:44:22 +00:00
|
|
|
},
|
2011-04-07 06:07:20 +00:00
|
|
|
on_select_row: function (event) {
|
2011-04-07 09:09:00 +00:00
|
|
|
var $target = $(event.currentTarget);
|
|
|
|
if (!$target.parent().is('tbody')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// count number of preceding siblings to line clicked
|
|
|
|
var row = this.rows[$target.prevAll().length];
|
2011-04-07 06:07:20 +00:00
|
|
|
|
|
|
|
var index = _.indexOf(this.dataset.ids, row.id);
|
|
|
|
if (index == undefined || index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.dataset.index = index;
|
|
|
|
_.delay(_.bind(function () {
|
|
|
|
this.view_manager.on_mode_switch('form');
|
|
|
|
}, this));
|
|
|
|
|
|
|
|
},
|
2011-04-04 15:34:26 +00:00
|
|
|
do_show: function () {
|
|
|
|
// TODO: re-trigger search
|
|
|
|
this.$element.show();
|
|
|
|
},
|
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
|
|
|
},
|
2011-03-31 14:44:22 +00:00
|
|
|
do_search: function (domains, contexts, groupbys) {
|
|
|
|
var self = this;
|
|
|
|
this.rpc('/base/session/eval_domain_and_context', {
|
|
|
|
domains: domains,
|
|
|
|
contexts: contexts,
|
|
|
|
group_by_seq: groupbys
|
|
|
|
}, function (results) {
|
|
|
|
// TODO: handle non-empty results.group_by with read_group
|
2011-04-04 15:06:19 +00:00
|
|
|
self.dataset.context = results.context;
|
|
|
|
self.dataset.domain = results.domain;
|
|
|
|
self.dataset.fetch(self.dataset.fields, 0, self.limit, self.do_fill_table);
|
2011-03-31 14:44:22 +00:00
|
|
|
});
|
2011-04-05 10:38:35 +00:00
|
|
|
},
|
|
|
|
do_update: function () {
|
|
|
|
var self = this;
|
|
|
|
self.dataset.fetch(self.dataset.fields, 0, self.limit, self.do_fill_table);
|
2011-04-07 12:46:39 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Gets the ids of all currently selected records, if any
|
|
|
|
* @returns a list of ids, empty if no record is selected (or the list view is not selectable
|
|
|
|
*/
|
|
|
|
get_selection: function () {
|
|
|
|
if (!this.options.selectable) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
var rows = this.rows;
|
|
|
|
return this.$element.find('th.oe-record-selector input:checked')
|
|
|
|
.closest('tr').map(function () {
|
|
|
|
return rows[$(this).prevAll().length].id;
|
|
|
|
}).get();
|
2011-03-30 14:00:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
openerp.base.TreeView = openerp.base.Controller.extend({
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|