]> git.proxmox.com Git - pve-manager.git/blob - www/manager6/Workspace.js
fix reselection on vm/ct migrate in gui
[pve-manager.git] / www / manager6 / Workspace.js
1 /*
2 * Workspace base class
3 *
4 * popup login window when auth fails (call onLogin handler)
5 * update (re-login) ticket every 15 minutes
6 *
7 */
8
9 Ext.define('PVE.Workspace', {
10 extend: 'Ext.container.Viewport',
11
12 title: 'Proxmox Virtual Environment',
13
14 loginData: null, // Data from last login call
15
16 onLogin: function(loginData) {},
17
18 // private
19 updateLoginData: function(loginData) {
20 var me = this;
21 me.loginData = loginData;
22 PVE.CSRFPreventionToken = loginData.CSRFPreventionToken;
23 PVE.UserName = loginData.username;
24
25 if (loginData.cap) {
26 Ext.state.Manager.set('GuiCap', loginData.cap);
27 }
28
29 // creates a session cookie (expire = null)
30 // that way the cookie gets deleted after browser window close
31 Ext.util.Cookies.set('PVEAuthCookie', loginData.ticket, null, '/', null, true);
32 me.onLogin(loginData);
33 },
34
35 // private
36 showLogin: function() {
37 var me = this;
38
39 PVE.Utils.authClear();
40 PVE.UserName = null;
41 me.loginData = null;
42
43 if (!me.login) {
44 me.login = Ext.create('PVE.window.LoginWindow', {
45 handler: function(data) {
46 me.login = null;
47 me.updateLoginData(data);
48 PVE.Utils.checked_command(function() {}); // display subscription status
49 }
50 });
51 }
52 me.onLogin(null);
53 me.login.show();
54 },
55
56 initComponent : function() {
57 var me = this;
58
59 Ext.tip.QuickTipManager.init();
60
61 // fixme: what about other errors
62 Ext.Ajax.on('requestexception', function(conn, response, options) {
63 if (response.status == 401) { // auth failure
64 me.showLogin();
65 }
66 });
67
68 me.callParent();
69
70 if (!PVE.Utils.authOK()) {
71 me.showLogin();
72 } else {
73 if (me.loginData) {
74 me.onLogin(me.loginData);
75 }
76 }
77
78 Ext.TaskManager.start({
79 run: function() {
80 var ticket = PVE.Utils.authOK();
81 if (!ticket || !PVE.UserName) {
82 return;
83 }
84
85 Ext.Ajax.request({
86 params: {
87 username: PVE.UserName,
88 password: ticket
89 },
90 url: '/api2/json/access/ticket',
91 method: 'POST',
92 success: function(response, opts) {
93 var obj = Ext.decode(response.responseText);
94 me.updateLoginData(obj.data);
95 }
96 });
97 },
98 interval: 15*60*1000
99 });
100
101 }
102 });
103
104 Ext.define('PVE.StdWorkspace', {
105 extend: 'PVE.Workspace',
106
107 alias: ['widget.pveStdWorkspace'],
108
109 // private
110 setContent: function(comp) {
111 var me = this;
112
113 var cont = me.child('#content');
114
115 var lay = cont.getLayout();
116
117 var cur = lay.getActiveItem();
118
119 if (comp) {
120 PVE.Utils.setErrorMask(cont, false);
121 comp.border = false;
122 cont.add(comp);
123 if (cur !== null && lay.getNext()) {
124 lay.next();
125 var task = Ext.create('Ext.util.DelayedTask', function(){
126 cont.remove(cur);
127 });
128 task.delay(10);
129 }
130 }
131 else {
132 // helper for cleaning the content when logging out
133 cont.removeAll();
134 }
135 },
136
137 selectById: function(nodeid) {
138 var me = this;
139 var tree = me.down('pveResourceTree');
140 tree.selectById(nodeid);
141 },
142
143 onLogin: function(loginData) {
144 var me = this;
145
146 me.updateUserInfo();
147
148 if (loginData) {
149 PVE.data.ResourceStore.startUpdate();
150
151 PVE.Utils.API2Request({
152 url: '/version',
153 method: 'GET',
154 success: function(response) {
155 PVE.VersionInfo = response.result.data;
156 me.updateVersionInfo();
157 }
158 });
159 }
160 },
161
162 updateUserInfo: function() {
163 var me = this;
164
165 var ui = me.query('#userinfo')[0];
166
167 if (PVE.UserName) {
168 var msg = Ext.String.format(gettext("You are logged in as {0}"), "'" + PVE.UserName + "'");
169 ui.update('<div class="x-unselectable" style="white-space:nowrap;">' + msg + '</div>');
170 } else {
171 ui.update('');
172 }
173 ui.updateLayout();
174 },
175
176 updateVersionInfo: function() {
177 var me = this;
178
179 var ui = me.query('#versioninfo')[0];
180
181 if (PVE.VersionInfo) {
182 var version = PVE.VersionInfo.version + '-' + PVE.VersionInfo.release + '/' +
183 PVE.VersionInfo.repoid;
184 ui.update('Virtual Environment ' + version);
185 } else {
186 ui.update('Virtual Environment');
187 }
188 ui.updateLayout();
189 },
190
191 initComponent : function() {
192 var me = this;
193
194 Ext.History.init();
195
196 var sprovider = Ext.create('PVE.StateProvider');
197 Ext.state.Manager.setProvider(sprovider);
198
199 var selview = Ext.create('PVE.form.ViewSelector');
200
201 var rtree = Ext.createWidget('pveResourceTree', {
202 viewFilter: selview.getViewFilter(),
203 flex: 1,
204 selModel: {
205 selType: 'treemodel',
206 listeners: {
207 selectionchange: function(sm, selected) {
208 if (selected.length > 0) {
209 var n = selected[0];
210 var tlckup = {
211 root: 'PVE.dc.Config',
212 node: 'PVE.node.Config',
213 qemu: 'PVE.qemu.Config',
214 lxc: 'PVE.lxc.Config',
215 storage: 'PVE.storage.Browser',
216 pool: 'pvePoolConfig'
217 };
218 var comp = {
219 xtype: tlckup[n.data.type || 'root'] ||
220 'pvePanelConfig',
221 showSearch: (n.data.id === 'root') ||
222 Ext.isDefined(n.data.groupbyid),
223 pveSelNode: n,
224 workspace: me,
225 viewFilter: selview.getViewFilter()
226 };
227 PVE.curSelectedNode = n;
228 me.setContent(comp);
229 }
230 }
231 }
232 }
233 });
234
235 selview.on('select', function(combo, records) {
236 if (records) {
237 var view = combo.getViewFilter();
238 rtree.setViewFilter(view);
239 }
240 });
241
242 var caps = sprovider.get('GuiCap');
243
244 var createVM = Ext.createWidget('button', {
245 pack: 'end',
246 margin: '3 5 0 0',
247 baseCls: 'x-btn',
248 iconCls: 'fa fa-desktop',
249 text: gettext("Create VM"),
250 disabled: !caps.vms['VM.Allocate'],
251 handler: function() {
252 var wiz = Ext.create('PVE.qemu.CreateWizard', {});
253 wiz.show();
254 }
255 });
256
257 var createCT = Ext.createWidget('button', {
258 pack: 'end',
259 margin: '3 5 0 0',
260 baseCls: 'x-btn',
261 iconCls: 'fa fa-cube',
262 text: gettext("Create CT"),
263 disabled: !caps.vms['VM.Allocate'],
264 handler: function() {
265 var wiz = Ext.create('PVE.lxc.CreateWizard', {});
266 wiz.show();
267 }
268 });
269
270 sprovider.on('statechange', function(sp, key, value) {
271 if (key === 'GuiCap' && value) {
272 caps = value;
273 createVM.setDisabled(!caps.vms['VM.Allocate']);
274 createCT.setDisabled(!caps.vms['VM.Allocate']);
275 }
276 });
277
278 Ext.apply(me, {
279 layout: { type: 'border' },
280 border: false,
281 items: [
282 {
283 region: 'north',
284 layout: {
285 type: 'hbox',
286 align: 'middle'
287 },
288 baseCls: 'x-plain',
289 defaults: {
290 baseCls: 'x-plain'
291 },
292 border: false,
293 margin: '2 0 2 5',
294 items: [
295 {
296 html: '<a class="x-unselectable" target=_blank href="http://www.proxmox.com">' +
297 '<img style="padding-top:4px;padding-right:5px" src="/pve2/images/proxmox_logo.png"/></a>'
298 },
299 {
300 minWidth: 200,
301 id: 'versioninfo',
302 html: 'Virtual Environment'
303 },
304 {
305 padding: 5,
306 html: '<a href="https://bugzilla.proxmox.com" target="_blank">BETA</a>'
307 },
308 {
309 xtype: 'pveGlobalSearchField',
310 tree: rtree
311 },
312 {
313 flex: 1
314 },
315 {
316 pack: 'end',
317 id: 'userinfo',
318 stateful: false
319 },
320 {
321 xtype: 'button',
322 margin: '0 10 0 3',
323 iconCls: 'fa black fa-gear',
324 userCls: 'pointer',
325 handler: function() {
326 var win = Ext.create('PVE.window.Settings');
327 win.show();
328 }
329 },
330 {
331 xtype: 'pveHelpButton',
332 hidden: false,
333 baseCls: 'x-btn',
334 iconCls: 'fa fa-info-circle x-btn-icon-el-default-toolbar-small ',
335 listenToGlobalEvent: false,
336 onlineHelp: 'pve_documentation_index',
337 margin: '0 5 0 0'
338 },
339 createVM,
340 createCT,
341 {
342 pack: 'end',
343 margin: '0 5 0 0',
344 xtype: 'button',
345 baseCls: 'x-btn',
346 iconCls: 'fa fa-sign-out',
347 text: gettext("Logout"),
348 handler: function() {
349 PVE.data.ResourceStore.loadData([], false);
350 me.showLogin();
351 me.setContent(null);
352 var rt = me.down('pveResourceTree');
353 rt.clearTree();
354
355 // empty the stores of the StatusPanel child items
356 var statusPanels = Ext.ComponentQuery.query('pveStatusPanel grid');
357 Ext.Array.forEach(statusPanels, function(comp) {
358 if (comp.getStore()) {
359 comp.getStore().loadData([], false);
360 }
361 });
362 }
363 }
364 ]
365 },
366 {
367 region: 'center',
368 stateful: true,
369 stateId: 'pvecenter',
370 minWidth: 100,
371 minHeight: 100,
372 id: 'content',
373 xtype: 'container',
374 layout: { type: 'card' },
375 border: false,
376 margin: '0 5 0 0',
377 items: []
378 },
379 {
380 region: 'west',
381 stateful: true,
382 stateId: 'pvewest',
383 itemId: 'west',
384 xtype: 'container',
385 border: false,
386 layout: { type: 'vbox', align: 'stretch' },
387 margin: '0 0 0 5',
388 split: true,
389 width: 200,
390 items: [ selview, rtree ],
391 listeners: {
392 resize: function(panel, width, height) {
393 var viewWidth = me.getSize().width;
394 if (width > viewWidth - 100) {
395 panel.setWidth(viewWidth - 100);
396 }
397 }
398 }
399 },
400 {
401 xtype: 'pveStatusPanel',
402 stateful: true,
403 stateId: 'pvesouth',
404 itemId: 'south',
405 region: 'south',
406 margin:'0 5 5 5',
407 title: gettext('Logs'),
408 collapsible: true,
409 header: false,
410 height: 200,
411 split:true,
412 listeners: {
413 resize: function(panel, width, height) {
414 var viewHeight = me.getSize().height;
415 if (height > (viewHeight - 150)) {
416 panel.setHeight(viewHeight - 150);
417 }
418 }
419 }
420 }
421 ]
422 });
423
424 me.callParent();
425
426 me.updateUserInfo();
427
428 // on resize, center all modal windows
429 Ext.on('resize', function(){
430 var wins = Ext.ComponentQuery.query('window[modal]');
431 if (wins.length > 0) {
432 wins.forEach(function(win){
433 win.alignTo(me, 'c-c');
434 });
435 }
436 });
437 }
438 });
439