Merge remote branch 'origin/xorg/maint' into xorg/master
[platal.git] / htdocs / javascript / survey.js
1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21 (function($) {
22 var dispatchType = function(method) {
23 return function(type) {
24 var name = type + '_' + method;
25 var args = Array.prototype.slice.call(arguments, 1);
26 if ($.isFunction(this[name])) {
27 return this[name].apply(this, args);
28 }
29 return this;
30 };
31 };
32
33 $.extend({
34 questions: function() {
35 return $('.q_edit:not(#questions)');
36 },
37
38 lastQuestion: function() {
39 return $.questions().last();
40 },
41
42 renumberQuestions: function() {
43 var q = $.questions();
44 q.each(function(idx) {
45 var elt = $(this);
46 var old_id = elt.attr('id');
47 var new_id = 'q_edit[' + idx + ']';
48 var fixAttrs;
49
50 if (old_id === new_id) {
51 return;
52 }
53
54 fixAttrs = function(attr) {
55 elt.find('[' + attr + '^="' + old_id + '"]').each(function() {
56 var cid = $(this).attr(attr);
57 if (cid.startsWith(old_id)) {
58 $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
59 }
60 });
61 };
62 fixAttrs('id');
63 fixAttrs('name');
64 elt.children().children('.q_edit_label').text('Question ' + (idx + 1));
65 elt.attr('id', new_id);
66 });
67 },
68
69 debugPrintQuestions: function() {
70 var q = $.questions();
71 var str = '';
72 q.each(function() {
73 str += $(this).attr('id') + '\n';
74 });
75 alert(str);
76 }
77 });
78
79 $.fn.extend({
80 showQuestions: function(questions) {
81 var data = $('#question_base').tmpl(questions);
82 this.empty();
83 data.appendTo(this);
84 return this;
85 },
86
87 /* Edition form */
88 prepareQuestions: function(questions) {
89 var q, child;
90 for (q in questions) {
91 q = questions[q];
92 child = this.addQuestion(q);
93 if ($.isArray(q.children)) {
94 child.prepareQuestions(q.children);
95 }
96 }
97 return this;
98 },
99
100 isQuestion: function() {
101 return this.hasClass('q_edit');
102 },
103
104 isRootSection: function() {
105 return this.attr('id') === 'questions';
106 },
107
108 question: function() {
109 return this.isQuestion() ? this : this.parentQuestion();
110 },
111
112 questionForm: function() {
113 return this.question().children('.q_edit_form');
114 },
115
116 qid: function() {
117 var question = this.question();
118 if (typeof question.get(0) === 'undefined') {
119 return;
120 }
121 var id = question.attr('id');
122 if (id.substr(0, 7) !== 'q_edit[') {
123 return;
124 }
125 if (id.charAt(id.length - 1) !== ']') {
126 return;
127 }
128 id = id.substr(7, id.length - 8);
129 return parseInt(id, 10);
130 },
131
132 parentQuestion: function() {
133 return this.parent().closest('.q_edit');
134 },
135
136 childrenContainer: function() {
137 var question = this.question();
138 return question.isRootSection() ? question : question.questionForm().children();
139 },
140
141 childrenQuestion: function() {
142 return this.childrenContainer().children('.q_edit');
143 },
144
145 addQuestion: function(q) {
146 var id = $.lastQuestion().qid();
147 if (!id) {
148 id = 0;
149 } else {
150 id++;
151 }
152 if (!q) {
153 q = { qid: id };
154 }
155 console.log(q);
156 var question = $("#q_edit_new").tmpl(q);
157 var type = question
158 .find('select[name$="[type]"]')
159 .change(function () {
160 var type = $(this).val();
161 var form = question.children('.q_edit_form');
162 var qid = $(this).qid();
163 form.empty();
164 if (type) {
165 $("#q_edit_base").tmpl({ qid: qid, type: type })
166 .bindQuestion(type, qid)
167 .appendTo(form);
168 }
169 return true;
170 });
171 if (type.val()) {
172 question.children('.q_edit_form')
173 .bindQuestion(type.val(), q.qid, q)
174 }
175 this.childrenContainer().children('.add_question').before(question);
176 $.renumberQuestions();
177 return question;
178 },
179
180 bindQuestion: dispatchType('bindQuestion'),
181
182 removeQuestion: function(force) {
183 var question = this.parentQuestion();
184 if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
185 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
186 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
187 return this;
188 }
189 }
190 var res;
191 if (question.isRootSection()) {
192 res = question.empty();
193 } else {
194 res = question.remove();
195 }
196 $.renumberQuestions();
197 return res;
198 },
199
200 buildParentsQuestions: function() {
201 var $this = $(this);
202 $.questions().each(function() {
203 var parent = $(this).parentQuestion();
204 if (!parent.isRootSection()) {
205 $('<input>', {
206 type: 'hidden',
207 name: 'q_edit[' + $(this).qid() + '][parent]',
208 value: parent.qid()
209 }).appendTo($this);
210 }
211 });
212 return $this;
213 },
214
215 /* Multiple choices questions */
216 multiple_selectSubtype: function() {
217 return this.find('select[name$="[subtype]"]');
218 },
219
220 multiple_bindQuestion: function(id, parameters) {
221 var $question = this;
222 var answer;
223 var value;
224 this.multiple_selectSubtype()
225 .assertLength(1)
226 .change(function() {
227 $question.find('.q_edit_answer_box')
228 .empty()
229 .append($('<input>', {
230 type: $(this).val(),
231 disabled: "disabled"
232 }));
233 });
234 if (parameters) {
235 for (answer = 0; answer < parameters.answers.length; answer++) {
236 this.multiple_addAnswer(parameters.answers[answer]);
237 }
238 }
239 return this;
240 },
241
242 multiple_addAnswer: function(value) {
243 var question = this.question();
244 var answer = $("#q_edit_multiple_answer").tmpl({ qid: question.qid(), value: value });
245 question.childrenContainer().children('.add_answer').before(answer);
246 question.multiple_selectSubtype().change();
247 return answer;
248 },
249
250 multiple_removeAnswer: function() {
251 return this.parent().remove();
252 }
253 });
254 }(jQuery));
255
256
257 $(function() {
258 $(".datepicker").datepicker({
259 hideIfNoPrevNext: true,
260 minDate: new Date()
261 });
262 });
263
264
265 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: