app/soc/content/js/survey-take-090627.js
changeset 2423 d6b4be046c26
child 2526 8f29bfb9eb52
equal deleted inserted replaced
2422:44c500fc0eca 2423:d6b4be046c26
       
     1 /* Copyright 2009 the Melange authors.
       
     2  *
       
     3  * Licensed under the Apache License, Version 2.0 (the "License");
       
     4  * you may not use this file except in compliance with the License.
       
     5  * You may obtain a copy of the License at
       
     6  *
       
     7  *   http://www.apache.org/licenses/LICENSE-2.0
       
     8  *
       
     9  * Unless required by applicable law or agreed to in writing, software
       
    10  * distributed under the License is distributed on an "AS IS" BASIS,
       
    11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    12  * See the License for the specific language governing permissions and
       
    13  * limitations under the License.
       
    14  */
       
    15 
       
    16 /*
       
    17 *
       
    18 * @author <a href="mailto:ajaksu@gmail.com">Daniel Diniz</a>
       
    19 * @author <a href="mailto:jamesalexanderlevy@gmail.com">James Levy</a>
       
    20 */
       
    21 
       
    22 (function ($) {
       
    23   $(function () {
       
    24 
       
    25     /*
       
    26     * == Setup Survey on Page Load ==
       
    27     *
       
    28     */
       
    29 
       
    30     var widget = $('div#survey_widget');
       
    31     widget.parents('td.formfieldvalue:first').css({
       
    32       'float': 'left',
       
    33       'width': 200
       
    34     });
       
    35 
       
    36     // TODO(ajaksu) survey below is unused, remove if no known use is predicted
       
    37     var survey = widget.find('tbody:first');
       
    38 
       
    39     if (widget.hasClass('create')) {
       
    40 
       
    41       /*
       
    42       * == Set Custom Field Rules ==
       
    43       *
       
    44       */
       
    45       widget.find('input').each(
       
    46         function () {
       
    47           $(this).preserveDefaultText($(this).val());
       
    48         }
       
    49       );
       
    50 
       
    51       widget.find('textarea').each(
       
    52         function () {
       
    53           $(this).preserveDefaultText($(this).val()).attr('overflow', 'auto')
       
    54           .growfield();
       
    55         }
       
    56       );
       
    57     }
       
    58 
       
    59     else { // survey has saved results
       
    60       widget.find('textarea').each(
       
    61         function () {
       
    62           $(this).attr('overflow', 'auto').growfield();
       
    63         }
       
    64       ).end()
       
    65       .find('.pick_multi').each(
       
    66         function () {
       
    67           $(this).find('input').each(
       
    68             function () {
       
    69               // if $(this).attr('checked', 'true');});
       
    70             }
       
    71           );
       
    72         }
       
    73       );
       
    74     }
       
    75 
       
    76     /*
       
    77     * == Configure Project ==
       
    78     *
       
    79     */
       
    80 
       
    81     // remember if form has been touched
       
    82     $('input,textarea,select').change(
       
    83       function () {
       
    84         if ($(this).attr('id') === 'id_project') {
       
    85           return;
       
    86         }
       
    87         $('form:first').data('touched', true);
       
    88       }
       
    89     );
       
    90 
       
    91   // remember initially chosen project choice
       
    92     $('select#id_project').blur(
       
    93       function () {
       
    94         $(this).data('selected', $(this).find('option:first'));
       
    95       }
       
    96     ).change(
       
    97       function () {
       
    98         if ($('form:first').data('touched') === true) {
       
    99           // if form has been touched, send confirmation dialog
       
   100           var save_check = confirm(["Switching projects will lose unsaved ",
       
   101                                     "edits made to this survey."].join(""));
       
   102           if (!save_check) {
       
   103             $(this).data('selected').attr('selected', 'selected');
       
   104             return false;
       
   105           }
       
   106         }
       
   107 
       
   108         if ($(this).val() !== 'None') {
       
   109           // redirect with new project GET param
       
   110           window.location = [window.location.href.split('?')[0],  "?project=",
       
   111                              $(this).val()].join("");
       
   112         }
       
   113       }
       
   114     );
       
   115 
       
   116     // insert project link after project select field
       
   117     $('div#project_link').insertAfter($('select#id_project')).show();
       
   118 
       
   119     /*
       
   120     * == Survey Submission Handler ==
       
   121     *
       
   122     */
       
   123 
       
   124     // validate form
       
   125     $('input[type=submit]').bind(
       
   126       'click',
       
   127       function (e) {
       
   128         e.preventDefault();
       
   129 
       
   130         // validate project and grade choice fields
       
   131         if ($('select#id_project') &&
       
   132         $('select#id_project').val() === 'None') {
       
   133           return alert('Please Choose a Project');
       
   134         }
       
   135 
       
   136         if ($('select#id_grade') && $('select#id_grade').val() === 'None') {
       
   137           return alert('Please Choose a Grade');
       
   138         }
       
   139         $('form').trigger('submit');
       
   140 
       
   141       }
       
   142     );
       
   143 
       
   144     $('form').bind('submit',
       
   145       function () {
       
   146         $('input#id_s_html').val(
       
   147           widget.find('div#survey_options').remove().end().html()
       
   148         );
       
   149       }
       
   150     );
       
   151   });
       
   152 }(jQuery));