Scriptaculous- Ajax

Description

Java,J2EE,Struts,Hibernate,JSF,Goolge web development toolkit(GWT),Spring,Dojo,Html,Xhtml

Reviews
Shared by: M Sampath kumar
Categories
Stats
views:
101
rating:
not rated
reviews:
0
posted:
10/8/2009
language:
English
pages:
0
© 2008 Marty Hall The Script.aculo.us JavaScript Library Part I: j p Ajax-Specific Features Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/ajax.html p j Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Topics in This Section p • Overview of Scriptaculous • Installation and documentation • Autocomplete textfields – Local version – Ajax version • In place Editor In-place – Free-text values – Values from combo box 4 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Introduction I t d ti Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Overview • Foundation – B il on top of Prototype Built fP • Ajax-specific features – Autocompleting textfields • Textfields with dropdown list of matching choices • Covered in first half of this section – In-place editors • Clickable text that you can edit and send to server • Covered in second half of this section • General features – Visual effects (fade in, fade out, highlighting) • Covered in next section – Drag and drop • Covered in later section 6 Java EE training: http://courses.coreservlets.com Downloading and Installation g • Download – http://script.aculo.us/downloads • Unzip and grab .js files out of “src” folder – Usually put in subdirectory of scripts since there are many files • This tutorial corresponds to Scriptaculous 1.8.1 • Online documentation – http://wiki.script.aculo.us/ • Online forum – htt // http://groups.google.com/group/rubyonrails-spinoffs l / / b il i ff • Prerequisite – Scriptaculous requires Prototype. I am using 1.6. Prototype 16 7 • So your HTML page needs to load both libraries • See separate lectures on Prototype Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Autocompleter.Local A t l t L l Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Autocompleter.Local p • Idea – Specify JavaScript array that has choices for textfield. Dropdown textfield box shown with choices that start with what has been typed so far • http://wiki.script.aculo.us/scriptaculous/show/Autocompletion • Steps – Make an instance of Autocompleter.Local • new Autocompleter.Local(textFieldID, divID, array) – Enable it when page loads • window onload = function() { new Autocompleter Local( ); window.onload Autocompleter.Local(...); }; • Prototype gurus might use document.observe("dom:loaded", ...) instead of window.onload window onload – Make an empty div with a designated CSS name •
– Attach style sheet that makes div absolutely p y y positioned, p border , puts on div, turns off bullets for ul lists, and makes a different background color for li.selected Java EE training: http://courses.coreservlets.com 9 Autocompleter.Local Example: JavaScript var langString = "Java,C,C++,PHP,Visual Basic,..."; var langArray = languageString split(","); languageString.split( ); window.onload = function() { new Autocompleter.Local("langField1", "langMenu1", Autocompleter.Local( langField1 , langMenu1 , langArray); }; function googleSearch(id) { var language = getValue(id); window.location.href = "http://www.google.com/search?q=" + language; } function getValue(id) { return(escape(document.getElementById(id).value)); } Java EE training: http://courses.coreservlets.com 10 Autocompleter.Local Example: HTML Header p // g/ / Scriptaculous and Autocomplete sc pt src= If you know you will only use certain parts of scriptaculous library you can save library, download time by loading only some of it. To load all of it, just omit the "load=" part. I.e., . Autocomplete is in controls, but relies on effects internally. 11 Java EE training: http://courses.coreservlets.com Autocompleter.Local Example: Main HTML ...
Autocomplete.Local
/fi ld t ... 12 Java EE training: http://courses.coreservlets.com Autocompleter.Local Example: CSS .autocomplete { position: absolute; color: #333333; background-color: #ffffff; border: 1px solid #666666; font-family: Arial, sans-serif; overflow: hidden; } .autocomplete ul { padding: 0; a g : margin: 0; list-style: none; overflow: auto; } 13 Java EE training: http://courses.coreservlets.com Autocompleter.Local Example: CSS (Continued) .autocomplete li { display: block; white-space: nowrap; cursor: pointer; margin: 0px; padding-left: 5px; padding-right: 5px; border: 0px solid #ffffff; } .autocomplete li.selected { bac g ou d co o : #ccee ; background-color: #cceeff; border-top: 1px solid #99bbcc; border-bottom: 1px solid #99bbcc; } 14 Java EE training: http://courses.coreservlets.com Autocompleter.Local Example: Results 15 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Ajax.Autocompleter Aj A t l t Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Ajax.Autocompleter j p • Idea – Specify URL that computes list of choices. Dropdown box shown choices with choices that start with what has been typed so far • http://wiki.script.aculo.us/scriptaculous/show/Autocompletion – Same steps as with Autocompleter.Local except • Specify url instead of array (url should return
    list) • Use class Ajax.Autocompleter instead of Autocompleter.Local • Steps: summary • Steps – Make an instance of Ajax.Autocompleter j p ( ) • new Ajax.Autocompleter(textFieldID, divID, url) – Enable it when page loads • window.onload = function() { new Ajax.Autocompleter(...); }; – Make an empty div with a designated CSS name •
    – Attach same style sheet as with Autocompleter.Local Java EE training: http://courses.coreservlets.com 17 Ajax.Autocompleter Example: JavaScript var langString = "Java,C,C++,PHP,Visual Basic,..."; var langArray = languageString.split(","); languageString split(" "); window.onload = function() { new Autocompleter.Local("langField1", "langMenu1", Autocompleter Local("langField1" "langMenu1" langArray); new Ajax.Autocompleter("langField2", "langMenu2", "languages"); "l ") }; This is the relative URL. By default it will be passed POST URL data of name-of-langField2=value-of-langField2 18 Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: HTML
    Ajax.Autocomplete

    By default name of field is default, parameter name 19 Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: Servlet public class LanguageCompleter extends HttpServlet { private static final String languageString = "Java,C,C++,..."; private static final String[] languageArray = languageString.split(","); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); The name (not id!) of textfield was "langPrefix". String languagePrefix = request.getParameter("langPrefix"); List languages = findLanguages(languagePrefix); request.setAttribute("languages", languages); String outputPage = "/WEB-INF/results/language-list.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(outputPage); dispatcher.include(request, response); } 20 Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: Servlet (Continued) public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } private List findLanguages(String languagePrefix) { languagePrefix = languagePrefix.toUpperCase(); List languages = new ArrayList(); for(String language: languageArray) { if(language.toUpperCase().startsWith(languagePrefix)) { languages.add(language); } } return(languages); } } 21 Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: JSP <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix= c prefix="c" %>
    • ${language}
    22 Return value of URL should be
    • ...
    • ...
    • ...
    Style sheet on client will suppress the bullets. Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: web.xml web xml LanguageCompleter coreservlets.LanguageCompleter LanguageCompleter LanguageCompleter /languages 23 Java EE training: http://courses.coreservlets.com Ajax.Autocompleter Example: Results 24 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Autocompleter Options Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Autocompleter Options p p • Idea – Autocompleter.Local and Ajax.Autocompleter accept an options array as the fourth argument • new Ajax.Autocompleter(fieldID, divID url Ajax Autocompleter(fieldID divID, url, { opt1: ..., opt2: ..., ... }); • Legal properties for both classes – autoSelect (default: false) • Should value automatically be inserted into textfield if there is only one matching choice? y g – frequency (default: 0.4) • Interval in seconds between attempts to autocomplete – minChars (default: 1) i Ch (d f lt • The number of characters before autocompletion kicks in 26 Java EE training: http://courses.coreservlets.com Autocompleter Options (Continued) • Options for Autocompleter.Local – choices (default 10) h i (d f l • Maximum number of entries to display – partialSearch (default true) • Should match be made at beginning of any word? False means to match beginning of first word only. – partialChars (default 2) • Number of characters before partial search kicks in. When number of chars is greater or equal to minChars but less than partialChars, you get a non-partial search (first word). – f llSearch (default false) fullSearch (defa lt • Should match be anywhere? False means to match only at the beginning of each candidate. – selector (default: function that searches array) • Function that does real work of doing match and building ul list. 27 Java EE training: http://courses.coreservlets.com Autocompleter Options (Continued) • Options for Ajax.Autocompleter – i di indicator (default: none) (d f l ) • Id of element that should be shown while waiting for server result, then hidden again – paramName (d f lt name of textfield) N (default: f t tfi ld) • parameter name sent to server (parameter value is textfield value, of course) – parameters (default: empty) t (d f lt t ) • Extra string sent to server (fixed parameters) – tokens (default: empty) • Array of delimeters: each entry outside delimeters is autocompleted separately – afterUpdateElement (default: none) • Function to run when user makes choice. Lets you respond automatically to selections. 28 Java EE training: http://courses.coreservlets.com Autocompleter Options Example: JavaScript var langString = "Java,C,C++,PHP,Visual Basic,..."; var langArray = languageString split(","); languageString.split( ); window.onload = function() { new Autocompleter.Local("langField1", "langMenu1", Autocompleter.Local( langField1 , langMenu1 , langArray); new Ajax.Autocompleter("langField2", "langMenu2", "languages"); languages ); new Ajax.Autocompleter("langField3", "langMenu3", "languages-slow", { indicator: "indicatorRegion", g , paramName: "langPrefix"}); }; 29 Java EE training: http://courses.coreservlets.com Autocompleter Options Example: HTML
    Ajax.Autocomplete with Indicator


    M 3" l " t l t " /di
    30 Java EE training: http://courses.coreservlets.com Autocompleter Options Example: Java public class SlowLanguageCompleter extends LanguageCompleter { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Thread.sleep(2000); } catch(InterruptedException ie) {} super.doGet(request, response); } } 31 Java EE training: http://courses.coreservlets.com Autocompleter Options Example: web.xml web xml SlowLanguageCompleter coreservlets.SlowLanguageCompleter SlowLanguageCompleter SlowLanguageCompleter /languages-slow 32 Java EE training: http://courses.coreservlets.com Autocompleter Options Example: Results 33 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall In-Place Editors I Pl Edit Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. In-Place Editors • Idea – Text that you can click on, edit, and send to server on edit – Text is highlighted when mouse over it – Text passed to server as “value” request parameter • Text shown as “Saving ” while waiting for server Saving... – Value returned from server is new textual value • Might not be exactly what user entered • Basics – new Ajax.InPlaceEditor("id-of-element", "url"); – Click on text to get editable textfield – new Ajax.InPlaceCollectionEditor ("id-of-element", "url" { collection: ["Option1", ... "OptionN"]}); – Click on text to get select box of options – Documentation • http://wiki.script.aculo.us/scriptaculous/show/InPlaceEditing 35 Java EE training: http://courses.coreservlets.com InPlaceEditor Example p • JavaScript window.onload = function() { new Ajax.InPlaceEditor("element-to-edit", "show-value.jsp"); }; • HTML

    Here is Some Text

    • JSP <% try { Thread.sleep(2000); } catch(InterruptedException ie) {} t h(I t t dE ti i ) %>You sent "${param.value}" 36 Java EE training: http://courses.coreservlets.com InPlaceEditor Example: Results p 37 Java EE training: http://courses.coreservlets.com InPlaceCollectionEditor Example • JavaScript window.onload = function() { i d l d f ti () new Ajax.InPlaceEditor("element-to-edit", "show-value.jsp"); new Ajax.InPlaceCollectionEditor ("flyer-status", "show-value.jsp", { collection: ["Silver", "Gold", "Platinum", "Wood", "Hay", "Stubble"]}); }; • HTML

    Frequent Flyer Status: Silver

    • JSP <% try { Thread.sleep(2000); y p ; } catch(InterruptedException ie) {} %>You sent "${param.value}" 38 Java EE training: http://courses.coreservlets.com InPlaceCollectionEditor Example: Results 39 Java EE training: http://courses.coreservlets.com In-Place Editor Options: Basic Options • okButton (default: true) – Should you use "ok" button? Often set to false. • okText (default: "ok") – T of confirmation button Text f fi i b • cancelLink (default: true) – Should you have "cancel" link? cancel • savingText (default "Saving...") – Text to show while waiting for server response • clickToEditText (default: "Click to edit") – Text to show in tooltip when mouse hovers over text p • paramName (default: "value") 40 – Request parameter name Java EE training: http://courses.coreservlets.com In-Place Editor Options: Ajax.Request Ajax Request Options • Idea – You can use ajaxOptions to specify an options object that gets passed to the underlying Ajax.Request object • new Ajax InPlaceEditor("id", "url", { ajaxOptions: { blah }); Ajax.InPlaceEditor( id url • For {blah} above, you can use all of the same options as discussed in the earlier section on the Prototype Ajax libraries • Most important usage: extra parameters – You are editing a first name, but what customer id does that first name go with? • var params = { param1: $("div-id").innerHTML, p param2: $F("textfield-id")}; ( )} • var options = { parameters: params } • new Ajax.InPlaceEditor("id", "url", { ajaxOptions: options }); 41 Java EE training: http://courses.coreservlets.com In-Place Editor Options: Collection Options • collection – Array of choices • loadCollectionURL – URL that returns JavaScript array of choices h J S i f h i • loadCollectionText – Text to show while waiting for server to send array of choices 42 Java EE training: http://courses.coreservlets.com In-Place Editor Options: Styling Options • highlightColor (default: pale yellow) – Color to show when mouse is over and when result first comes back from server • highlightendColor (default: white) – Color to show temporarily when hightlighting finishes • hoverClassName, formClassName, loadingClassName, savingClassName • (defaults: none, "inplaceeditor-form", "inplaceeditorloading", "inplaceeditor-saving") l di " "i l dit i ") – CSS name of elements at different stages • formID – id of form that is created when you click 43 Java EE training: http://courses.coreservlets.com In-Place Editor Options: Callback Options • onEnterHover – Defaults to setting background color • onLeaveHover – D f l to f di out background color Defaults fading b k d l • onEnterEditMode • onFailure – Defaults to showing error message • onComplete – This is often used when you are doing server-side validation of values. You can check a custom header from the server and show error message if needed. • onLeaveEditMode 44 Java EE training: http://courses.coreservlets.com Advanced Example: JavaScript p p window.onload = function() { var options = { okButton: false, clickToEditText: "Click to update your info" }; new Ajax.InPlaceEditor("firstName", "update-firstName", options); new Ajax.InPlaceEditor("lastName", Ajax InPlaceEditor("lastName" "update-lastName", options); new Ajax.InPlaceEditor("email", "update-email", options); new Ajax.InPlaceEditor("flyerNum", "update-flyerNum", " d t fl N " options); }; 45 Java EE training: http://courses.coreservlets.com Advanced Example: Servlet for Initial Page public class ShowTraveler extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control", "no-cache"); response.setHeader( Pragma , no cache ); response.setHeader("Pragma", "no-cache"); HttpSession session = request.getSession(); Traveler traveler = (Traveler)session.getAttribute("traveler"); if (traveler == null) { traveler = new Traveler(); session.setAttribute("traveler", traveler); } String outputPage = i "/WEB-INF/results/show-traveler.jsp"; RequestDispatcher dispatcher = request.getRequestDispatcher(outputPage); dispatcher.include(request, response); } } 46 Java EE training: http://courses.coreservlets.com Advanced Example: JSP for Initial Page ...
      style="font-size: 18px">
    • First name: ${traveler.firstName}
    • Last name: ${traveler.lastName}
    • Email address: ${traveler.email}
    • Frequent Flyer Number: ${traveler.flyerNum}
    ... 47 Java EE training: http://courses.coreservlets.com Advanced Example: Helper Class for Initial Page public class Traveler implements Serializable { p private String firstName = "Joe"; g ; private String lastName = "Traveler"; private String email = "joe@gmail.com"; private String flyerNum = "a1234"; public Traveler(String firstName, String lastName, String email, String flyerNum) { setFirstName(firstName); setLastName(lastName); setEmail(email); setFlyerNum(flyerNum); } public Traveler () {} // Standard getters and setters } 48 Java EE training: http://courses.coreservlets.com Advanced Example: Initial Page: Results 49 Java EE training: http://courses.coreservlets.com Advanced Example: Servlets for Updating • Purpose – These are the servlets specified earlier in the Ajax.InPlaceEditor constructors • update-firstName update-lastName update-email update firstName, update lastName, update email, update-flyerNum • Behavior – They look up the session, get a Traveler object from the session, then store first name, last name, etc. in that object – They also have a delay to simulate use on a loaded network • This lets you see "Saving..." message while waiting for server update 50 Java EE training: http://courses.coreservlets.com Advanced Example: Servlets for Updating (Parent) public abstract class UpdateTraveler extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); HttpSession session = request.getSession(); Traveler traveler = (Traveler)session.getAttribute("traveler"); if (traveler == null) { traveler = new Traveler(); session.setAttribute("traveler", traveler); i tAtt ib t ("t l " t l ) } 51 Java EE training: http://courses.coreservlets.com Advanced Example: Servlets for Updating (Parent, Cont.) (Parent Cont ) String newVal = request.getParameter("value"); if ((newVal != null) && !(newVal.trim().equals(""))) { !(newVal.trim().equals( ))) doUpdate(traveler, newVal); } PrintWriter out = response.getWriter(); try { Thread.sleep(2000); } catch(InterruptedException ie) {} out.print(getResult(traveler)); } public abstract void doUpdate(Traveler traveler, String newVal); St i V l) public abstract String getResult(Traveler traveler); } 52 Java EE training: http://courses.coreservlets.com Advanced Example: Servlets for Updating (First Name) public class UpdateFirstName extends UpdateTraveler { public void doUpdate(Traveler traveler, String newVal) { traveler.setFirstName(newVal); } public String getResult(Traveler traveler) { return(traveler.getFirstName()); } } 53 Java EE training: http://courses.coreservlets.com Advanced Example: Results p 54 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Wrap-up Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Recommended Books • Prototype and script.aculo.us: You Never Knew J K JavaScript C ld Do Thi ! S i Could D This! – By Christophe Porteneuve • Prototype and Scriptaculous in Action – By Dave Crane, Bear Bebeault, Tom Locke 56 Java EE training: http://courses.coreservlets.com Summary y window.onload = function() { new Autocompleter Local("fieldID1", Autocompleter.Local( fieldID1 "divID1", arrayOfChoices); new Ajax Autocompleter("fieldID2" Ajax.Autocompleter("fieldID2", "divID2", "urlThatReturnsList"); new Ajax Autocompleter("fieldID3" Ajax.Autocompleter("fieldID3", "divID3", "urlThatReturnsList" { i dic t indicator: "i d id" "ind-id", paramName: "p-name", more-options...}); }; } 57 Java EE training: http://courses.coreservlets.com Summary ( y (Continued) ) window.onload = function() { new Ajax.InPlaceEditor("id-to-edit-1", "url-to-send-value"); new Ajax.InPlaceCollectionEditor ("id-to-edit-2", "url-to-send-value", { collection: ["Option1",...,"OptionN"]}); }; 58 Java EE training: http://courses.coreservlets.com © 2008 Marty Hall Questions? Q ti ? Customized Java EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.

