2c1e8b002731c88974ab1aacfa1b5a0dbded9bc1
[fnpeditor.git] / src / editor / plugins / core / switch.js
1 define(function(require) {
2     
3 'use strict';
4 /* globals gettext */
5
6 var _ = require('libs/underscore');
7
8
9 var createSwitchAction = function(createParams) {
10     return  {
11         name: createParams.name,
12         params: {
13             fragment: {type: 'context', name: 'fragment'}
14         },
15         getState: function(params) {
16             var state = {
17                     label: createParams.to.name
18                 },
19                 f = params.fragment,
20                 description;
21
22
23             if(
24                 !(f && f.isValid()) ||
25                 !((f instanceof f.CaretFragment) || (f instanceof f.TextRangeFragment && f.getCommonParent()))
26             ) {
27                 return _.extend(state, {
28                     allowed: false,
29                     description: 'wrong or no selection'
30                 });
31             }
32
33             var node = f instanceof f.CaretFragment ? f.node.parent() : f.getCommonParent(),
34                 alreadyInTarget = node.isInside(createParams.to),
35                 toSwitch = node,
36                 textNodePath = (f.node || f.startNode).getPath();
37
38             if(!toSwitch.is(createParams.from)) {
39                 toSwitch = toSwitch.getParent(createParams.from);
40             }
41
42             description = gettext('Switch to') + ' ' + createParams.to.name;
43             return _.extend(state, {
44                 allowed: !!toSwitch || alreadyInTarget,
45                 toggled: alreadyInTarget,
46                 description: description,
47                 execute: alreadyInTarget ? function() {} : function(callback) {
48                     f.document.transaction(function() {
49                         if(createParams.to.tagName) {
50                             toSwitch = toSwitch.setTag(createParams.to.tagName);
51                         }
52                         if(!_.isUndefined(createParams.to.klass)) {
53                             toSwitch.setClass(createParams.to.klass);
54                         }
55                         return f.document.createFragment(f.CaretFragment, {node: f.document.getNodeByPath(textNodePath), offset: f.offset});
56                     }, {
57                         metadata: {
58                             description: description,
59                             fragment: params.fragment
60                         },
61                         success: callback
62                     });
63                 }
64             });
65         }
66     };
67 };
68
69 var headerAction = createSwitchAction({name: 'switchToHeader', from: {tagName: 'div', klass: 'p'}, to: {tagName: 'header', klass: '', name: gettext('header')}}),
70     paragraphAction = createSwitchAction({name: 'switchToParagraph', from: {tagName: 'header'}, to: {tagName: 'div', klass: 'p', name: gettext('paragraph')}}),
71     imageAction = createSwitchAction({name: 'switchToImage', from: {}, to: {tagName: 'div', klass: 'img', name: gettext('image')}}),
72     videoAction = createSwitchAction({name: 'switchToVideo', from: {}, to: {tagName: 'div', klass: 'video', name: gettext('video')}});
73
74 return {
75     actions: [headerAction, paragraphAction, imageAction, videoAction],
76     canvasActionHandler: {
77         handles: [headerAction, paragraphAction, imageAction, videoAction]
78         // handle: function(canvas, action, ret) {
79         //     var params = {},
80         //         f;
81         //     if(ret && ret.node2) {
82         //         f = ret.oldFragment;
83         //         if(f && f instanceof f.CaretFragment) {
84         //             params.caretTo = f.offset;
85         //         }
86         //         canvas.setCurrentElement(ret.node2, params);
87         //     }
88         // }
89     }
90 };
91
92 });