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