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 var exerciseNode = this.wlxmlNode;
57 this.createContainer(this.wlxmlNode.object.getDescription(), {
58 resetBackground: true,
59 manages: function(node, removedFrom) {
60 if(node.is('list.orderable')) {
63 return exerciseNode.sameNode(node.parent() || removedFrom); //!n.hasFollowingSibing(this.params.listnode);
65 dom: this.view.dom.find('.description')
70 onNodeAdded: function(event) {
71 var node = event.meta.node;
72 if(this.wlxmlNode.object.isItemNode(node)) {
76 onNodeAttrChange: function(event) {
77 var node = event.meta.node;
78 if(node.is('item.answer') && node.parent() && node.parent().is('list.orderable')) {
82 reloadView: function() {
83 this.view.clearItems();
84 this.wlxmlNode.object.getItems().forEach(function(item) {
85 this.view.addItem(item);
88 getVerticallyFirstTextElement: function() {
90 this.containers.some(function(container) {
91 toret = container.getVerticallyFirstTextElement();
98 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};