Workspace.js 8.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Workspace base class
 *
 * popup login window when auth fails (call onLogin handler)
 * update (re-login) ticket every 15 minutes
 *
 */

Ext.define('PVE.Workspace', {
    extend: 'Ext.container.Viewport',

    title: 'Proxmox Virtual Environment',

    loginData: null, // Data from last login call

    onLogin: function(loginData) {},

    // private
    updateLoginData: function(loginData) {
	var me = this;
	me.loginData = loginData;
	PVE.CSRFPreventionToken = loginData.CSRFPreventionToken;
	PVE.UserName = loginData.username;
24 25 26 27 28

	if (loginData.cap) {
	    Ext.state.Manager.set('GuiCap', loginData.cap);
	}

29 30 31
	// creates a session cookie (expire = null) 
	// that way the cookie gets deleted after browser window close
	Ext.util.Cookies.set('PVEAuthCookie', loginData.ticket, null, '/', null, true);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	me.onLogin(loginData);
    },

    // private
    showLogin: function() {
	var me = this;

	PVE.Utils.authClear();
	PVE.UserName = null;
	me.loginData = null;

	if (!me.login) {
	    me.login = Ext.create('PVE.window.LoginWindow', {
		handler: function(data) {
		    me.login = null;
		    me.updateLoginData(data);
		}
	    });
	}
	me.onLogin(null);
        me.login.show();
    },

    initComponent : function() {
	var me = this;

	Ext.tip.QuickTipManager.init();

	// fixme: what about other errors
	Ext.Ajax.on('requestexception', function(conn, response, options) {
	    if (response.status == 401) { // auth failure
		me.showLogin();
	    }
	});

	document.title = me.title;

	me.callParent();

        if (!PVE.Utils.authOK()) {
	    me.showLogin();
	} else { 
	    if (me.loginData) {
		me.onLogin(me.loginData);
	    }
	}

	Ext.TaskManager.start({
	    run: function() {
		var ticket = PVE.Utils.authOK();
		if (!ticket || !PVE.UserName) {
		    return;
		}

		Ext.Ajax.request({
		    params: { 
			username: PVE.UserName,
			password: ticket
		    },
		    url: '/api2/json/access/ticket',
		    method: 'POST',
		    success: function(response, opts) {
			var obj = Ext.decode(response.responseText);
			me.updateLoginData(obj.data);
		    }
		});
	    },
	    interval: 15*60*1000
	});

    }
});

Ext.define('PVE.ConsoleWorkspace', {
    extend: 'PVE.Workspace',

    alias: ['widget.pveConsoleWorkspace'],

110
    title: gettext('Console'),
111 112 113 114 115 116 117 118 119 120

    initComponent : function() {
	var me = this;

	var param = Ext.Object.fromQueryString(window.location.search);
	var consoleType = me.consoleType || param.console;

	var content;
	if (consoleType === 'kvm') {
	    me.title = "VM " + param.vmid;
121 122 123
	    if (param.vmname) {
		me.title += " ('" + param.vmname + "')";
	    }
124 125 126 127
	    content = {
		xtype: 'pveKVMConsole',
		vmid: param.vmid,
		nodename: param.node,
128
		vmname: param.vmname,
129 130 131 132
		toplevel: true
	    };
	} else if (consoleType === 'openvz') {
	    me.title = "CT " + param.vmid;
133 134 135
	    if (param.vmname) {
		me.title += " ('" + param.vmname + "')";
	    }
136 137 138 139
	    content = {
		xtype: 'pveOpenVZConsole',
		vmid: param.vmid,
		nodename: param.node,
140
		vmname: param.vmname,
141 142 143
		toplevel: true
	    };
	} else if (consoleType === 'shell') {
144
	    me.title = "node '" + param.node;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	    content = {
		xtype: 'pveShell',
		nodename: param.node,
		toplevel: true
	    };
	} else {
	    content = {
		border: false,
		bodyPadding: 10,
		html: 'Error: No such console type' 
	    };
	}

	Ext.apply(me, {
	    layout: { type: 'fit' },
	    border: false,
	    items: [ content ]
	});

	me.callParent();       
    }
});

