1 | package dbnp.studycapturing |
---|
2 | import 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$ |
---|
13 | * $Author$ |
---|
14 | * $Date$ |
---|
15 | */ |
---|
16 | class WizardTagLib extends JavascriptTagLib { |
---|
17 | // define the tag namespace (e.g.: <wizard:action ... /> |
---|
18 | static namespace = "wizard" |
---|
19 | |
---|
20 | /** |
---|
21 | * ajaxButton tag, this is a modified version of the default |
---|
22 | * grails submitToRemote tag to work with grails webflows. |
---|
23 | * Usage is identical to submitToRemote with the only exception |
---|
24 | * that a 'name' form element attribute is required. E.g. |
---|
25 | * <wizard:ajaxButton name="myAction" value="myButton ... /> |
---|
26 | * |
---|
27 | * @see http://www.grails.org/WebFlow |
---|
28 | * @see http://www.grails.org/Tag+-+submitToRemote |
---|
29 | * @todo perhaps some methods should be moved to a more generic |
---|
30 | * 'webflow' taglib |
---|
31 | * @param map attributes |
---|
32 | * @param string body |
---|
33 | */ |
---|
34 | def ajaxButton = { attrs, body -> |
---|
35 | // fetch the element name from the attributes |
---|
36 | def elementName = attrs['name'].replaceAll(/ /,"_") |
---|
37 | |
---|
38 | // generate a normal submitToRemote button |
---|
39 | def button = submitToRemote(attrs,body) |
---|
40 | |
---|
41 | /** |
---|
42 | * as of now (grails 1.2.0 and jQuery 1.3.2.4) the grails webflow does |
---|
43 | * not properly work with AJAX as the submitToRemote button does not |
---|
44 | * handle and submit the form properly. In order to support webflows |
---|
45 | * this method modifies two parts of a 'normal' submitToRemote button: |
---|
46 | * |
---|
47 | * 1) replace 'this' with 'this.form' as the 'this' selector in a button |
---|
48 | * action refers to the button and / or the action upon that button. |
---|
49 | * However, it should point to the form the button is part of as the |
---|
50 | * the button should submit the form data. |
---|
51 | * 2) prepend the button name to the serialized data. The default behaviour |
---|
52 | * of submitToRemote is to remove the element name altogether, while |
---|
53 | * the grails webflow expects a parameter _eventId_BUTTONNAME to execute |
---|
54 | * the appropriate webflow action. Hence, we are going to prepend the |
---|
55 | * serialized formdata with an _eventId_BUTTONNAME parameter. |
---|
56 | */ |
---|
57 | button = button.replaceFirst(/data\:jQuery\(this\)\.serialize\(\)/, "data:\'_eventId_${elementName}=1&\'+jQuery(this.form).serialize()") |
---|
58 | |
---|
59 | // render button |
---|
60 | out << button |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * render the wizard navigation button |
---|
65 | * @param map attrs (supports: previous="true/false" and next="true/false" |
---|
66 | */ |
---|
67 | def previousNext = { attrs -> |
---|
68 | def buttons = new LinkedHashMap() |
---|
69 | buttons.previous = (attrs.get('previous') == null || (attrs.get('previous') instanceof String && attrs.get('previous') == "true")) ? true : false |
---|
70 | buttons.next = (attrs.get('next') == null || (attrs.get('next') instanceof String && attrs.get('next') == "true")) ? true : false |
---|
71 | |
---|
72 | out << render(template:"/wizard/common/buttons", model:[button:buttons]) |
---|
73 | } |
---|
74 | } |
---|