be4782a39e933f183521ab4504c48dac3922eb1a
[fnpeditor.git] / src / editor / plugins / core / edumed / replace / actions.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var _ = require('libs/underscore'),
8     Dialog = require('views/dialog/dialog');
9
10 var markToReplace = {
11     name: 'markToReplace',
12     params: {
13         fragment: {type: 'context', name: 'fragment'}
14     },
15     stateDefaults: {
16         icon: null,
17         label: gettext('Mark to replace'),
18         execute: function(callback, params) {
19             var doc = params.fragment.document,
20                 dialog = Dialog.create({
21                     title: gettext('Enter text to replace with'),
22                     executeButtonText: gettext('Apply'),
23                     cancelButtonText: gettext('Cancel'),
24                     fields: [
25                         {label: gettext('Text'), name: 'text', type: 'input'}
26                     ]
27                 });
28
29
30             dialog.on('execute', function(event) {
31                 doc.transaction(function() {
32                     var wrapper = params.fragment.startNode.parent().wrapText({
33                             _with: {tagName: 'span', attrs: {'class': 'answer', answer: event.formData.text}},
34                             offsetStart: params.fragment.startOffset,
35                             offsetEnd: params.fragment.endOffset,
36                             textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
37                         }),
38                         last = _.last(wrapper.contents());
39
40                     return doc.createFragment(doc.CaretFragment, {node: last, offset: last.getText().length});
41                 }, {
42                     metadata: {
43                         description: gettext('Mark to replace')
44                     },
45                     success: function(ret) { event.success(); callback(ret);}
46                 });
47             });
48             dialog.show();
49
50         }
51     },
52     getState: function(params) {
53         return {
54             allowed: params.fragment &&
55                         params.fragment.isValid() &&
56                         params.fragment instanceof params.fragment.TextRangeFragment &&
57                         params.fragment.hasSameBoundries() &&
58                         params.fragment.startNode.isInside('exercise.replace') &&
59                         !params.fragment.startNode.isInside({tagName: 'span', klass: 'answer'}),
60                         
61             description: gettext('Mark selection to replacement')
62         };
63     }
64 };
65
66 var removeReplaceMark = {
67     name: 'removeReplaceMark',
68     params: {
69         fragment: {type: 'context', name: 'fragment'}
70     },
71     stateDefaults: {
72         icon: null,
73         label: gettext('Remove replace mark'),
74         execute: function(callback, params) {
75             var doc = params.fragment.document;
76
77             doc.transaction(function() {
78                 var ret = params.fragment.node.getParent('answer').unwrapContent();
79
80                 return doc.createFragment(doc.CaretFragment, {node:ret.element2, offset: ret.element2.getText().length});
81             }, {
82                 metadata: {
83                     description: gettext('Remove replace mark')
84                 },
85                 success: callback
86             });
87         }
88     },
89     getState: function(params) {
90         return {
91             allowed: params.fragment &&
92                         params.fragment.isValid() &&
93                         params.fragment instanceof params.fragment.NodeFragment &&
94                         params.fragment.node.isInside('exercise.replace') &&
95                         params.fragment.node.isInside('answer'),
96                         
97             description: gettext('Remove replace mark')
98         };
99     }
100 };
101
102 var editReplaceMark = {
103     name: 'editReplaceMark',
104     params: {
105         fragment: {type: 'context', name: 'fragment'}
106     },
107     stateDefaults: {
108         icon: null,
109         label: gettext('Edit replace mark'),
110         execute: function(callback, params) {
111             var doc = params.fragment.document,
112                 answerNode = params.fragment.node.getParent('answer'),
113                 dialog = Dialog.create({
114                     title: gettext('Edit text to replace with'),
115                     executeButtonText: gettext('Apply'),
116                     cancelButtonText: gettext('Cancel'),
117                     fields: [
118                         {label: gettext('Text'), name: 'text', type: 'input', initialValue: answerNode.getAttr('answer')}
119                     ]
120                 });
121
122
123             dialog.on('execute', function(event) {
124                 doc.transaction(function() {
125                     answerNode.setAttr('answer', event.formData.text);
126                     var node = answerNode.contents()[0];
127                     return doc.createFragment(doc.CaretFragment, {node: node, offset: node.getText().length});
128                 }, {
129                     metadata: {
130                         description: gettext('Edit answer')
131                     },
132                     success: function(ret) { event.success(); callback(ret);}
133                 });
134             });
135             dialog.show();
136
137         }
138     },
139     getState: function(params) {
140         return {
141             allowed: params.fragment &&
142                         params.fragment.isValid() &&
143                         params.fragment instanceof params.fragment.NodeFragment &&
144                         params.fragment.node.isInside('exercise.replace') &&
145                         params.fragment.node.isInside('answer'),
146                     
147             description: gettext('Mark selection to replacement')
148         };
149     }
150 };
151
152 return [markToReplace, removeReplaceMark, editReplaceMark];
153
154 });