smartxml: removing node attributes by setting undefined as their value
[fnpeditor.git] / src / wlxml / extensions / edumed / edumed.js
index 3176ae0..ca29ad3 100644 (file)
@@ -3,7 +3,14 @@ define(function(require) {
 'use strict';
 
 var _ = require('libs/underscore'),
-    orderExerciseTemplate = require('libs/text!./order.xml');
+    templates = {
+        order: require('libs/text!./order.xml'),
+        choice: require('libs/text!./choice.xml'),
+        'choice.single': require('libs/text!./choiceSingle.xml'),
+        'choice.true-or-false': require('libs/text!./choiceTrueOrFalse.xml'),
+        gap: require('libs/text!./gaps.xml'),
+        replace: require('libs/text!./replace.xml')
+    };
 
 var Item = function(node, exerciseNode) {
     Object.defineProperty(this, 'text', {
@@ -149,11 +156,75 @@ var extension = {wlxmlClass: {'exercise.order': {
     }
 }}};
 
+var choiceMethods = {
+    isContextRoot: function(node) {
+        return this.object.isChoiceList(node.parent()) || this.sameNode(node);
+    },
+    getChoiceList: function() {
+        return this.contents()
+            .filter(function(n) { return this.object.isChoiceList(n); }.bind(this))[0];
+    },
+    isChoiceList: function(node) {
+        return node.is('list') && this.sameNode(node.parent());
+    },
+    isChoiceListItem: function(node) {
+        return this.object.isChoiceList(node.parent()) && node.is('item.answer');
+    }
+};
+
+extension.wlxmlClass['exercise.choice'] = {
+    transformations: {
+        setAnswer: function(itemNode, answer) {
+            if(!this.object.isChoiceListItem(itemNode)) {
+                return;
+            }
+            itemNode.setAttr('answer', answer ? 'true' : 'false');
+        }
+    },
+    methods: choiceMethods
+};
+
+extension.wlxmlClass['exercise.choice.single'] = {
+    transformations: {
+        markAsAnswer: function(itemNode) {
+            if(!this.object.isChoiceListItem(itemNode)) {
+                return;
+            }
+            this.object.getChoiceList().contents()
+                .filter(function(node) { return node.is('item.answer'); })
+                .forEach(function(node) {
+                    node.setAttr('answer', node.sameNode(itemNode) ? 'true' : 'false');
+                });
+        }
+    },
+    methods: choiceMethods
+};
+
+extension.wlxmlClass['exercise.choice.true-or-false'] = {
+    transformations: {
+        setAnswer: function(itemNode, answer) {
+            if(!this.object.isChoiceListItem(itemNode)) {
+                return;
+            }
+            itemNode.setAttr('answer', answer ? 'true' : 'false');
+        }
+    },
+    methods: choiceMethods
+};
+
+extension.wlxmlClass['exercise.gap'] = extension.wlxmlClass['exercise.replace'] = {
+    methods: {
+        isContextRoot: function(node) {
+            return this.sameNode(node);
+        }
+    }
+};
+
 extension.document = {
     methods: {
          edumedCreateExerciseNode: function(klass) {
             void(klass);
-            return this.createDocumentNode(orderExerciseTemplate);
+            return this.createDocumentNode(templates[klass]);
          }
     }
 };