]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/StateProvider.js
ui: eslint: fix various spacing related issues
[pve-manager.git] / www / manager6 / StateProvider.js
1 /* This state provider keeps part of the state inside
2 * the browser history.
3 *
4 * We compress (shorten) url using dictionary based compression
5 * i.e. use column separated list instead of url encoded hash:
6 * #v\d* version/format
7 * := indicates string values
8 * :\d+ lookup value in dictionary hash
9 * #v1:=value1:5:=value2:=value3:...
10 */
11
12 Ext.define('PVE.StateProvider', {
13 extend: 'Ext.state.LocalStorageProvider',
14
15 // private
16 setHV: function(name, newvalue, fireEvents) {
17 var me = this;
18
19 var changes = false;
20 var oldtext = Ext.encode(me.UIState[name]);
21 var newtext = Ext.encode(newvalue);
22 if (newtext != oldtext) {
23 changes = true;
24 me.UIState[name] = newvalue;
25 //console.log("changed old " + name + " " + oldtext);
26 //console.log("changed new " + name + " " + newtext);
27 if (fireEvents) {
28 me.fireEvent("statechange", me, name, { value: newvalue });
29 }
30 }
31 return changes;
32 },
33
34 // private
35 hslist: [
36 // order is important for notifications
37 // [ name, default ]
38 ['view', 'server'],
39 ['rid', 'root'],
40 ['ltab', 'tasks'],
41 ['nodetab', ''],
42 ['storagetab', ''],
43 ['sdntab', ''],
44 ['pooltab', ''],
45 ['kvmtab', ''],
46 ['lxctab', ''],
47 ['dctab', ''],
48 ],
49
50 hprefix: 'v1',
51
52 compDict: {
53 sdn: 53,
54 cloudinit: 52,
55 replication: 51,
56 system: 50,
57 monitor: 49,
58 'ha-fencing': 48,
59 'ha-groups': 47,
60 'ha-resources': 46,
61 'ceph-log': 45,
62 'ceph-crushmap': 44,
63 'ceph-pools': 43,
64 'ceph-osdtree': 42,
65 'ceph-disklist': 41,
66 'ceph-monlist': 40,
67 'ceph-config': 39,
68 ceph: 38,
69 'firewall-fwlog': 37,
70 'firewall-options': 36,
71 'firewall-ipset': 35,
72 'firewall-aliases': 34,
73 'firewall-sg': 33,
74 firewall: 32,
75 apt: 31,
76 members: 30,
77 snapshot: 29,
78 ha: 28,
79 support: 27,
80 pools: 26,
81 syslog: 25,
82 ubc: 24,
83 initlog: 23,
84 openvz: 22,
85 backup: 21,
86 resources: 20,
87 content: 19,
88 root: 18,
89 domains: 17,
90 roles: 16,
91 groups: 15,
92 users: 14,
93 time: 13,
94 dns: 12,
95 network: 11,
96 services: 10,
97 options: 9,
98 console: 8,
99 hardware: 7,
100 permissions: 6,
101 summary: 5,
102 tasks: 4,
103 clog: 3,
104 storage: 2,
105 folder: 1,
106 server: 0,
107 },
108
109 decodeHToken: function(token) {
110 var me = this;
111
112 var state = {};
113 if (!token) {
114 Ext.Array.each(me.hslist, function(rec) {
115 state[rec[0]] = rec[1];
116 });
117 return state;
118 }
119
120 // return Ext.urlDecode(token);
121
122 var items = token.split(':');
123 var prefix = items.shift();
124
125 if (prefix != me.hprefix) {
126 return me.decodeHToken();
127 }
128
129 Ext.Array.each(me.hslist, function(rec) {
130 var value = items.shift();
131 if (value) {
132 if (value[0] === '=') {
133 value = decodeURIComponent(value.slice(1));
134 } else {
135 Ext.Object.each(me.compDict, function(key, cv) {
136 if (value == cv) {
137 value = key;
138 return false;
139 }
140 });
141 }
142 }
143 state[rec[0]] = value;
144 });
145
146 return state;
147 },
148
149 encodeHToken: function(state) {
150 var me = this;
151
152 // return Ext.urlEncode(state);
153
154 var ctoken = me.hprefix;
155 Ext.Array.each(me.hslist, function(rec) {
156 var value = state[rec[0]];
157 if (!Ext.isDefined(value)) {
158 value = rec[1];
159 }
160 value = encodeURIComponent(value);
161 if (!value) {
162 ctoken += ':';
163 } else {
164 var comp = me.compDict[value];
165 if (Ext.isDefined(comp)) {
166 ctoken += ":" + comp;
167 } else {
168 ctoken += ":=" + value;
169 }
170 }
171 });
172
173 return ctoken;
174 },
175
176 constructor: function(config) {
177 var me = this;
178
179 me.callParent([config]);
180
181 me.UIState = me.decodeHToken(); // set default
182
183 var history_change_cb = function(token) {
184 //console.log("HC " + token);
185 if (!token) {
186 var res = window.confirm(gettext('Are you sure you want to navigate away from this page?'));
187 if (res) {
188 // process text value and close...
189 Ext.History.back();
190 } else {
191 Ext.History.forward();
192 }
193 return;
194 }
195
196 var newstate = me.decodeHToken(token);
197 Ext.Array.each(me.hslist, function(rec) {
198 if (typeof newstate[rec[0]] == "undefined") {
199 return;
200 }
201 me.setHV(rec[0], newstate[rec[0]], true);
202 });
203 };
204
205 var start_token = Ext.History.getToken();
206 if (start_token) {
207 history_change_cb(start_token);
208 } else {
209 var htext = me.encodeHToken(me.UIState);
210 Ext.History.add(htext);
211 }
212
213 Ext.History.on('change', history_change_cb);
214 },
215
216 get: function(name, defaultValue) {
217 var me = this;
218 var data;
219
220 if (typeof me.UIState[name] != "undefined") {
221 data = { value: me.UIState[name] };
222 } else {
223 data = me.callParent(arguments);
224 if (!data && name === 'GuiCap') {
225 data = { vms: {}, storage: {}, access: {}, nodes: {}, dc: {}, sdn: {} };
226 }
227 }
228
229 //console.log("GET " + name + " " + Ext.encode(data));
230 return data;
231 },
232
233 clear: function(name) {
234 var me = this;
235
236 if (typeof me.UIState[name] != "undefined") {
237 me.UIState[name] = null;
238 }
239
240 me.callParent(arguments);
241 },
242
243 set: function(name, value, fireevent) {
244 var me = this;
245
246 //console.log("SET " + name + " " + Ext.encode(value));
247 if (typeof me.UIState[name] != "undefined") {
248 var newvalue = value ? value.value : null;
249 if (me.setHV(name, newvalue, fireevent)) {
250 var htext = me.encodeHToken(me.UIState);
251 Ext.History.add(htext);
252 }
253 } else {
254 me.callParent(arguments);
255 }
256 },
257 });