Support multiline text answers.
[platal.git] / htdocs / javascript / survey.js
index aad469f..80614a9 100644 (file)
  ***************************************************************************/
 
 (function($) {
-    var dispatchType(method) {
+    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]();
+                return this[name].apply(this, args);
             }
             return this;
         };
             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)
+                        $("#q_edit_base").tmpl({ qid: qid, type: type })
+                            .bindQuestion(type, qid)
                             .appendTo(form);
                     }
                     return true;
                 });
+            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;
         },
 
         /* 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));