×
Namespaces

Variants
Actions
(Difference between revisions)

Catching menu events in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
dekuykin (Talk | contribs)
(New page: __NOTOC__ __NOEDITSECTION__ {{CodeSnippet |id=  |platform=S60 5th Edition |devices=Nokia 5800 XM |category=Web Runtime (WRT) |subcategory=UI |creationdate=January 26, 2008 |keywords=m...)
 
croozeus (Talk | contribs)

Revision as of 13:45, 26 February 2009


Article Metadata
Tested with
Devices(s): Nokia 5800 XM
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: menu, menu.onShow, MenuItem.onSelect
Created: (26 Jan 2008)
Last edited: croozeus (26 Feb 2009)


Overview

This code snippet demonstrates how to handle onShow menu event. It shows the count how many times menu was opened on screen.

It uses JavaScript WRT menu object and menu.onShow property to set the handler for menu events. Property MenuItem.onSelect is using to set handler for menu item selecting events.

Source file: Relevant HTML components

    <p id="menuOpenCountPhrase">
Menu was opened <b>0</b> times!
</p>
<input id="menuOpenCount" type="hidden" value="0"/>

Source file: JavaScript file

// Menu items
var menuItemCancel = null;
var menuItemReset = null;
 
// Menu id's
var CMD_MENU_CANCEL = 10;
var CMD_MENU_RESET = 11;
 
window.onload = init;
 
// Initializes the widget
function init() {
createMenu();
}
 
// Creates the main menu
function createMenu() {
// Create the "Cancel" menu item
menuItemCancel = new MenuItem("Cancel", CMD_MENU_CANCEL);
menuItemCancel.onSelect = onMenuItemSelected;
window.menu.append(menuItemCancel);
 
// Create the "Reset" menu item
menuItemReset = new MenuItem("Reset", CMD_MENU_RESET);
menuItemReset.onSelect = onMenuItemSelected;
window.menu.append(menuItemReset);
 
// Show right softkey
window.menu.showSoftkeys();
// Set menu's onShow event handler
window.menu.onShow = incrementCounter;
}
 
// Gets called when a menu item is selected
function onMenuItemSelected(menuId) {
switch (menuId) {
case CMD_MENU_CANCEL:
// Do nothing.
break;
case CMD_MENU_RESET:
// Reset the counter of menu events
try {
document.getElementById('menuOpenCount').value = "0";
} catch (e) {
alert("Error: " + e);
}
 
// Reset the menu openings count phrase
try {
document.getElementById('menuOpenCountPhrase').innerHTML =
"Menu was opened <b>0</b> times!";
} catch (e) {
alert("Error: " + e);
}
 
break;
}
}
 
/**
* Increments counter of menu opening events.
*/

function incrementCounter() {
// Get reference to counter value
try {
var counterLabel = document.getElementById('menuOpenCount');
} catch (e) {
alert("Error: " + e);
}
 
if (counterLabel == undefined) {
alert("Error: Menu events counter object is undefined!");
}
 
// Get current count of menu opening events
var menuOpenCount = Number(counterLabel.value);
// Increment counter
var menuOpenCountNew = menuOpenCount + 1;
counterLabel.value = String(menuOpenCountNew);
// Form the menu openings count phrase
try {
document.getElementById('menuOpenCountPhrase').innerHTML =
"Menu was opened <b>" + menuOpenCountNew + "</b> times!";
} catch (e) {
alert("Error: " + e);
}
}

Postconditions

View of launched snippet contains information - how many times menu was opened. Every time the menu will be opened - indicator on screen will be updated. On click the "Reset" menu item counter is reseted to value "0".


Supplementary material

  • You can test the handling of menu event in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: Media:Catching_menu_events_in_WRT.zip.
  • You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: link_to_patchs
143 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×