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

Last change on this file since 1590 was 1590, checked in by work@…, 12 years ago
  • fixing remaining test issues
  • Property svn:keywords set to Rev Author Date
File size: 11.2 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: 1590 $
15 * $Author: work@osx.eu $
16 * $Date: 2011-03-04 15:28:45 +0000 (vr, 04 mrt 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>';
146                out << '<span class="publication_none" id="' + attrs.name + '_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
207                attrs.description = 'Contacts';
208                // render list with publications currently available
209                baseElement.call(
210                        '_contactList',
211                        attrs,
212                        body
213                )
214
215                attrs.description = '';
216
217                // render 'publications list'
218                out << '<div id="' + attrs.name + '_dialog" class="contacts_dialog" style="display: none;">'
219                baseElement.call(
220                        '_personSelect',
221                        attrs,
222                        body
223                )
224                baseElement.call(
225                        '_roleSelect',
226                        attrs,
227                        body
228                )
229                baseElement.call(
230                        '_contactAddButtonAddition',
231                        attrs,
232                        body
233                )
234                out << '</div>';
235
236                // render 'Add contact button'
237                baseElement.call(
238                        '_contactAddDialogButton',
239                        attrs,
240                        body
241                )
242        }
243
244        def _contactList = { attrs, body ->
245                def display_none = 'none';
246                if (!attrs.get('value') || attrs.get('value').size() == 0) {
247                        display_none = 'inline';
248                }
249
250                // Add a unordered list
251                out << '<ul class="contact_list" id="' + attrs.name + '_list">';
252
253                out << '<li>';
254                out << '<span class="contacts_none" id="' + attrs.name + '_none" style="display: ' + display_none + ';">';
255                out << 'No contacts selected';
256                out << '</span>';
257                out << '</li>';
258
259                out << '</ul>';
260
261                // Add the contacts using javascript
262                out << '<script type="text/javascript">'
263                if (attrs.get('value') && attrs.get('value').size() > 0) {
264                        def i = 0;
265                        attrs.get('value').each {
266                                out << 'showContact( ';
267                                out << '  "' + attrs.name + '",';
268                                out << '  "' + it.person.id + '-' + it.role.id + '",';
269                                out << '  "' + it.person.lastName + ', ' + it.person.firstName + (it.person.prefix ? ' ' + it.person.prefix : '') + '",';
270                                out << '  "' + it.role.name + '",';
271                                out << '  ' + i++;
272                                out << ');';
273                        }
274                }
275                out << '</script>';
276
277                def ids = '';
278                if (attrs.get('value') && attrs.get('value').size() > 0) {
279                        ids = attrs.get('value').collect { it.person.id + '-' + it.role.id }
280                        ids = ids.join(',');
281                }
282                out << '<input type="hidden" name="' + attrs.name + '_ids" value="' + ids + '" id="' + attrs.name + '_ids">';
283        }
284
285        def _contactAddSelect = { attrs, body ->
286                out << _personSelect(attrs) + _roleSelect(attrs);
287        }
288
289        def _contactAddButtonAddition = { attrs, body ->
290                out << '<input type="button" onClick="if( addContact ( \'' + attrs.name + '\' ) ) { $(\'#' + attrs.name + '_dialog\').hide(); $( \'#' + attrs.name + '_dialogButton\' ).show(); }" value="Add">';
291                out << '<input type="button" onClick="$(\'#' + attrs.name + '_dialog\').hide(); $( \'#' + attrs.name + '_dialogButton\' ).show();" value="Close">';
292        }
293
294        def _contactAddDialogButton = { attrs, body ->
295                out << '<input type="button" onClick="$( \'#' + attrs.name + '_dialog\' ).show(); $(this).hide();" id="' + attrs.name + '_dialogButton" value="Add Contact">';
296        }
297        /**
298         * Person select element
299         * @param Map attributes
300         */
301        def _personSelect = { attrs ->
302                def selectAttrs = new LinkedHashMap();
303
304                // define 'from'
305                def persons = Person.findAll().sort({ a, b -> a.lastName == b.lastName ? (a.firstName <=> b.firstName) : (a.lastName <=> b.lastName) } as Comparator);
306                selectAttrs.from = persons.collect { it.lastName + ', ' + it.firstName + (it.prefix ? ' ' + it.prefix : '') }
307                selectAttrs.keys = persons.id;
308
309                // add 'rel' attribute
310                selectAttrs.rel = 'person'
311                selectAttrs.name = attrs.name + '_person';
312
313                // add a dummy field
314                selectAttrs.from.add(0,'')
315                selectAttrs.keys.add(0,'')
316
317                out << "Person: " + select(selectAttrs)
318        }
319
320        /**
321         * Role select element
322         * @param Map attributes
323         */
324        def _roleSelect = { attrs ->
325                def selectAttrs = new LinkedHashMap();
326
327                // define 'from'
328                def roles = PersonRole.findAll();
329                selectAttrs.from = roles.collect { it.name };
330                selectAttrs.keys = roles.id;
331
332                // add 'rel' attribute
333                selectAttrs.rel = 'role'
334                selectAttrs.name = attrs.name + '_role';
335
336                // add a dummy field
337                selectAttrs.from.add(0,'')
338                selectAttrs.keys.add(0,'')
339
340                out << "Role: " + select(selectAttrs)
341        }
342
343
344        def UserSelectElement = { attrs, body ->
345                // render list with publications currently available
346                baseElement.call(
347                        '_userList',
348                        attrs,
349                        body
350                )
351        }
352
353        /**
354         * Renders an input box for publications
355         */
356        def userSelect = { attrs, body ->
357                if (attrs.get('value') == null) {
358                        attrs.value = [];
359                }
360                if (attrs.get('description') == null) {
361                        attrs.description = '';
362                }
363               
364                out << '<form id="' + attrs.name + '_form" onSubmit="return false;">';
365                out << select(
366                        name: attrs.get("name"),
367                        value: '',
368                        from: SecUser.list(),
369                        optionValue: 'username',
370                        optionKey: 'id',
371                        style: 'width: 400px;'
372                );
373                out << '</form>';
374        }
375
376        def _userList = { attrs, body ->
377                def display_none = 'none';
378                if (!attrs.get('value') || attrs.get('value').size() == 0) {
379                        display_none = 'inline';
380                }
381
382                // Add a unordered list
383                out << '<ul class="user_list" id="' + attrs.name + '_list">';
384
385                out << '<li>';
386                out << '<span class="user_none" id="' + attrs.name + '_none" style="display: ' + display_none + ';">';
387                out << '-';
388                out << '</span>';
389                out << '</li>';
390
391                out << '</ul>';
392
393                // Add the publications using javascript
394                out << '<script type="text/javascript">'
395                if (attrs.get('value') && attrs.get('value').size() > 0) {
396                        def i = 0;
397                        attrs.get('value').each {
398                                out << 'showUser( ';
399                                out << '  "' + attrs.name + '",';
400                                out << '  ' + it.id + ',';
401                                out << '  "' + it.username + '",';
402                                out << '  ' + i++;
403                                out << ');';
404                        }
405                }
406                out << '</script>';
407
408                def ids;
409                if (attrs.get('value') && attrs.get('value').size() > 0) {
410                        ids = attrs.get('value').id.join(',')
411                } else {
412                        ids = '';
413                }
414                out << '<input type="hidden" name="' + attrs.name + '_ids" value="' + ids + '" id="' + attrs.name + '_ids">';
415               
416                out << _userAddButton( attrs, body );
417        }
418
419        def _userAddButton = { attrs, body ->
420                if( attrs.get( 'noForm', false ) ) {
421                        // Only show the add button. The dialog that is created with this method otherwise,
422                        // should be created somewhere outside the form.
423                } else {
424                        out << userDialog( attrs, body );
425                }
426
427                out << '<input class="addButton" type="button" onClick="openUserDialog(\'' + attrs.name + '\' );" value="Add User">';
428        }
429       
430        def userDialog = { attrs, body ->
431                // Output the dialog for the publications
432                out << '<div id="' + attrs.name + '_dialog">';
433                out << '<p>Select a user from the database.</p>';
434                out << userSelect(attrs, body);
435                out << '</div>';
436                out << '<script type="text/javascript">';
437                out << '  createUserDialog( "' + attrs.name + '" );'
438                out << '</script>';
439        }
440
441}
Note: See TracBrowser for help on using the repository browser.