1 define(function(require) {
7 var _ = require('libs/underscore');
9 var documentElement = require('modules/documentCanvas/canvas/documentElement'),
10 OrderExerciseView = require('./view');
12 var OrderExerciceElement = Object.create(documentElement.DocumentNodeElement.prototype);
13 _.extend(OrderExerciceElement, {
15 documentElement.DocumentNodeElement.prototype.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 manages: function(node, removedFrom) {
59 if(node.is('list.orderable')) {
62 return exerciseNode.sameNode(node.parent() || removedFrom); //!n.hasFollowingSibing(this.params.listnode);
64 dom: this.view.dom.find('.description')
69 onNodeAdded: function(event) {
70 var node = event.meta.node;
71 if(this.wlxmlNode.object.isItemNode(node)) {
75 onNodeAttrChange: function(event) {
76 var node = event.meta.node;
77 if(node.is('item.answer') && node.parent() && node.parent().is('list.orderable')) {
81 reloadView: function() {
82 this.view.clearItems();
83 this.wlxmlNode.object.getItems().forEach(function(item) {
84 this.view.addItem(item);
87 getVerticallyFirstTextElement: function() {
89 this.containers.some(function(container) {
90 toret = container.getVerticallyFirstTextElement();
97 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};