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

Last change on this file since 1802 was 1802, checked in by robert@…, 12 years ago

Solved styling issues with required contacts in simplewizard, introduced in r1775.

  • Property svn:keywords set to Rev Author Date
File size: 11.3 KB
Line 
1package dbnp.studycapturing
2
3import dbnp.authentication.SecUser
4import org.dbnp.gdt.*
5
6/**
7 * Wizard tag library
8 *
9 * @author Jeroen Wesbeek
10 * @since 20100113
11 * @package wizard
12 *
13 * Revision information:
14 * $Rev: 1802 $
15 * $Author: robert@isdat.nl $
16 * $Date: 2011-05-03 12:33:45 +0000 (di, 03 mei 2011) $
17 */
18class WizardTagLib extends GdtTagLib {
19        def authenticationService
20       
21        /**
22         * Study form element
23         * @param Map attributes
24         * @param Closure help content
25         */
26        def studyElement = { attrs, body ->
27                // render study element
28                baseElement.call(
29                        'studySelect',
30                        attrs,
31                        body
32                )
33        }
34
35        /**
36         * render a study select element
37         * @param Map attrs
38         */
39        def studySelect = { attrs ->
40                // Find all studies the user has access to (max 100)
41                attrs.from = Study.giveWritableStudies(authenticationService.getLoggedInUser(), 100);
42
43                // got a name?
44                if (!attrs.name) {
45                        attrs.name = "study"
46                }
47
48                // got result?
49                if (attrs.from.size() > 0) {
50                        out << select(attrs)
51                } else {
52                        // no, return false to make sure this element
53                        // is not rendered in the template
54                        return false
55                }
56        }
57
58        /**
59         * Protocol form element
60         * @param Map attributes
61         * @param Closure help content
62         *
63        def protocolElement = { attrs, body ->
64                // render protocol element
65                baseElement.call(
66                        'protocolSelect',
67                        attrs,
68                        body
69                )
70        }
71
72        /**
73         * render a protocol select element
74         * @param Map attrs
75         *
76        def protocolSelect = { attrs ->
77                // fetch all protocold
78                attrs.from = Protocol.findAll() // for now, all protocols
79
80                // got a name?
81                if (!attrs.name) {
82                        attrs.name = 'protocol'
83                }
84
85                out << select(attrs)
86        }
87
88        def show = { attrs ->
89                // is object parameter set?
90                def o = attrs.object
91
92                println o.getProperties();
93                o.getProperties().each {
94                        println it
95                }
96
97                out << "!! test version of 'show' tag !!"
98        }
99         */
100
101        def PublicationSelectElement = { attrs, body ->
102                attrs.description = 'Publications';
103                // render list with publications currently available
104                baseElement.call(
105                        '_publicationList',
106                        attrs,
107                        body
108                )
109        }
110
111        /**
112         * Renders a input box for publications
113         */
114        def publicationSelect = { attrs, body ->
115                if (attrs.get('value') == null) {
116                        attrs.value = [];
117                }
118                if (attrs.get('description') == null) {
119                        attrs.description = '';
120                }
121                out << '<form id="' + attrs.name + '_form" onSubmit="return false;">';
122                out << textField(
123                        name: attrs.get("name"),
124                        value: '',
125                        rel: 'publication-pubmed',
126                        style: 'width: 400px;'
127                );
128                out << '</form>';
129                out << '<script type="text/javascript">';
130                out << '  var onSelect = function( chooserObject, inputElement, event, ui ) { selectPubMedAdd( chooserObject, inputElement, event, ui ); enableButton( ".' + attrs.name + '_publication_dialog", "Add", true ); };'
131                out << '  iField = $( "#' + attrs.get('name') + '" );';
132                out << '  new PublicationChooser().initAutocomplete( iField, { "select" : onSelect } );';
133                out << '</script>';
134        }
135
136        def _publicationList = { attrs, body ->
137                def display_none = 'none';
138                if (!attrs.get('value') || attrs.get('value').size() == 0) {
139                        display_none = 'inline';
140                }
141
142                // Add a unordered list
143                out << '<ul class="publication_list" id="' + attrs.name + '_list">';
144
145                out << '<li class="empty" id="' + attrs.name + '_none" >';
146                out << '<span class="publication_none" style="display: ' + display_none + ';">';
147                out << 'No publications selected';
148                out << '</span>';
149                out << '</li>';
150
151                out << '</ul>';
152
153                // Add the publications using javascript
154                out << '<script type="text/javascript">'
155                if (attrs.get('value') && attrs.get('value').size() > 0) {
156                        def i = 0;
157                        attrs.get('value').each {
158                                out << 'showPublication( ';
159                                out << '  "' + attrs.name + '",';
160                                out << '  ' + it.id + ',';
161                                out << '  "' + it.title + '",';
162                                out << '  "' + it.authorsList + '",';
163                                out << '  ' + i++;
164                                out << ');';
165                        }
166                }
167                out << '</script>';
168
169                def ids;
170                if (attrs.get('value') && attrs.get('value').size() > 0) {
171                        ids = attrs.get('value').id.join(',')
172                } else {
173                        ids = '';
174                }
175                out << '<input type="hidden" name="' + attrs.name + '_ids" value="' + ids + '" id="' + attrs.name + '_ids">';
176               
177                out << _publicationAddButton( attrs, body );
178        }
179
180        def _publicationAddButton = { attrs, body ->
181               
182                if( attrs.get( 'noForm', false ) ) {
183                        // Only show the add button. The dialog that is created with this method otherwise,
184                        // should be created somewhere outside the form.
185                } else {
186                        out << publicationDialog( attrs, body );
187                }
188       
189                out << '<input class="addButton" type="button" onClick="openPublicationDialog(\'' + attrs.name + '\' );" value="Add Publication">';
190        }
191       
192        // Show the add publications dialog
193        def publicationDialog = { attrs, body ->
194                // Output the dialog for the publications
195                out << '<div id="' + attrs.name + '_dialog">';
196                out << '<p>Search for a publication on pubmed. You can search on a part of the title, authors or pubmed ID. </p>';
197                out << publicationSelect(attrs, body);
198                out << '</div>';
199                out << '<script type="text/javascript">';
200                out << '  createPublicationDialog( "' + attrs.name + '" );'
201                out << '</script>';
202        }
203       
204
205        def ContactSelectElement = { attrs, body ->
206                attrs.description = 'Contacts';
207
208                // render list with publications currently available
209                attrs['required']='true'
210                baseElement.call(
211                        '_contactList',
212                        attrs,
213                        body
214                )
215                attrs.required          = false
216                attrs.description       = '';
217
218                // render 'constacts list'
219                out << '<div id="' + attrs.name + '_dialog" class="contacts_dialog" style="display: none;">'
220                baseElement.call(
221                        '_personSelect',
222                        attrs,
223                        body
224                )
225                baseElement.call(
226                        '_roleSelect',
227                        attrs,
228                        body
229                )
230                baseElement.call(
231                        '_contactAddButtonAddition',
232                        attrs,
233                        body
234                )
235                out << '</div>';
236
237                // render 'Add contact button'
238                baseElement.call(
239                        '_contactAddDialogButton',
240                        attrs,
241                        body
242                )
243        }
244
245        def _contactList = { attrs, body ->
246                def display_none = 'none';
247                if (!attrs.get('value') || attrs.get('value').size() == 0) {
248                        display_none = 'inline';
249                }
250
251                // Add a unordered list
252                out << '<ul class="contact_list" id="' + attrs.name + '_list">';
253
254                out << '<li class="empty" id="' + attrs.name + '_none">';
255                out << '<span class="contacts_none" style="display: ' + display_none + ';">';
256                out << 'No contacts selected';
257                out << '</span>';
258                out << '</li>';
259
260                out << '</ul>';
261
262                // Add the contacts using javascript
263                out << '<script type="text/javascript">'
264                if (attrs.get('value') && attrs.get('value').size() > 0) {
265                        def i = 0;
266                        attrs.get('value').each {
267                                out << 'showContact( ';
268                                out << '  "' + attrs.name + '",';
269                                out << '  "' + it.person.id + '-' + it.role.id + '",';
270                                out << '  "' + it.person.lastName + ', ' + it.person.firstName + (it.person.prefix ? ' ' + it.person.prefix : '') + '",';
271                                out << '  "' + it.role.name + '",';
272                                out << '  ' + i++;
273                                out << ');';
274                        }
275                }
276                out << '</script>';
277
278                def ids = '';
279                if (attrs.get('value') && attrs.get('value').size() > 0) {
280                        ids = attrs.get('value').collect { it.person.id + '-' + it.role.id }
281                        ids = ids.join(',');
282                }
283                out << '<input type="hidden" name="' + attrs.name + '_ids" value="' + ids + '" id="' + attrs.name + '_ids">';
284        }
285
286        def _contactAddSelect = { attrs, body ->
287                out << _personSelect(attrs) + _roleSelect(attrs);
288        }
289
290        def _contactAddButtonAddition = { attrs, body ->
291                out << '<input type="button" onClick="if( addContact ( \'' + attrs.name + '\' ) ) { $(\'#' + attrs.name + '_dialog\').hide(); $( \'#' + attrs.name + '_dialogButton\' ).show(); }" value="Add">';
292                out << '<input type="button" onClick="$(\'#' + attrs.name + '_dialog\').hide(); $( \'#' + attrs.name + '_dialogButton\' ).show();" value="Close">';
293        }
294
295        def _contactAddDialogButton = { attrs, body ->
296                out << '<input type="button" onClick="$( \'#' + attrs.name + '_dialog\' ).show(); $(this).hide();" id="' + attrs.name + '_dialogButton" value="Add Contact">';
297        }
298        /**
299         * Person select element
300         * @param Map attributes
301         */
302        def _personSelect = { attrs ->
303                def selectAttrs = new LinkedHashMap();
304
305                // define 'from'
306                def persons = Person.findAll().sort({ a, b -> a.lastName == b.lastName ? (a.firstName <=> b.firstName) : (a.lastName <=> b.lastName) } as Comparator);
307                selectAttrs.from = persons.collect { it.lastName + ', ' + it.firstName + (it.prefix ? ' ' + it.prefix : '') }
308                selectAttrs.keys = persons.id;
309
310                // add 'rel' attribute
311                selectAttrs.rel = 'person'
312                selectAttrs.name = attrs.name + '_person';
313
314                // add a dummy field
315                selectAttrs.from.add(0,'')
316                selectAttrs.keys.add(0,'')
317
318                out << "Person: " + select(selectAttrs)
319        }
320
321        /**
322         * Role select element
323         * @param Map attributes
324         */
325        def _roleSelect = { attrs ->
326                def selectAttrs = new LinkedHashMap();
327
328                // define 'from'
329                def roles = PersonRole.findAll();
330                selectAttrs.from = roles.collect { it.name };
331                selectAttrs.keys = roles.id;
332
333                // add 'rel' attribute
334                selectAttrs.rel = 'role'
335                selectAttrs.name = attrs.name + '_role';
336
337                // add a dummy field
338                selectAttrs.from.add(0,'')
339                selectAttrs.keys.add(0,'')
340
341                out << "Role: " + select(selectAttrs)
342        }
343
344
345        def UserSelectElement = { attrs, body ->
346                // render list with publications currently available
347                baseElement.call(
348                        '_userList',
349                        attrs,
350                        body
351                )
352        }
353
354        /**
355         * Renders an input box for publications
356         */
357        def userSelect = { attrs, body ->
358                if (attrs.get('value') == null) {
359                        attrs.value = [];
360                }
361                if (attrs.get('description') == null) {
362                        attrs.description = '';
363                }
364               
365                out << '<form id="' + attrs.name + '_form" onSubmit="return false;">';
366                out << select(
367                        name: attrs.get("name"),
368                        value: '',
369                        from: SecUser.list(),
370                        optionValue: 'username',
371                        optionKey: 'id',
372                        style: 'width: 400px;'
373                );
374                out << '</form>';
375        }
376
377        def _userList = { attrs, body ->
378                def display_none = 'none';
379                if (!attrs.get('value') || attrs.get('value').size() == 0) {
380                        display_none = 'inline';
381                }
382
383                // Add a unordered list
384                out << '<ul class="user_list" id="' + attrs.name + '_list">';
385
386                out << '<li>';
387                out << '<span class="user_none" id="' + attrs.name + '_none" style="display: ' + display_none + ';">';
388                out << '-';
389                out << '</span>';
390                out << '</li>';
391
392                out << '</ul>';
393
394                // Add the publications using javascript
395                out << '<script type="text/javascript">'
396                if (attrs.get('value') && attrs.get('value').size() > 0) {
397                        def i = 0;
398                        attrs.get('value').each {
399                                out << 'showUser( ';
400                                out << '  "' + attrs.name + '",';
401                                out << '  ' + it.id + ',';
402                                out << '  "' + it.username + '",';
403                                out << '  ' + i++;
404                                out << ');';
405                        }
406                }
407                out << '</script>';
408
409                def ids;
410                if (attrs.get('value') && attrs.get('value').size() > 0) {
411                        ids = attrs.get('value').id.join(',')
412                } else {
413                        ids = '';
414                }
415                out << '<input type="hidden" name="' + attrs.name + '_ids" value="' + ids + '" id="' + attrs.name + '_ids">';
416               
417                out << _userAddButton( attrs, body );
418        }
419
420        def _userAddButton = { attrs, body ->
421                if( attrs.get( 'noForm', false ) ) {
422                        // Only show the add button. The dialog that is created with this method otherwise,
423                        // should be created somewhere outside the form.
424                } else {
425                        out << userDialog( attrs, body );
426                }
427
428                out << '<input class="addButton" type="button" onClick="openUserDialog(\'' + attrs.name + '\' );" value="Add User">';
429        }
430       
431        def userDialog = { attrs, body ->
432                // Output the dialog for the publications
433                out << '<div id="' + attrs.name + '_dialog">';
434                out << '<p>Select a user from the database.</p>';
435                out << userSelect(attrs, body);
436                out << '</div>';
437                out << '<script type="text/javascript">';
438                out << '  createUserDialog( "' + attrs.name + '" );'
439                out << '</script>';
440        }
441
442}
Note: See TracBrowser for help on using the repository browser.