1 define(function(require) {
7 var _ = require('libs/underscore'),
8 Dialog = require('views/dialog/dialog');
11 name: 'markToReplace',
13 fragment: {type: 'context', name: 'fragment'}
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'),
25 {label: gettext('Text'), name: 'text', type: 'input'}
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()]
38 last = _.last(wrapper.contents());
40 return doc.createFragment(doc.CaretFragment, {node: last, offset: last.getText().length});
43 description: gettext('Mark to replace')
45 success: function(ret) { event.success(); callback(ret);}
52 getState: function(params) {
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'}),
61 description: gettext('Mark selection to replacement')
66 var removeReplaceMark = {
67 name: 'removeReplaceMark',
69 fragment: {type: 'context', name: 'fragment'}
73 label: gettext('Remove replace mark'),
74 execute: function(callback, params) {
75 var doc = params.fragment.document;
77 doc.transaction(function() {
78 var ret = params.fragment.node.getParent('answer').unwrapContent();
80 return doc.createFragment(doc.CaretFragment, {node:ret.element2, offset: ret.element2.getText().length});
83 description: gettext('Remove replace mark')
89 getState: function(params) {
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'),
97 description: gettext('Remove replace mark')
102 var editReplaceMark = {
103 name: 'editReplaceMark',
105 fragment: {type: 'context', name: 'fragment'}
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'),
118 {label: gettext('Text'), name: 'text', type: 'input', initialValue: answerNode.getAttr('answer')}
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});
130 description: gettext('Edit answer')
132 success: function(ret) { event.success(); callback(ret);}
139 getState: function(params) {
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'),
147 description: gettext('Mark selection to replacement')
152 return [markToReplace, removeReplaceMark, editReplaceMark];