3f12f265afa6f6929f61b20cf8f3042664a399fb
[fnpeditor.git] / src / editor / plugins / core / edumed / replace / element.js
1 define(function(require) {
2     
3 'use strict';
4
5
6 var $ = require('libs/jquery'),
7     _ = require('libs/underscore'),
8     documentElement = require('modules/documentCanvas/canvas/documentElement'),
9     genericElement = require('modules/documentCanvas/canvas/genericElement'),
10     viewTemplate = require('libs/text!./view.html'),
11     tipTemplate = require('libs/text!./tip.html');
12
13 var AnswerElement = Object.create(genericElement);
14 _.extend(AnswerElement, {
15     init: function() {
16         genericElement.init.call(this);
17         this.tip = $(tipTemplate);
18         this.tip.text(this.wlxmlNode.getAttr('answer') || '');
19         this.tip.on('click', function(e) {
20             var doc = this.wlxmlNode.document,
21                 textNode = this.wlxmlNode.contents()[0];
22             e.preventDefault();
23             e.stopPropagation();
24
25             if(textNode) {
26                 this.canvas.select(doc.createFragment(doc.CaretFragment, {node:textNode, offset: textNode.getText().length}));
27                 this.canvas.showContextMenu(this, {x: e.clientX, y: e.clientY});
28             }
29         }.bind(this));
30         this.addWidget(this.tip);
31     },
32     onNodeAttrChange: function(event) {
33         if(event.meta.attr === 'answer') {
34             this.tip.text(event.meta.newVal || '');
35         }
36     }
37 });
38
39 var ReplaceExerciseElement = Object.create(documentElement.DocumentNodeElement.prototype);
40 _.extend(ReplaceExerciseElement, {
41     init: function() {
42         documentElement.DocumentNodeElement.prototype.init.call(this);
43         var view  = $(_.template(viewTemplate)());
44         this._container().append(view);
45
46
47         this.elementsRegister.register(
48             {tag: 'span', klass: 'answer', prototype: AnswerElement}
49         );
50
51         this.createContainer(this.wlxmlNode.contents(), {
52             manages: function() {
53                 return true;
54             },
55             dom: view.find('.content')
56         });
57
58         this.addToContextMenu('core.markToReplace');
59         this.contextMenuActions[0].on('actionExecuted', function(ret) {
60             if(ret instanceof this.wlxmlNode.document.Fragment && ret.isValid()) {
61                 this.canvas.select(ret);
62             }
63         }.bind(this));
64         this.addToContextMenu('core.editReplaceMark');
65         this.addToContextMenu('core.removeReplaceMark');
66
67     },
68     getVerticallyFirstTextElement: function() {
69         var toret;
70         this.containers.some(function(container) {
71             toret = container.getVerticallyFirstTextElement();
72             return !!toret;
73         });
74         return toret;
75     }
76 });
77
78 return {tag: 'div', klass: 'exercise.replace', prototype: ReplaceExerciseElement};
79
80 });
81
82
83     
84
85
86