January 29th, 2008
this.options.selectionColor; } }, The updateSelection() method does all the real work of actually changing the visual state of the selection. It updates the span created in the selection list we ll write that code on day 5 and sets its style.backgroundColor to the value speci- fied as the options.selectionColor of our component s Configuration object. Before we leave the topic of key-down handling, there s one more bit of book- keeping to take care of. Because we handle the arrow keys on the key-down rather than the key-up, we have to change the Up Arrow from its default behavior of moving the caret backward within the text field. We do this with the moveCaret- ToEnd() method called on a one-millisecond delay via setTimeout. The move- CaretToEnd() method is implemented as shown in listing 10.30. Listing 10.30 TextSuggest moveCaretToEnd() method moveCaretToEnd: function() { var pos = this.input.value.length; if (this.input.setSelectionRange) { this.input.setSelectionRange(pos,pos); } else if(this.input.createTextRange){ var m = this.input.createTextRange(); m.moveStart( character ,pos); m.collapse(); m.select(); } }, Refactoring 411 Now, let s move onto the key-up handling. The key-up implementation is a bit simpler than the key-down. All it has to do is broker its event to one of a couple of places based on the value in the input field and the key that was pressed. Let s take a look at the details in listing 10.31. Listing 10.31 TextSuggest key-up handlers keyupHandler: function(e) { if ( this.input.length == 0 && !this.isOp
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
Posted in Linux | No Comments »
January 29th, 2008
iginal script in terms of functionality is in the handling of the arrow keys. The arrow keys in our TextSuggest compo- nent handle the movement of the selection based on the onkeydown event rather than the onkeyup event. This is done solely as a usability improvement. It s some- what disconcerting to see the selection remain where it is when you press one of the arrow keys, only to see it move once you release the key. keydownHandler() therefore handles the movement of the selection. Note that the selection manip- ulation methods are methods of the TextSuggest component. The controller, because it saved a reference to the component at construction time, can call these methods through the saved object reference this.textSuggest. The selec- tion manipulation methods of TextSuggest are shown in listing 10.29 for the sake of completeness. Listing 10.29 TextSuggest selection manipulation methods moveSelectionUp: function() { if ( this.selectedIndex > 0 ) { this.updateSelection(this.selectedIndex - 1); } }, 410 CHAPTER 10 Type-ahead suggest moveSelectionDown: function() { if ( this.selectedIndex < (this.suggestions.length - 1) ) { this.updateSelection(this.selectedIndex + 1); } }, updateSelection: function(n) { var span = $(this.id +"_"+this.selectedIndex); if (span){ Clear previous selection span.style.backgroundColor = ""; } this.selectedIndex = n; var span = $(this.id+"_"+this.selectedIndex); if (span){ span.style.backgroundColor =
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.
Posted in Linux | No Comments »
January 29th, 2008
this.keyupHandler.bindAsEventListener(this); this.input.onkeydown = this.keydownHandler.bindAsEventListener(this); this.input.onblur = this.onblurHandler.bindAsEventListener(this); if ( this.isOpera ) this.input.onkeypress = this.keyupHandler.bindAsEventListener(this); }, All the relevant events that we need to listen to along with the Opera-specific hack mentioned in the first round of our script development are set up in this method. You will recall that the bindAsEventListener() method is a closure mechanism provided courtesy of the Prototype library. This mechanism allows Refactoring 409 our handlers to call first-class methods on the controller and normalizes the IE and W3C event models. Very nice, indeed. keyupHandler(), keydownHandler(), onblurHandler(), and their helper methods are mostly a repackaging of what s already been covered with a few changes. We ll show the full range of methods next and point out differences from the original script along the way. We ll start by discussing keydownHandler() and its manipulation of the selection. The key- downHandler() method is shown in listing 10.28. Listing 10.28 keydownHandler() method keydownHandler: function(e) { var upArrow = 38; var downArrow = 40; if ( e.keyCode == upArrow ) { this.textSuggest.moveSelectionUp(); setTimeout( this.moveCaretToEnd.bind(this), 1 ); } else if ( e.keyCode == downArrow ) { this.textSuggest.moveSelectionDown(); } }, The most significant difference from the or
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.
Posted in Linux | No Comments »
January 28th, 2008
f the Prototype library. Finally, createSuggestionsDiv() is called to create the contain- ing div element, which holds the UI for the suggestions. The TextSuggestKeyHandler We ve decided to put the broker of the events into a dedicated controller class. There s nothing new or revolutionary about it, but it s definitely a helpful way to separate class responsibilities. In reality, the design could be further sepa- rated by creating explicit classes for the model and view roles to provide a full MVC p te n T i e e cs i let t t e u e, b t w wil b e k d w t e at r . hs x r i e s f o h s r u e l r a o n h 408 CHAPTER 10 Type-ahead suggest architecture of the RSS reader in chapter 13 with a set of classes that satisfies a traditional MVC pattern. The controller is constructed in the same way as our main class using Class.create() and an initialize() method. The constructor is shown in listing 10.26. Listing 10.26 The TextSuggestKeyHandler constructor TextSuggestKeyHandler = Class.create(); TextSuggestKeyHandler.prototype = { initialize: function( textSuggest ) { Reference to TextSuggest this.textSuggest = textSuggest; this.input = this.textSuggest.textInput; this.addKeyHandling(); }, // rest of API }, Upon construction, the controller holds a reference to the suggest component along with the native HTML form input field. It then adds the handlers onto the input field via this.addKeyHandling(). The addKeyHandling() method is shown in listing 10.27. Listing 10.27 The keyboard handler addKeyHandling: function() { this.input.onkeyup =
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.
Posted in Linux | No Comments »
January 28th, 2008
Posted in Linux | No Comments »
January 27th, 2008
sms being put in place, we ll just cover much of the content here at a high level and talk about our response handling in terms of the algorithm. The first thing we do is to parse the response via the createSuggestions() method into an in-memory representation of the suggestions held in the suggestions property. The suggestions property is an array of objects, each with a text and a value property corresponding to the and elements of each in the XML response. The remainder of the ajaxUpdate() method s algorithm is fairly straightfor- ward and should be easy to follow. If no suggestions were found, the pop-up is hidden and the internal value held by the component via a hidden field is cleared. If suggestions were found, the drop-down UI element is created, popu- lated with the suggestions, and displayed, and the selection is updated to be the first one in the list. At this point, the response is considered to be handled, so the this.handlingRequest property discussed earlier is set back to false. Finally, the ajaxUpdate() method checks if there are any pending requests. If so, it sets the pendingRequest flag back to false, takes the current value in the input field for the lastRequestString, and initiates another request cycle via sendRequest- ForSuggestions(). This concludes the full request/response cycle for the Ajax support and wraps up day 3. We ve accomplished quite a bit today, plugging in an open source framework that fully Ajax-enables our component-meeting requirement, num- ber 7, as well as making sure that it s done in a way that s configurable and sup- ports multiple instances on the same page, satisfying requirements 2 and 3. We ll get into the details of what it means to create, position, show, and hide the UI for the pop-up on day 5. In the meantime, we ll hook up the component events and take care of the keyboard and mouse handling. 10.5.4 Day 4: handling events Now that the suggest component is fully Ajax enabled, it s time to hook it into the events produced by the native input field s responses to the keyboard. If you are an astute reader, you will have guessed that the code that initiates this process was back in the constructor hiding in a call to the injectSuggestBehavior() method. This is the code that initiates all modifications to the DOM of the existing markup, including the event handling, extra inputs, and the container for the suggestions. It s all done programmatically so we don t have to touch any of the HTML code on the page, per requirement number 1. The behavi
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.
Posted in Linux | No Comments »
January 27th, 2008
} else { Create and this.updateSuggestionsDiv(); show UI this.showSuggestions(); this.updateSelection(0); } Finish handling response this.handlingRequest = false; if ( this.pendingRequest ) { this.pendingRequest = false; this.lastRequestString = this.textInput.value; Send another request this.sendRequestForSuggestions(); } }, createSuggestions: function(ajaxResponse) { this.suggestions = []; var entries = ajaxResponse.getElementsByTagName( entry ); for ( var i = 0 ; i < entries.length ; i++ ) { var strText = this.getElementContent( entries[i].getElementsByTagName( text )[0]); var strValue = this.getElementContent( entries[i].getElementsByTagName( value )[0]); this.suggestions.push({ text: strText, value: strValue }); } }, getElementContent: function(element) { return element.firstChild.data; } 406 CHAPTER 10 Type-ahead suggest Because we want to focus solely on the Ajax mechani
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.
Posted in Linux | No Comments »
January 26th, 2008
ptions.ignoreCase); var additionalParms = this.options.requestParameters || []; for( var i=0 ; i < additionalParms.length ; i++ ) callParms.push(additionalParms[i]); The array of parameters to send to the sendRequest() method via apply is popu- lated from a combination of the internal state of the object, things like the ID and the lastRequestString, as well as specific properties of the Options object (for example, count, matchAnywhere, ignoreCase) . However, we also have to provide a mechanism for the user of our component to pass in external request parameters as well . For this, we look for the exist- ence of a requestParameters property on the options object. If it is non-null, it s assumed to be an array of strings of the form key=value. The array is iterated over and added to the callParms already populated with the component-specific parameters. Finally, the request is sent via ajaxEngine.sendRequest.apply( ajaxEngine, callParms ); Whew! Request sending all done. Now let s hope the server is up and running and we get a response. And let s talk about how we will handle it when it does. Refactoring 405 Text suggest handling the Ajax response We went to a lot of trouble to provide a robust request-sending capability, so we d better make sure we properly handle the response or all our hard work will be in vain. Recall that Rico s ajaxEngine routes the request back to the handler object s ajaxUpdate() method, passing the content of the element. So, by implication, we must write an ajaxUpdate() method, and that method will be the entry point into our response handling. The ajaxUpdate() method is shown in listing 10.24 along with its parsing helper methods, createSuggestions() and getElementContent(). Listing 10.24 Ajax response handling ajaxUpdate: function( ajaxResponse ) { Create suggestions this.createSuggestions( ajaxResponse ); if ( this.suggestions.length == 0 ) { this.hideSuggestions(); $( this.id + “_hidden”).value = “”;
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
Posted in Linux | No Comments »
January 26th, 2008
ajaxEngine.sendRequest.apply( ajaxEngine, Send the Ajax request callParms ); }, To understand what this method does, we first need to talk about a JavaScript mechanism we are making use of on the very last line of the method: ajaxEngine.sendRequest.apply( ajaxEngine, callParms ); This uses a method called apply(), which is available to all function objects (see appendix B for more details). Let s illustrate the usage with a simpler example: Greeter.prototype.greetPeople = function(str1, str2) { alert( hello + str1 + and + str2) }; Suppose we have an instance of Greeter called friendlyPerson, and we want to call the greetPeople() method on that object. But we don t have the parameters 404 CHAPTER 10 Type-ahead suggest in a form that is easy to pass. We actually have an array of people. This is where the apply method comes in handy. We can write the code as var people = [ “Joe”, “Sally” ]; friendlyPerson.greetPeople.apply( friendlyPerson, people ); The apply() method converts the array passed in as the second argument to first- class method parameters and invokes the method on the object passed in as the first parameter. The previous code is equivalent to friendlyPerson.greetPeople( people[0], people[1] ); Now back to the task at hand. We have to call ajaxEngine s sendRequest() method, which takes as its first parameter the logical name of the request, and a variable number of string parameters of the form key=value represent- ing the request parameters. Therein lies the rub. We have request parameters from different sources, and we don t know how many we have. Let s look at the code again: var callParms = []; callParms.push( this.id + _request ); … callParms.push( ignore_case= + this.o
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.
Posted in Linux | No Comments »
January 25th, 2008
e to enforce independently of that being done. So, let s assume that some piece of code yet to be written will decide that it needs to send a request to get some suggestions. Let s call it sendRequestFor- Suggestions() and implement it as follows: sendRequestForSuggestions: function() { if ( this.handlingRequest ) { this.pendingRequest = true; return; } this.handlingRequest = true; this.callRicoAjaxEngine(); }, Refactoring 403 All this code does is to conditionally call this.callRicoAjaxEngine() if a request is not still being processed. This simple mechanism turns an internal boolean property, this.handlingRequest, to true as soon as an Ajax request is made and back to false (shown later) once the request has been handled. This is a very sim- ple mechanism to use to throttle the sending of events based on the speed of the server. The boolean property this.pendingRequest is set to true if the method is called while a request is currently being processed. This state will let the handler know that it may have to send another request once the one being processed is finished. Now let s peek under the hood and look at the callRicoAjaxEngine() method shown in listing 10.23. Listing 10.23 Using the Rico ajaxEngine callRicoAjaxEngine: function() { var callParms = []; callParms.push( this.id + _request ); Build the callParms.push( id= + this.id); parameter callParms.push( count= + this.options.count); array callParms.push( query= + this.lastRequestString); callParms.push( match_anywhere= + this.options.matchAnywhere); callParms.push( ignore_case= + this.options.ignoreCase); var additionalParms = this.options.requestParameters || []; for( var i=0 ; i < additionalParms.length ; i++ ) callParms.push(additionalParms[i]);
Check Tomcat Web Hosting services for best quality webspace to host your web application.
Posted in Linux | No Comments »