1 define(function(require) {
5 var _ = require('libs/underscore'),
7 order: require('libs/text!./order.xml'),
8 choice: require('libs/text!./choice.xml'),
9 'choice.single': require('libs/text!./choiceSingle.xml'),
10 'choice.true-or-false': require('libs/text!./choiceTrueOrFalse.xml'),
11 gap: require('libs/text!./gaps.xml'),
12 replace: require('libs/text!./replace.xml')
15 var Item = function(node, exerciseNode) {
16 Object.defineProperty(this, 'text', {
19 var firstNode = node.contents()[0];
20 if(firstNode && firstNode.nodeType === Node.TEXT_NODE) {
21 return firstNode.getText();
27 this.exerciseNode = exerciseNode;
29 _.extend(Item.prototype, {
30 setText: function(text) {
32 var contents = this.node.contents();
33 if(contents.length === 1 && contents[0].nodeType === Node.TEXT_NODE) {
34 contents[0].setText(text);
36 contents.forEach(function(childNode) {
39 contents.append({text: text});
45 getAnswer: function() {
46 var toret = parseInt(this.node.getAttr('answer'), 10);
52 setAnswer: function(answer) {
53 answer = parseInt(answer, 10);
55 if(!_.isNumber(answer)) {
59 this.exerciseNode.object.getItems()
60 .sort(function(item1, item2) {
61 if(item1.getAnswer() > item2.getAnswer()) {
66 .some(function(item) {
67 if(item.getAnswer() === prev && !item.node.sameNode(this.node)) {
68 item.node.setAttr('answer', prev+1);
72 this.node.setAttr('answer', answer);
77 var isItemsList = function(node) {
78 return node.is('list.orderable');
82 var extension = {wlxmlClass: {'exercise.order': {
84 isContextRoot: function(node) {
85 return this.object.isItemNode(node) || this.sameNode(node);
87 getItems: function() {
91 this.contents().some(function(node) {
92 if(isItemsList(node)) {
94 .filter(function(node) {
95 return node.is('item.answer');
97 .forEach(function(node) {
98 toret.push(new Item(node, exerciseNode));
105 isItemNode: function(node) {
110 this.contents().some(function(node) {
111 if(isItemsList(node)) {
116 return list && list.sameNode(node.parent());
118 getDescription: function() {
120 this.contents().some(function(node) {
121 if(isItemsList(node)) {
130 addItem: function(text) {
132 this.contents().some(function(node) {
133 if(isItemsList(node)) {
134 var itemNode = this.document.createDocumentNode({tagName: 'div', attrs: {'class': 'item.answer', answer: this.object.getItems().length+1}});
135 toret = itemNode.append({text: text});
136 node.append(itemNode);
142 setDescription: function(text) {
143 this.contents().some(function(node) {
146 textNode = node.contents()[0];
147 if(!(textNode && textNode.nodeType === Node.TEXT_NODE)) {
148 node.prepend({text:text});
150 textNode.setText(text);
159 var choiceMethods = {
160 isContextRoot: function(node) {
161 return this.object.isChoiceList(node.parent()) || this.sameNode(node);
163 getChoiceList: function() {
164 return this.contents()
165 .filter(function(n) { return this.object.isChoiceList(n); }.bind(this))[0];
167 isChoiceList: function(node) {
168 return node.is('list') && this.sameNode(node.parent());
170 isChoiceListItem: function(node) {
171 return this.object.isChoiceList(node.parent()) && node.is('item.answer');
175 extension.wlxmlClass['exercise.choice'] = {
177 setAnswer: function(itemNode, answer) {
178 if(!this.object.isChoiceListItem(itemNode)) {
181 itemNode.setAttr('answer', answer ? 'true' : 'false');
184 methods: choiceMethods
187 extension.wlxmlClass['exercise.choice.single'] = {
189 markAsAnswer: function(itemNode) {
190 if(!this.object.isChoiceListItem(itemNode)) {
193 this.object.getChoiceList().contents()
194 .filter(function(node) { return node.is('item.answer'); })
195 .forEach(function(node) {
196 node.setAttr('answer', node.sameNode(itemNode) ? 'true' : 'false');
200 methods: choiceMethods
203 extension.wlxmlClass['exercise.choice.true-or-false'] = {
205 setAnswer: function(itemNode, answer) {
206 if(!this.object.isChoiceListItem(itemNode)) {
209 itemNode.setAttr('answer', answer ? 'true' : 'false');
212 methods: choiceMethods
215 extension.wlxmlClass['exercise.gap'] = extension.wlxmlClass['exercise.replace'] = {
217 isContextRoot: function(node) {
218 return this.sameNode(node);
223 extension.document = {
225 edumedCreateExerciseNode: function(klass) {
227 return this.createDocumentNode(templates[klass]);