Workspace.js 7.61 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 24 25 26 27 28 29 30 31 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*
 * 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',
    requires: [	     
	'Ext.tip.*',   
	'PVE.Utils', 
	'PVE.window.LoginWindow'
    ],

    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;
	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) {
			// cookie is automatically updated
			var obj = Ext.decode(response.responseText);
			me.updateLoginData(obj.data);
		    }
		});
	    },
	    interval: 15*60*1000
	});

    }
});

Ext.define('PVE.ConsoleWorkspace', {
    extend: 'PVE.Workspace',
    requires: [	  
	'PVE.KVMConsole'
    ],

    alias: ['widget.pveConsoleWorkspace'],

    title: 'Proxmox Console',

    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;
	    content = {
		xtype: 'pveKVMConsole',
		vmid: param.vmid,
		nodename: param.node,
126 127 128 129 130 131 132 133
		toplevel: true
	    };
	} else if (consoleType === 'openvz') {
	    me.title = "CT " + param.vmid;
	    content = {
		xtype: 'pveOpenVZConsole',
		vmid: param.vmid,
		nodename: param.node,
134 135 136 137 138 139 140 141 142 143 144 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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
		toplevel: true
	    };
	} else if (consoleType === 'shell') {
	    me.title = "node " + param.node + " - Proxmox Shell";
	    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',
    requires: [	  
	'Ext.History',
	'Ext.state.*',
	'Ext.selection.*',
	'PVE.form.ViewSelector', 
	'PVE.data.ResourceStore',
	'PVE.tree.ResourceTree'
    ],

    alias: ['widget.pveStdWorkspace'],

    // private
    defaultContent: {
	title: 'Nothing selected',
	region: 'center'
    },

    setContent: function(comp) {
	var me = this;
	
	if (!comp) { 
	    comp = me.defaultContent;
	}

	var cont = me.child('#content');
	cont.removeAll(true);
	cont.add(comp);
	cont.doLayout();
    },

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

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

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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) {
	    ui.update('<div class="x-unselectable" style="white-space:nowrap;">You are logged in as "' + PVE.UserName + '"</div>');
	} else {
	    ui.update('');
	}
	ui.doLayout();
    },

    initComponent : function() {
	var me = this;

	Ext.History.init();
	Ext.state.Manager.setProvider(Ext.create('PVE.StateProvider'));

	//document.title = ;

	var selview = new PVE.form.ViewSelector({
	    listeners: {
		select: function(combo, records) { 
		    if (records && records.length) {
			var view = combo.getViewFilter();
			combo.up('pveResourceTree').setViewFilter(view);
		    }
		}
	    }
	});

	var rtree = Ext.createWidget('pveResourceTree', {
	    width: 200,
	    region: 'west',
	    margins: '0 0 0 5',
	    split: true,
	    viewFilter: selview.getViewFilter(),
	    tbar: [ ' ', selview ],
	    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',
262
			    openvz: 'PVE.openvz.Config',
263 264 265 266 267 268 269 270 271 272 273 274
			    storage: 'PVE.storage.Browser'
			};
			
			if (selected.length > 0) {
			    var n = selected[0];
			    comp = { 
				xtype: tlckup[n.data.type || 'root'] || 
				    'PVE.panel.Config',
				layout: { type: 'fit' },
				showSearch: (n.data.id === 'root') ||
				    Ext.isDefined(n.data.groupbyid),
				pveSelNode: n,
275
				workspace: me,
276 277 278 279 280 281 282 283 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 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
				viewFilter: selview.getViewFilter()
			    };
			}

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

	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,
			    html: '<span class="x-panel-header-text">Proxmox Virtual Environment<br>Version ' + PVE.GUIVersion + "</span>"
			},
			{
			    pack: 'end',
			    margins: '8 10 0 10',
			    id: 'userinfo',
			    stateful: false
			},
			{
			    pack: 'end',
			    margins: '3 5 0 0',
			    xtype: 'button',
			    baseCls: 'x-btn',
			    text: "Logout",
			    handler: function() { 
				PVE.data.ResourceStore.stopUpdate();
				me.showLogin(); 
				me.setContent(); 
				var rt = me.down('pveResourceTree');
				rt.clearTree();
			    }
			},
			{
			    pack: 'end',
			    margins: '3 5 0 0',
			    xtype: 'button',
			    baseCls: 'x-btn',
			    text: "Create VM",
			    handler: function() {
				var wiz = Ext.create('PVE.qemu.CreateWizard', {});
				wiz.show();
			    } 
			},
			{
			    pack: 'end',
			    margins: '3 5 0 0',
			    xtype: 'button',
			    baseCls: 'x-btn',
			    text: "Create CT",
			    handler: function() {
				var wiz = Ext.create('PVE.openvz.CreateWizard', {});
				wiz.show();
			    } 
			}
		    ]
		},
		{
		    region: 'center',
		    id: 'content',
		    xtype: 'panel',
		    layout: { type: 'fit' },
		    border: false,
		    stateful: false,
		    margins:'0 5 0 0',
		    items: [ me.defaultContent ]
		},
		rtree,
		{
		    xtype: 'pveStatusPanel',
		    region: 'south',
		    margins:'0 5 5 5',
		    height: 200,       
		    collapsible: true,
		    split:true
		}
	    ]
	});

	me.callParent();

	me.updateUserInfo();
    }
});