e91faa435865d4426b6089acf67733b7110599db
[fnpeditor.git] / src / editor / plugins / core / edumed / gaps / actions.js
1 define(function(require) {
2     
3 'use strict';
4
5 /* globals gettext */
6
7 var _ = require('libs/underscore');
8
9
10 var createGap = {
11     name: 'createGap',
12     params: {
13         fragment: {type: 'context', name: 'fragment'}
14     },
15     stateDefaults: {
16         icon: null,
17         label: gettext('Create a gap'),
18         execute: function(callback, params) {
19             var doc = params.fragment.document;
20
21             doc.transaction(function() {
22                 var wrapper = params.fragment.startNode.parent().wrapText({
23                         _with: {tagName: 'aside', attrs: {'class': 'gap'}},
24                         offsetStart: params.fragment.startOffset,
25                         offsetEnd: params.fragment.endOffset,
26                         textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
27                     }),
28                     last = _.last(wrapper.contents());
29
30                 return doc.createFragment(doc.CaretFragment, {node: last, offset: last.getText().length});
31             }, {
32                 metadata: {
33                     description: gettext('Create a gap')
34                 },
35                 success: callback
36             });
37         }
38     },
39     getState: function(params) {
40         return {
41             allowed: params.fragment &&
42                         params.fragment.isValid() &&
43                         params.fragment instanceof params.fragment.TextRangeFragment &&
44                         params.fragment.hasSiblingBoundries() &&
45                         params.fragment.startNode.isInside('exercise.gap') &&
46                         !params.fragment.startNode.isInside({tagName: 'aside', klass: 'gap'}),
47                         
48             description: gettext('Turn selection into a gap')
49         };
50     }
51 };
52
53 var removeGap = {
54     name: 'removeGap',
55     params: {
56         fragment: {type: 'context', name: 'fragment'}
57     },
58     stateDefaults: {
59         icon: null,
60         label: gettext('Remove a gap'),
61         execute: function(callback, params) {
62             var doc = params.fragment.document;
63
64             doc.transaction(function() {
65                 var ret = params.fragment.node.getParent('gap').unwrapContent();
66
67                 return doc.createFragment(doc.CaretFragment, {node:ret.element2, offset: ret.element2.getText().length});
68             }, {
69                 metadata: {
70                     description: gettext('Remove a gap')
71                 },
72                 success: callback
73             });
74         }
75     },
76     getState: function(params) {
77         return {
78             allowed: params.fragment &&
79                         params.fragment.isValid() &&
80                         params.fragment instanceof params.fragment.NodeFragment &&
81                         params.fragment.node.isInside('exercise.gap') &&
82                         params.fragment.node.isInside('gap'),
83                         
84             description: gettext('Remove a gap')
85         };
86     }
87 };
88
89
90 return [createGap, removeGap];
91
92 });