a81155e1b07105a524c1af92e5f7cb2f0008a217
[fnpeditor.git] / src / editor / plugins / core / edumed / order / element.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var _ = require('libs/underscore');
8
9 var elementBase = require('plugins/core/edumed/elementBase'),
10     OrderExerciseView = require('./view');
11
12 var OrderExerciceElement = Object.create(elementBase);
13 _.extend(OrderExerciceElement, {
14     init: function() {
15         elementBase.init.call(this);
16         
17         this.view = new OrderExerciseView(this, this.wlxmlNode);
18         this._container().append(this.view.dom);
19
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});
25             }.bind(this), {
26                 metadata: {
27                     description: gettext('Add item to exercise')
28                 },
29                 success: function(ret) {
30                     this.canvas.select(ret);
31                 }.bind(this)
32             });
33         }.bind(this));
34
35         this.view.on('moveAnswer', function(sourceItem, targetItem, where) {
36             this.wlxmlNode.document.transaction(function() {
37                 sourceItem.setAnswer(targetItem.getAnswer() + (where === 'before' ? 0 : 1));
38             }, {
39                 metadata: {
40                     description: gettext('Change solution')
41                 }
42             });
43         }.bind(this));
44
45         this.view.on('moveItem', function(sourceItem, targetItem, where) {
46             this.wlxmlNode.document.transaction(function() {
47                 targetItem.node[where](sourceItem.node);
48             }, {
49                 metadata: {
50                     description: gettext('Change order')
51                 }
52             });
53             
54         }.bind(this));
55
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')) {
61                     return false;
62                 }
63                 return exerciseNode.sameNode(node.parent() || removedFrom); //!n.hasFollowingSibing(this.params.listnode);    
64             },
65             dom: this.view.dom.find('.description')
66         });
67
68         this.reloadView();
69     },
70     onNodeAdded: function(event) {
71         var node = event.meta.node;
72         if(this.wlxmlNode.object.isItemNode(node)) {
73             this.reloadView();
74         }
75     },
76     onNodeAttrChange: function(event) {
77         var node = event.meta.node;
78         if(node.is('item.answer') && node.parent() && node.parent().is('list.orderable')) {
79             this.reloadView();
80         }
81     },
82     reloadView: function() {
83         this.view.clearItems();
84         this.wlxmlNode.object.getItems().forEach(function(item) {
85             this.view.addItem(item);
86         }.bind(this));
87     },
88     getVerticallyFirstTextElement: function() {
89         var toret;
90         this.containers.some(function(container) {
91             toret = container.getVerticallyFirstTextElement();
92             return !!toret;
93         });
94         return toret;
95     }
96 });
97
98 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};
99
100 });