]>
Commit | Line | Data |
---|---|---|
281ad45b DM |
1 | Ext.define('Proxmox.Mixin.CBind', { |
2 | extend: 'Ext.Mixin', | |
3 | ||
4 | mixinConfig: { | |
5 | before: { | |
6 | initComponent: 'cloneTemplates' | |
7 | } | |
8 | }, | |
9 | ||
10 | cloneTemplates: function() { | |
11 | var me = this; | |
13fc756d | 12 | |
281ad45b DM |
13 | if (typeof(me.cbindData) == "function") { |
14 | me.cbindData = me.cbindData(me.initialConfig) || {}; | |
15 | } | |
13fc756d | 16 | |
281ad45b DM |
17 | var getConfigValue = function(cname) { |
18 | ||
19 | if (cname in me.initialConfig) { | |
20 | return me.initialConfig[cname]; | |
21 | } | |
22 | if (cname in me.cbindData) { | |
23 | return me.cbindData[cname]; | |
13fc756d | 24 | } |
281ad45b DM |
25 | if (cname in me) { |
26 | return me[cname]; | |
27 | } | |
28 | throw "unable to get cbind data for '" + cname + "'"; | |
29 | }; | |
13fc756d | 30 | |
281ad45b DM |
31 | var applyCBind = function(obj) { |
32 | var cbind = obj.cbind, prop, cdata, cvalue, match, found; | |
33 | if (!cbind) return; | |
34 | ||
35 | for (prop in cbind) { | |
36 | cdata = cbind[prop]; | |
37 | ||
38 | found = false; | |
fbdf4ca7 | 39 | if (match = /^\{(!)?([a-z_][a-z0-9_]*)\}$/i.exec(cdata)) { |
281ad45b DM |
40 | var cvalue = getConfigValue(match[2]); |
41 | if (match[1]) cvalue = !cvalue; | |
42 | obj[prop] = cvalue; | |
43 | found = true; | |
c6f1ab1b TL |
44 | } else if (match = /^\{(!)?([a-z_][a-z0-9_]*(\.[a-z_][a-z0-9_]*)+)\}$/i.exec(cdata)) { |
45 | var keys = match[2].split('.'); | |
46 | var cvalue = getConfigValue(keys.shift()); | |
47 | keys.forEach(function(k) { | |
48 | if (k in cvalue) { | |
49 | cvalue = cvalue[k]; | |
50 | } else { | |
51 | throw "unable to get cbind data for '" + match[2] + "'"; | |
52 | } | |
53 | }); | |
54 | if (match[1]) cvalue = !cvalue; | |
55 | obj[prop] = cvalue; | |
56 | found = true; | |
281ad45b | 57 | } else { |
fbdf4ca7 | 58 | obj[prop] = cdata.replace(/{([a-z_][a-z0-9_]*)\}/ig, function(match, cname) { |
281ad45b DM |
59 | var cvalue = getConfigValue(cname); |
60 | found = true; | |
61 | return cvalue; | |
62 | }); | |
63 | } | |
64 | if (!found) { | |
65 | throw "unable to parse cbind template '" + cdata + "'"; | |
66 | } | |
67 | ||
68 | } | |
69 | }; | |
70 | ||
71 | if (me.cbind) { | |
72 | applyCBind(me); | |
73 | } | |
13fc756d | 74 | |
281ad45b DM |
75 | var cloneTemplateArray = function(org) { |
76 | var copy, i, found, el, elcopy, arrayLength; | |
77 | ||
78 | arrayLength = org.length; | |
79 | found = false; | |
80 | for (i = 0; i < arrayLength; i++) { | |
81 | el = org[i]; | |
82 | if (el.constructor == Object && el.xtype) { | |
83 | found = true; | |
84 | break; | |
85 | } | |
86 | } | |
87 | ||
88 | if (!found) return org; // no need to copy | |
89 | ||
90 | copy = []; | |
91 | for (i = 0; i < arrayLength; i++) { | |
92 | el = org[i]; | |
93 | if (el.constructor == Object && el.xtype) { | |
94 | elcopy = cloneTemplateObject(el); | |
95 | if (elcopy.cbind) { | |
96 | applyCBind(elcopy); | |
97 | } | |
98 | copy.push(elcopy); | |
7d9225e6 DM |
99 | } else if (el.constructor == Array) { |
100 | elcopy = cloneTemplateArray(el); | |
281ad45b DM |
101 | copy.push(elcopy); |
102 | } else { | |
103 | copy.push(el); | |
104 | } | |
105 | } | |
106 | return copy; | |
107 | }; | |
13fc756d | 108 | |
281ad45b DM |
109 | var cloneTemplateObject = function(org) { |
110 | var res = {}, prop, el, copy; | |
111 | for (prop in org) { | |
112 | el = org[prop]; | |
113 | if (el.constructor == Object && el.xtype) { | |
114 | copy = cloneTemplateObject(el); | |
115 | if (copy.cbind) { | |
116 | applyCBind(copy); | |
117 | } | |
118 | res[prop] = copy; | |
119 | } else if (el.constructor == Array) { | |
120 | copy = cloneTemplateArray(el); | |
121 | res[prop] = copy; | |
122 | } else { | |
123 | res[prop] = el; | |
124 | } | |
125 | } | |
126 | return res; | |
127 | }; | |
128 | ||
129 | var condCloneProperties = function() { | |
130 | var prop, el, i, tmp; | |
13fc756d | 131 | |
281ad45b DM |
132 | for (prop in me) { |
133 | el = me[prop]; | |
134 | if (el === undefined || el === null) continue; | |
135 | if (typeof(el) === 'object' && el.constructor == Object) { | |
c33f0878 | 136 | if (el.xtype && prop != 'config') { |
281ad45b DM |
137 | me[prop] = cloneTemplateObject(el); |
138 | } | |
139 | } else if (el.constructor == Array) { | |
140 | tmp = cloneTemplateArray(el); | |
141 | me[prop] = tmp; | |
142 | } | |
143 | } | |
144 | }; | |
145 | ||
146 | condCloneProperties(); | |
147 | } | |
148 | }); |