The Doc Dialer: Handling the Onclick Event
The Doc Dialer
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
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
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
Produced by Yehuda Shiran and Tomer Shiran
Created: February 14, 2000
Revised: February 14, 2000
URL: https://www.webreference.com/js/column57/6.html