Lot of improvements of the survey edition form.
[platal.git] / htdocs / javascript / survey.js
index 15ae19e..834b9e5 100644 (file)
  ***************************************************************************/
 
 (function($) {
+    $.extend({
+        questions: function() {
+            return $('.q_edit:not(#questions)');
+        },
+
+        lastQuestion: function() {
+            return $.questions().last();
+        },
+
+        renumberQuestions: function() {
+            var q = $.questions();
+            q.each(function(idx) {
+                var elt = $(this);
+                var old_id = elt.attr('id');
+                var new_id = 'q_edit[' + idx + ']';
+                if (old_id == new_id) {
+                    return;
+                }
+
+                var children = elt.children(':not(.q_edit)');
+                while (children.length > 0) {
+                    children.filter('.q_edit_label').text('Question ' + (idx + 1));
+                    children.children('[name*="' + old_id + '"]').each(function() {
+                        function replace(attr) {
+                            var cid = $(this).attr(attr);
+                            if (cid.substr(0, id.length) == old_id) {
+                               $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
+                            }
+                        }
+                        replace('id');
+                        replace('name');
+                    });
+                    children = children.children(':not(.q_edit)');
+                }
+                elt.attr('id', new_id);
+            });
+        },
+
+        debugPrintQuestions: function() {
+            var q = $.questions();
+            var str = '';
+            q.each(function() {
+                str += $(this).attr('id') + '\n';
+            });
+            alert(str);
+        }
+    });
+
     $.fn.extend({
         showQuestions: function(questions) {
             var data = $('#question_base').tmpl(questions);
             return this;
         },
 
-        addQuestion: function(id) {
+        /* Edition form */
+        isQuestion: function() {
+            return this.hasClass('q_edit');
+        },
+
+        isRootSection: function() {
+            return this.attr('id') == 'questions';
+        },
+
+        question: function() {
+            return this.isQuestion() ? this : this.parentQuestion();
+        },
+
+        qid: function() {
+            var question = this.question();
+            if (question.get(0) == undefined) {
+                return undefined;
+            }
+            var id = question.attr('id');
+            if (id.substr(0, 7) != 'q_edit[') {
+                return undefined;
+            }
+            if (id.charAt(id.length - 1) != ']') {
+                return undefined;
+            }
+            id = id.substr(7, id.length - 8);
+            return parseInt(id);
+        },
+
+        parentQuestion: function() {
+            return this.closest('.q_edit');
+        },
+
+        childrenContainer: function() {
+            var question = this.question();
+            return question.isRootSection() ? question : question.children('.q_edit_form').children();
+        },
+
+        childrenQuestion: function() {
+            return this.childrenContainer().children('.q_edit');
+        },
+
+        addQuestion: function() {
+            var id = $.lastQuestion().qid();
+            if (id == undefined) {
+                id = 0;
+            } else {
+                id++;
+            }
             var question = $("#q_edit_new").tmpl([{ qid: id } ]);
             question
                 .children('select')
                     $("#q_edit_base").tmpl([ { qid: id, type: type } ]).appendTo(form);
                     return true;
                 });
-            question.appendTo(this);
-            return this;
-        }
+            var dest = this.question();
+            var res = this.childrenContainer().children('.add_question').before(question);
+            $.renumberQuestions();
+            return res;
+        },
+
+        removeQuestion: function(force) {
+            var question = this.parentQuestion();
+            if (!force && question.children('.q_edit_form').children().children('.q_edit').length > 0) {
+                if (!alert('Vous avez demander la suppression d\'une section contenant des questions. '
+                         + 'Ces questions seront supprimées. Etes-vous sur de vouloir continuer ?')) {
+                    return this;
+                }
+            }
+            var res;
+            if (question.isRootSection()) {
+                res = question.empty();
+            } else {
+                res = question.remove();
+            }
+            $.renumberQuestions();
+            return res;
+        },
     });
 })(jQuery);