Support multiline text answers.
[platal.git] / htdocs / javascript / survey.js
index 9b3bfee..80614a9 100644 (file)
  ***************************************************************************/
 
 (function($) {
+    var dispatchType = function(method) {
+        return function(type) {
+            var name = type + '_' + method;
+            var args = Array.prototype.slice.call(arguments, 1);
+            if ($.isFunction(this[name])) {
+                return this[name].apply(this, args);
+            }
+            return this;
+        };
+    };
+
     $.extend({
         questions: function() {
             return $('.q_edit:not(#questions)');
                 var elt = $(this);
                 var old_id = elt.attr('id');
                 var new_id = 'q_edit[' + idx + ']';
-                if (old_id == new_id) {
+                var fixAttrs;
+
+                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));
-                            }
+                fixAttrs = function(attr) {
+                    elt.find('[' + attr + '^="' + old_id + '"]').each(function() {
+                        var cid = $(this).attr(attr);
+                        if (cid.startsWith(old_id)) {
+                            $(this).attr(attr, new_id + cid.substring(old_id.length, cid.length));
                         }
-                        replace('id');
-                        replace('name');
                     });
-                    children = children.children(':not(.q_edit)');
-                }
+                };
+                fixAttrs('id');
+                fixAttrs('name');
+                elt.children().children('.q_edit_label').text('Question ' + (idx + 1));
                 elt.attr('id', new_id);
             });
         },
 
         /* Edition form */
         prepareQuestions: function(questions) {
-            for (var q in questions) {
-                var q = questions[q];
-                var child = this.addQuestion(q);
+            var q, child;
+            for (q in questions) {
+                q = questions[q];
+                child = this.addQuestion(q);
                 if ($.isArray(q.children)) {
                     child.prepareQuestions(q.children);
                 }
         },
 
         isRootSection: function() {
-            return this.attr('id') == 'questions';
+            return this.attr('id') === 'questions';
         },
 
         question: function() {
 
         qid: function() {
             var question = this.question();
-            if (question.get(0) == undefined) {
-                return undefined;
+            if (typeof question.get(0) === 'undefined') {
+                return;
             }
             var id = question.attr('id');
-            if (id.substr(0, 7) != 'q_edit[') {
-                return undefined;
+            if (id.substr(0, 7) !== 'q_edit[') {
+                return;
             }
-            if (id.charAt(id.length - 1) != ']') {
-                return undefined;
+            if (id.charAt(id.length - 1) !== ']') {
+                return;
             }
             id = id.substr(7, id.length - 8);
-            return parseInt(id);
+            return parseInt(id, 10);
         },
 
         parentQuestion: function() {
 
         addQuestion: function(q) {
             var id = $.lastQuestion().qid();
-            if (id == undefined) {
+            if (!id) {
                 id = 0;
-            } else {
+            } else {
                 id++;
             }
-            if (q == null) {
-                q = { qid: id }
+            if (!q) {
+                q = { qid: id };
             }
+            console.log(q);
             var question = $("#q_edit_new").tmpl(q);
-            question
-                .children('select')
+            var type = question
+                .find('select[name$="[type]"]')
                 .change(function () {
                     var type = $(this).val();
                     var form = question.children('.q_edit_form');
+                    var qid = $(this).qid();
                     form.empty();
-                    if (type != '') {
-                        $("#q_edit_base").tmpl({ qid: id, type: type })
-                            .bindQuestion(type)
+                    if (type) {
+                        $("#q_edit_base").tmpl({ qid: qid, type: type })
+                            .bindQuestion(type, qid)
                             .appendTo(form);
                     }
                     return true;
                 });
-            var dest = this.question();
-            var res = this.childrenContainer().children('.add_question').before(question);
+            if (type.val()) {
+                question.children('.q_edit_form')
+                        .bindQuestion(type.val(), q.qid, q.parameters)
+            }
+            this.childrenContainer().children('.add_question').before(question);
             $.renumberQuestions();
             return question;
         },
 
-        bindQuestion: function(type) {
-            var name = type + '_bindQuestion';
-            if ($.isFunction(this[name])) {
-                this[name]();
-            }
-            return this;
-        },
+        bindQuestion: dispatchType('bindQuestion'),
 
         removeQuestion: function(force) {
             var question = this.parentQuestion();
         },
 
         /* Multiple choices questions */
-        multiple_bindQuestion: function() {
+        multiple_selectSubtype: function() {
+            return this.find('select[name$="[subtype]"]');
+        },
+
+        multiple_bindQuestion: function(id, parameters) {
+            var $question = this;
+            var answer;
+            var value;
+            this.multiple_selectSubtype()
+                .assertLength(1)
+                .change(function() {
+                    $question.find('.q_edit_answer_box')
+                        .empty()
+                        .append($('<input>', {
+                            type: $(this).val(),
+                            disabled: "disabled"
+                        }));
+                });
+            if (parameters) {
+                for (answer = 0; answer < parameters.answers.length; answer++) {
+                    this.multiple_addAnswer(parameters.answers[answer]);
+                }
+            }
             return this;
         },
 
-        multiple_addAnswer: function() {
-            var answer = $("#q_edit_multiple_answer").tmpl({ qid: this.qid() });
-            this.childrenContainer().children('.add_answer').before(answer);
+        multiple_addAnswer: function(value) {
+            var question = this.question();
+            var answer = $("#q_edit_multiple_answer").tmpl({ qid: question.qid(), value: value });
+            question.childrenContainer().children('.add_answer').before(answer);
+            question.multiple_selectSubtype().change();
             return answer;
+        },
+
+        multiple_removeAnswer: function() {
+            return this.parent().remove();
         }
     });
-})(jQuery);
+}(jQuery));
 
 
 $(function() {