]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - mixin/CBind.js
8276047df3a0f64e9d9595f9d48e48d8f7f25de9
[proxmox-widget-toolkit.git] / mixin / CBind.js
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;
12
13 if (typeof(me.cbindData) == "function") {
14 me.cbindData = me.cbindData(me.initialConfig);
15 }
16 me.cbindData = me.cbindData || {};
17
18 var getConfigValue = function(cname) {
19
20 if (cname in me.initialConfig) {
21 return me.initialConfig[cname];
22 }
23 if (cname in me.cbindData) {
24 let res = me.cbindData[cname];
25 if (typeof(res) == "function") {
26 return res(me.initialConfig);
27 } else {
28 return res;
29 }
30 }
31 if (cname in me) {
32 return me[cname];
33 }
34 throw "unable to get cbind data for '" + cname + "'";
35 };
36
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;
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)) {
49 var cvalue = getConfigValue(match[2]);
50 if (match[1]) cvalue = !cvalue;
51 obj[prop] = cvalue;
52 found = true;
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;
66 } else {
67 obj[prop] = cdata.replace(/{([a-z_][a-z0-9_]*)\}/ig, function(match, cname) {
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 }
83
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);
108 } else if (el.constructor == Array) {
109 elcopy = cloneTemplateArray(el);
110 copy.push(elcopy);
111 } else {
112 copy.push(el);
113 }
114 }
115 return copy;
116 };
117
118 var cloneTemplateObject = function(org) {
119 var res = {}, prop, el, copy;
120 for (prop in org) {
121 el = org[prop];
122 if (el === undefined || el === null) {
123 res[prop] = el;
124 continue;
125 }
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;
144
145 for (prop in me) {
146 el = me[prop];
147 if (el === undefined || el === null) continue;
148 if (typeof(el) === 'object' && el.constructor == Object) {
149 if (el.xtype && prop != 'config') {
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 });