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