|
1 /* Copyright 2009 the Melange authors. |
|
2 * |
|
3 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
4 * you may not use this file except in compliance with the License. |
|
5 * You may obtain a copy of the License at |
|
6 * |
|
7 * http://www.apache.org/licenses/LICENSE-2.0 |
|
8 * |
|
9 * Unless required by applicable law or agreed to in writing, software |
|
10 * distributed under the License is distributed on an "AS IS" BASIS, |
|
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
12 * See the License for the specific language governing permissions and |
|
13 * limitations under the License. |
|
14 */ |
|
15 |
|
16 /* |
|
17 * |
|
18 * @author <a href="mailto:ajaksu@gmail.com">Daniel Diniz</a> |
|
19 * @author <a href="mailto:jamesalexanderlevy@gmail.com">James Levy</a> |
|
20 */ |
|
21 |
|
22 (function ($) { |
|
23 |
|
24 var DEFAULT_LONG_ANSWER_TEXT = 'Write a Custom Prompt For This Question...'; |
|
25 var DEFAULT_SHORT_ANSWER_TEXT = 'Write a Custom Prompt...'; |
|
26 |
|
27 $(function () { |
|
28 /* |
|
29 * == Set Selectors == |
|
30 * |
|
31 */ |
|
32 var widget = $('div#survey_widget'); |
|
33 |
|
34 widget.parents('td.formfieldvalue:first').css({ |
|
35 'float': 'left', |
|
36 'width': 200 |
|
37 }); |
|
38 |
|
39 /* |
|
40 * == Setup for existing surveys == |
|
41 * |
|
42 */ |
|
43 |
|
44 if ($('input#id_title').val() === '' && $('.formfielderror').length < 1) { |
|
45 widget.find('tr').remove(); |
|
46 } |
|
47 |
|
48 widget.find('table:first').show(); |
|
49 |
|
50 /* |
|
51 * Restore survey content html from editPost |
|
52 * if POST fails |
|
53 */ |
|
54 |
|
55 var SURVEY_PREFIX = 'survey__'; |
|
56 var del_el = ["<a class='delete'><img '", |
|
57 "src='/soc/content/images/minus.gif'/></a>"].join(""); |
|
58 var del_li = ["<a class='delete_item' id='del_", |
|
59 "' ><img src='/soc/content/images/minus.gif'/></a> "]; |
|
60 |
|
61 var survey_html = $('form').find("#id_survey_html").attr('value'); |
|
62 |
|
63 function renderHTML() { |
|
64 // render existing survey forms |
|
65 widget.find('td > label').prepend(del_el).end(); |
|
66 |
|
67 $('ol').find('li').each( |
|
68 function () { |
|
69 $(this).prepend(del_li.join($(this).attr('id'))).end(); |
|
70 } |
|
71 ); |
|
72 widget.find('.short_answer').each( |
|
73 function () { |
|
74 $(this).attr('name', SURVEY_PREFIX + $(this).getPosition() + |
|
75 'short_answer__' + $(this).attr('name')); |
|
76 } |
|
77 ); |
|
78 |
|
79 // add index information to choice fields |
|
80 widget.find('[name=create-option-button]').each( |
|
81 function () { |
|
82 $( |
|
83 '#index_for_' + $(this).attr('value') |
|
84 ) |
|
85 .val( |
|
86 $(this).getPosition() |
|
87 ); |
|
88 } |
|
89 ); |
|
90 |
|
91 widget.find('.long_answer').each( |
|
92 function () { |
|
93 $(this).attr('name', SURVEY_PREFIX + $(this).getPosition() + |
|
94 'long_answer__' + $(this).attr('name')) |
|
95 .attr('overflow', 'auto'); |
|
96 // TODO: replace scrollbar with jquery autogrow |
|
97 } |
|
98 ); |
|
99 } |
|
100 |
|
101 if (survey_html && survey_html.length > 1) { |
|
102 widget.html(survey_html); // we don't need to re-render HTML |
|
103 |
|
104 widget.find('.long_answer,input').each( |
|
105 function () { |
|
106 $(this).val($(this).attr('val')); |
|
107 } |
|
108 ); |
|
109 } |
|
110 else { |
|
111 renderHTML(); |
|
112 } |
|
113 |
|
114 var survey = widget.find('tbody:first'); |
|
115 var options = widget.find('#survey_options'); |
|
116 |
|
117 /* |
|
118 * == Handle Enter key on dialogs == |
|
119 */ |
|
120 $('form input, form button, form select').keypress( |
|
121 function (e) { |
|
122 if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) { |
|
123 $(this).parents('.ui-dialog:first').find(":button:first").click(); |
|
124 return false; |
|
125 } |
|
126 } |
|
127 ); |
|
128 |
|
129 /* |
|
130 * == Display survey answers inline == |
|
131 */ |
|
132 $('a.fetch_answers').click( |
|
133 function () { |
|
134 var user = this.id.replace('results_for_', ''); |
|
135 var path = window.location.pathname; |
|
136 path = path.replace('/edit/', '/show/').replace('/results/', '/show/'); |
|
137 |
|
138 // TODO(ajaksu) add Date().getTime() to query arg if needed |
|
139 var query = '?read_only=true&user_results=' + user; |
|
140 var scrollable = ['<div style="overflow-y: auto; ', |
|
141 'margin-bottom: 100px;"></div>'].join(""); |
|
142 $(scrollable).load(path + query + ' #survey_widget').dialog({ |
|
143 title: user, |
|
144 height: 500, |
|
145 width: 700 |
|
146 }); |
|
147 } |
|
148 ); |
|
149 |
|
150 /* |
|
151 * == Initiation == |
|
152 * |
|
153 * Runs on PageLoad and Each Time Field is Added to Survey |
|
154 * |
|
155 */ |
|
156 |
|
157 survey.bind('init', |
|
158 function () { |
|
159 // TODO(jamslevy) unnecessarily redundant |
|
160 // TODO(jamslevy) This should be refactored as a jQuery function that |
|
161 // acts on only a single field and it should be merged with renderHTML |
|
162 // since they have comparable functionality. |
|
163 |
|
164 widget.find('input').each( |
|
165 function () { |
|
166 if (($(this).val().length < 1 || |
|
167 $(this).val() === DEFAULT_SHORT_ANSWER_TEXT) && |
|
168 ($(this).attr('type') !== 'hidden')) { |
|
169 $(this).preserveDefaultText(DEFAULT_SHORT_ANSWER_TEXT); |
|
170 } |
|
171 } |
|
172 ); |
|
173 |
|
174 widget.find('.long_answer, .tooltip_entry').each( |
|
175 function () { |
|
176 if ($(this).val().length < 1 || |
|
177 $(this).val() === DEFAULT_LONG_ANSWER_TEXT) { |
|
178 $(this).preserveDefaultText(DEFAULT_LONG_ANSWER_TEXT); |
|
179 } |
|
180 $(this).growfield(); |
|
181 } |
|
182 ); |
|
183 |
|
184 widget.find('a.delete img').click( |
|
185 function () { |
|
186 // delete a field |
|
187 var this_field = $(this).parents('tr:first'); |
|
188 var deleted_id = $(this_field).find('label').attr('for'); |
|
189 var delete_this = confirm(["Deleting this field will remove all ", |
|
190 "answers submitted for this field. ", |
|
191 "Continue?"].join("")); |
|
192 if (delete_this) { |
|
193 var edit_form = $('#EditForm'); |
|
194 var deleted_field = $('#__deleted__'); |
|
195 if (deleted_field.val()) { |
|
196 deleted_field.val(deleted_field.val() + ',' + |
|
197 deleted_id.replace('id_', '')).end(); |
|
198 } |
|
199 else { |
|
200 var deleted_input = $("<input type='hidden' value='" + |
|
201 deleted_id.replace('id_', '') + "' />"); |
|
202 deleted_input.attr({'id': '__deleted__'}).attr({ |
|
203 'name': '__deleted__' |
|
204 }); |
|
205 edit_form.append(deleted_input); |
|
206 } |
|
207 this_field.next('tr').remove().end() |
|
208 .remove(); |
|
209 } |
|
210 } |
|
211 ); |
|
212 |
|
213 // Add list/choice-field item to survey |
|
214 $('[name=create-option-button]').each( |
|
215 function () { |
|
216 $(this).click( |
|
217 function () { |
|
218 var new_option_val = $('#new_item_field_ul_id'); |
|
219 var new_option_dialog = $("#new_item_dialog"); |
|
220 |
|
221 new_option_val.val($(this).parents('fieldset').children('ol') |
|
222 .attr('id')); |
|
223 |
|
224 new_option_dialog.dialog('open').find('input:first').focus(); |
|
225 } |
|
226 ) |
|
227 .hover( |
|
228 function () { |
|
229 $(this).addClass("ui-state-hover"); |
|
230 }, |
|
231 function () { |
|
232 $(this).removeClass("ui-state-hover"); |
|
233 } |
|
234 ) |
|
235 .mousedown( |
|
236 function () { |
|
237 $(this).addClass("ui-state-active"); |
|
238 } |
|
239 ) |
|
240 .mouseup( |
|
241 function () { |
|
242 $(this).removeClass("ui-state-active"); |
|
243 } |
|
244 ); |
|
245 } |
|
246 ); |
|
247 |
|
248 options.find('.AddQuestion').click( |
|
249 function (e) { |
|
250 // Choose a field type |
|
251 $("#new_question_button_id").val($(this).attr('id')); |
|
252 var question_options_div = $('#question_options_div'); |
|
253 if ($(this).attr('id') === 'choice') { |
|
254 question_options_div.show(); |
|
255 } |
|
256 else { |
|
257 question_options_div.hide(); |
|
258 } |
|
259 |
|
260 $("#new_question_dialog").dialog('open').find('input:first') |
|
261 .focus(); |
|
262 } |
|
263 ); |
|
264 }).trigger('init') |
|
265 .bind('option_init', |
|
266 function () { |
|
267 |
|
268 // Delete list/choice-field item from survey |
|
269 widget.find('a.delete_item').click( |
|
270 function () { |
|
271 var to_delete = this.id.replace('del_', ''); |
|
272 $('#delete_item_field').val(to_delete); |
|
273 $('#delete_item_dialog').dialog('open'); |
|
274 } |
|
275 ).end(); |
|
276 |
|
277 } |
|
278 ).trigger('option_init'); |
|
279 |
|
280 |
|
281 /* GSOC ROLE-SPECIFIC FIELD PLUGIN |
|
282 * Choice between student/mentor renders required GSOC specific fields |
|
283 */ |
|
284 |
|
285 var taking_access_field = $('select#id_taking_access'); |
|
286 |
|
287 var addRoleFields = function (role_type) { |
|
288 // these should ideally be generated with django forms |
|
289 // TODO: apply info tooltips |
|
290 var CHOOSE_A_PROJECT_FIELD = [ |
|
291 '<tr class="role-specific"><th><label>Choose Project:</label></th>', |
|
292 '<td> <select disabled=TRUE id="id_survey__NA__selection__project"', |
|
293 ' name="survey__1__selection__see"><option>Survey Taker\'s Projects', |
|
294 'For This Program</option></select> </td></tr>' |
|
295 ].join(""); |
|
296 |
|
297 var CHOOSE_A_GRADE_FIELD = [ |
|
298 '<tr class="role-specific"><th><label>Assign Grade:</label></th><td>', |
|
299 '<select disabled=TRUE id="id_survey__NA__selection__grade"', |
|
300 'name="survey__1__selection__see"><option>Pass/Fail</option></select>', |
|
301 '</td></tr>' |
|
302 ].join(""); |
|
303 |
|
304 // flush existing role-specific fields |
|
305 var role_specific_fields = survey.find('tr.role-specific'); |
|
306 role_specific_fields.remove(); |
|
307 |
|
308 switch (role_type) { |
|
309 case "mentor evaluation": |
|
310 survey.prepend(CHOOSE_A_PROJECT_FIELD); |
|
311 survey.append(CHOOSE_A_GRADE_FIELD); |
|
312 break; |
|
313 |
|
314 case "student evaluation": |
|
315 survey.prepend(CHOOSE_A_PROJECT_FIELD); |
|
316 break; |
|
317 } |
|
318 }; |
|
319 |
|
320 taking_access_field.change( |
|
321 function () { |
|
322 var role_type = $(this).val(); |
|
323 addRoleFields(role_type); |
|
324 } |
|
325 ); |
|
326 |
|
327 addRoleFields(taking_access_field.val()); |
|
328 |
|
329 /* |
|
330 * == Survey Submission Handler == |
|
331 */ |
|
332 // Bind submit |
|
333 $('form').bind('submit', |
|
334 function () { |
|
335 |
|
336 /* |
|
337 * get rid of role-specific fields |
|
338 */ |
|
339 survey.find('tr.role-specific').remove(); |
|
340 |
|
341 /* |
|
342 * Save survey content html from editPost |
|
343 * if POST fails |
|
344 */ |
|
345 |
|
346 // save field vals |
|
347 widget.find('.long_answer,input').each( |
|
348 function () { |
|
349 $(this).attr('val', $(this).val()); |
|
350 } |
|
351 ); |
|
352 |
|
353 $(this).find("#id_survey_html").attr('value', widget.html()); |
|
354 |
|
355 // don't save default value |
|
356 widget.find('input').each( |
|
357 function () { |
|
358 if ($(this).val() === DEFAULT_SHORT_ANSWER_TEXT) { |
|
359 $(this).val(''); |
|
360 } |
|
361 } |
|
362 ); |
|
363 |
|
364 // don't save default value |
|
365 widget.find('.long_answer, .tooltip_entry').each( |
|
366 function () { |
|
367 if ($(this).val() === DEFAULT_LONG_ANSWER_TEXT) { |
|
368 $(this).val(''); |
|
369 } |
|
370 } |
|
371 ); |
|
372 |
|
373 // get rid of the options |
|
374 $('input#id_s_html') |
|
375 .val( |
|
376 widget |
|
377 .find( |
|
378 'div#survey_options' |
|
379 ) |
|
380 .remove() |
|
381 .end() |
|
382 .html() |
|
383 ); |
|
384 // only needed for HTML |
|
385 |
|
386 // Get option order per field |
|
387 survey.find('.sortable').each( |
|
388 function () { |
|
389 $('#order_for_' + this.id) |
|
390 .val( |
|
391 $(this).sortable( |
|
392 'serialize' |
|
393 ) |
|
394 ); |
|
395 } |
|
396 ); |
|
397 } |
|
398 ); |
|
399 }); |
|
400 }(jQuery)); |
|
401 |
|
402 |
|
403 (function ($) { |
|
404 /* |
|
405 * == Utils == |
|
406 * |
|
407 */ |
|
408 jQuery.fn.extend({ |
|
409 |
|
410 // get position of survey field |
|
411 getPosition: function () { |
|
412 var this_fieldset = $(this).parents('fieldset:first'); |
|
413 var this_table = this_fieldset.parents('table:first'); |
|
414 var position = this_table.find('fieldset').index(this_fieldset) + '__'; |
|
415 return position; |
|
416 } |
|
417 }); |
|
418 }(jQuery)); |
|
419 |
|
420 |
|
421 (function ($) { |
|
422 /* |
|
423 * == Sortable options == |
|
424 */ |
|
425 $(function () { |
|
426 $(".sortable").each( |
|
427 function (i, domEle) { |
|
428 $(domEle).sortable().disableSelection().end(); |
|
429 } |
|
430 ); |
|
431 }); |
|
432 }(jQuery)); |
|
433 |
|
434 |
|
435 (function ($) { |
|
436 /* |
|
437 * == Editable options == |
|
438 */ |
|
439 $(function () { |
|
440 function onSubmitEditable(content) { |
|
441 var id_ = $(this).parent().attr('id').replace('-li-', '_'); |
|
442 id_ = id_ + '__field'; |
|
443 $('#' + id_).val(content.current); |
|
444 } |
|
445 $('.editable_option').editable({ |
|
446 editBy: 'dblclick', |
|
447 submit: 'change', |
|
448 cancel: 'cancel', |
|
449 onSubmit: onSubmitEditable |
|
450 }); |
|
451 }); |
|
452 }(jQuery)); |
|
453 |
|
454 |
|
455 (function ($) { |
|
456 $(function () { |
|
457 var del_li = ["<a class='delete_item' id='del_", |
|
458 "' ><img src='/soc/content/images/minus.gif'/></a> "]; |
|
459 |
|
460 // Confirmation dialog for deleting list/choice-field item from survey |
|
461 $("#delete_item_dialog").dialog({ |
|
462 autoOpen: false, |
|
463 bgiframe: true, |
|
464 resizable: false, |
|
465 height: 300, |
|
466 modal: true, |
|
467 overlay: { |
|
468 backgroundColor: '#000', |
|
469 opacity: 0.5 |
|
470 }, |
|
471 buttons: { |
|
472 'Delete this item': function () { |
|
473 $('#' + $('#delete_item_field').val()).remove(); |
|
474 $('#delete_item_field').val(''); |
|
475 $(this).dialog('close'); |
|
476 }, |
|
477 Cancel: function () { |
|
478 $('#delete_item_field').val(''); |
|
479 $(this).dialog('close'); |
|
480 } |
|
481 } |
|
482 }); |
|
483 |
|
484 |
|
485 // Dialog for adding list/choice-field item to survey |
|
486 $("#new_item_dialog").dialog({ |
|
487 bgiframe: true, |
|
488 autoOpen: false, |
|
489 height: 300, |
|
490 modal: true, |
|
491 buttons: { |
|
492 'Add option': function () { |
|
493 var ol_id = $('#new_item_field_ul_id').val(); |
|
494 var ol = $('#' + ol_id); |
|
495 var name = $('#new_item_name').val(); |
|
496 var i = ol.find('li').length; |
|
497 var id_ = 'id_' + ol_id + '_' + i; |
|
498 var option_html = $([ |
|
499 '<li id="id-li-', ol_id, '_', i, |
|
500 '" class="ui-state-defaolt sortable_li">', |
|
501 '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>', |
|
502 '<span id="', id_, '" class="editable_option" name="', id_, |
|
503 '__field">', name, '</span>', '<input type="hidden" id="', id_, |
|
504 '__field" name="', id_, '__field" value="', |
|
505 name.replace(/\"/g,'"'), '" >', '</li>' |
|
506 ].join("")); |
|
507 |
|
508 ol.append( |
|
509 option_html |
|
510 .prepend( |
|
511 del_li.join( |
|
512 option_html.attr('id') |
|
513 ) |
|
514 ) |
|
515 ); |
|
516 ol.sortable().disableSelection(); |
|
517 $('#new_item_name').val(''); |
|
518 $('#new_item_field_ol_id').val(''); |
|
519 $(this).dialog('close'); |
|
520 }, |
|
521 Cancel: function () { |
|
522 $('#new_item_name').val(''); |
|
523 $('#new_item_field_ul_id').val(''); |
|
524 $(this).dialog('close'); |
|
525 } |
|
526 } |
|
527 }); |
|
528 }); |
|
529 }(jQuery)); |
|
530 |
|
531 |
|
532 (function ($) { |
|
533 $(function () { |
|
534 // Dialog for adding new question to survey |
|
535 var SURVEY_PREFIX = 'survey__'; |
|
536 var del_el = ["<a class='delete'><img '", |
|
537 "src='/soc/content/images/minus.gif'/></a>"].join(""); |
|
538 var del_li = ["<a class='delete_item' id='del_", |
|
539 "' ><img src='/soc/content/images/minus.gif'/></a> "]; |
|
540 |
|
541 |
|
542 var widget = $('div#survey_widget'); |
|
543 var survey = widget.find('tbody:first'); |
|
544 |
|
545 $("#new_question_dialog").dialog({ |
|
546 bgiframe: true, |
|
547 autoOpen: false, |
|
548 height: 400, |
|
549 modal: true, |
|
550 buttons: { |
|
551 'Add question': function () { |
|
552 var button_id = $("#new_question_button_id").val(); |
|
553 var survey_table = $('div#survey_widget').find('tbody:first'); |
|
554 $("#new_question_button_id").val(''); |
|
555 |
|
556 var field_template = $(["<tr><th><label>", del_el, |
|
557 "</label></th><td> </td></tr>"].join("")); |
|
558 |
|
559 var field_name = $("#new_question_name").val(); |
|
560 var question_content = $("#new_question_content").val(); |
|
561 var question_options = $("#new_question_options").val(); |
|
562 |
|
563 if (field_name !== '') { |
|
564 $("#new_question_name").val(''); |
|
565 $("#new_question_content").val(''); |
|
566 $("#new_question_options").val(''); |
|
567 |
|
568 var new_field = false; |
|
569 var type = button_id + "__"; |
|
570 var field_count = survey_table.find('fieldset').length; |
|
571 var new_field_count = field_count + 1 + '__'; |
|
572 |
|
573 var MIN_ROWS = 10; |
|
574 var MAX_ROWS = MIN_ROWS * 2; |
|
575 var DEFAULT_OPTION_TEXT = 'Add A New Option...'; |
|
576 var default_option = ["<option>", DEFAULT_OPTION_TEXT, |
|
577 "</option>"].join(""); |
|
578 |
|
579 // create the HTML for the field |
|
580 switch (button_id) { |
|
581 case "short_answer": |
|
582 new_field = ["<fieldset>\n", |
|
583 '<label for="required_for_', |
|
584 field_name, '">Required</label>', |
|
585 '<select id="required_for_', field_name, |
|
586 '" name="required_for_', field_name, |
|
587 '"><option value="True" selected="selected">True', |
|
588 '</option>', '<option value="False">False</option>', |
|
589 '</select>', '<label for="comment_for_', |
|
590 field_name, '">Allow Comments</label>', |
|
591 '<select id="comment_for_', field_name, |
|
592 '" name="comment_for_', field_name, '">', |
|
593 '<option value="True" selected="selected">', |
|
594 'True</option>', '<option value="False">', |
|
595 'False</option>', '</select>', |
|
596 "<input type='text' ", |
|
597 "class='short_answer'>", "</fieldset>" |
|
598 ].join(""); |
|
599 break; |
|
600 case "long_answer": |
|
601 field_count = survey_table.find('fieldset').length; |
|
602 new_field_count = field_count + 1 + '__'; |
|
603 new_field = ['<fieldset>\n', '<label for="required_for_', |
|
604 field_name, '">Required</label>', |
|
605 '<select id="required_for_', field_name, |
|
606 '" name="required_for_', field_name, |
|
607 '"><option value="True" selected="selected">True', |
|
608 '</option>', '<option value="False">False</option>', |
|
609 '</select>', '<label for="comment_for_', |
|
610 field_name, '">Allow Comments</label>', |
|
611 '<select id="comment_for_', field_name, |
|
612 '" name="comment_for_', field_name, '">', |
|
613 '<option value="True" selected="selected">', |
|
614 'True</option>', '<option value="False">', |
|
615 'False</option>', '</select>', |
|
616 "<textarea cols='40' rows='", MIN_ROWS, |
|
617 "' class='long_answer'/>", '</fieldset>' |
|
618 ].join(""); |
|
619 break; |
|
620 case "selection": |
|
621 new_field = ["<select><option></option>", default_option, |
|
622 "</select>"].join(""); |
|
623 break; |
|
624 case "pick_multi": |
|
625 new_field = ["<fieldset class='fieldset'><input type='button'", |
|
626 "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"] |
|
627 .join(""); |
|
628 break; |
|
629 case "choice": |
|
630 new_field = ["<fieldset class='fieldset'><input type='button'", |
|
631 "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"] |
|
632 .join(""); |
|
633 break; |
|
634 } |
|
635 |
|
636 if (new_field) { |
|
637 var question_for = [ |
|
638 '\n <input type="hidden" name="NEW_', field_name, |
|
639 '" id="NEW_', field_name, '" value="', question_content, |
|
640 '"/>' |
|
641 ].join(""); |
|
642 |
|
643 field_count = survey_table.find('fieldset').length; |
|
644 new_field_count = field_count + 1 + '__'; |
|
645 var formatted_name = (SURVEY_PREFIX + new_field_count + type + |
|
646 field_name); |
|
647 if (button_id === 'choice') { |
|
648 var name = (field_name); |
|
649 new_field = $([ |
|
650 '<fieldset>\n', '<label for="required_for_', name, |
|
651 '">Required</label>', |
|
652 '<select id="required_for_', name, '" name="required_for_', |
|
653 name, '"><option value="True" selected="selected">True', |
|
654 '</option>', '<option value="False">False</option>', |
|
655 '</select>', '<label for="comment_for_', name, |
|
656 '">Allow Comments</label>', '<select id="comment_for_', name, |
|
657 '" name="comment_for_', name, '">', |
|
658 '<option value="True" selected="selected">True</option>', |
|
659 '<option value="False">False</option>', |
|
660 '</select>', |
|
661 '<label for="render_for_', name, |
|
662 '">Render as</label>', '\n <select id="render_for_', name, |
|
663 '" name="render_for_', name, '">', '\n <option', |
|
664 'selected="selected" value="select">select</option>', |
|
665 '\n <option value="checkboxes">checkboxes</option>', |
|
666 '\n <option value="radio_buttons">radio_buttons</option>', |
|
667 '\n </select>', '\n <input type="hidden" id="order_for_', |
|
668 name, '\n " name="order_for_', name, '" value=""/>', |
|
669 '\n <input type="hidden" id="index_for_', name, |
|
670 '\n " name="index_for_', name, '" value="', |
|
671 (field_count + 1), '"/>\n <ol id="', name, |
|
672 '" class="sortable"></ol>', |
|
673 question_for, '\n <button name="create-option-button"', |
|
674 'id="create-option-button__', name, |
|
675 '" class="ui-button ui-state-default ui-corner-all" value="', |
|
676 name, '" onClick="return false;">Create new option', |
|
677 '</button>\n</fieldset>' |
|
678 ].join("")); |
|
679 |
|
680 $(new_field).attr({ |
|
681 'id': 'id_' + formatted_name, |
|
682 'name': formatted_name |
|
683 }); |
|
684 field_template |
|
685 .find( |
|
686 'label' |
|
687 ) |
|
688 .attr( |
|
689 'for', |
|
690 'NEW_' + name |
|
691 ) |
|
692 .append(question_content).end() |
|
693 .find( |
|
694 'td' |
|
695 ) |
|
696 .append(new_field); |
|
697 survey_table.append(field_template).end(); |
|
698 |
|
699 if (question_options) { |
|
700 |
|
701 var options_array = question_options.split('\n'); |
|
702 var ol = $('#' + name); |
|
703 var length = options_array.length; |
|
704 var oname = ''; |
|
705 var id_ = ''; |
|
706 var option_html = ''; |
|
707 |
|
708 for (var i = 0; i < length; i = i + 1) { |
|
709 id_ = 'id_' + name + '_' + i; |
|
710 oname = options_array[i]; |
|
711 option_html = $([ |
|
712 '<li id="id-li-', name, '_', i, |
|
713 '" class="ui-state-defaolt sortable_li">', |
|
714 '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>', |
|
715 '<span id="' + id_ + '" class="editable_option" name="', |
|
716 id_, '__field">', oname, '</span>', '<input ', |
|
717 'type="hidden" id="', id_, '__field" name="', id_, |
|
718 '__field" value="', oname.replace(/\"/g,'"'), '" >', '</li>' |
|
719 ].join("")); |
|
720 ol.append(option_html.prepend( |
|
721 del_li.join(option_html.attr('id')))); |
|
722 ol.sortable().disableSelection(); |
|
723 } |
|
724 |
|
725 survey.trigger('option_init'); |
|
726 } |
|
727 } |
|
728 |
|
729 else { |
|
730 new_field = $(new_field); |
|
731 // maybe the name should be serialized in a more common format |
|
732 $(new_field).find('.long_answer, .short_answer').attr({ |
|
733 'id': 'id_' + formatted_name, |
|
734 'name': formatted_name |
|
735 }); |
|
736 field_template.find( |
|
737 'label' |
|
738 ) |
|
739 .attr( |
|
740 'for', |
|
741 'id_' + formatted_name |
|
742 ) |
|
743 .append(question_content).end() |
|
744 .find( |
|
745 'td' |
|
746 ) |
|
747 .append(new_field).append($(question_for)); |
|
748 |
|
749 survey_table.append(field_template); |
|
750 } |
|
751 |
|
752 survey.trigger('init'); |
|
753 |
|
754 } |
|
755 } |
|
756 $("#new_question_name").val(''); |
|
757 $("#new_question_content").val(''); |
|
758 $("#new_question_options").val(''); |
|
759 $(this).dialog('close'); |
|
760 }, |
|
761 |
|
762 Cancel: function () { |
|
763 $('#new_question_name').val(''); |
|
764 $("#new_question_button_id").val(''); |
|
765 $("#new_question_content").val(''); |
|
766 $("#new_question_options").val(''); |
|
767 $(this).dialog('close'); |
|
768 } |
|
769 } |
|
770 }); |
|
771 }); |
|
772 }(jQuery)); |