aad469f992cbd4f9b5c8270ad7b7fa35b1f455ec
[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(method) {
23 return function(type) {
24 var name = type + '_' + method;
25 if ($.isFunction(this[name])) {
26 return this[name]();
27 }
28 return this;
29 };
30 };
31
32 $.extend({
33 questions: function() {
34 return $('.q_edit:not(#questions)');
35 },
36
37 lastQuestion: function() {
38 return $.questions().last();
39 },
40
41 renumberQuestions: function() {
42 var q = $.questions();
43 q.each(function(idx) {
44 var elt = $(this);
45 var old_id = elt.attr('id');
46 var new_id = 'q_edit[' + idx + ']';
47 var fixAttrs;
48
49 if (old_id === new_id) {
50 return;
51 }
52
53 fixAttrs = function(attr) {
54 elt.find('[' + attr + '^="' + old_id + '"]').each(function() {
55 var cid = $(this).attr(attr);
56 if (cid.startsWith(old_id)) {
57 $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
58 }
59 });
60 };
61 fixAttrs('id');
62 fixAttrs('name');
63 elt.children().children('.q_edit_label').text('Question ' + (idx + 1));
64 elt.attr('id', new_id);
65 });
66 },
67
68 debugPrintQuestions: function() {
69 var q = $.questions();
70 var str = '';
71 q.each(function() {
72 str += $(this).attr('id') + '\n';
73 });
74 alert(str);
75 }
76 });
77
78 $.fn.extend({
79 showQuestions: function(questions) {
80 var data = $('#question_base').tmpl(questions);
81 this.empty();
82 data.appendTo(this);
83 return this;
84 },
85
86 /* Edition form */
87 prepareQuestions: function(questions) {
88 var q, child;
89 for (q in questions) {
90 q = questions[q];
91 child = this.addQuestion(q);
92 if ($.isArray(q.children)) {
93 child.prepareQuestions(q.children);
94 }
95 }
96 return this;
97 },
98
99 isQuestion: function() {
100 return this.hasClass('q_edit');
101 },
102
103 isRootSection: function() {
104 return this.attr('id') === 'questions';
105 },
106
107 question: function() {
108 return this.isQuestion() ? this : this.parentQuestion();
109 },
110
111 questionForm: function() {
112 return this.question().children('.q_edit_form');
113 },
114
115 qid: function() {
116 var question = this.question();
117 if (typeof question.get(0) === 'undefined') {
118 return;
119 }
120 var id = question.attr('id');
121 if (id.substr(0, 7) !== 'q_edit[') {
122 return;
123 }
124 if (id.charAt(id.length - 1) !== ']') {
125 return;
126 }
127 id = id.substr(7, id.length - 8);
128 return parseInt(id, 10);
129 },
130
131 parentQuestion: function() {
132 return this.parent().closest('.q_edit');
133 },
134
135 childrenContainer: function() {
136 var question = this.question();
137 return question.isRootSection() ? question : question.questionForm().children();
138 },
139
140 childrenQuestion: function() {
141 return this.childrenContainer().children('.q_edit');
142 },
143
144 addQuestion: function(q) {
145 var id = $.lastQuestion().qid();
146 if (!id) {
147 id = 0;
148 } else {
149 id++;
150 }
151 if (!q) {
152 q = { qid: id };
153 }
154 var question = $("#q_edit_new").tmpl(q);
155 question
156 .children('select')
157 .change(function () {
158 var type = $(this).val();
159 var form = question.children('.q_edit_form');
160 form.empty();
161 if (type) {
162 $("#q_edit_base").tmpl({ qid: id, type: type })
163 .bindQuestion(type)
164 .appendTo(form);
165 }
166 return true;
167 });
168 this.childrenContainer().children('.add_question').before(question);
169 $.renumberQuestions();
170 return question;
171 },
172
173 bindQuestion: dispatchType('bindQuestion'),
174
175 removeQuestion: function(force) {
176 var question = this.parentQuestion();
177 if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
178 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
179 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
180 return this;
181 }
182 }
183 var res;
184 if (question.isRootSection()) {
185 res = question.empty();
186 } else {
187 res = question.remove();
188 }
189 $.renumberQuestions();
190 return res;
191 },
192
193 buildParentsQuestions: function() {
194 var $this = $(this);
195 $.questions().each(function() {
196 var parent = $(this).parentQuestion();
197 if (!parent.isRootSection()) {
198 $('<input>', {
199 type: 'hidden',
200 name: 'q_edit[' + $(this).qid() + '][parent]',
201 value: parent.qid()
202 }).appendTo($this);
203 }
204 });
205 return $this;
206 },
207
208 /* Multiple choices questions */
209 multiple_bindQuestion: function() {
210 return this;
211 },
212
213 multiple_addAnswer: function() {
214 var answer = $("#q_edit_multiple_answer").tmpl({ qid: this.qid() });
215 this.childrenContainer().children('.add_answer').before(answer);
216 return answer;
217 }
218 });
219 }(jQuery));
220
221
222 $(function() {
223 $(".datepicker").datepicker({
224 hideIfNoPrevNext: true,
225 minDate: new Date()
226 });
227 });
228
229
230 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: