|
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 |
|
32 // TODO(ajaksu) survey below is unused, remove if no known use is predicted |
|
33 var survey = widget.find('tbody:first'); |
|
34 |
|
35 if (widget.hasClass('create')) { |
|
36 |
|
37 /* |
|
38 * == Set Custom Field Rules == |
|
39 * |
|
40 */ |
|
41 widget.find('textarea').each( |
|
42 function () { |
|
43 $(this).attr('overflow', 'auto').growfield(); |
|
44 } |
|
45 ); |
|
46 } |
|
47 |
|
48 else { // survey has saved results |
|
49 widget.find('textarea').each( |
|
50 function () { |
|
51 $(this).attr('overflow', 'auto').growfield(); |
|
52 } |
|
53 ).end() |
|
54 .find('.pick_multi').each( |
|
55 function () { |
|
56 $(this).find('input').each( |
|
57 /*jslint white: false */ |
|
58 function () { |
|
59 // if $(this).attr('checked', 'true');}); |
|
60 } |
|
61 /*jslint white: true */ |
|
62 ); |
|
63 } |
|
64 ); |
|
65 } |
|
66 |
|
67 /* |
|
68 * == Configure Project == |
|
69 * |
|
70 */ |
|
71 |
|
72 // remember if form has been touched |
|
73 $('input,textarea,select').change( |
|
74 function () { |
|
75 if ($(this).attr('id') === 'id_project') { |
|
76 return; |
|
77 } |
|
78 $('form:first').data('touched', true); |
|
79 } |
|
80 ); |
|
81 |
|
82 // remember initially chosen project choice |
|
83 $('select#id_project').blur( |
|
84 function () { |
|
85 $(this).data('selected', $(this).find('option:first')); |
|
86 } |
|
87 ).change( |
|
88 function () { |
|
89 if ($('form:first').data('touched') === true) { |
|
90 // if form has been touched, send confirmation dialog |
|
91 var save_check = confirm(["Switching projects will lose unsaved ", |
|
92 "edits made to this survey."].join("")); |
|
93 if (!save_check) { |
|
94 $(this).data('selected').attr('selected', 'selected'); |
|
95 return false; |
|
96 } |
|
97 } |
|
98 |
|
99 if ($(this).val() !== 'None') { |
|
100 // redirect with new project GET param |
|
101 window.location = [window.location.href.split('?')[0], "?project=", |
|
102 $(this).val()].join(""); |
|
103 } |
|
104 } |
|
105 ); |
|
106 |
|
107 // insert project link after project select field |
|
108 $('div#project_link').insertAfter($('select#id_project')).show(); |
|
109 |
|
110 /* |
|
111 * == Survey Submission Handler == |
|
112 * |
|
113 */ |
|
114 |
|
115 // validate form |
|
116 $('input[type=submit]').bind( |
|
117 'click', |
|
118 function (e) { |
|
119 e.preventDefault(); |
|
120 |
|
121 // validate project and grade choice fields |
|
122 if ($('select#id_project') && |
|
123 $('select#id_project').val() === 'None') { |
|
124 return alert('Please Choose a Project'); |
|
125 } |
|
126 |
|
127 if ($('select#id_grade') && $('select#id_grade').val() === 'None') { |
|
128 return alert('Please Choose a Grade'); |
|
129 } |
|
130 $('form').trigger('submit'); |
|
131 |
|
132 } |
|
133 ); |
|
134 |
|
135 $('form').bind('submit', |
|
136 function () { |
|
137 $('input#id_s_html').val( |
|
138 widget.find('div#survey_options').remove().end().html() |
|
139 ); |
|
140 } |
|
141 ); |
|
142 |
|
143 /* |
|
144 * == Customize Comment Appearance == |
|
145 */ |
|
146 |
|
147 var comments = widget.find('td > .comment'); |
|
148 comments.each(function () { |
|
149 $(this).parents('tr:first') |
|
150 .prev().css({'margin-top': '-10px'}) |
|
151 .find('label').css({'font-size': '11px'}); |
|
152 }); |
|
153 |
|
154 }); |
|
155 }(jQuery)); |