editor: bottom panel
[fnpeditor.git] / src / editor / plugins / core / core.js
1 define(function(require) {
2     
3 'use strict';
4 /* globals gettext */
5
6 var _ = require('libs/underscore'),
7     templates = require('plugins/core/templates'),
8     footnote = require('plugins/core/footnote'),
9     switchTo = require('plugins/core/switch'),
10     lists = require('plugins/core/lists'),
11     plugin = {name: 'core', actions: [], canvas: {}, documentExtension: {textNode: {}}},
12     Dialog = require('views/dialog/dialog');
13
14
15 plugin.documentExtension.textNode.transformations = {
16     breakContent: {
17         impl: function(args) {
18             var node = this,
19                 newNodes, emptyText;
20             newNodes = node.split({offset: args.offset});
21             [newNodes.first, newNodes.second].some(function(newNode) {
22                 if(!(newNode.contents().length)) {
23                     emptyText = newNode.append({text: ''});
24                     return true; // break
25                 }
26             });
27             return _.extend(newNodes, {emptyText: emptyText});
28         },
29         getChangeRoot: function() {
30             return this.context.parent().parent();
31         }
32     },
33     mergeContentUp: function() {
34         var myPrev = this.prev(),
35             ret;
36
37         if(myPrev) {
38             ret = myPrev.append(this);
39             return {node: ret, offset: ret.sameNode(this) ? null : ret.getText().length - this.getText().length};
40         } else {
41             var range = this.parent().unwrapContent();
42             return {node: range.element1, offset: 0};
43         }
44     }
45 };
46
47 var undoRedoAction = function(dir) {
48     return {
49         name: dir,
50         params: {
51             document: {type: 'context', name: 'document'},
52         },
53         stateDefaults: {
54             label: dir === 'undo' ? '<-' : '->',
55             icon: 'share-alt',
56             iconStyle: dir === 'undo' ? '-webkit-transform: scale(-1,1); transform: scale(-1, 1)' : '',
57             execute: function(params) {
58                 params.document[dir]();
59             },
60         },
61         getState: function(params) {
62             var allowed = params.document && !!(params.document[dir+'Stack'].length),
63                 desc = dir === 'undo' ? gettext('Undo') : gettext('Redo'),
64                 descEmpty = dir === 'undo' ? gettext('There is nothing to undo') : gettext('There is nothing to redo');
65             return {
66                 allowed: allowed,
67                 description: allowed ? desc : descEmpty
68             };
69         }
70     };
71 };
72
73 var pad = function(number) {
74     if(number < 10) {
75         number = '0' + number;
76     }
77     return number;
78 };
79
80 var commentAction = {
81     name: 'comment',
82     params: {
83         fragment: {type: 'context', name: 'fragment'}
84     },
85     stateDefaults: {
86         icon: 'comment',
87         execute: function(params, editor) {
88             /* globals Node */
89             var node = params.fragment.node;
90             if(node.nodeType === Node.TEXT_NODE) {
91                 node = node.parent();
92             }
93             node.document.transaction(function() {
94                 var comment =  node.after({tagName: 'aside', attrs: {'class': 'comment'}});
95                 comment.append({text:''});
96                 var user = editor.getUser(), creator;
97                 if(user) {
98                     creator = user.name;
99                     if(user.email) {
100                         creator += ' (' + user.email + ')';
101                     }
102                 } else {
103                     creator = 'anonymous';
104                 }
105
106                 var currentDate = new Date(),
107                     dt = pad(currentDate.getDate()) + '-' +
108                                 pad((currentDate.getMonth() + 1))  + '-' +
109                                 pad(currentDate.getFullYear()) + ' ' +
110                                 pad(currentDate.getHours()) + ':' +
111                                 pad(currentDate.getMinutes()) + ':' +
112                                 pad(currentDate.getSeconds());
113
114                 var metadata = comment.getMetadata();
115                 metadata.add({key: 'creator', value: creator});
116                 metadata.add({key: 'date', value: dt});
117             });
118         },
119     },
120     getState: function(params) {
121         var state = {
122             allowed: params.fragment && params.fragment.isValid() &&
123                         params.fragment instanceof params.fragment.NodeFragment && !params.fragment.node.isRoot()
124         };
125         if(state.allowed) {
126             state.description = gettext('Insert comment after current node');
127         }
128         return state;
129     }
130 };
131
132
133 var createWrapTextAction = function(createParams) {
134     return {
135         name: createParams.name,
136         params: {
137             fragment: {type: 'context', name: 'fragment'},
138         },
139         getState: function(params) {
140             var state = {
141                     label: this.config.label
142                 },
143                 parent;
144             
145             if(
146                 !params.fragment || !params.fragment.isValid() ||
147                 !(params.fragment instanceof params.fragment.TextRangeFragment) ||
148                 !params.fragment.hasSiblingBoundries()) {
149                     return _.extend(state, {allowed: false});
150             }
151             
152             parent = params.fragment.startNode.parent();
153             if(parent && parent.is(createParams.klass) || parent.isInside(createParams.klass)) {
154                 return _.extend(state, {allowed: false});
155             }
156
157             return _.extend(state, {
158                 allowed: true,
159                 execute: function(params) {
160                     params.fragment.document.transaction(function() {
161                         var parent = params.fragment.startNode.parent();
162                         return parent.wrapText({
163                             _with: {tagName: 'span', attrs: {'class': createParams.klass}},
164                             offsetStart: params.fragment.startOffset,
165                             offsetEnd: params.fragment.endOffset,
166                             textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
167                         });
168                     });
169                 }
170             });
171         }
172     };
173 };
174
175
176 var createLinkFromSelection = function(params) {
177     var doc = params.fragment.document,
178         dialog = Dialog.create({
179             title: gettext('Create link'),
180             executeButtonText: gettext('Apply'),
181             cancelButtonText: gettext('Cancel'),
182             fields: [
183                 {label: gettext('Link'), name: 'href', type: 'input'}
184             ]
185         });
186     
187     dialog.on('execute', function(event) {
188         doc.transaction(function() {
189             var span =  params.fragment.startNode.parent().wrapText({
190                 _with: {tagName: 'span', attrs: {'class': 'link'}},
191                 offsetStart: params.fragment.startOffset,
192                 offsetEnd: params.fragment.endOffset,
193                 textNodeIdx: [params.fragment.startNode.getIndex(), params.fragment.endNode.getIndex()]
194             });
195             span.setAttr('href', event.formData.href);
196             event.success();
197             return span;
198         });
199     });
200     dialog.show();
201 };
202
203 var editLink = function(params) {
204     var doc = params.fragment.document,
205         link = params.fragment.node.getParent('link'),
206         dialog = Dialog.create({
207             title: gettext('Edit link'),
208             executeButtonText: gettext('Apply'),
209             cancelButtonText: gettext('Cancel'),
210             fields: [
211                 {label: gettext('Link'), name: 'href', type: 'input', initialValue: link.getAttr('href')}
212             ]
213         });
214     
215     dialog.on('execute', function(event) {
216         doc.transaction(function() {
217             link.setAttr('href', event.formData.href);
218             event.success();
219         });
220     });
221     dialog.show();
222 };
223
224 var linkAction = {
225     name: 'link',
226     params: {
227         fragment: {type: 'context', name: 'fragment'}
228     },
229     stateDefaults: {
230         label: gettext('link')
231     },
232     getState: function(params) {
233         if(!params.fragment || !params.fragment.isValid()) {
234             return {allowed: false};
235         }
236
237         if(params.fragment instanceof params.fragment.TextRangeFragment) {
238             if(!params.fragment.hasSiblingBoundries() || params.fragment.startNode.parent().is('link')) {
239                 return {allowed: false};
240             }
241             return {
242                 allowed: true,
243                 description: gettext('Create link from selection'),
244                 execute: createLinkFromSelection
245             };
246         }
247
248         if(params.fragment instanceof params.fragment.CaretFragment) {
249             if(params.fragment.node.isInside('link')) {
250                 return {allowed: true, toggled: true, execute: editLink};
251             }
252         }
253         return {allowed: false};
254     }
255 };
256
257
258 plugin.actions = [
259     undoRedoAction('undo'),
260     undoRedoAction('redo'),
261     commentAction,
262     createWrapTextAction({name: 'emphasis', klass: 'emp'}),
263     createWrapTextAction({name: 'cite', klass: 'cite'}),
264     linkAction
265 ].concat(plugin.actions, templates.actions, footnote.actions, switchTo.actions, lists.actions);
266
267
268
269 plugin.config = function(config) {
270     // templates.actions[0].config(config.templates);
271     templates.actions[0].params.template.options = config.templates;
272 };
273
274 return plugin;
275
276 });