[ADD] on_write, dix some bugs with listview

bzr revid: xmo@openerp.com-20110824123029-7hyaf0eyxffxui7e
This commit is contained in:
Xavier Morel 2011-08-24 14:30:29 +02:00
commit 6753f639d1
3 changed files with 95 additions and 62 deletions

View File

@ -92,7 +92,7 @@ openerp.base.list_editable = function (openerp) {
if (!this.options.editable) {
return this._super(event);
}
this.edit_record();
this.edit_record($(event.currentTarget).data('id'));
},
/**
* Checks if a record is being edited, and if so cancels it
@ -104,8 +104,8 @@ openerp.base.list_editable = function (openerp) {
return cancelled.promise();
}
if (this.edition_index !== null) {
this.reload_record(this.edition_index).then(function () {
if (this.edition_id != null) {
this.reload_record(self.records.get(this.edition_id)).then(function () {
cancelled.resolve();
});
} else {
@ -115,13 +115,14 @@ openerp.base.list_editable = function (openerp) {
self.edition_form.stop();
self.edition_form.$element.remove();
delete self.edition_form;
delete self.edition_index;
delete self.edition_id;
delete self.edition;
});
return cancelled.promise();
},
/**
* Adapts this list's view description to be suitable to the inner form view of a row being edited.
* Adapts this list's view description to be suitable to the inner form
* view of a row being edited.
*
* @returns {Object} fields_view_get's view section suitable for putting into form view of editable rows.
*/
@ -140,10 +141,10 @@ openerp.base.list_editable = function (openerp) {
render_row_as_form: function (row) {
var self = this;
this.cancel_pending_edition().then(function () {
var record_id = $(row).data('id');
var $new_row = $('<tr>', {
id: _.uniqueId('oe-editable-row-'),
'data-id': $(row).data('id'),
'data-index': $(row).data('index'),
'data-id': record_id,
'class': $(row).attr('class') + ' oe_forms',
click: function (e) {e.stopPropagation();}
})
@ -176,7 +177,7 @@ openerp.base.list_editable = function (openerp) {
self.$current.append($new_row);
}
self.edition = true;
self.edition_index = self.dataset.index;
self.edition_id = record_id;
self.edition_form = _.extend(new openerp.base.FormView(
self, $new_row.attr('id'), self.dataset, false), {
template: 'ListView.row.form',
@ -201,6 +202,25 @@ openerp.base.list_editable = function (openerp) {
});
});
},
handle_onwrite: function (source_record_id) {
var self = this;
var on_write_callback = self.view.fields_view.arch.attrs.on_write;
if (!on_write_callback) { return; }
this.dataset.call(on_write_callback, [source_record_id], function (ids) {
_(ids).each(function (id) {
var record = self.records.get(id);
if (!record) {
// insert after the source record
var index = self.records.indexOf(
self.records.get(source_record_id)) + 1;
record = new openerp.base.list.Record({id: id});
self.records.add(record, {at: index});
self.dataset.ids.splice(index, 0, id);
}
self.reload_record(record);
});
});
},
/**
* Saves the current row, and triggers the edition of its following
* sibling if asked.
@ -210,11 +230,16 @@ openerp.base.list_editable = function (openerp) {
save_row: function (edit_next) {
var self = this;
this.edition_form.do_save(function (result) {
if (result.created && !self.edition_index) {
if (result.created && !self.edition_id) {
self.records.add({id: result.result},
{at: self.options.editable === 'top' ? 0 : null});
self.edition_index = self.dataset.index;
self.edition_id = result.result;
}
var edited_record = self.records.get(self.edition_id),
next_record = self.records.at(
self.records.indexOf(edited_record) + 1);
self.handle_onwrite(self.edition_id);
self.cancel_pending_edition().then(function () {
$(self).trigger('saved', [self.dataset]);
if (!edit_next) {
@ -224,8 +249,16 @@ openerp.base.list_editable = function (openerp) {
self.new_record();
return;
}
self.dataset.next();
self.edit_record();
var next_record_id;
if (next_record) {
next_record_id = next_record.get('id');
self.dataset.index = _(self.dataset.ids)
.indexOf(next_record_id);
} else {
self.dataset.index = 0;
next_record_id = self.records.at(0).get('id');
}
self.edit_record(next_record_id);
});
}, this.options.editable === 'top');
},
@ -238,12 +271,12 @@ openerp.base.list_editable = function (openerp) {
/**
* Edits record currently selected via dataset
*/
edit_record: function () {
edit_record: function (record_id) {
this.render_row_as_form(
this.$current.children(':eq(' + this.dataset.index + ')'));
this.$current.find('[data-id=' + record_id + ']'));
$(this).trigger(
'edit',
[this.records.at(this.dataset.index).get('id'), this.dataset]);
[record_id, this.dataset]);
},
new_record: function () {
this.dataset.index = null;

View File

@ -641,12 +641,22 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
'reset': $.proxy(this, 'on_records_reset'),
'change': function (event, record) {
var $row = self.$current.find('[data-id=' + record.get('id') + ']');
$row.replaceWith(self.render_record($row.data('index')));
$row.replaceWith(self.render_record(record));
},
'add': function (ev, records, record, index) {
$('<tr>').attr({
var $new_row = $('<tr>').attr({
'data-id': record.get('id')
}).insertAfter(self.$current.children(':eq(' + index + ')'));
});
if (index === 0) {
$new_row.prependTo(self.$current);
} else {
var previous_record = records.at(index-1),
$previous_sibling = self.$current.find(
'[data-id=' + previous_record.get('id') + ']');
$new_row.insertAfter($previous_sibling);
}
self.refresh_zebra(index, 1);
}
};
@ -672,16 +682,17 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
var $target = $(e.currentTarget),
field = $target.closest('td').data('field'),
$row = $target.closest('tr'),
record_id = self.row_id($row),
index = self.row_position($row);
record_id = self.row_id($row);
$(self).trigger('action', [field, record_id, function () {
return self.reload_record(index);
return self.reload_record(self.records.get(record_id));
}]);
})
.delegate('tr', 'click', function (e) {
e.stopPropagation();
self.dataset.index = self.row_position(e.currentTarget);
self.dataset.index = self.records.indexOf(
self.records.get(
self.row_id(e.currentTarget)));
self.row_clicked(e);
});
},
@ -718,15 +729,6 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
});
return result;
},
/**
* Returns the index of the row in the list of rows.
*
* @param {Object} row the selected row
* @returns {Number} the position of the row in this.rows
*/
row_position: function (row) {
return $(row).data('index');
},
/**
* Returns the identifier of the object displayed in the provided table
* row
@ -755,48 +757,43 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
});
},
/**
* Reloads the record at index ``row_index`` in the list's rows.
* Reloads the provided record by re-reading its content from the server.
*
* By default, simply re-renders the record. If the ``fetch`` parameter is
* provided and ``true``, will first fetch the record anew.
*
* @param {Number} record_index index of the record to reload
* @param {Boolean} fetch fetches the record from remote before reloading it
* @param {Record} record
* @returns {$.Deferred} promise to the finalization of the reloading
*/
reload_record: function (record_index) {
var r = this.records.at(record_index);
reload_record: function (record) {
return this.dataset.read_ids(
[r.get('id')],
[record.get('id')],
_.pluck(_(this.columns).filter(function (r) {
return r.tag === 'field';
}), 'name'),
function (record) {
_(record[0]).each(function (value, key) {
r.set(key, value, {silent: true});
function (records) {
_(records[0]).each(function (value, key) {
record.set(key, value, {silent: true});
});
r.trigger('change', r);
record.trigger('change', record);
}
);
},
/**
* Renders a list record to HTML
*
* @param {Number} record_index index of the record to render in ``this.rows``
* @param {Record} record index of the record to render in ``this.rows``
* @returns {String} QWeb rendering of the selected record
*/
render_record: function (record_index) {
render_record: function (record) {
var index = this.records.indexOf(record);
return QWeb.render('ListView.row', {
columns: this.columns,
options: this.options,
record: this.records.at(record_index),
row_parity: (record_index % 2 === 0) ? 'even' : 'odd',
row_index: record_index,
record: record,
row_parity: (index % 2 === 0) ? 'even' : 'odd',
render_cell: openerp.base.format_cell
});
},
/**
* Fixes @data-index on all table rows, and fixes the even/odd classes
* Fixes fixes the even/odd classes
*
* @param {Number} [from_index] index from which to resequence
* @param {Number} [offset = 0] selection offset for DOM, in case there are rows to ignore in the table
@ -810,8 +807,7 @@ openerp.base.ListView.List = openerp.base.Class.extend( /** @lends openerp.base.
var index = from_index + i;
// reset record-index accelerators on rows and even/odd
var even = index%2 === 0;
$(e).attr('data-index', index)
.toggleClass('even', even)
$(e).toggleClass('even', even)
.toggleClass('odd', !even);
});
}
@ -1075,14 +1071,12 @@ openerp.base.ListView.Groups = openerp.base.Class.extend( /** @lends openerp.bas
// ondrop, move relevant record & fix sequences
list.$current.sortable({
stop: function (event, ui) {
var from = ui.item.data('index'),
to = ui.item.prev().data('index') || 0;
if (from === to) { return; }
var to_move = list.records.at(from);
list.records.remove(to_move);
list.records.add(to_move, {at: to});
var to_move = list.records.get(ui.item.data('id')),
target_id = ui.item.prev().data('id');
list.refresh_zebra();
list.records.remove(to_move);
var to = target_id ? list.records.indexOf(list.records.get(target_id)) : 0;
list.records.add(to_move, { at: to });
// resequencing time!
var record, index = to,
@ -1096,6 +1090,8 @@ openerp.base.ListView.Groups = openerp.base.Class.extend( /** @lends openerp.bas
dataset.write(record.get('id'), {sequence: seq});
record.set('sequence', seq);
}
list.refresh_zebra();
}
});
},
@ -1440,6 +1436,11 @@ var Collection = openerp.base.Class.extend(/** @lends Collection# */{
});
return results;
},
pluck: function (fieldname) {
return this.map(function (record) {
return record.get(fieldname);
});
},
indexOf: function (record) {
return _(this.records).indexOf(record);
}

View File

@ -554,12 +554,11 @@
<t t-name="ListView.rows" t-foreach="records.length" t-as="index">
<t t-call="ListView.row">
<t t-set="record" t-value="records.at(index)"/>
<t t-set="row_index" t-value="index"/>
<t t-set="row_parity" t-value="index_parity"/>
</t>
</t>
<tr t-name="ListView.row" t-att-class="row_parity"
t-att-data-index="row_index" t-att-data-id="record.get('id')">
t-att-data-id="record.get('id')">
<t t-foreach="columns" t-as="column">
<td t-if="column.meta">