[IMP] make get_selection call and selected ListView event provide both ids of selected records and the selected records themselves

bzr revid: xmo@openerp.com-20110525092813-ciahklygwshkpdl0
This commit is contained in:
Xavier Morel 2011-05-25 11:28:13 +02:00
parent 069da6ed5e
commit 3dbc88c696
1 changed files with 41 additions and 18 deletions

View File

@ -61,9 +61,8 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
columns: this.columns columns: this.columns
}); });
$(this.groups).bind({ $(this.groups).bind({
'selected': function (e, selection) { 'selected': function (e, ids, records) {
self.$element.find('#oe-list-delete') self.do_select(ids, records);
.toggle(!!selection.length);
}, },
'deleted': function (e, ids) { 'deleted': function (e, ids) {
self.do_delete(ids); self.do_delete(ids);
@ -330,6 +329,16 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
// TODO only refresh modified rows // TODO only refresh modified rows
}); });
}, },
/**
* 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) {
this.$element.find('#oe-list-delete')
.toggle(!!ids.length);
},
/** /**
* Handles signal for the addition of a new record (can be a creation, * Handles signal for the addition of a new record (can be a creation,
* can be the addition from a remote source, ...) * can be the addition from a remote source, ...)
@ -344,7 +353,7 @@ openerp.base.ListView = openerp.base.View.extend( /** @lends openerp.base.ListVi
* Handles deletion of all selected lines * Handles deletion of all selected lines
*/ */
do_delete_selected: function () { do_delete_selected: function () {
this.do_delete(this.groups.get_selection()); this.do_delete(this.groups.get_selection().ids);
} }
// TODO: implement reorder (drag and drop rows) // TODO: implement reorder (drag and drop rows)
}); });
@ -389,7 +398,8 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
.appendTo(document.body) .appendTo(document.body)
.delegate('th.oe-record-selector', 'click', function (e) { .delegate('th.oe-record-selector', 'click', function (e) {
e.stopPropagation(); e.stopPropagation();
$(self).trigger('selected', [self.get_selection()]); var selection = self.get_selection();
$(self).trigger('selected', [selection.ids, selection.records]);
}) })
.delegate('td.oe-record-delete button', 'click', function (e) { .delegate('td.oe-record-delete button', 'click', function (e) {
e.stopPropagation(); e.stopPropagation();
@ -422,17 +432,24 @@ openerp.base.ListView.List = Class.extend( /** @lends openerp.base.ListView.List
}, },
/** /**
* Gets the ids of all currently selected records, if any * Gets the ids of all currently selected records, if any
* @returns {Array} empty if no record is selected (or the list view is not selectable) * @returns {Object} object with the keys ``ids`` and ``records``, holding respectively the ids of all selected records and the records themselves.
*/ */
get_selection: function () { get_selection: function () {
if (!this.options.selectable) { if (!this.options.selectable) {
return []; return [];
} }
var rows = this.rows; var rows = this.rows;
return this.$current.find('th.oe-record-selector input:checked') var result = {ids: [], records: []};
.closest('tr').map(function () { this.$current.find('th.oe-record-selector input:checked')
return rows[$(this).prevAll().length].data.id.value; .closest('tr').each(function () {
}).get(); var record = {};
_(rows[$(this).prevAll().length].data).each(function (obj, key) {
record[key] = obj.value;
});
result.ids.push(record.id);
result.records.push(record);
});
return result;
}, },
/** /**
* Returns the index of the row in the list of rows. * Returns the index of the row in the list of rows.
@ -615,7 +632,8 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
self = this; self = this;
$(child).bind('selected', function (e) { $(child).bind('selected', function (e) {
// can have selections spanning multiple links // can have selections spanning multiple links
$this.trigger(e, [self.get_selection()]); var selection = self.get_selection();
$this.trigger(e, [selection.ids, selection.records]);
}).bind('action', function (e, name, id, callback) { }).bind('action', function (e, name, id, callback) {
if (!callback) { if (!callback) {
callback = function () { callback = function () {
@ -683,15 +701,20 @@ openerp.base.ListView.Groups = Class.extend( /** @lends openerp.base.ListView.Gr
return $element; return $element;
}, },
/** /**
* Returns the ids of all selected records for this group * Returns the ids of all selected records for this group, and the records
* themselves
*/ */
get_selection: function () { get_selection: function () {
return _(this.children).chain() var ids = [], records = [];
.map(function (child) {
return child.get_selection(); _(this.children)
}) .each(function (child) {
.flatten() var selection = child.get_selection();
.value(); ids.push.apply(ids, selection.ids);
records.push.apply(records, selection.records);
});
return {ids: ids, records: records};
}, },
apoptosis: function () { apoptosis: function () {
_(this.children).each(function (child) { _(this.children).each(function (child) {