summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPetr Vobornik <pvoborni@redhat.com>2013-09-17 12:55:23 +0200
committerAlexander Bokovoy <abokovoy@redhat.com>2014-02-11 15:02:51 +0200
commit79833356202cb68e0bf30ec70c0877799f59f045 (patch)
tree27889d4ed28744c07d4ed669bb7247576b3c7c7f
parent0da0fc4322bd2be03f3889312dd3f9d256f493a3 (diff)
downloadfreeipa-79833356202cb68e0bf30ec70c0877799f59f045.zip
freeipa-79833356202cb68e0bf30ec70c0877799f59f045.tar.gz
freeipa-79833356202cb68e0bf30ec70c0877799f59f045.tar.xz
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.
-rw-r--r--install/ui/src/freeipa/_base/Spec_mod.js50
1 files changed, 49 insertions, 1 deletions
diff --git a/install/ui/src/freeipa/_base/Spec_mod.js b/install/ui/src/freeipa/_base/Spec_mod.js
index 34f79f7..ae1d981 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;
},
@@ -99,6 +101,29 @@ define(['dojo/_base/declare',
},
/**
+ * 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<replace_rules.length; i++) {
+ var conds = replace_rules[i][0];
+ var new_spec = replace_rules[i][1];
+
+ for (var j=0; j<arr.length; j++) {
+ if (this._match(arr[j], conds)) {
+ arr[j] = new_spec;
+ break;
+ }
+ }
+ }
+
+ return spec;
+ },
+
_match: function(value, cond) {
var match = true;