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