]> git.proxmox.com Git - pve-manager.git/blob - www/manager/Workspace.js
imported from svn 'pve-manager/pve2'
[pve-manager.git] / www / manager / 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 requires: [
12 'Ext.tip.*',
13 'PVE.Utils',
14 'PVE.window.LoginWindow'
15 ],
16
17 title: 'Proxmox Virtual Environment',
18
19 loginData: null, // Data from last login call
20
21 onLogin: function(loginData) {},
22
23 // private
24 updateLoginData: function(loginData) {
25 var me = this;
26 me.loginData = loginData;
27 PVE.CSRFPreventionToken = loginData.CSRFPreventionToken;
28 PVE.UserName = loginData.username;
29 me.onLogin(loginData);
30 },
31
32 // private
33 showLogin: function() {
34 var me = this;
35
36 PVE.Utils.authClear();
37 PVE.UserName = null;
38 me.loginData = null;
39
40 if (!me.login) {
41 me.login = Ext.create('PVE.window.LoginWindow', {
42 handler: function(data) {
43 me.login = null;
44 me.updateLoginData(data);
45 }
46 });
47 }
48 me.onLogin(null);
49 me.login.show();
50 },
51
52 initComponent : function() {
53 var me = this;
54
55 Ext.tip.QuickTipManager.init();
56
57 // fixme: what about other errors
58 Ext.Ajax.on('requestexception', function(conn, response, options) {
59 if (response.status == 401) { // auth failure
60 me.showLogin();
61 }
62 });
63
64 document.title = me.title;
65
66 me.callParent();
67
68 if (!PVE.Utils.authOK()) {
69 me.showLogin();
70 } else {
71 if (me.loginData) {
72 me.onLogin(me.loginData);
73 }
74 }
75
76 Ext.TaskManager.start({
77 run: function() {
78 var ticket = PVE.Utils.authOK();
79 if (!ticket || !PVE.UserName) {
80 return;
81 }
82
83 Ext.Ajax.request({
84 params: {
85 username: PVE.UserName,
86 password: ticket
87 },
88 url: '/api2/json/access/ticket',
89 method: 'POST',
90 success: function(response, opts) {
91 // cookie is automatically updated
92 var obj = Ext.decode(response.responseText);
93 me.updateLoginData(obj.data);
94 }
95 });
96 },
97 interval: 15*60*1000
98 });
99
100 }
101 });
102
103 Ext.define('PVE.ConsoleWorkspace', {
104 extend: 'PVE.Workspace',
105 requires: [
106 'PVE.KVMConsole'
107 ],
108
109 alias: ['widget.pveConsoleWorkspace'],
110
111 title: 'Proxmox Console',
112
113 initComponent : function() {
114 var me = this;
115
116 var param = Ext.Object.fromQueryString(window.location.search);
117 var consoleType = me.consoleType || param.console;
118
119 var content;
120 if (consoleType === 'kvm') {
121 me.title = "VM " + param.vmid;
122 content = {
123 xtype: 'pveKVMConsole',
124 vmid: param.vmid,
125 nodename: param.node,
126 toplevel: true
127 };
128 } else if (consoleType === 'shell') {
129 me.title = "node " + param.node + " - Proxmox Shell";
130 content = {
131 xtype: 'pveShell',
132 nodename: param.node,
133 toplevel: true
134 };
135 } else {
136 content = {
137 border: false,
138 bodyPadding: 10,
139 html: 'Error: No such console type'
140 };
141 }
142
143 Ext.apply(me, {
144 layout: { type: 'fit' },
145 border: false,
146 items: [ content ]
147 });
148
149 me.callParent();
150 }
151 });
152
153 Ext.define('PVE.StdWorkspace', {
154 extend: 'PVE.Workspace',
155 requires: [
156 'Ext.History',
157 'Ext.state.*',
158 'Ext.selection.*',
159 'PVE.form.ViewSelector',
160 'PVE.data.ResourceStore',
161 'PVE.tree.ResourceTree'
162 ],
163
164 alias: ['widget.pveStdWorkspace'],
165
166 // private
167 defaultContent: {
168 title: 'Nothing selected',
169 region: 'center'
170 },
171
172 setContent: function(comp) {
173 var me = this;
174
175 if (!comp) {
176 comp = me.defaultContent;
177 }
178
179 var cont = me.child('#content');
180 cont.removeAll(true);
181 cont.add(comp);
182 cont.doLayout();
183 },
184
185 selectById: function(nodeid) {
186 var me = this;
187 var tree = me.down('pveResourceTree');
188 tree.selectById(nodeid);
189 },
190
191 onLogin: function(loginData) {
192 var me = this;
193
194 me.updateUserInfo();
195
196 if (loginData) {
197 PVE.data.ResourceStore.startUpdate();
198 }
199 },
200
201 updateUserInfo: function() {
202 var me = this;
203
204 var ui = me.query('#userinfo')[0];
205
206 if (PVE.UserName) {
207 ui.update('<div class="x-unselectable" style="white-space:nowrap;">You are logged in as "' + PVE.UserName + '"</div>');
208 } else {
209 ui.update('');
210 }
211 ui.doLayout();
212 },
213
214 initComponent : function() {
215 var me = this;
216
217 Ext.History.init();
218 Ext.state.Manager.setProvider(Ext.create('PVE.StateProvider'));
219
220 //document.title = ;
221
222 var selview = new PVE.form.ViewSelector({
223 listeners: {
224 select: function(combo, records) {
225 if (records && records.length) {
226 var view = combo.getViewFilter();
227 combo.up('pveResourceTree').setViewFilter(view);
228 }
229 }
230 }
231 });
232
233 var rtree = Ext.createWidget('pveResourceTree', {
234 width: 200,
235 region: 'west',
236 margins: '0 0 0 5',
237 split: true,
238 viewFilter: selview.getViewFilter(),
239 tbar: [ ' ', selview ],
240 selModel: new Ext.selection.TreeModel({
241 listeners: {
242 selectionchange: function(sm, selected) {
243 var comp;
244 var tlckup = {
245 root: 'PVE.dc.Config',
246 node: 'PVE.node.Config',
247 qemu: 'PVE.qemu.Config',
248 storage: 'PVE.storage.Browser'
249 };
250
251 if (selected.length > 0) {
252 var n = selected[0];
253 comp = {
254 xtype: tlckup[n.data.type || 'root'] ||
255 'PVE.panel.Config',
256 layout: { type: 'fit' },
257 showSearch: (n.data.id === 'root') ||
258 Ext.isDefined(n.data.groupbyid),
259 pveSelNode: n,
260 viewFilter: selview.getViewFilter()
261 };
262 }
263
264 me.setContent(comp);
265 }
266 }
267 })
268 });
269
270 Ext.apply(me, {
271 layout: { type: 'border' },
272 border: false,
273 items: [
274 {
275 region: 'north',
276 height: 30,
277 layout: {
278 type: 'hbox',
279 align : 'middle'
280 },
281 baseCls: 'x-plain',
282 defaults: {
283 baseCls: 'x-plain'
284 },
285 border: false,
286 margins: '2 0 5 0',
287 items: [
288 {
289 margins: '0 0 0 4',
290 html: '<a class="x-unselectable" target=_blank href="http://www.proxmox.com">' +
291 '<img height=30 width=209 src="/pve2/images/proxmox_logo.png"/></a>'
292 },
293 {
294 minWidth: 200,
295 flex: 1,
296 html: '<span class="x-panel-header-text">Proxmox Virtual Environment<br>Version ' + PVE.GUIVersion + "</span>"
297 },
298 {
299 pack: 'end',
300 margins: '8 10 0 10',
301 id: 'userinfo',
302 stateful: false
303 },
304 {
305 pack: 'end',
306 margins: '3 5 0 0',
307 xtype: 'button',
308 baseCls: 'x-btn',
309 text: "Logout",
310 handler: function() {
311 PVE.data.ResourceStore.stopUpdate();
312 me.showLogin();
313 me.setContent();
314 var rt = me.down('pveResourceTree');
315 rt.clearTree();
316 }
317 },
318 {
319 pack: 'end',
320 margins: '3 5 0 0',
321 xtype: 'button',
322 baseCls: 'x-btn',
323 text: "Create VM",
324 handler: function() {
325 var wiz = Ext.create('PVE.qemu.CreateWizard', {});
326 wiz.show();
327 }
328 },
329 {
330 pack: 'end',
331 margins: '3 5 0 0',
332 xtype: 'button',
333 baseCls: 'x-btn',
334 text: "Create CT",
335 handler: function() {
336 var wiz = Ext.create('PVE.openvz.CreateWizard', {});
337 wiz.show();
338 }
339 }
340 ]
341 },
342 {
343 region: 'center',
344 id: 'content',
345 xtype: 'panel',
346 layout: { type: 'fit' },
347 border: false,
348 stateful: false,
349 margins:'0 5 0 0',
350 items: [ me.defaultContent ]
351 },
352 rtree,
353 {
354 xtype: 'pveStatusPanel',
355 region: 'south',
356 margins:'0 5 5 5',
357 height: 200,
358 collapsible: true,
359 split:true
360 }
361 ]
362 });
363
364 me.callParent();
365
366 me.updateUserInfo();
367 }
368 });
369