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