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