]> git.proxmox.com Git - proxmox-widget-toolkit.git/blame - mixin/CBind.js
use eslint and execute as check target
[proxmox-widget-toolkit.git] / mixin / CBind.js
CommitLineData
281ad45b
DM
1Ext.define('Proxmox.Mixin.CBind', {
2 extend: 'Ext.Mixin',
3
4 mixinConfig: {
5 before: {
01031528
TL
6 initComponent: 'cloneTemplates',
7 },
281ad45b
DM
8 },
9
10 cloneTemplates: function() {
05a977a2 11 let me = this;
13fc756d 12
4f1b9b8c 13 if (typeof me.cbindData === "function") {
1d5e523a 14 me.cbindData = me.cbindData(me.initialConfig);
281ad45b 15 }
1d5e523a 16 me.cbindData = me.cbindData || {};
13fc756d 17
05a977a2 18 let getConfigValue = function(cname) {
281ad45b
DM
19 if (cname in me.initialConfig) {
20 return me.initialConfig[cname];
21 }
22 if (cname in me.cbindData) {
91e77fa9 23 let res = me.cbindData[cname];
4f1b9b8c 24 if (typeof res === "function") {
91e77fa9
TL
25 return res(me.initialConfig);
26 } else {
27 return res;
28 }
13fc756d 29 }
281ad45b
DM
30 if (cname in me) {
31 return me[cname];
32 }
33 throw "unable to get cbind data for '" + cname + "'";
34 };
13fc756d 35
05a977a2
TL
36 let applyCBind = function(obj) {
37 let cbind = obj.cbind, cdata;
281ad45b
DM
38 if (!cbind) return;
39
05a977a2
TL
40 for (const prop in cbind) { // eslint-disable-line guard-for-in
41 let match, found;
281ad45b
DM
42 cdata = cbind[prop];
43
44 found = false;
14985163
TL
45 if (typeof cdata === 'function') {
46 obj[prop] = cdata(getConfigValue, prop);
47 found = true;
05a977a2
TL
48 } else if ((match = /^\{(!)?([a-z_][a-z0-9_]*)\}$/i.exec(cdata))) {
49 let cvalue = getConfigValue(match[2]);
281ad45b
DM
50 if (match[1]) cvalue = !cvalue;
51 obj[prop] = cvalue;
52 found = true;
05a977a2
TL
53 } else if ((match = /^\{(!)?([a-z_][a-z0-9_]*(\.[a-z_][a-z0-9_]*)+)\}$/i.exec(cdata))) {
54 let keys = match[2].split('.');
55 let cvalue = getConfigValue(keys.shift());
c6f1ab1b
TL
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 {
05a977a2
TL
67 obj[prop] = cdata.replace(/{([a-z_][a-z0-9_]*)\}/ig, (_match, cname) => {
68 let cvalue = getConfigValue(cname);
281ad45b
DM
69 found = true;
70 return cvalue;
71 });
72 }
73 if (!found) {
74 throw "unable to parse cbind template '" + cdata + "'";
75 }
281ad45b
DM
76 }
77 };
78
79 if (me.cbind) {
80 applyCBind(me);
81 }
13fc756d 82
05a977a2
TL
83 let cloneTemplateObject;
84 let cloneTemplateArray = function(org) {
85 let copy, i, found, el, elcopy, arrayLength;
281ad45b
DM
86
87 arrayLength = org.length;
88 found = false;
89 for (i = 0; i < arrayLength; i++) {
90 el = org[i];
05a977a2 91 if (el.constructor === Object && el.xtype) {
281ad45b
DM
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];
05a977a2 102 if (el.constructor === Object && el.xtype) {
281ad45b
DM
103 elcopy = cloneTemplateObject(el);
104 if (elcopy.cbind) {
105 applyCBind(elcopy);
106 }
107 copy.push(elcopy);
05a977a2 108 } else if (el.constructor === Array) {
7d9225e6 109 elcopy = cloneTemplateArray(el);
281ad45b
DM
110 copy.push(elcopy);
111 } else {
112 copy.push(el);
113 }
114 }
115 return copy;
116 };
13fc756d 117
05a977a2
TL
118 cloneTemplateObject = function(org) {
119 let res = {}, prop, el, copy;
120 for (prop in org) { // eslint-disable-line guard-for-in
281ad45b 121 el = org[prop];
f27b0f68
TL
122 if (el === undefined || el === null) {
123 res[prop] = el;
124 continue;
125 }
05a977a2 126 if (el.constructor === Object && el.xtype) {
281ad45b
DM
127 copy = cloneTemplateObject(el);
128 if (copy.cbind) {
129 applyCBind(copy);
130 }
131 res[prop] = copy;
05a977a2 132 } else if (el.constructor === Array) {
281ad45b
DM
133 copy = cloneTemplateArray(el);
134 res[prop] = copy;
135 } else {
136 res[prop] = el;
137 }
138 }
139 return res;
140 };
141
05a977a2
TL
142 let condCloneProperties = function() {
143 let prop, el, tmp;
13fc756d 144
05a977a2 145 for (prop in me) { // eslint-disable-line guard-for-in
281ad45b
DM
146 el = me[prop];
147 if (el === undefined || el === null) continue;
05a977a2
TL
148 if (typeof el === 'object' && el.constructor === Object) {
149 if (el.xtype && prop !== 'config') {
281ad45b
DM
150 me[prop] = cloneTemplateObject(el);
151 }
05a977a2 152 } else if (el.constructor === Array) {
281ad45b
DM
153 tmp = cloneTemplateArray(el);
154 me[prop] = tmp;
155 }
156 }
157 };
158
159 condCloneProperties();
01031528 160 },
281ad45b 161});