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