Symbian.org/preview/script/lib/menu.js
changeset 12 d3fff58a7af9
parent 11 aaba47256eea
child 13 3a1db8573f1e
equal deleted inserted replaced
11:aaba47256eea 12:d3fff58a7af9
     1 /**
       
     2  * widget object constructor
       
     3  * @param {void}
       
     4  *     widget()
       
     5  * @return {void}
       
     6  */ 
       
     7 
       
     8 if (typeof window.menu == "undefined" || !window.menu) 
       
     9 {
       
    10 	window.menu = 
       
    11 	{
       
    12 		author : 'Nokia WRT Emulation Library',
       
    13 		items : [],
       
    14 		index : null,
       
    15 		isDimmed : false,	
       
    16 			
       
    17 		//	Event triggers
       
    18 		onShow : null,
       
    19 		onRightSoftKeySelect : null,
       
    20 	};
       
    21 	
       
    22 	
       
    23 	/*
       
    24 		Function 	:	menu.append()
       
    25 		Argument	:	MenuItem Object
       
    26 		Returns		:	Void
       
    27 		Description	:	Function appends MenuItem to a Menu Object
       
    28 	*/
       
    29 	menu.append = function(MenuItem)
       
    30 	{
       
    31 		if(this.allowedTypeOf(MenuItem))
       
    32 		{
       
    33 			var i;
       
    34 			var flag = true;
       
    35 			try{
       
    36 			for(var key in this.items)
       
    37 			{
       
    38 				if(this.items[key].id == MenuItem.id)
       
    39 				{	
       
    40 					flag = false; 
       
    41 					break; 
       
    42 				}
       
    43 			}} catch(e){ }
       
    44 			if(flag)
       
    45 			{
       
    46 				//	MenuItem.parent = this;
       
    47 				this.items[MenuItem.id] = MenuItem;
       
    48 			}
       
    49 		}
       
    50 	}
       
    51 	
       
    52 	
       
    53 	/*
       
    54 		Function 	:	menu.remove()
       
    55 		Argument	:	MenuItem Object
       
    56 		Returns		:	Void
       
    57 		Description	:	Function Remove the menuItem and its children from the container options menu.
       
    58 	*/
       
    59 	menu.remove = function(MenuItem)
       
    60 	{
       
    61 		if(!this.allowedTypeOf(MenuItem))
       
    62 			return false;
       
    63 	
       
    64 		var flag = false;
       
    65 		if (this.items.length) {
       
    66 			for (var key in this.items) {
       
    67 				if (this.items[key].id == MenuItem.id) {
       
    68 					flag = true;
       
    69 					break;
       
    70 				}
       
    71 			}
       
    72 		}
       
    73 		if(flag)
       
    74 		{
       
    75 			this.items.splice(key, 1);
       
    76 		}
       
    77 	}
       
    78 	
       
    79 	/*
       
    80 		Function 	:	menu.clear()
       
    81 		Argument	:	Void
       
    82 		Returns		:	Void
       
    83 		Description	:	Clears (deletes) all the menu items in the menupane.
       
    84 	*/
       
    85 	menu.clear = function()
       
    86 	{
       
    87 		try
       
    88 		{
       
    89 			this.items.splice(0, this.items.length);
       
    90 		}catch(e){}
       
    91 	}
       
    92 	
       
    93 	
       
    94 	/*
       
    95 		Function 	:	Menu.getMenuItemById(id)
       
    96 		Argument	:	Integer
       
    97 		Returns		:	MenuItem Object
       
    98 		Description	:	Function get the MenuItem Object with the reference of id
       
    99 	*/
       
   100 	menu.getMenuItemById = function(id)
       
   101 	{
       
   102 		var menuItemRef = menu.menuItemExhistsById(this, id, 0);
       
   103 		if(this.allowedTypeOf(menuItemRef))
       
   104 			return menuItemRef;
       
   105 		else
       
   106 			return undefined;
       
   107 	}
       
   108 	
       
   109 	
       
   110 	/*
       
   111 		Function 	:	Menu.getMenuItemByName(name)
       
   112 		Argument	:	String
       
   113 		Returns		:	MenuItem Object
       
   114 		Description	:	Function get the MenuItem Object with the reference of String name
       
   115 	*/
       
   116 	menu.getMenuItemByName = function(name)
       
   117 	{
       
   118 		var menuItemRef = menu.menuItemExhistsById(this, name, 1);
       
   119 	
       
   120 	//	if(menuItemRef !=null)
       
   121 		if(this.allowedTypeOf(menuItemRef))
       
   122 			return menuItemRef;
       
   123 		else
       
   124 			return undefined;
       
   125 	}
       
   126 	
       
   127 	/*
       
   128 		Function 	:	Menu.setRightSoftkeyLabel()
       
   129 		Argument	:	String, Function
       
   130 		Returns		:	Void
       
   131 		Description	:	Set the label of the right soft key to str. This enables the default text 
       
   132 						to be changed from exit and a new function assigned by setting a callbackfunction
       
   133 	*/
       
   134 
       
   135 	menu.setRightSoftkeyLabel = function(label, callback)
       
   136 	{
       
   137 		window.menu = this;
       
   138 		try
       
   139 		{
       
   140 			if(typeof label != '' && !label)
       
   141 				this.setExitToRsk();			
       
   142 
       
   143 			else if(typeof callback != 'function' && !callback)
       
   144 				this.setExitToRsk();			
       
   145 			
       
   146 			else if (_BRIDGE_REF.nokia.menu.setRsk(callback)) {
       
   147 				_BRIDGE_REF.parent.$("#RskLabel > a")[0].innerHTML = label;
       
   148 				_BRIDGE_REF.nokia.menu.rsk_label = label;
       
   149 				_BRIDGE_REF.nokia.menu.rsk_event = callback;
       
   150 				_BRIDGE_REF.nokia.menu.is_rsk_overridden = true;
       
   151 			}
       
   152 			else 
       
   153 				this.setExitToRsk();
       
   154 
       
   155 		}catch(e){ 
       
   156 			// alert(e);
       
   157 		 }
       
   158 	}
       
   159 
       
   160 
       
   161 	menu.setExitToRsk = function()
       
   162 	{
       
   163 		this.onRightSoftKeySelect = null;
       
   164 
       
   165 		_BRIDGE_REF.nokia.menu.is_rsk_overridden = false;
       
   166 		_BRIDGE_REF.nokia.menu.rsk_label = '';
       
   167 		_BRIDGE_REF.nokia.menu.rsk_event = null;
       
   168 		_BRIDGE_REF.parent.$("#RskLabel > a")[0].innerHTML = 'Exit';
       
   169 
       
   170 		_BRIDGE_REF.nokia.menu.setRsk(function(){
       
   171 			_BRIDGE_REF.nokia.menu.exit();
       
   172 		});
       
   173 	}
       
   174 	
       
   175 	/*
       
   176 		Function 	:	Menu.showSoftkeys()
       
   177 		Argument	:	Void
       
   178 		Returns		:	Void
       
   179 		Description	:	Makes the softkeys visible. By default the softkeys are not visible
       
   180 	
       
   181 	*/
       
   182 	menu.showSoftkeys = function()
       
   183 	{
       
   184 		/*
       
   185 		 *  Shows showSoftkeys
       
   186 		 */
       
   187 		_BRIDGE_REF.nokia.menu.softkeys_visibility = true;
       
   188 		_BRIDGE_REF.nokia.menu.showSoftKeys();
       
   189 	}
       
   190 	
       
   191 	/*
       
   192 		Function 	:	Menu.hideSoftkeys()
       
   193 		Argument	:	Void
       
   194 		Returns		:	Void
       
   195 		Description	:	Makes the softkeys invisible. By default the softkeys are not visible. 
       
   196 	
       
   197 	*/
       
   198 	menu.hideSoftkeys = function()
       
   199 	{
       
   200 		/*
       
   201 		 *  Hide showSoftkeys
       
   202 		 */
       
   203 		_BRIDGE_REF.nokia.menu.softkeys_visibility = false;
       
   204 		_BRIDGE_REF.nokia.menu.hideSoftKeys();
       
   205 	}
       
   206 	
       
   207 	
       
   208 	/*	
       
   209 	 *  
       
   210 	 * ----------------------------------------------------------------
       
   211 	 * Exta Functionalities which helps to make main functions to work
       
   212 	 * ----------------------------------------------------------------
       
   213 	 *  
       
   214 	*/
       
   215 	
       
   216 	menu.cancel = function()
       
   217 	{
       
   218 		_BRIDGE_REF.nokia.menu.cancel();
       
   219 	}
       
   220 	
       
   221 	menu.exit = function()
       
   222 	{
       
   223 		_BRIDGE_REF.nokia.menu.exit();
       
   224 	}
       
   225 	
       
   226 	
       
   227 	menu.triggeLSKEvent = function()
       
   228 	{
       
   229 		if(typeof(window.menu.onShow) == 'function')
       
   230 		{
       
   231 				window.menu.onShow();
       
   232 		}
       
   233 		_BRIDGE_REF.parent.$('#softKeysPane').show();
       
   234 		this.show();
       
   235 	}
       
   236 	
       
   237 	menu.triggerEvent = function(MenuItemId)
       
   238 	{
       
   239 		try{
       
   240 			var menuItemRef = this.menuItemExhistsById(this, MenuItemId, 0);
       
   241 			if(menuItemRef != null)
       
   242 			{
       
   243 				if(typeof menuItemRef.onSelect == 'function')
       
   244 					menuItemRef.onSelect(MenuItemId);
       
   245 		
       
   246 				if(_BRIDGE_REF.helper.getElementsLengthInObject(menuItemRef.items))
       
   247 					this.show(MenuItemId);
       
   248 				else
       
   249 					this.cancel();
       
   250 
       
   251 			}else
       
   252 			{
       
   253 				this.show();
       
   254 			}
       
   255 		}
       
   256 		catch(e)
       
   257 		{
       
   258 			alert('triggeEvent: '+MenuItemId+' >> '+e);
       
   259 		}
       
   260 	}
       
   261 	
       
   262 	menu.hasChild = function(parentId)
       
   263 	{
       
   264 		for(var i in this.items)
       
   265 		{
       
   266 			if(this.items[i].parentId == parentId)
       
   267 			{	
       
   268 				 return true;
       
   269 			}
       
   270 		}
       
   271 		return false;
       
   272 	}
       
   273 	
       
   274 	
       
   275 	menu.allowedTypeOf = function(MenuItem)
       
   276 	{
       
   277 		try
       
   278 		{
       
   279 			if( (typeof(MenuItem) == 'object') && (MenuItem.type == 'MenuItem'))
       
   280 				return true;			
       
   281 		}
       
   282 		catch(e)
       
   283 		{
       
   284 			return false;
       
   285 		}
       
   286 	}
       
   287 	
       
   288 	menu.show = function(parentId)
       
   289 	{
       
   290 		try
       
   291 		{
       
   292 			var menuItemsPane = _BRIDGE_REF.parent.$('#MenuItemsArea')
       
   293 			menuItemsPane = menuItemsPane[0];
       
   294 			
       
   295 			menuItemsPane.innerHTML = '';
       
   296 			
       
   297 			var ul = document.createElement('ul');
       
   298 			var ele = window.menu;
       
   299 
       
   300 			if(typeof parentId != 'undefined' && typeof parentId == 'object')
       
   301 			{
       
   302 				if (typeof window.menu.onShow != null && typeof window.menu.onShow == 'function') {
       
   303 					window.menu.onShow();
       
   304 				}
       
   305 			}
       
   306 
       
   307 			if(typeof parentId == 'number')
       
   308 			{
       
   309 				var tempRef = menu.menuItemExhistsById(ele, parentId, 0);
       
   310 	
       
   311 				if(typeof parentId != 'undefined' && typeof tempRef != 'undefined')
       
   312 					ele = tempRef;
       
   313 			}
       
   314 
       
   315 			if(_BRIDGE_REF.helper.getElementsLengthInObject(ele.items))
       
   316 			{
       
   317 				for(var key in ele.items)
       
   318 				{
       
   319 					if(!ele.items[key].isDimmed){
       
   320 						
       
   321 						try{
       
   322 							ul.appendChild(menu.create_menuElement(ele.items[key]));
       
   323 						}catch(e){  }
       
   324 					}
       
   325 				}
       
   326 				if(typeof parentId == 'number' && _BRIDGE_REF.helper.getElementsLengthInObject(ele.items))
       
   327 				{
       
   328 					if(ele.parent)
       
   329 						ul.appendChild(menu.create_normalMenuItem('Back', ele.parent.id));	
       
   330 					else
       
   331 						ul.appendChild(menu.create_normalMenuItem('Back', null));	
       
   332 				}
       
   333 				else
       
   334 				{
       
   335 					ul.appendChild(menu.create_exitMenuItem());	
       
   336 				}
       
   337 
       
   338 
       
   339 				if(_BRIDGE_REF.helper.getElementsLengthInObject(ele.items) > 5)
       
   340 					menuItemsPane.style.overflowY = 'scroll';
       
   341 				else
       
   342 					menuItemsPane.style.overflowY = 'hidden';
       
   343 
       
   344 			}
       
   345 			else
       
   346 			{
       
   347 				menuItemsPane.style.overflowY = 'hidden';
       
   348 				ul.appendChild(menu.create_exitMenuItem());	
       
   349 			}
       
   350 			menuItemsPane.innerHTML = '<ul>'+ul.innerHTML+'</ul>';
       
   351 			
       
   352 			_BRIDGE_REF.nokia.menu.show();
       
   353 		}
       
   354 		catch(e)
       
   355 		{
       
   356 			alert('menu.show: '+e);
       
   357 		}
       
   358 	}
       
   359 
       
   360 
       
   361 
       
   362 /*
       
   363 *
       
   364 *	HELPER FUNCTIONS
       
   365 *
       
   366 */
       
   367 
       
   368 	menu.menuItemExhistsById = function(menuReference, value, argumentType)
       
   369 	{
       
   370 		var flag = null;
       
   371 		
       
   372 		for(var key in menuReference.items)
       
   373 		{
       
   374 			if(!argumentType)
       
   375 			{
       
   376 				if(menuReference.items[key].id == value)
       
   377 				{	
       
   378 					flag = true; 
       
   379 					break; 
       
   380 				}
       
   381 			}
       
   382 			else
       
   383 			{
       
   384 				if(menuReference.items[key].name == value)
       
   385 				{	
       
   386 					flag = true; 
       
   387 					break; 
       
   388 				}
       
   389 			}
       
   390 			
       
   391 			if(menuReference.items[key].items != undefined && menuReference.items[key].items.length)
       
   392 			{
       
   393 				var temp = this.menuItemExhistsById(menuReference.items[key], value, argumentType);
       
   394 				if(temp)
       
   395 					return temp;
       
   396 			}
       
   397 		}
       
   398 		if(flag)
       
   399 		{
       
   400 			// crate a package and send it
       
   401 			menuReference.items[key].index = key;
       
   402 			return menuReference.items[key];
       
   403 		}
       
   404 		else
       
   405 			return null;
       
   406 	}
       
   407 	
       
   408 	menu.create_menuElement = function(MenuItem) 
       
   409 	{
       
   410 		var listitem = document.createElement('li');
       
   411 		listitem.id = MenuItem.id;
       
   412 		listitem.setAttribute('onClick', 'javascript:NOKIA.emulator.child.menu.triggerEvent('+MenuItem.id+');');
       
   413 	
       
   414 	    var anchor = document.createElement('a');
       
   415 		anchor.id = 'subMenuItem_'+MenuItem.id;
       
   416 		anchor.innerHTML = MenuItem.name;
       
   417 		if(_BRIDGE_REF.helper.getElementsLengthInObject(MenuItem.items))
       
   418 	 	{  
       
   419 			anchor.className = 'subMenuItem';
       
   420 			anchor.setAttribute('href', 'javascript:NOKIA.emulator.child.menu.show('+MenuItem.id+');');
       
   421 		}
       
   422 	    listitem.appendChild(anchor);
       
   423 		return (listitem);
       
   424 	}
       
   425 	
       
   426 	menu.create_normalMenuItem = function(MenuTitle, index) 
       
   427 	{
       
   428 	    var listitem = document.createElement('li');
       
   429 	
       
   430 	    var anchor = document.createElement('a');
       
   431 		anchor.id = 'subMenuItem_BACK';
       
   432 		anchor.innerHTML = MenuTitle;
       
   433 	
       
   434 		if (MenuTitle == 'Back') {
       
   435 			listitem.className = 'exitOrBackBtn';
       
   436 			anchor.setAttribute('href', 'javascript:NOKIA.emulator.child.menu.triggerEvent(' + index + ');');
       
   437 		}
       
   438 		else 
       
   439 			anchor.setAttribute('href', 'javascript:NOKIA.emulator.child.menu.triggerEvent(' + index + ');');
       
   440 	    
       
   441 		listitem.appendChild(anchor);
       
   442 		return (listitem);
       
   443 	}
       
   444 	
       
   445 	menu.create_exitMenuItem = function() 
       
   446 	{
       
   447 	    var listitem = document.createElement('li');
       
   448 		listitem.className = 'exitOrBackBtn';
       
   449 	    var anchor = document.createElement('a');
       
   450 		anchor.id = 'subMenuItem_EXIT';
       
   451 		anchor.innerHTML = 'Exit';
       
   452 		anchor.setAttribute('href', 'javascript:NOKIA.emulator.child.menu.exit();');
       
   453 		listitem.setAttribute('onClick', 'javascript:NOKIA.emulator.child.menu.exit();');
       
   454 		
       
   455 	    listitem.appendChild(anchor);
       
   456 		return (listitem);
       
   457 	}
       
   458 	
       
   459 	menu.triggeRSK = function()
       
   460 	{
       
   461 		try {
       
   462 			if (window.menu) {
       
   463 				if (childToParent_Reference.$('softKeysPane').style.display != 'none') {
       
   464 					if (window.menu.onRightSoftKeySelect != null) {
       
   465 						window.menu.onRightSoftKeySelect();
       
   466 						window.menu.cancel();
       
   467 					}
       
   468 					else {
       
   469 						window.menu.cancel();
       
   470 					}
       
   471 				}
       
   472 			}
       
   473 		}catch(e)
       
   474 		{
       
   475 			alert(e);
       
   476 		}
       
   477 	}
       
   478 	
       
   479 	menu.triggeLSK = function()
       
   480 	{
       
   481 		if(window.menu)
       
   482 		{
       
   483 			window.menu.show();
       
   484 			if(typeof(window.menu.onShow) == 'function')
       
   485 			{
       
   486 				if(window.menu.onShow)
       
   487 				{
       
   488 					window.menu.onShow();
       
   489 				}
       
   490 			}
       
   491 		}
       
   492 	}
       
   493 
       
   494 
       
   495 	//	make TRUE menu.js script loaded
       
   496 	window.parent.NOKIA.scriptsLoaded.menu = true;
       
   497 }