app/soc/content/js/survey-edit-090627.js
changeset 2505 77ac8273f7a2
parent 2504 74b115310fb0
child 2506 c98bf890156e
equal deleted inserted replaced
2504:74b115310fb0 2505:77ac8273f7a2
     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       widget.find('.long_answer').each(
       
    80         function () {
       
    81           $(this).attr('name', SURVEY_PREFIX + $(this).getPosition() +
       
    82                              'long_answer__' + $(this).attr('name'))
       
    83           .attr('overflow', 'auto');
       
    84           // TODO: replace scrollbar with jquery autogrow
       
    85         }
       
    86       );
       
    87     }
       
    88 
       
    89     if (survey_html && survey_html.length > 1) {
       
    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 ($) {
       
   391   /*
       
   392   * == Utils ==
       
   393   *
       
   394   */
       
   395   jQuery.fn.extend({
       
   396 
       
   397     // get position of survey field
       
   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;
       
   403     }
       
   404   });
       
   405 }(jQuery));
       
   406 
       
   407 
       
   408 (function ($) {
       
   409   /*
       
   410   * == Sortable options ==
       
   411   */
       
   412   $(function () {
       
   413     $(".sortable").each(
       
   414       function (i, domEle) {
       
   415         $(domEle).sortable().disableSelection().end();
       
   416       }
       
   417     );
       
   418   });
       
   419 }(jQuery));
       
   420 
       
   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
       
   437     });
       
   438   });
       
   439 }(jQuery));
       
   440 
       
   441 
       
   442 (function ($) {
       
   443   $(function () {
       
   444   var del_li = ["<a class='delete_item' id='del_",
       
   445                 "' ><img src='/soc/content/images/minus.gif'/></a> "];
       
   446 
       
   447     // Confirmation dialog for deleting list/choice-field item from survey
       
   448     $("#delete_item_dialog").dialog({
       
   449       autoOpen: false,
       
   450       bgiframe: true,
       
   451       resizable: false,
       
   452       height: 300,
       
   453       modal: true,
       
   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         }
       
   468       }
       
   469     });
       
   470 
       
   471 
       
   472     //  Dialog for adding list/choice-field item to survey
       
   473     $("#new_item_dialog").dialog({
       
   474       bgiframe: true,
       
   475       autoOpen: false,
       
   476       height: 300,
       
   477       modal: true,
       
   478       buttons: {
       
   479         'Add option': function () {
       
   480           var ol_id =  $('#new_item_field_ul_id').val();
       
   481           var ol = $('#' + ol_id);
       
   482           var name = $('#new_item_name').val();
       
   483           var i = ol.find('li').length;
       
   484           var id_ = 'id_' + ol_id + '_' + i;
       
   485           var option_html = $([
       
   486             '<li id="id-li-', ol_id, '_', i,
       
   487             '" class="ui-state-defaolt sortable_li">',
       
   488             '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>',
       
   489             '<span id="', id_, '" class="editable_option" name="', id_,
       
   490             '__field">', name, '</span>', '<input type="hidden" id="', id_,
       
   491             '__field" name="', id_, '__field" value="', name, '" >', '</li>'
       
   492           ].join(""));
       
   493 
       
   494           ol.append(
       
   495             option_html
       
   496             .prepend(
       
   497               del_li.join(
       
   498                 option_html.attr('id')
       
   499               )
       
   500             )
       
   501           );
       
   502           ol.sortable().disableSelection();
       
   503           $('#new_item_name').val('');
       
   504           $('#new_item_field_ol_id').val('');
       
   505           $(this).dialog('close');
       
   506         },
       
   507         Cancel: function () {
       
   508           $('#new_item_name').val('');
       
   509           $('#new_item_field_ul_id').val('');
       
   510           $(this).dialog('close');
       
   511         }
       
   512       }
       
   513     });
       
   514   });
       
   515 }(jQuery));
       
   516 
       
   517 
       
   518 (function ($) {
       
   519   $(function () {
       
   520     //  Dialog for adding new question to survey
       
   521     var SURVEY_PREFIX = 'survey__';
       
   522     var del_el = ["<a class='delete'><img '",
       
   523               "src='/soc/content/images/minus.gif'/></a>"].join("");
       
   524     var del_li = ["<a class='delete_item' id='del_",
       
   525                   "' ><img src='/soc/content/images/minus.gif'/></a> "];
       
   526 
       
   527 
       
   528     var widget = $('div#survey_widget');
       
   529     var survey = widget.find('tbody:first');
       
   530 
       
   531     $("#new_question_dialog").dialog({
       
   532       bgiframe: true,
       
   533       autoOpen: false,
       
   534       height: 400,
       
   535       modal: true,
       
   536       buttons: {
       
   537         'Add question': function () {
       
   538           var button_id = $("#new_question_button_id").val();
       
   539           var survey_table = $('div#survey_widget').find('tbody:first');
       
   540           $("#new_question_button_id").val('');
       
   541 
       
   542           var field_template =  $(["<tr><th><label>", del_el,
       
   543                                    "</label></th><td>  </td></tr>"].join(""));
       
   544 
       
   545           var field_name = $("#new_question_name").val();
       
   546           var question_content = $("#new_question_content").val();
       
   547           var question_options = $("#new_question_options").val();
       
   548 
       
   549           if (field_name !== '') {
       
   550             $("#new_question_name").val('');
       
   551             $("#new_question_content").val('');
       
   552             $("#new_question_options").val('');
       
   553 
       
   554             var new_field = false;
       
   555             var type = button_id + "__";
       
   556             var field_count = survey_table.find('tr').length;
       
   557             var new_field_count = field_count + 1 + '__';
       
   558 
       
   559             var MIN_ROWS = 10;
       
   560             var MAX_ROWS = MIN_ROWS * 2;
       
   561             var DEFAULT_OPTION_TEXT = 'Add A New Option...';
       
   562             var default_option = ["<option>", DEFAULT_OPTION_TEXT,
       
   563                                   "</option>"].join("");
       
   564 
       
   565             // create the HTML for the field
       
   566             switch (button_id) {
       
   567             case "short_answer":
       
   568               new_field = ["<fieldset>\n",
       
   569                           '<label for="required_for_',
       
   570                            field_name, '">Required</label>',
       
   571                            '<select id="required_for_', field_name,
       
   572                            '" name="required_for_', field_name,
       
   573                            '"><option value="True" selected="selected">True',
       
   574                            '</option>', '<option value="False">False</option>',
       
   575                            '</select><br/>', '<label for="comment_for_',
       
   576                            field_name, '">Allow Comments</label>',
       
   577                            '<select id="comment_for_', field_name,
       
   578                            '" name="comment_for_', field_name, '">',
       
   579                            '<option value="True" selected="selected">',
       
   580                            'True</option>', '<option value="False">',
       
   581                            'False</option>', '</select><br/>',
       
   582                           "<input type='text' ",
       
   583                            "class='short_answer'>", "</fieldset>"
       
   584                           ].join("");
       
   585               break;
       
   586             case "long_answer":
       
   587               field_count = survey_table.find('tr').length;
       
   588               new_field_count = field_count + 1 + '__';
       
   589               new_field = ['<fieldset>\n', '<label for="required_for_',
       
   590                            field_name, '">Required</label>',
       
   591                            '<select id="required_for_', field_name,
       
   592                            '" name="required_for_', field_name,
       
   593                            '"><option value="True" selected="selected">True',
       
   594                            '</option>', '<option value="False">False</option>',
       
   595                            '</select><br/>', '<label for="comment_for_',
       
   596                            field_name, '">Allow Comments</label>',
       
   597                            '<select id="comment_for_', field_name,
       
   598                            '" name="comment_for_', field_name, '">',
       
   599                            '<option value="True" selected="selected">',
       
   600                            'True</option>', '<option value="False">',
       
   601                            'False</option>', '</select><br/>',
       
   602                            "<textarea cols='40' rows='", MIN_ROWS,
       
   603                            "' class='long_answer'/>", '</fieldset>'
       
   604                           ].join("");
       
   605               break;
       
   606             case "selection":
       
   607               new_field = ["<select><option></option>", default_option,
       
   608                            "</select>"].join("");
       
   609               break;
       
   610             case "pick_multi":
       
   611               new_field = ["<fieldset class='fieldset'><input type='button'",
       
   612                            "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"]
       
   613                           .join("");
       
   614               break;
       
   615             case "choice":
       
   616               new_field = ["<fieldset class='fieldset'><input type='button'",
       
   617                            "value='", DEFAULT_OPTION_TEXT, "' /></fieldset>"]
       
   618                           .join("");
       
   619               break;
       
   620             }
       
   621 
       
   622             if (new_field) {
       
   623               var question_for = [
       
   624                 '\n  <input type="hidden" name="NEW_', field_name,
       
   625                 '" id="NEW_', field_name, '" value="', question_content,
       
   626                 '"/>'
       
   627               ].join("");
       
   628 
       
   629               field_count = survey_table.find('tr').length;
       
   630               new_field_count = field_count + 1 + '__';
       
   631               var formatted_name = (SURVEY_PREFIX + new_field_count + type +
       
   632                                     field_name);
       
   633               if (button_id === 'choice')  {
       
   634                 var name = (field_name);
       
   635                 new_field = $([
       
   636                   '<fieldset>\n', '<label for="required_for_', name,
       
   637                   '">Required</label>',
       
   638                   '<select id="required_for_', name, '" name="required_for_',
       
   639                   name, '"><option value="True" selected="selected">True',
       
   640                   '</option>', '<option value="False">False</option>',
       
   641                   '</select><br/>', '<label for="comment_for_', name,
       
   642                   '">Allow Comments</label>', '<select id="comment_for_', name,
       
   643                   '" name="comment_for_', name, '">',
       
   644                   '<option value="True" selected="selected">True</option>',
       
   645                   '<option value="False">False</option>',
       
   646                   '</select><br/>',
       
   647                   '<label for="render_for_', name,
       
   648                   '">Render as</label>', '\n  <select id="render_for_', name,
       
   649                   '" name="render_for_', name, '">', '\n    <option',
       
   650                   'selected="selected" value="select">select</option>',
       
   651                   '\n    <option value="checkboxes">checkboxes</option>',
       
   652                   '\n    <option value="radio_buttons">radio_buttons</option>',
       
   653                   '\n  </select>', '\n  <input type="hidden" id="order_for_',
       
   654                   name, '\n  " name="order_for_', name, '" value=""/>',
       
   655                   '\n  <input type="hidden" id="index_for_', name,
       
   656                   '\n  " name="index_for_', name, '" value="',
       
   657                   (field_count + 1), '"/>\n  <ol id="', name,
       
   658                   '" class="sortable"></ol>',
       
   659                   question_for, '\n  <button name="create-option-button"',
       
   660                   'id="create-option-button__', name,
       
   661                   '" class="ui-button ui-state-default ui-corner-all" value="',
       
   662                   name, '" onClick="return false;">Create new option',
       
   663                   '</button>\n</fieldset>'
       
   664                 ].join(""));
       
   665 
       
   666                 $(new_field).attr({
       
   667                   'id': 'id_' + formatted_name,
       
   668                   'name': formatted_name
       
   669                 });
       
   670                 field_template
       
   671                 .find(
       
   672                   'label'
       
   673                 )
       
   674                 .attr(
       
   675                   'for',
       
   676                   'NEW_' + name
       
   677                 )
       
   678                 .append(question_content).end()
       
   679                 .find(
       
   680                   'td'
       
   681                 )
       
   682                 .append(new_field);
       
   683                 survey_table.append(field_template).end();
       
   684 
       
   685                 if (question_options) {
       
   686 
       
   687                   var options_array = question_options.split('\n');
       
   688                   var ol = $('#' + name);
       
   689                   var length = options_array.length;
       
   690                   var oname = '';
       
   691                   var id_ = '';
       
   692                   var option_html = '';
       
   693 
       
   694                   for (var i = 0; i < length; i = i + 1) {
       
   695                     id_ = 'id_' + name + '_' + i;
       
   696                     oname = options_array[i];
       
   697                     option_html = $([
       
   698                       '<li id="id-li-', name, '_', i,
       
   699                       '" class="ui-state-defaolt sortable_li">',
       
   700                       '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>',
       
   701                       '<span id="' + id_ + '" class="editable_option" name="',
       
   702                       id_, '__field">', oname, '</span>', '<input ',
       
   703                       'type="hidden" id="', id_, '__field" name="', id_,
       
   704                       '__field" value="', oname, '" >', '</li>'
       
   705                     ].join(""));
       
   706                     ol.append(option_html.prepend(
       
   707                       del_li.join(option_html.attr('id'))));
       
   708                     ol.sortable().disableSelection();
       
   709                   }
       
   710 
       
   711                   survey.trigger('option_init');
       
   712                 }
       
   713               }
       
   714 
       
   715               else {
       
   716                 new_field = $(new_field);
       
   717                 // maybe the name should be serialized in a more common format
       
   718                 $(new_field).find('.long_answer, .short_answer').attr({
       
   719                   'id': 'id_' + formatted_name,
       
   720                   'name': formatted_name
       
   721                 });
       
   722                 field_template.find(
       
   723                   'label'
       
   724                 )
       
   725                 .attr(
       
   726                   'for',
       
   727                   'id_' + formatted_name
       
   728                 )
       
   729                 .append(question_content + ":").end()
       
   730                 .find(
       
   731                   'td'
       
   732                 )
       
   733                 .append(new_field).append($(question_for));
       
   734 
       
   735                 survey_table.append(field_template);
       
   736               }
       
   737 
       
   738               survey.trigger('init');
       
   739 
       
   740             }
       
   741           }
       
   742           $("#new_question_name").val('');
       
   743           $("#new_question_content").val('');
       
   744           $("#new_question_options").val('');
       
   745           $(this).dialog('close');
       
   746         },
       
   747 
       
   748         Cancel: function () {
       
   749           $('#new_question_name').val('');
       
   750           $("#new_question_button_id").val('');
       
   751           $("#new_question_content").val('');
       
   752           $("#new_question_options").val('');
       
   753           $(this).dialog('close');
       
   754         }
       
   755       }
       
   756     });
       
   757   });
       
   758 }(jQuery));