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