[ADD] successor method to list collections, to safely get the "next" record in a nested (groupby) context
bzr revid: xmo@openerp.com-20120710123718-kiot6zzji07a4iao
This commit is contained in:
parent
611fc6cc4f
commit
079e90afbb
|
@ -1849,7 +1849,7 @@ var Collection = instance.web.Class.extend(/** @lends Collection# */{
|
||||||
* @returns this
|
* @returns this
|
||||||
*/
|
*/
|
||||||
remove: function (record) {
|
remove: function (record) {
|
||||||
var index = _(this.records).indexOf(record);
|
var index = this.indexOf(record);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
_(this._proxies).each(function (proxy) {
|
_(this._proxies).each(function (proxy) {
|
||||||
proxy.remove(record);
|
proxy.remove(record);
|
||||||
|
@ -1885,12 +1885,14 @@ var Collection = instance.web.Class.extend(/** @lends Collection# */{
|
||||||
|
|
||||||
// underscore-type methods
|
// underscore-type methods
|
||||||
find: function (callback) {
|
find: function (callback) {
|
||||||
var record = null;
|
var record;
|
||||||
for(var section in this._proxies) {
|
for(var section in this._proxies) {
|
||||||
if (this._proxies.hasOwnProperty(section)) {
|
if (!this._proxies.hasOwnProperty(section)) {
|
||||||
record = this._proxies[section].find(callback);
|
continue
|
||||||
|
}
|
||||||
|
if ((record = this._proxies[section].find(callback))) {
|
||||||
|
return record;
|
||||||
}
|
}
|
||||||
if (record) { return record; }
|
|
||||||
}
|
}
|
||||||
for(var i=0; i<this.length; ++i) {
|
for(var i=0; i<this.length; ++i) {
|
||||||
record = this.records[i];
|
record = this.records[i];
|
||||||
|
@ -1923,6 +1925,25 @@ var Collection = instance.web.Class.extend(/** @lends Collection# */{
|
||||||
},
|
},
|
||||||
indexOf: function (record) {
|
indexOf: function (record) {
|
||||||
return _(this.records).indexOf(record);
|
return _(this.records).indexOf(record);
|
||||||
|
},
|
||||||
|
succ: 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].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);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Collection.include(Events);
|
Collection.include(Events);
|
||||||
|
|
|
@ -359,12 +359,8 @@ openerp.web.list_editable = function (instance) {
|
||||||
if (saveInfo.created) {
|
if (saveInfo.created) {
|
||||||
return self.startEdition();
|
return self.startEdition();
|
||||||
}
|
}
|
||||||
var next_index = self.records.indexOf(saveInfo.record) + 1;
|
return self.startEdition(
|
||||||
if (next_index === self.records.length) {
|
self.records.succ(saveInfo.record, {wraparound: true}));
|
||||||
next_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self.startEdition(self.records.at(next_index));
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
keyup_ESCAPE: function () {
|
keyup_ESCAPE: function () {
|
||||||
|
|
|
@ -133,7 +133,7 @@ $(document).ready(function () {
|
||||||
strictEqual(changed, 1);
|
strictEqual(changed, 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
module('list-collections-degenerate', {
|
module('list-collections', {
|
||||||
setup: function () {
|
setup: function () {
|
||||||
openerp = window.openerp.init([]);
|
openerp = window.openerp.init([]);
|
||||||
window.openerp.web.corelib(openerp);
|
window.openerp.web.corelib(openerp);
|
||||||
|
@ -145,7 +145,7 @@ $(document).ready(function () {
|
||||||
window.openerp.web.list(openerp);
|
window.openerp.web.list(openerp);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
test('Fetch from collection', function () {
|
test('degenerate-fetch', function () {
|
||||||
var c = new openerp.web.list.Collection();
|
var c = new openerp.web.list.Collection();
|
||||||
strictEqual(c.length, 0);
|
strictEqual(c.length, 0);
|
||||||
c.add({id: 1, value: 2});
|
c.add({id: 1, value: 2});
|
||||||
|
@ -163,7 +163,7 @@ $(document).ready(function () {
|
||||||
strictEqual(r2.get('id'), 1);
|
strictEqual(r2.get('id'), 1);
|
||||||
strictEqual(r2.get('value'), 2);
|
strictEqual(r2.get('value'), 2);
|
||||||
});
|
});
|
||||||
test('Add at index', function () {
|
test('degenerate-indexed-add', function () {
|
||||||
var c = new openerp.web.list.Collection([
|
var c = new openerp.web.list.Collection([
|
||||||
{id: 1, value: 5},
|
{id: 1, value: 5},
|
||||||
{id: 2, value: 10},
|
{id: 2, value: 10},
|
||||||
|
@ -175,7 +175,7 @@ $(document).ready(function () {
|
||||||
strictEqual(c.at(1).get('value'), 55);
|
strictEqual(c.at(1).get('value'), 55);
|
||||||
strictEqual(c.at(3).get('value'), 20);
|
strictEqual(c.at(3).get('value'), 20);
|
||||||
});
|
});
|
||||||
test('Remove record', function () {
|
test('degenerate-remove', function () {
|
||||||
var c = new openerp.web.list.Collection([
|
var c = new openerp.web.list.Collection([
|
||||||
{id: 1, value: 5},
|
{id: 1, value: 5},
|
||||||
{id: 2, value: 10},
|
{id: 2, value: 10},
|
||||||
|
@ -188,7 +188,7 @@ $(document).ready(function () {
|
||||||
equal(c.get(2), undefined);
|
equal(c.get(2), undefined);
|
||||||
strictEqual(c.at(1).get('value'), 20);
|
strictEqual(c.at(1).get('value'), 20);
|
||||||
});
|
});
|
||||||
test('Remove unbind', function () {
|
test('degenerate-remove-bound', function () {
|
||||||
var changed = false,
|
var changed = false,
|
||||||
c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
|
c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
|
||||||
c.bind('change', function () { changed = true; });
|
c.bind('change', function () { changed = true; });
|
||||||
|
@ -198,7 +198,7 @@ $(document).ready(function () {
|
||||||
ok(!changed, 'removed records should not trigger events in their ' +
|
ok(!changed, 'removed records should not trigger events in their ' +
|
||||||
'parent collection');
|
'parent collection');
|
||||||
});
|
});
|
||||||
test('Reset', function () {
|
test('degenerate-reset', function () {
|
||||||
var event, obj, c = new openerp.web.list.Collection([
|
var event, obj, c = new openerp.web.list.Collection([
|
||||||
{id: 1, value: 5},
|
{id: 1, value: 5},
|
||||||
{id: 2, value: 10},
|
{id: 2, value: 10},
|
||||||
|
@ -218,7 +218,7 @@ $(document).ready(function () {
|
||||||
strictEqual(c.length, 1);
|
strictEqual(c.length, 1);
|
||||||
strictEqual(c.get(42).get('value'), 55);
|
strictEqual(c.get(42).get('value'), 55);
|
||||||
});
|
});
|
||||||
test('Reset unbind', function () {
|
test('degenerate-reset-bound', function () {
|
||||||
var changed = false,
|
var changed = false,
|
||||||
c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
|
c = new openerp.web.list.Collection([ {id: 1, value: 5} ]);
|
||||||
c.bind('change', function () { changed = true; });
|
c.bind('change', function () { changed = true; });
|
||||||
|
@ -229,7 +229,7 @@ $(document).ready(function () {
|
||||||
'parent collection');
|
'parent collection');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Events propagation', function () {
|
test('degenerate-propagations', function () {
|
||||||
var values = [];
|
var values = [];
|
||||||
var c = new openerp.web.list.Collection([
|
var c = new openerp.web.list.Collection([
|
||||||
{id: 1, value: 5},
|
{id: 1, value: 5},
|
||||||
|
@ -260,6 +260,41 @@ $(document).ready(function () {
|
||||||
c.at(1).set('wealth', 5);
|
c.at(1).set('wealth', 5);
|
||||||
strictEqual(total, 47);
|
strictEqual(total, 47);
|
||||||
});
|
});
|
||||||
|
test('degenerate-successor', function () {
|
||||||
|
var root = new openerp.web.list.Collection([
|
||||||
|
{id: 1, value: 1},
|
||||||
|
{id: 2, value: 2},
|
||||||
|
{id: 3, value: 3},
|
||||||
|
{id: 4, value: 5},
|
||||||
|
{id: 5, value: 8},
|
||||||
|
]);
|
||||||
|
|
||||||
|
deepEqual(root.succ(root.at(2)).attributes,
|
||||||
|
root.at(3).attributes,
|
||||||
|
"should return the record at (index + 1) from the pivot");
|
||||||
|
equal(root.succ(root.at(4)), null,
|
||||||
|
"should return null as successor to last record");
|
||||||
|
deepEqual(root.succ(root.at(4), {wraparound: true}).attributes,
|
||||||
|
root.at(0).attributes,
|
||||||
|
"should return index 0 as successor to last record if" +
|
||||||
|
" wraparound is set");
|
||||||
|
});
|
||||||
|
test('successor', function () {
|
||||||
|
var root = new openerp.web.list.Collection();
|
||||||
|
root.proxy('first').add([{id: 1, value: 1}, {id: 2, value: 2}]);
|
||||||
|
root.proxy('second').add([{id: 3, value: 3}, {id: 4, value: 5}]);
|
||||||
|
root.proxy('third').add([{id: 5, value: 8}, {id: 6, value: 13}]);
|
||||||
|
|
||||||
|
deepEqual(root.succ(root.get(3)).attributes,
|
||||||
|
root.get(4).attributes,
|
||||||
|
"should get successor");
|
||||||
|
equal(root.succ(root.get(4)),
|
||||||
|
null,
|
||||||
|
"successors do not cross collections");
|
||||||
|
deepEqual(root.succ(root.get(4), {wraparound: true}).attributes,
|
||||||
|
root.get(3).attributes,
|
||||||
|
"should wraparound within a collection");
|
||||||
|
});
|
||||||
|
|
||||||
module('list-hofs', {
|
module('list-hofs', {
|
||||||
setup: function () {
|
setup: function () {
|
||||||
|
|
Loading…
Reference in New Issue