1 define(function(require) {
5 var _ = require('libs/underscore'),
6 orderExerciseTemplate = require('libs/text!./order.xml');
8 var Item = function(node, exerciseNode) {
9 Object.defineProperty(this, 'text', {
12 var firstNode = node.contents()[0];
13 if(firstNode && firstNode.nodeType === Node.TEXT_NODE) {
14 return firstNode.getText();
20 this.exerciseNode = exerciseNode;
22 _.extend(Item.prototype, {
23 setText: function(text) {
25 var contents = this.node.contents();
26 if(contents.length === 1 && contents[0].nodeType === Node.TEXT_NODE) {
27 contents[0].setText(text);
29 contents.forEach(function(childNode) {
32 contents.append({text: text});
38 getAnswer: function() {
39 var toret = parseInt(this.node.getAttr('answer'), 10);
45 setAnswer: function(answer) {
46 answer = parseInt(answer, 10);
48 if(!_.isNumber(answer)) {
52 this.exerciseNode.object.getItems()
53 .sort(function(item1, item2) {
54 if(item1.getAnswer() > item2.getAnswer()) {
59 .some(function(item) {
60 if(item.getAnswer() === prev && !item.node.sameNode(this.node)) {
61 item.node.setAttr('answer', prev+1);
65 this.node.setAttr('answer', answer);
70 var isItemsList = function(node) {
71 return node.is('list.orderable');
75 var extension = {wlxmlClass: {'exercise.order': {
77 isContextRoot: function(node) {
78 return this.object.isItemNode(node) || this.sameNode(node);
80 getItems: function() {
84 this.contents().some(function(node) {
85 if(isItemsList(node)) {
87 .filter(function(node) {
88 return node.is('item.answer');
90 .forEach(function(node) {
91 toret.push(new Item(node, exerciseNode));
98 isItemNode: function(node) {
103 this.contents().some(function(node) {
104 if(isItemsList(node)) {
109 return list && list.sameNode(node.parent());
111 getDescription: function() {
113 this.contents().some(function(node) {
114 if(isItemsList(node)) {
123 addItem: function(text) {
125 this.contents().some(function(node) {
126 if(isItemsList(node)) {
127 var itemNode = this.document.createDocumentNode({tagName: 'div', attrs: {'class': 'item.answer', answer: this.object.getItems().length+1}});
128 toret = itemNode.append({text: text});
129 node.append(itemNode);
135 setDescription: function(text) {
136 this.contents().some(function(node) {
139 textNode = node.contents()[0];
140 if(!(textNode && textNode.nodeType === Node.TEXT_NODE)) {
141 node.prepend({text:text});
143 textNode.setText(text);
152 extension.document = {
154 edumedCreateExerciseNode: function(klass) {
156 return this.createDocumentNode(orderExerciseTemplate);