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') || (removedFrom && removedFrom.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 onNodeDetached: function(event) {
83 var node = event.meta.node;
84 if(node.is('item.answer') && event.meta.parent && event.meta.parent.is('list.orderable')) {
88 reloadView: function() {
89 this.view.clearItems();
90 this.wlxmlNode.object.getItems().forEach(function(item) {
91 this.view.addItem(item);
94 getVerticallyFirstTextElement: function() {
96 this.containers.some(function(container) {
97 toret = container.getVerticallyFirstTextElement();
104 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};