]> git.proxmox.com Git - proxmox-widget-toolkit.git/blob - mixin/CBind.js
2b69cca9b05fb2e49b2284c145a48b3586a97981
[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_]*)\}$/.exec(cdata)) {
40 var cvalue = getConfigValue(match[2]);
41 if (match[1]) cvalue = !cvalue;
42 obj[prop] = cvalue;
43 found = true;
44 } else {
45 obj[prop] = cdata.replace(/{([a-z_][a-z0-9_]*)\}/g, function(match, cname) {
46 var cvalue = getConfigValue(cname);
47 found = true;
48 return cvalue;
49 });
50 }
51 if (!found) {
52 throw "unable to parse cbind template '" + cdata + "'";
53 }
54
55 }
56 };
57
58 if (me.cbind) {
59 applyCBind(me);
60 }
61
62 var cloneTemplateArray = function(org) {
63 var copy, i, found, el, elcopy, arrayLength;
64
65 arrayLength = org.length;
66 found = false;
67 for (i = 0; i < arrayLength; i++) {
68 el = org[i];
69 if (el.constructor == Object && el.xtype) {
70 found = true;
71 break;
72 }
73 }
74
75 if (!found) return org; // no need to copy
76
77 copy = [];
78 for (i = 0; i < arrayLength; i++) {
79 el = org[i];
80 if (el.constructor == Object && el.xtype) {
81 elcopy = cloneTemplateObject(el);
82 if (elcopy.cbind) {
83 applyCBind(elcopy);
84 }
85 copy.push(elcopy);
86 } else if (el.constructor == Object && el.xtype) {
87 elcopy = cloneTemplateObject(el);
88 if (elcopy.cbind) {
89 applyCBind(elcopy);
90 }
91 copy.push(elcopy);
92 } else {
93 copy.push(el);
94 }
95 }
96 return copy;
97 };
98
99 var cloneTemplateObject = function(org) {
100 var res = {}, prop, el, copy;
101 for (prop in org) {
102 el = org[prop];
103 if (el.constructor == Object && el.xtype) {
104 copy = cloneTemplateObject(el);
105 if (copy.cbind) {
106 applyCBind(copy);
107 }
108 res[prop] = copy;
109 } else if (el.constructor == Array) {
110 copy = cloneTemplateArray(el);
111 res[prop] = copy;
112 } else {
113 res[prop] = el;
114 }
115 }
116 return res;
117 };
118
119 var condCloneProperties = function() {
120 var prop, el, i, tmp;
121
122 for (prop in me) {
123 el = me[prop];
124 if (el === undefined || el === null) continue;
125 if (typeof(el) === 'object' && el.constructor == Object) {
126 if (el.xtype) {
127 me[prop] = cloneTemplateObject(el);
128 }
129 } else if (el.constructor == Array) {
130 tmp = cloneTemplateArray(el);
131 me[prop] = tmp;
132 }
133 }
134 };
135
136 condCloneProperties();
137 }
138 });