Add phase on exact position

https://fedorahosted.org/freeipa/ticket/3235
This commit is contained in:
Petr Vobornik 2013-03-29 15:27:58 +01:00
parent 344e15452a
commit 1b90b3b65e
4 changed files with 113 additions and 12 deletions

View File

@ -112,7 +112,7 @@ define([
},
/**
* Add task for a phase.
* Adds task for a phase.
*
* At phase execution, tasks are sorted by priority and executed in
* that order.
@ -134,17 +134,48 @@ define([
},
/**
* Adds a phase.
* Adds a phase
*
* Possible options:
* before: 'name-of-phase'
* after: 'name-of-phase'
* position: 'position for new phase'
*
* @param {String} phase name
* @param {Array} tasks
* @param {Object} options
*/
add_phase: function(name, tasks) {
add_phase: function(name, tasks, options) {
var phase = {
name: name,
tasks: tasks || []
};
this.phases.put(name, phase);
var position;
if (options) {
if (options.before) {
position = this.phases.get_key_index(options.before);
} else if (options.after) {
position = this.phases.get_key_index(options.after);
if (position === -1) position = this.phases.length;
else position++;
} else if (options.position) {
position = options.position;
}
}
this.phases.put(name, phase, position);
},
/**
* Checks if phases with given name exists
*
* @param {String} name
* @return {Boolean}
*/
exists: function(name) {
return !!this.phases.get(name);
},
constructor: function(spec) {

View File

@ -54,10 +54,39 @@ define([
controller: new Phase_controller(spec),
/**
* Registers phase task
* Registers a phase task
*
* @param {String} Phase name
* @param {Function} Task handler. Should return promise if async.
* @param {Number} Priority of task. Default 10.
*/
on: function(phase_name, handler, priority) {
this.controller.add_task(phase_name, handler, priority);
},
/**
* Adds a phase
*
* Possible options:
* before: 'name-of-phase'
* after: 'name-of-phase'
* position: 'position for new phase'
*
* @param {String} Phase name
* @param {Object} Options
*/
add: function(phase_name, options) {
this.controller.add_phase(phase_name, null, options);
},
/**
* Checks if phases with given name exists
*
* @param {String} Name
* @return {Boolean}
*/
exists: function(name) {
return this.controller.exists(name);
}
};

View File

@ -35,15 +35,25 @@ jQuery.ordered_map = jQuery.fn.ordered_map = function(map) {
return that.map[key];
};
that.put = function(key, value) {
that.put = function(key, value, position) {
var undefined;
var i = that.get_key_index(key);
if (i >= 0) {
that.values[i] = value;
} else {
that.keys.push(key);
that.values.push(value);
that.length = that.keys.length;
if (typeof position !== 'number') {
that.keys.push(key);
that.values.push(value);
that.length = that.keys.length;
} else {
if (position < 0) position = 0;
else if (position > that.length) position = that.length;
that.keys.splice(position, 0, key);
that.values.splice(position, 0, value);
that.length = that.keys.length;
}
}
that.map[key] = value;

View File

@ -43,16 +43,47 @@ test("Testing $.ordered_map.put().", function() {
var key2 = 'key2';
var value2 = 'value2';
var key3 = 'key3';
var value3 = 'value3';
var key4 = 'key4';
var value4 = 'value4';
var key5 = 'key5';
var value5 = 'value5';
var key6 = 'key6';
var value6 = 'value6';
var key7 = 'key7';
var value7 = 'value7';
var key8 = 'key8';
var value8 = 'value8';
var map = {};
map[key1] = value1;
map[key2] = value2;
map[key3] = value3;
map[key4] = value4;
map[key5] = value5;
map[key6] = value6;
map[key7] = value7;
map[key8] = value8;
test.put(key1, value1);
test.put(key2, value2);
strictEqual(test.length, 2, 'Checking length.');
deepEqual(test.keys, [key1, key2], 'Checking keys.');
deepEqual(test.values, [value1, value2], 'Checking values.');
test.put(key3, value3, 1); //put before key2
test.put(key4, value4, 0); //put at beginning
test.put(key5, value5, -2); //put at beginning
test.put(key6, value6, 5); //put at end
test.put(key7, value7, 100); //put at end
test.put(key8, value8, 'foobar'); //put at end
strictEqual(test.length, 8, 'Checking length.');
deepEqual(test.keys, [key5, key4, key1, key3, key2, key6, key7, key8], 'Checking keys.');
deepEqual(test.values, [value5, value4, value1, value3, value2, value6, value7, value8], 'Checking values.');
deepEqual(test.map, map, 'Checking map.');
});