[ADD] Collection#pred as well, while we're at it

bzr revid: xmo@openerp.com-20120710131205-voi1nc5rkvewjr2f
This commit is contained in:
Xavier Morel 2012-07-10 15:12:05 +02:00
parent 079e90afbb
commit 5b6df7ebeb
2 changed files with 63 additions and 1 deletions

View File

@ -1931,7 +1931,7 @@ var Collection = instance.web.Class.extend(/** @lends Collection# */{
var result;
for(var section in this._proxies) {
if (!this._proxies.hasOwnProperty(section)) {
continue
continue;
}
if ((result = this._proxies[section].succ(record, options))) {
return result;
@ -1944,6 +1944,27 @@ var Collection = instance.web.Class.extend(/** @lends Collection# */{
return this.at(0);
}
return this.at(next_index);
},
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);
}
});
Collection.include(Events);

View File

@ -278,6 +278,9 @@ $(document).ready(function () {
root.at(0).attributes,
"should return index 0 as successor to last record if" +
" wraparound is set");
deepEqual(root.succ(root.at(2), {wraparound: true}).attributes,
root.at(3).attributes,
"wraparound should have no effect if not succ(last_record)");
});
test('successor', function () {
var root = new openerp.web.list.Collection();
@ -295,6 +298,44 @@ $(document).ready(function () {
root.get(3).attributes,
"should wraparound within a collection");
});
test('degenerate-predecessor', 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.pred(root.at(2)).attributes,
root.at(1).attributes,
"should return the record at (index - 1) from the pivot");
equal(root.pred(root.at(0)), null,
"should return null as predecessor to first record");
deepEqual(root.pred(root.at(0), {wraparound: true}).attributes,
root.at(4).attributes,
"should return last record as predecessor to first record" +
" if wraparound is set");
deepEqual(root.pred(root.at(1), {wraparound: true}).attributes,
root.at(0).attributes,
"wraparound should have no effect if not pred(first_record)");
});
test('predecessor', 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.pred(root.get(4)).attributes,
root.get(3).attributes,
"should get predecessor");
equal(root.pred(root.get(3)),
null,
"predecessor do not cross collections");
deepEqual(root.pred(root.get(3), {wraparound: true}).attributes,
root.get(4).attributes,
"should wraparound within a collection");
});
module('list-hofs', {
setup: function () {