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