1 define(function(require) {
7 var _ = require('libs/underscore');
9 var elementBase = require('plugins/core/edumed/elementBase'),
10 OrderExerciseView = require('./view');
12 var OrderExerciceElement = Object.create(elementBase);
13 _.extend(OrderExerciceElement, {
15 elementBase.init.call(this);
17 this.view = new OrderExerciseView(this, this.wlxmlNode);
18 this._container().append(this.view.dom);
20 this.view.on('newItemRequested', function() {
21 this.wlxmlNode.document.transaction(function() {
22 var textNode = this.wlxmlNode.object.addItem('');
23 var doc = this.wlxmlNode.document;
24 return doc.createFragment(doc.CaretFragment, {node: textNode, offset:0});
27 description: gettext('Add item to exercise')
29 success: function(ret) {
30 this.canvas.select(ret);
35 this.view.on('moveAnswer', function(sourceItem, targetItem, where) {
36 this.wlxmlNode.document.transaction(function() {
37 sourceItem.setAnswer(targetItem.getAnswer() + (where === 'before' ? 0 : 1));
40 description: gettext('Change solution')
45 this.view.on('moveItem', function(sourceItem, targetItem, where) {
46 this.wlxmlNode.document.transaction(function() {
47 targetItem.node[where](sourceItem.node);
50 description: gettext('Change order')
56 this.view.on('shuffleItems', function () {
57 this.wlxmlNode.document.transaction(function() {
61 description: gettext('Set random order')
66 var exerciseNode = this.wlxmlNode;
67 this.createContainer(this.wlxmlNode.object.getDescription(), {
68 resetBackground: true,
69 manages: function(node, removedFrom) {
70 if(node.is('list.orderable') || (removedFrom && removedFrom.is('list.orderable'))) {
73 return exerciseNode.sameNode(node.parent() || removedFrom); //!n.hasFollowingSibing(this.params.listnode);
75 dom: this.view.dom.find('.description')
80 onNodeAdded: function(event) {
81 var node = event.meta.node;
82 if(this.wlxmlNode.object.isItemNode(node)) {
86 onNodeAttrChange: function(event) {
87 var node = event.meta.node;
88 if(node.is('item.answer') && node.parent() && node.parent().is('list.orderable')) {
92 onNodeDetached: function(event) {
93 var node = event.meta.node;
94 if(node.is('item.answer') && event.meta.parent && event.meta.parent.is('list.orderable')) {
99 reloadView: function() {
100 this.view.clearItems();
101 this.wlxmlNode.object.getItems().forEach(function(item) {
102 this.view.addItem(item);
105 updateNumbers: function () {
107 this.view.sortedItemViews().forEach(function (itemView) {
108 itemView.item.setAnswer(answer);
112 shuffleItems: function () {
113 var items = this.wlxmlNode.object.getItems();
114 var currentIndex = items.length, randomIndex;
115 while (0 !== currentIndex) {
116 randomIndex = Math.floor(Math.random() * currentIndex);
118 items[currentIndex].node.after(items[randomIndex].node);
122 getVerticallyFirstTextElement: function() {
124 this.containers.some(function(container) {
125 toret = container.getVerticallyFirstTextElement();
132 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};