Merge branch 'xorg/1.0.2/master' 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 $.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
70 $.fn.extend({
71 showQuestions: function(questions) {
72 var data = $('#question_base').tmpl(questions);
73 this.empty();
74 data.appendTo(this);
75 return this;
76 },
77
78 /* Edition form */
79 prepareQuestions: function(questions) {
80 var data = $('#q_edit_new').tmpl(questions);
81 data.prependTo(this);
82 return this;
83 },
84
85 isQuestion: function() {
86 return this.hasClass('q_edit');
87 },
88
89 isRootSection: function() {
90 return this.attr('id') == 'questions';
91 },
92
93 question: function() {
94 return this.isQuestion() ? this : this.parentQuestion();
95 },
96
97 qid: function() {
98 var question = this.question();
99 if (question.get(0) == undefined) {
100 return undefined;
101 }
102 var id = question.attr('id');
103 if (id.substr(0, 7) != 'q_edit[') {
104 return undefined;
105 }
106 if (id.charAt(id.length - 1) != ']') {
107 return undefined;
108 }
109 id = id.substr(7, id.length - 8);
110 return parseInt(id);
111 },
112
113 parentQuestion: function() {
114 return this.parent().closest('.q_edit');
115 },
116
117 childrenContainer: function() {
118 var question = this.question();
119 return question.isRootSection() ? question : question.children('.q_edit_form').children();
120 },
121
122 childrenQuestion: function() {
123 return this.childrenContainer().children('.q_edit');
124 },
125
126 addQuestion: function() {
127 var id = $.lastQuestion().qid();
128 if (id == undefined) {
129 id = 0;
130 else {
131 id++;
132 }
133 var question = $("#q_edit_new").tmpl([{ qid: id } ]);
134 question
135 .children('select')
136 .change(function () {
137 var type = $(this).val();
138 var form = question.children('.q_edit_form');
139 form.empty();
140 $("#q_edit_base").tmpl([ { qid: id, type: type } ]).appendTo(form);
141 return true;
142 });
143 var dest = this.question();
144 var res = this.childrenContainer().children('.add_question').before(question);
145 $.renumberQuestions();
146 return res;
147 },
148
149 removeQuestion: function(force) {
150 var question = this.parentQuestion();
151 if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
152 if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
153 + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
154 return this;
155 }
156 }
157 var res;
158 if (question.isRootSection()) {
159 res = question.empty();
160 } else {
161 res = question.remove();
162 }
163 $.renumberQuestions();
164 return res;
165 },
166
167 buildParentsQuestions: function() {
168 var $this = $(this);
169 $.questions().each(function() {
170 var parent = $(this).parentQuestion();
171 if (!parent.isRootSection()) {
172 $('<input>', {
173 type: 'hidden',
174 name: 'q_edit[' + $(this).qid() + '][parent]',
175 value: parent.qid()
176 }).appendTo($this);
177 }
178 });
179 return $this;
180 }
181 });
182 })(jQuery);
183
184
185 $(function() {
186 $(".datepicker").datepicker({
187 hideIfNoPrevNext: true,
188 minDate: new Date()
189 });
190 });
191
192
193 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: