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