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