diff --git a/addons/web/static/src/js/view_list.js b/addons/web/static/src/js/view_list.js index 2d1a06173cd..27b39a40b22 100644 --- a/addons/web/static/src/js/view_list.js +++ b/addons/web/static/src/js/view_list.js @@ -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); diff --git a/addons/web/static/test/list-utils.js b/addons/web/static/test/list-utils.js index 18069943b2d..baec728528e 100644 --- a/addons/web/static/test/list-utils.js +++ b/addons/web/static/test/list-utils.js @@ -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 () {