From 79833356202cb68e0bf30ec70c0877799f59f045 Mon Sep 17 00:00:00 2001 From: Petr Vobornik Date: Tue, 17 Sep 2013 12:55:23 +0200 Subject: Declarative replacement of array item in specification object This patch adds option to define which item of which array attribute of specification object will be replaced by a new value. The difference between combination of $add and $del is that it keeps position of that item in the array. --- install/ui/src/freeipa/_base/Spec_mod.js | 50 +++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/install/ui/src/freeipa/_base/Spec_mod.js b/install/ui/src/freeipa/_base/Spec_mod.js index 34f79f726..ae1d9813f 100644 --- a/install/ui/src/freeipa/_base/Spec_mod.js +++ b/install/ui/src/freeipa/_base/Spec_mod.js @@ -38,9 +38,10 @@ define(['dojo/_base/declare', * $add: array of add rules * $del: array of del rules * $set: array of set rules + * $replace: array of replace rules * } * - * The order of modification is del, add, set. + * The order of modification is del, add, set, replace. * * @param {Object} spec * @param {Object} diff @@ -52,6 +53,7 @@ define(['dojo/_base/declare', this.del(spec, diff.$del); this.add(spec, diff.$add); this.set(spec, diff.$set); + this.replace(spec, diff.$replace); return spec; }, @@ -98,6 +100,29 @@ define(['dojo/_base/declare', return this._apply_rules(spec, rules, this._set); }, + /** + * Replace objects in an arrays + * + * A rule is a pair of path to an array and a objects to replace in that array + * [ + * 'path.to.spec.array', + * [ + * [match, new_obj_spec], + * [match2, new_obj_spec_2], + * ... + * ] + * ] + * Example of replacement specs: + * ['add', { name: 'add', hide_cond: [] }] + * ['{ name: 'add' }', { name: 'add', hide_cond: [] }] + * @param {Object} spec + * @param {Array} rules + */ + replace: function(spec, rules) { + + return this._apply_rules(spec, rules, this._replace); + }, + /** * Removes all rule props * @param {Object} diff @@ -106,6 +131,7 @@ define(['dojo/_base/declare', delete diff.$add; delete diff.$del; delete diff.$set; + delete diff.$replace; }, _apply_rules: function(spec, rules, method) { @@ -165,6 +191,28 @@ define(['dojo/_base/declare', return spec; }, + _replace: function(spec, rule) { + + var path = rule[0]; + var replace_rules = rule[1]; + var arr = lang.getObject(path, false, spec); + if (!arr) return spec; + + for (var i=0; i