package/ui/phpvirtualbox/js/phpvirtualbox.js
/**
 * Menu class for use with context or button menus
 * 
 * @constructor
 * @class vboxMenu
 * @param {Object}
 *            name - name of menu
 *            id - optional HTMLNode id of menu to use
 * 			  menuItems - list of menu items to add
 *            language_context - translation language context
 */
Menu class for use with context or button menus

@constructor
@class vboxMenu
@param {Object}
name - name of menu
id - optional HTMLNode id of menu to use
menuItems - list of menu items to add
language_context - translation language context
Menu class for use with context or button menus @constructor @class vboxMenu @param { Object } name - name of menu id - optional HTMLNode id of menu to use menuItems - list of menu items to add language_context - translation language context
function vboxMenu(options) {
	var self = this;
	this.name = options.name;
	this.menuItems = {};
	this.iconStringDisabled = '_disabled';
	this.id = options.id;
	this.language_context = options.language_context;
	this.menuId = function() {
		if(self.id) return self.id;
		return self.name + 'Menu';
	};
	this.addMenu = function(m) {
		$('#vboxPane').append(self.menuElement(m,self.menuId()));
	};
	this.menuElement = function(m, mid) {
		var ul = null;
		if(mid) {
			ul = $('#'+mid);
			if(ul && ul.length) {
				ul.empty();
			} else {
				ul = $('<ul />').attr({'id':mid,'style':'display: none;'});
			}
		} else {
			ul = $('<ul />').attr({'style':'display: none;'});
		}
		ul.addClass('contextMenu');
		for(var i in m) {
			if(typeof m[i] == 'function') continue;
			var item = self.menuItem(m[i]);
			self.menuItems[m[i].name] = m[i];
			if(m[i].children && m[i].children.length) {
				item.append(self.menuElement(m[i].children, self.menuId()+'-submenu-' + i));
			}
			ul.append(item);
		}
		return ul;
	};
	this.menuClickCallback = function(i, item) {
		return self.menuItems[i].click(item);
	};
	this.menuItem = function(i) {
	    var label = trans(i.label, i.language_context ? i.language_context : self.language_context);
		return $('<li />').addClass((i.separator ? 'separator': '')).addClass((i.cssClass ? i.cssClass: '')).append($('<a />')
			.html(label)
			.attr({
				'style': (i.icon ? 'background-image: url('+self.menuIcon(i,false)+')': ''),
				'id': self.name+i.name,'href':'#'+i.name
			}));		
	};
	this.menuIcon = function(i,disabled) {
		if(!i.icon) return '';
		return 'images/vbox/' + i.icon + (disabled ? self.iconStringDisabled: '') + '_16px.png';
	};
	this.update = function(testObj) {
		for(var i in self.menuItems) {
			if(!self.menuItems[i].enabled) continue;
			var mi = $('#'+self.name+i);
			if(!self.menuItems[i].enabled(testObj)) {
				if(self.menuItems[i].hide_on_disabled) {
					mi.parent().hide();
				} else {
					self.disableItem(i,mi);
				}
			} else {
				if(self.menuItems[i].hide_on_disabled) { 
					mi.parent().show();
				} else {
					self.enableItem(i,mi);
				}
			}
		}
	};
	this.disableItem = function(i, mi) {
		if(!mi) mi = $('#'+self.name+i);
		if(self.menuItems[i].icon)
			mi.css({'background-image':'url('+self.menuIcon(self.menuItems[i],true)+')'}).parent().addClass('disabled');
		else
			mi.parent().addClass('disabled');		
	};
	this.enableItem = function(i, mi) {
		if(!mi) mi = $('#'+self.name+i);
		if(self.menuItems[i].icon)
			mi.css({'background-image':'url('+self.menuIcon(self.menuItems[i],false)+')'}).parent().removeClass('disabled');
		else
			mi.parent().removeClass('disabled');		
	};
	if(options.menuItems) self.addMenu(options.menuItems);
}
function vboxMenu ( options ) { var self = this ; this . name = options . name ; this . menuItems = { } ; this . iconStringDisabled = '_disabled' ; this . id = options . id ; this . language_context = options . language_context ; this . menuId = function ( ) { if ( self . id ) return self . id ; return self . name + 'Menu' ; } ; this . addMenu = function ( m ) { $ ( '#vboxPane' ) . append ( self . menuElement ( m , self . menuId ( ) ) ) ; } ; this . menuElement = function ( m , mid ) { var ul = null ; if ( mid ) { ul = $ ( '#' + mid ) ; if ( ul && ul . length ) { ul . empty ( ) ; } else { ul = $ ( '<ul />' ) . attr ( { 'id' : mid , 'style' : 'display: none;' } ) ; } } else { ul = $ ( '<ul />' ) . attr ( { 'style' : 'display: none;' } ) ; } ul . addClass ( 'contextMenu' ) ; for ( var i in m ) { if ( typeof m [ i ] == 'function' ) continue ; var item = self . menuItem ( m [ i ] ) ; self . menuItems [ m [ i ] . name ] = m [ i ] ; if ( m [ i ] . children && m [ i ] . children . length ) { item . append ( self . menuElement ( m [ i ] . children , self . menuId ( ) + '-submenu-' + i ) ) ; } ul . append ( item ) ; } return ul ; } ; this . menuClickCallback = function ( i , item ) { return self . menuItems [ i ] . click ( item ) ; } ; this . menuItem = function ( i ) { var label = trans ( i . label , i . language_context ? i . language_context : self . language_context ) ; return $ ( '<li />' ) . addClass ( ( i . separator ? 'separator' : '' ) ) . addClass ( ( i . cssClass ? i . cssClass : '' ) ) . append ( $ ( '<a />' ) . html ( label ) . attr ( { 'style' : ( i . icon ? 'background-image: url(' + self . menuIcon ( i , false ) + ')' : '' ) , 'id' : self . name + i . name , 'href' : '#' + i . name } ) ) ; } ; this . menuIcon = function ( i , disabled ) { if ( ! i . icon ) return '' ; return 'images/vbox/' + i . icon + ( disabled ? self . iconStringDisabled : '' ) + '_16px.png' ; } ; this . update = function ( testObj ) { for ( var i in self . menuItems ) { if ( ! self . menuItems [ i ] . enabled ) continue ; var mi = $ ( '#' + self . name + i ) ; if ( ! self . menuItems [ i ] . enabled ( testObj ) ) { if ( self . menuItems [ i ] . hide_on_disabled ) { mi . parent ( ) . hide ( ) ; } else { self . disableItem ( i , mi ) ; } } else { if ( self . menuItems [ i ] . hide_on_disabled ) { mi . parent ( ) . show ( ) ; } else { self . enableItem ( i , mi ) ; } } } } ; this . disableItem = function ( i , mi ) { if ( ! mi ) mi = $ ( '#' + self . name + i ) ; if ( self . menuItems [ i ] . icon ) mi . css ( { 'background-image' : 'url(' + self . menuIcon ( self . menuItems [ i ] , true ) + ')' } ) . parent ( ) . addClass ( 'disabled' ) ; else mi . parent ( ) . addClass ( 'disabled' ) ; } ; this . enableItem = function ( i , mi ) { if ( ! mi ) mi = $ ( '#' + self . name + i ) ; if ( self . menuItems [ i ] . icon ) mi . css ( { 'background-image' : 'url(' + self . menuIcon ( self . menuItems [ i ] , false ) + ')' } ) . parent ( ) . removeClass ( 'disabled' ) ; else mi . parent ( ) . removeClass ( 'disabled' ) ; } ; if ( options . menuItems ) self . addMenu ( options . menuItems ) ; }
Menu class for use with context or button menus
Menu class for use with context or button menus
/**
	 * return menu id
	 * 
	 * @memberOf vboxMenu
	 * @return {String} the HTMLNode id of this menu
	 */ /**
	 * Add menu to menu object
	 * 
	 * @memberOf vboxMenu
	 * @param {Object}
	 *            m - menu configuration object
	 */ /**
	 * Traverse menu configuration object and generate a
	 * <UL>
	 * containing menu items
	 * 
	 * @memberOf vboxMenu
	 * @param {Object}
	 *            m - menu configuration object
	 * @param {String}
	 *            mid - the optional id to use for the generated HTMLNode
	 * @return {HTMLNode} menu
	 *         <UL>
	 *         node containing menu items and submenus
	 */ // get menu item // Add to menu list // Children? /**
	 * Menu click callback
	 * 
	 * @memberOf vboxMenu
	 * @param {Integer}
	 *            i - menu item index number
	 * @param {Object}
	 *            item - optional selected item
	 * @return return value of menu item's click() function
	 */ /**
	 * generate menu item HTML
	 * 
	 * @memberOf vboxMenu
	 * @param {Object}
	 *            i - menu item's configuration object
	 * @return {HTMLNode}
	 *         <li> containing menu item
	 */ /**
	 * Return a URL to use for menu item's icon
	 * 
	 * @memberOf vboxMenu
	 * @param {Object}
	 *            i - menu item configuration object
	 * @param {Boolean}
	 *            disabled - whether or not the icon should be disabled
	 * @return {String} url to icon to use
	 */ /**
	 * Update all menu items
	 * 
	 * @memberOf vboxMenu
	 * @param {Object}
	 *            testObj - object used to test for enabled()
	 * @return null
	 */ // If enabled function doesn't exist, there's nothing to do // Disabled // Enabled /**
	 * Disable a single menu item
	 * 
	 * @memberOf vboxMenu
	 * @param {String}
	 *            i - menu item's name
	 * @param {Object}
	 *            mi - optional menu item HTMLNode or jQuery object
	 */ /**
	 * Enable a single menu item
	 * 
	 * @memberOf vboxMenu
	 * @param {String}
	 *            i - menu item's name
	 * @param {Object}
	 *            mi - optional menu item HTMLNode or jQuery object
	 */ // Just add menu items if there were passed