b1131999634c169df5a6ddef66aef367642a7471
[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             manages: function(node, removedFrom) {
59                 if(node.is('list.orderable')) {
60                     return false;
61                 }
62                 return exerciseNode.sameNode(node.parent() || removedFrom); //!n.hasFollowingSibing(this.params.listnode);    
63             },
64             dom: this.view.dom.find('.description')
65         });
66
67         this.reloadView();
68     },
69     onNodeAdded: function(event) {
70         var node = event.meta.node;
71         if(this.wlxmlNode.object.isItemNode(node)) {
72             this.reloadView();
73         }
74     },
75     onNodeAttrChange: function(event) {
76         var node = event.meta.node;
77         if(node.is('item.answer') && node.parent() && node.parent().is('list.orderable')) {
78             this.reloadView();
79         }
80     },
81     reloadView: function() {
82         this.view.clearItems();
83         this.wlxmlNode.object.getItems().forEach(function(item) {
84             this.view.addItem(item);
85         }.bind(this));
86     },
87     getVerticallyFirstTextElement: function() {
88         var toret;
89         this.containers.some(function(container) {
90             toret = container.getVerticallyFirstTextElement();
91             return !!toret;
92         });
93         return toret;
94     }
95 });
96
97 return {tag: 'div', klass: 'exercise.order', prototype: OrderExerciceElement};
98
99 });