editor: core plugin - edumed - first approach to all three choice exercises
[fnpeditor.git] / src / editor / plugins / core / edumed / choice / choiceSingle.js
diff --git a/src/editor/plugins/core/edumed/choice/choiceSingle.js b/src/editor/plugins/core/edumed/choice/choiceSingle.js
new file mode 100644 (file)
index 0000000..9346363
--- /dev/null
@@ -0,0 +1,56 @@
+define(function(require) {
+    
+'use strict';
+
+/* globals gettext */
+
+var $ = require('libs/jquery'),
+    _ = require('libs/underscore'),
+    choiceBase = require('./choiceBase'),
+    ListView = require('./list');
+
+
+var choiceSingle = Object.create(choiceBase);
+_.extend(choiceSingle, {
+    type: 'single',
+    init: function() {
+        this._comboName = _.uniqueId('edumed_exercise_hash_');
+        choiceBase.init.call(this);
+    },
+    createListView: function(listNode) {
+        var el = this;
+        return new ListView(this, listNode, {
+            onItemViewAdded: function(itemView) {
+                var radiobox = new RadioView(itemView.node.getAttr('answer') === 'true', el._comboName, function() {
+                    itemView.node.document.transaction(function() {
+                        itemView.node.getParent('exercise.choice').object.markAsAnswer(itemView.node);
+                    }, {
+                        metadata: {
+                            description: gettext('Change answer')
+                        }
+                    });
+                    
+                });
+                itemView.addPrefixView(radiobox);
+            }
+        });
+    },
+    onNodeAttrChange: function(event) {
+        if(this.listView.listNode.sameNode(event.meta.node.parent())) {
+            this.listView.getItemView(event.meta.node).prefixView.dom.attr('checked', event.meta.newVal === 'true');
+        }
+    }
+});
+
+var RadioView = function(checked, name, onValueChange) {
+    this.dom = $('<input type="radio" style="float: left; margin-right: 40px;">')
+            .attr('checked', checked)
+            .attr('name', name);
+    this.dom.on('change', function() {
+        onValueChange(this.checked);
+    });
+};
+
+return choiceSingle;
+
+});
\ No newline at end of file