app/soc/content/js/survey-edit-090708.js
changeset 2572 1ad6d986be6d
parent 2570 851640749319
child 2670 884f808d8469
equal deleted inserted replaced
2571:dac91fecae38 2572:1ad6d986be6d
       
     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.twolineformfieldlabel > 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       width: 300,
       
   491       modal: true,
       
   492       buttons: {
       
   493         'Add option': function () {
       
   494           var ol_id =  $('#new_item_field_ul_id').val();
       
   495           var ol = $('#' + ol_id);
       
   496           var name = $('#new_item_name').val();
       
   497           var i = ol.find('li').length;
       
   498           var id_ = 'id_' + ol_id + '_' + i;
       
   499           var option_html = $([
       
   500             '<li id="id-li-', ol_id, '_', i,
       
   501             '" class="ui-state-defaolt sortable_li">',
       
   502             '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>',
       
   503             '<span id="', id_, '" class="editable_option" name="', id_,
       
   504             '__field">', name, '</span>', '<input type="hidden" id="', id_,
       
   505             '__field" name="', id_, '__field" value="',
       
   506             name.replace(/\"/g,'&quot;'), '" >', '</li>'
       
   507           ].join(""));
       
   508 
       
   509           ol.append(
       
   510             option_html
       
   511             .prepend(
       
   512               del_li.join(
       
   513                 option_html.attr('id')
       
   514               )
       
   515             )
       
   516           );
       
   517           ol.sortable().disableSelection();
       
   518           $('#new_item_name').val('');
       
   519           $('#new_item_field_ol_id').val('');
       
   520           $(this).dialog('close');
       
   521         },
       
   522         Cancel: function () {
       
   523           $('#new_item_name').val('');
       
   524           $('#new_item_field_ul_id').val('');
       
   525           $(this).dialog('close');
       
   526         }
       
   527       }
       
   528     });
       
   529   });
       
   530 }(jQuery));
       
   531 
       
   532 
       
   533 (function ($) {
       
   534   $(function () {
       
   535     //  Dialog for adding new question to survey
       
   536     var SURVEY_PREFIX = 'survey__';
       
   537     var del_el = ["<a class='delete'><img ",
       
   538               "src='/soc/content/images/minus.gif'/></a>"].join("");
       
   539     var del_li = ["<a class='delete_item' id='del_",
       
   540                   "' ><img src='/soc/content/images/minus.gif'/></a> "];
       
   541 
       
   542 
       
   543     var widget = $('div#survey_widget');
       
   544     var survey = widget.find('tbody:first');
       
   545 
       
   546     $("#new_question_dialog").dialog({
       
   547       bgiframe: true,
       
   548       autoOpen: false,
       
   549       height: 400,
       
   550       width: 300,
       
   551       modal: true,
       
   552       buttons: {
       
   553         'Add question': function () {
       
   554           var button_id = $("#new_question_button_id").val();
       
   555           var survey_table = $('div#survey_widget').find('tbody:first');
       
   556           $("#new_question_button_id").val('');
       
   557 
       
   558           var field_template =  $(["<tr><th><label>", del_el,
       
   559                                    "</label></th><td>  </td></tr>"].join(""));
       
   560 
       
   561           var field_name = $("#new_question_name").val();
       
   562           var question_content = $("#new_question_content").val();
       
   563           var question_options = $("#new_question_options").val();
       
   564 
       
   565           if (field_name !== '') {
       
   566             $("#new_question_name").val('');
       
   567             $("#new_question_content").val('');
       
   568             $("#new_question_options").val('');
       
   569 
       
   570             var new_field = false;
       
   571             var type = button_id + "__";
       
   572             var field_count = survey_table.find('fieldset').length;
       
   573             var new_field_count = field_count + 1 + '__';
       
   574 
       
   575             var MIN_ROWS = 10;
       
   576             var MAX_ROWS = MIN_ROWS * 2;
       
   577             var DEFAULT_OPTION_TEXT = 'Add A New Option...';
       
   578             var default_option = ["<option>", DEFAULT_OPTION_TEXT,
       
   579                                   "</option>"].join("");
       
   580 
       
   581             // create the HTML for the field
       
   582             switch (button_id) {
       
   583             case "short_answer":
       
   584               new_field = ["<fieldset>\n",
       
   585                           '<label for="required_for_',
       
   586                            field_name, '">Required</label>',
       
   587                            '<select id="required_for_', field_name,
       
   588                            '" name="required_for_', field_name,
       
   589                            '"><option value="True" selected="selected">True',
       
   590                            '</option>', '<option value="False">False</option>',
       
   591                            '</select>', '<label for="comment_for_',
       
   592                            field_name, '">Allow Comments</label>',
       
   593                            '<select id="comment_for_', field_name,
       
   594                            '" name="comment_for_', field_name, '">',
       
   595                            '<option value="True" selected="selected">',
       
   596                            'True</option>', '<option value="False">',
       
   597                            'False</option>', '</select>',
       
   598                           "<input type='text' ",
       
   599                            "class='short_answer'>", "</fieldset>"
       
   600                           ].join("");
       
   601               break;
       
   602             case "long_answer":
       
   603               field_count = survey_table.find('fieldset').length;
       
   604               new_field_count = field_count + 1 + '__';
       
   605               new_field = ['<fieldset>\n', '<label for="required_for_',
       
   606                            field_name, '">Required</label>',
       
   607                            '<select id="required_for_', field_name,
       
   608                            '" name="required_for_', field_name,
       
   609                            '"><option value="True" selected="selected">True',
       
   610                            '</option>', '<option value="False">False</option>',
       
   611                            '</select>', '<label for="comment_for_',
       
   612                            field_name, '">Allow Comments</label>',
       
   613                            '<select id="comment_for_', field_name,
       
   614                            '" name="comment_for_', field_name, '">',
       
   615                            '<option value="True" selected="selected">',
       
   616                            'True</option>', '<option value="False">',
       
   617                            'False</option>', '</select>',
       
   618                            "<textarea cols='40' rows='", MIN_ROWS,
       
   619                            "' class='long_answer'/>", '</fieldset>'
       
   620                           ].join("");
       
   621               break;
       
   622             case "selection":
       
   623               new_field = ["<select><option></option>", default_option,
       
   624                            "</select>"].join("");
       
   625               break;
       
   626             case "pick_multi":
       
   627               new_field = ["<fieldset class='fieldset'><input type='button'",
       
   628                            "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"]
       
   629                           .join("");
       
   630               break;
       
   631             case "choice":
       
   632               new_field = ["<fieldset class='fieldset'><input type='button'",
       
   633                            "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"]
       
   634                           .join("");
       
   635               break;
       
   636             }
       
   637 
       
   638             if (new_field) {
       
   639               var question_for = [
       
   640                 '\n  <input type="hidden" name="NEW_', field_name,
       
   641                 '" id="NEW_', field_name, '" value="', question_content,
       
   642                 '"/>'
       
   643               ].join("");
       
   644 
       
   645               field_count = survey_table.find('fieldset').length;
       
   646               new_field_count = field_count + 1 + '__';
       
   647               var formatted_name = (SURVEY_PREFIX + new_field_count + type +
       
   648                                     field_name);
       
   649               if (button_id === 'choice')  {
       
   650                 var name = (field_name);
       
   651                 new_field = $([
       
   652                   '<fieldset>\n', '<label for="required_for_', name,
       
   653                   '">Required</label>',
       
   654                   '<select id="required_for_', name, '" name="required_for_',
       
   655                   name, '"><option value="True" selected="selected">True',
       
   656                   '</option>', '<option value="False">False</option>',
       
   657                   '</select>', '<label for="comment_for_', name,
       
   658                   '">Allow Comments</label>', '<select id="comment_for_', name,
       
   659                   '" name="comment_for_', name, '">',
       
   660                   '<option value="True" selected="selected">True</option>',
       
   661                   '<option value="False">False</option>',
       
   662                   '</select>',
       
   663                   '<label for="render_for_', name,
       
   664                   '">Render as</label>', '\n  <select id="render_for_', name,
       
   665                   '" name="render_for_', name, '">', '\n    <option',
       
   666                   'selected="selected" value="select">select</option>',
       
   667                   '\n    <option value="checkboxes">checkboxes</option>',
       
   668                   '\n    <option value="radio_buttons">radio_buttons</option>',
       
   669                   '\n  </select>', '\n  <input type="hidden" id="order_for_',
       
   670                   name, '\n  " name="order_for_', name, '" value=""/>',
       
   671                   '\n  <input type="hidden" id="index_for_', name,
       
   672                   '\n  " name="index_for_', name, '" value="',
       
   673                   (field_count + 1), '"/>\n  <ol id="', name,
       
   674                   '" class="sortable"></ol>',
       
   675                   question_for, '\n  <button name="create-option-button"',
       
   676                   'id="create-option-button__', name,
       
   677                   '" class="ui-button ui-state-default ui-corner-all" value="',
       
   678                   name, '" onClick="return false;">Create new option',
       
   679                   '</button>\n</fieldset>'
       
   680                 ].join(""));
       
   681 
       
   682                 $(new_field).attr({
       
   683                   'id': 'id_' + formatted_name,
       
   684                   'name': formatted_name
       
   685                 });
       
   686                 field_template
       
   687                 .find(
       
   688                   'label'
       
   689                 )
       
   690                 .attr(
       
   691                   'for',
       
   692                   'NEW_' + name
       
   693                 )
       
   694                 .append(question_content).end()
       
   695                 .find(
       
   696                   'td'
       
   697                 )
       
   698                 .append(new_field);
       
   699                 survey_table.append(field_template).end();
       
   700 
       
   701                 if (question_options) {
       
   702 
       
   703                   var options_array = question_options.split('\n');
       
   704                   var ol = $('#' + name);
       
   705                   var length = options_array.length;
       
   706                   var oname = '';
       
   707                   var id_ = '';
       
   708                   var option_html = '';
       
   709 
       
   710                   for (var i = 0; i < length; i = i + 1) {
       
   711                     id_ = 'id_' + name + '_' + i;
       
   712                     oname = options_array[i];
       
   713                     option_html = $([
       
   714                       '<li id="id-li-', name, '_', i,
       
   715                       '" class="ui-state-defaolt sortable_li">',
       
   716                       '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>',
       
   717                       '<span id="' + id_ + '" class="editable_option" name="',
       
   718                       id_, '__field">', oname, '</span>', '<input ',
       
   719                       'type="hidden" id="', id_, '__field" name="', id_,
       
   720                       '__field" value="', oname.replace(/\"/g,'&quot;'), '" >', '</li>'
       
   721                     ].join(""));
       
   722                     ol.append(option_html.prepend(
       
   723                       del_li.join(option_html.attr('id'))));
       
   724                     ol.sortable().disableSelection();
       
   725                   }
       
   726 
       
   727                   survey.trigger('option_init');
       
   728                 }
       
   729               }
       
   730 
       
   731               else {
       
   732                 new_field = $(new_field);
       
   733                 // maybe the name should be serialized in a more common format
       
   734                 $(new_field).find('.long_answer, .short_answer').attr({
       
   735                   'id': 'id_' + formatted_name,
       
   736                   'name': formatted_name
       
   737                 });
       
   738                 field_template.find(
       
   739                   'label'
       
   740                 )
       
   741                 .attr(
       
   742                   'for',
       
   743                   'id_' + formatted_name
       
   744                 )
       
   745                 .append(question_content).end()
       
   746                 .find(
       
   747                   'td'
       
   748                 )
       
   749                 .append(new_field).append($(question_for));
       
   750 
       
   751                 survey_table.append(field_template);
       
   752               }
       
   753 
       
   754               survey.trigger('init');
       
   755 
       
   756             }
       
   757           }
       
   758           $("#new_question_name").val('');
       
   759           $("#new_question_content").val('');
       
   760           $("#new_question_options").val('');
       
   761           $(this).dialog('close');
       
   762         },
       
   763 
       
   764         Cancel: function () {
       
   765           $('#new_question_name').val('');
       
   766           $("#new_question_button_id").val('');
       
   767           $("#new_question_content").val('');
       
   768           $("#new_question_options").val('');
       
   769           $(this).dialog('close');
       
   770         }
       
   771       }
       
   772     });
       
   773   });
       
   774 }(jQuery));