spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column57


The Doc Dialer

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Handling the Onclick Event

The main challenge of our DOCJS Trie Phone is how to extract the relevant names from the trie for each additional key clicked by the user. Let's examine first the numPressed() function which is the event handler for all buttons:

function numPressed(digit) {
  if (digit == 0) {
    currentTrie = tree;
    outputStringToDisplay(welcomeStr);
  }
  else if (typeof(currentTrie) == "number") return;
       else if (digit >= 2 && digit <= 9 && currentTrie[digit]) {
         currentTrie = currentTrie[digit];
         updateBoard(currentTrie);
         printDisplay();
       }
  clearDisplayIndex();
}

The first if block handles a click on the 0 key which clears the display. Preparing for the following search, we first reassign currentTrie to the top level hierarchy of the trie:

currentTrie = tree;

We then print the welcome message to the display:

outputStringToDisplay(welcomeStr);

The else block handles the case where one of the keys 2 to 9 is clicked. First we need to check if there are still levels of hierarchies beneath where we are now in the trie. The indication for more levels is the type of currentTrie. If it's a number, then it means we are already at the bottom of the trie, pointing to a specific name. If it's an object, then there are at least one more level of hierarchy beneath the current position. If there are no more levels of hierarchy, we just return from the function:

else if (typeof(currentTrie) == "number") return;

In all other cases we need to check first that the new digit is leading to a new object:

if (digit >= 2 && digit <= 9 && currentTrie[digit])

Once we passed this test, we need to do advance the currentTrie to the next level of hierarchy:

currentTrie = currentTrie[digit];

We then update the board information by calling:

updateBoard(currentTrie);

And finally we print the board information to the display:

printDisplay();

The last line of the function clears the printList array, preparing it for the next digit clicked:

clearDisplayIndex();

The clearDisplayIndex() function is a simple assignment to false of the printList array:

function clearDisplayIndex() {
  for (i=0; i<=EMPMAX; i++) {
    printList[i] = false;
  }
}

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Created: February 14, 2000
Revised: February 14, 2000

URL: http://www.webreference.com/js/column57/6.html