[FIX] split selection of a range of ids and activation of a single id into two methods, write some tests

bzr revid: xmo@openerp.com-20110323072712-10gohth1mxrgh6o1
This commit is contained in:
Xavier Morel 2011-03-23 08:27:12 +01:00
parent 8570ae1fff
commit 99f90b17a2
2 changed files with 113 additions and 11 deletions

View File

@ -247,6 +247,10 @@ openerp.base.DataSet = openerp.base.Controller.extend({
* @returns itself
*/
prev: function () {
this._active_id_index -= 1;
if (this._active_id_index < 0) {
this._active_id_index = this._active_ids.length - 1;
}
return this;
},
/**
@ -254,26 +258,68 @@ openerp.base.DataSet = openerp.base.Controller.extend({
* @returns itself
*/
next: function () {
this._active_id_index += 1;
if (this._active_id_index >= this._active_ids.length) {
this._active_id_index = 0;
}
return this;
},
/**
* Sets active_ids and/or active_id by value:
* Sets active_ids by value:
*
* * If only <code>ids</code> is provided
* - Activates all ids part of the current selection
* - Sets active_id to be the first id of the selection
* * If only <code>id</code> is provided, activates this id (for both active_id and active_ids)
* * If both <code>id</code> and <code>ids</code> are provided
* - Activates all ids part of the current selection
* - If <code>id</code> is part of the active ids, activates it, otherwise see first option
* * Activates all ids part of the current selection
* * Sets active_id to be the first id of the selection
*
* @param {Array} [ids] the list of ids to activate
* @param {Object} [id] the id to activate
* @param {Array} ids the list of ids to activate
* @returns itself
*/
select: function (ids, id) {
select: function (ids) {
this._active_ids = ids;
this._active_id_index = 0;
return this;
},
/**
* Fetches the ids of the currently selected records, if any.
*/
get_active_ids: function () {
return this._active_ids;
},
/**
* Sets the current active_id by value
*
* If there are no active_ids selected, selects the provided id as the sole active_id
*
* If there are ids selected and the provided id is not in them, raise an error
*
* @param {Object} id the id to activate
* @returns itself
*/
activate: function (id) {
if(!this._active_ids) {
this._active_ids = [id];
this._active_id_index = 0;
} else {
var index = _.indexOf(this._active_ids, id);
if (index == -1) {
throw new Error(
"Could not find id " + id +
" in array [" + this._active_ids.join(', ') + "]");
}
this._active_id_index = index;
}
return this;
},
/**
* Fetches the id of the current active record, if any.
*
* @returns record? record id or <code>null</code>
*/
get_active_id: function () {
if (!this._active_ids) {
return null;
}
return this._active_ids[this._active_id_index];
}
});

View File

@ -243,6 +243,62 @@ $(document).ready(function () {
deepEqual([['foo', '=', 'bar']], dataset._domain);
});
module('ids_activation', {
setup: function () {
var openerp = window.openerp.init();
dataset = new openerp.base.DataSet();
}
});
test('activate id', function () {
dataset.activate(1);
deepEqual(dataset.get_active_ids(), [1]);
equal(dataset.get_active_id(), 1);
});
test('set active_ids', function () {
dataset.select([1, 2, 3]);
deepEqual(dataset.get_active_ids(), [1, 2, 3],
"selecting an ids range");
equal(dataset.get_active_id(), 1);
});
test('activate incorrect id', function () {
dataset.select([1, 2, 3]);
raises(function () { dataset.activate(42); },
"Activating an id not present in the selection is an error");
});
test('reset active id on set active ids', function () {
dataset.select([1, 2, 3]).activate(3).select([1, 2, 3]);
equal(dataset.get_active_id(), 1,
"selecting an ids range resets the active id");
});
module('active_id_iteration', {
setup: function () {
var openerp = window.openerp.init();
dataset = new openerp.base.DataSet();
dataset.select([1, 2, 3]);
}
});
test('step forward', function () {
dataset.activate(1);
dataset.next();
equal(dataset.get_active_id(), 2);
});
test('wraparound forward', function () {
dataset.activate(3);
dataset.next();
equal(dataset.get_active_id(), 1);
});
test('step back', function () {
dataset.activate(3);
dataset.prev();
equal(dataset.get_active_id(), 2);
});
test('wraparound back', function () {
dataset.activate(1);
dataset.prev();
equal(dataset.get_active_id(), 3);
});
module('active_ids', {
setup: function () {
openerp = window.openerp.init();