source: trunk/grails-app/taglib/dbnp/studycapturing/WizardTagLib.groovy @ 88

Last change on this file since 88 was 88, checked in by duh, 9 years ago
  • refactored the wizard code a bit
  • Property svn:keywords set to Rev Author Date
File size: 3.0 KB
Line 
1package dbnp.studycapturing
2import org.codehaus.groovy.grails.plugins.web.taglib.JavascriptTagLib
3
4/**
5 * Wizard tag library
6 *
7 * @author  Jeroen Wesbeek
8 * @since   20100113
9 * @package wizard
10 *
11 * Revision information:
12 * $Rev: 88 $
13 * $Author: duh $
14 * $Date: 2010-01-14 14:00:05 +0000 (do, 14 jan 2010) $
15 */
16class WizardTagLib extends JavascriptTagLib {
17  // define the tag namespace (e.g.: <wizard:action ... />
18  static namespace = "wizard"
19
20  // define the AJAX provider to use
21  static ajaxProvider = "jquery"
22 
23  /**
24   * ajaxButton tag, this is a modified version of the default
25   * grails submitToRemote tag to work with grails webflows.
26   * Usage is identical to submitToRemote with the only exception
27   * that a 'name' form element attribute is required. E.g.
28   * <wizard:ajaxButton name="myAction" value="myButton ... />
29   *
30   * @see   http://www.grails.org/WebFlow
31   * @see   http://www.grails.org/Tag+-+submitToRemote
32   * @todo  perhaps some methods should be moved to a more generic
33   *        'webflow' taglib
34   * @param map     attributes
35   * @param string  body
36   */
37  def ajaxButton = { attrs, body ->
38    // fetch the element name from the attributes
39    def elementName = attrs['name'].replaceAll(/ /,"_")
40   
41    // generate a normal submitToRemote button
42    def button = submitToRemote(attrs,body)
43
44    /**
45     * as of now (grails 1.2.0 and jQuery 1.3.2.4) the grails webflow does
46     * not properly work with AJAX as the submitToRemote button does not
47     * handle and submit the form properly. In order to support webflows
48     * this method modifies two parts of a 'normal' submitToRemote button:
49     *
50     * 1) replace 'this' with 'this.form' as the 'this' selector in a button
51     *    action refers to the button and / or the action upon that button.
52     *    However, it should point to the form the button is part of as the
53     *    the button should submit the form data.
54     * 2) prepend the button name to the serialized data. The default behaviour
55     *    of submitToRemote is to remove the element name altogether, while
56     *    the grails webflow expects a parameter _eventId_BUTTONNAME to execute
57     *    the appropriate webflow action. Hence, we are going to prepend the
58     *    serialized formdata with an _eventId_BUTTONNAME parameter.
59     */
60    button = button.replaceFirst(/data\:jQuery\(this\)\.serialize\(\)/, "data:\'_eventId_${elementName}=1&\'+jQuery(this.form).serialize()")
61
62    // render button
63    out << button
64  }
65
66  /**
67   * wizard navigation buttons render wrapper, in order to be able to add
68   * functionality in the future
69   */
70  def previousNext = { attrs ->
71    // define AJAX provider
72    setProvider([library:ajaxProvider])
73
74    // render navigation buttons
75    out << render(template:"/wizard/common/buttons")
76  }
77
78  def pageContent = { attrs, body ->
79    // define AJAX provider
80    setProvider([library:ajaxProvider])
81
82    // render new body content
83    out << render(template:"/wizard/common/tabs")
84    out << '<div class="content">'
85    out << body()
86    out << '</div>'
87    out << render(template:"/wizard/common/navigation")
88  }
89}
Note: See TracBrowser for help on using the repository browser.