Related docs
Scriptaculous- Effects
Views: 219  |  Downloads: 0
Ajax- Basics-2
Views: 10  |  Downloads: 2
Ajax In Action _2006_
Views: 96  |  Downloads: 21
Ajax
Views: 63  |  Downloads: 0
Ajax Tags- Advanced
Views: 58  |  Downloads: 2
Wrox - Beginning Ajax _2009_
Views: 101  |  Downloads: 25
EDUCATION
Views: 0  |  Downloads: 0
premium docs
Other docs by M Sampath ...
Money Dollar Cash
Views: 174  |  Downloads: 8
JavaSwing
Views: 47  |  Downloads: 4
JavaCore
Views: 9  |  Downloads: 1
JavaCore Table Of Contents
Views: 2  |  Downloads: 1
JavaAdvanced
Views: 38  |  Downloads: 0
JavaAdvanced Table Of Contents
Views: 1  |  Downloads: 0
J2EE
Views: 43  |  Downloads: 4
JSF
Views: 20  |  Downloads: 2
WebSecurityThreats
Views: 25  |  Downloads: 2
WebApplicationSecurity_speakernoted
Views: 5  |  Downloads: 0
WebApplicationSecurity
Views: 55  |  Downloads: 1
WebApplicationArchitecture_speakernoted
Views: 3  |  Downloads: 2
WebApplicationArchitecture
Views: 36  |  Downloads: 2
WalkThroughCarDemoJSFApp
Views: 5  |  Downloads: 1
tilesAdvancedFeatures
Views: 1  |  Downloads: 1