Ext.define('PVE.StdWorkspace', {
    extend: 'PVE.Workspace',

    alias: ['widget.pveStdWorkspace'],

    // private
    setContent: function(comp) {
	var me = this;
	
	var cont = me.child('#content');
	cont.removeAll(true);
Dietmar Maurer's avatar
Dietmar Maurer committed
179 180

	if (comp) {
Dietmar Maurer's avatar
Dietmar Maurer committed
181
	    PVE.Utils.setErrorMask(cont, false);
Dietmar Maurer's avatar
Dietmar Maurer committed
182 183 184
	    comp.border = false;
	    cont.add(comp);
	    cont.doLayout();
Dietmar Maurer's avatar
Dietmar Maurer committed
185 186
	} 
	// else {
187 188 189 190 191
	    // TODO: display something useful

	    // Note:: error mask has wrong zindex, so we do not
	    // use that - see bug 114
	    // PVE.Utils.setErrorMask(cont, 'nothing selected');
Dietmar Maurer's avatar
Dietmar Maurer committed
192
	//}
193 194 195 196 197 198 199 200
    },

    selectById: function(nodeid) {
	var me = this;
	var tree = me.down('pveResourceTree');
	tree.selectById(nodeid);
    },

201
    checkVmMigration: function(record) {
202 203
	var me = this;
	var tree = me.down('pveResourceTree');
204
	tree.checkVmMigration(record);
205 206
    },

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    onLogin: function(loginData) {
	var me = this;

	me.updateUserInfo();

	if (loginData) {
	    PVE.data.ResourceStore.startUpdate();
	}
    },

    updateUserInfo: function() {
	var me = this;

	var ui = me.query('#userinfo')[0];

	if (PVE.UserName) {
223 224
	    var msg =  Ext.String.format(gettext("You are logged in as {0}"), "'" + PVE.UserName + "'");
	    ui.update('<div class="x-unselectable" style="white-space:nowrap;">' + msg + '</div>');
225 226 227 228 229 230 231 232 233 234 235
	} else {
	    ui.update('');
	}
	ui.doLayout();
    },

    initComponent : function() {
	var me = this;

	Ext.History.init();

236 237 238
	var sprovider = Ext.create('PVE.StateProvider');
	Ext.state.Manager.setProvider(sprovider);

Dietmar Maurer's avatar
Dietmar Maurer committed
239
	var selview = new PVE.form.ViewSelector({});
240 241 242

	var rtree = Ext.createWidget('pveResourceTree', {
	    viewFilter: selview.getViewFilter(),
Dietmar Maurer's avatar
Dietmar Maurer committed
243
	    flex: 1,
244 245 246 247 248 249 250 251
	    selModel: new Ext.selection.TreeModel({
		listeners: {
		    selectionchange: function(sm, selected) {
			var comp;
			var tlckup = {
			    root: 'PVE.dc.Config',
			    node: 'PVE.node.Config',
			    qemu: 'PVE.qemu.Config',
252
			    openvz: 'PVE.openvz.Config',
Dietmar Maurer's avatar
Dietmar Maurer committed
253 254
			    storage: 'PVE.storage.Browser',
			    pool: 'pvePoolConfig'
255 256 257 258 259 260
			};
			
			if (selected.length > 0) {
			    var n = selected[0];
			    comp = { 
				xtype: tlckup[n.data.type || 'root'] || 
Dietmar Maurer's avatar
Dietmar Maurer committed
261
				    'pvePanelConfig',
262 263 264 265
				layout: { type: 'fit' },
				showSearch: (n.data.id === 'root') ||
				    Ext.isDefined(n.data.groupbyid),
				pveSelNode: n,
266
				workspace: me,
267 268 269 270 271 272 273 274 275 276
				viewFilter: selview.getViewFilter()
			    };
			}

			me.setContent(comp);
		    }
		}
	    })
	});

Dietmar Maurer's avatar
Dietmar Maurer committed
277 278 279 280 281 282 283
	selview.on('select', function(combo, records) { 
	    if (records && records.length) {
		var view = combo.getViewFilter();
		rtree.setViewFilter(view);
	    }
	});

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	var caps = sprovider.get('GuiCap');

	var createVM = Ext.createWidget('button', {
	    pack: 'end',
	    margins: '3 5 0 0',
	    baseCls: 'x-btn',
	    text: gettext("Create VM"),
	    disabled: !caps.vms['VM.Allocate'],
	    handler: function() {
		var wiz = Ext.create('PVE.qemu.CreateWizard', {});
		wiz.show();
	    } 
	});

	var createCT = Ext.createWidget('button', {
	    pack: 'end',
	    margins: '3 5 0 0',
	    baseCls: 'x-btn',
	    text: gettext("Create CT"),
	    disabled: !caps.vms['VM.Allocate'],
	    handler: function() {
		var wiz = Ext.create('PVE.openvz.CreateWizard', {});
		wiz.show();
	    } 
	});

	sprovider.on('statechange', function(sp, key, value) {
	    if (key === 'GuiCap' && value) {
		caps = value;
		createVM.setDisabled(!caps.vms['VM.Allocate']);
		createCT.setDisabled(!caps.vms['VM.Allocate']);
	    }
	});

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	Ext.apply(me, {
	    layout: { type: 'border' },
	    border: false,
	    items: [
		{
		    region: 'north',
		    height: 30,
		    layout: { 
			type: 'hbox',
			align : 'middle'
		    },
		    baseCls: 'x-plain',		
		    defaults: {
			baseCls: 'x-plain'			
		    },
		    border: false,
		    margins: '2 0 5 0',
		    items: [
			{
			    margins: '0 0 0 4',
			    html: '<a class="x-unselectable" target=_blank href="http://www.proxmox.com">' +
				'<img height=30 width=209 src="/pve2/images/proxmox_logo.png"/></a>'
			},
			{
			    minWidth: 200,
			    flex: 1,
344
			    html: '<span class="x-panel-header-text">Proxmox Virtual Environment<br>' + gettext('Version') + ' ' + PVE.GUIVersion + "</span>"
345 346 347 348 349 350 351 352 353 354 355 356
			},
			{
			    pack: 'end',
			    margins: '8 10 0 10',
			    id: 'userinfo',
			    stateful: false
			},
			{
			    pack: 'end',
			    margins: '3 5 0 0',
			    xtype: 'button',
			    baseCls: 'x-btn',
357
			    text: gettext("Logout"),
358 359 360 361 362 363 364
			    handler: function() { 
				PVE.data.ResourceStore.stopUpdate();
				me.showLogin(); 
				me.setContent(); 
				var rt = me.down('pveResourceTree');
				rt.clearTree();
			    }
365 366 367
			}, 
			createVM, 
			createCT
368 369 370 371 372
		    ]
		},
		{
		    region: 'center',
		    id: 'content',
Dietmar Maurer's avatar
Dietmar Maurer committed
373
		    xtype: 'container',
374 375 376
		    layout: { type: 'fit' },
		    border: false,
		    stateful: false,
Dietmar Maurer's avatar
Dietmar Maurer committed
377 378 379 380 381 382 383 384 385 386 387 388
		    margins: '0 5 0 0',
		    items: []
		},
		{
		    region: 'west',
		    xtype: 'container',
		    border: false,
		    layout: { type: 'vbox', align: 'stretch' },
		    margins: '0 0 0 5',
		    split: true,
		    width: 200,
		    items: [ selview, rtree ]
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
		},
		{
		    xtype: 'pveStatusPanel',
		    region: 'south',
		    margins:'0 5 5 5',
		    height: 200,       
		    split:true
		}
	    ]
	});

	me.callParent();

	me.updateUserInfo();
    }
});