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

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