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