editor: fix selecting text spanning multiple nodes in Chrome
[fnpeditor.git] / src / editor / modules / rng / rng.js
1 define([
2 'libs/underscore',
3 'fnpjs/layout',
4 'fnpjs/vbox',
5 'fnpjs/logging/logging',
6 'views/tabs/tabs',
7 'libs/text!./mainLayout.html',
8 'libs/text!./editingLayout.html',
9 'libs/text!./diffLayout.html',
10 ], function(_, layout, vbox, logging, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
11
12 'use strict';
13
14 return function(sandbox) {
15
16     /* globals gettext */
17
18     var logger = logging.getLogger('editor.modules.rng');
19     
20     function addMainTab(title, slug, view) {
21         views.mainTabs.addTab(title, slug, view);
22     }
23      
24     var commands = {
25         highlightDocumentElement: function(element, origin) {
26             ///'nodeBreadCrumbs', 'nodeFamilyTree'
27             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
28                 if(!origin || moduleName !== origin) {
29                     sandbox.getModule(moduleName).highlightElement(element);
30                 }
31             });
32         },
33         dimDocumentElement: function(element, origin) {
34             //'nodeBreadCrumbs', 'nodeFamilyTree'
35             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
36                 if(!origin || moduleName !== origin) {
37                     sandbox.getModule(moduleName).dimElement(element);
38                 }
39             });
40         },
41         jumpToDocumentElement: function(element) {
42             sandbox.getModule('documentCanvas').jumpToElement(element);
43         },
44         updateCurrentNodeElement: function(nodeElement) {
45             sandbox.getModule('nodePane').setNodeElement(nodeElement);
46             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
47             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
48             sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
49             sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
50         },
51         updateCurrentTextElement: function(textElement) {
52             sandbox.getModule('nodeFamilyTree').setElement(textElement);
53         }
54     };
55     
56
57     var views = {
58         mainLayout: new layout.Layout(mainLayoutTemplate),
59         mainTabs: (new tabs.View()).render(),
60         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
61         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
62         currentNodePaneLayout: new vbox.VBox(),
63         diffLayout: new layout.Layout(diffLayoutTemplate)
64     };
65     
66     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
67     addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
68     addMainTab(gettext('Source'), 'sourceEditor',  '');
69     addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
70     
71     sandbox.getDOM().append(views.mainLayout.getAsView());
72     
73     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
74
75     var wlxmlDocument, documentIsDirty;
76     
77     /* Events handling */
78     
79     var eventHandlers = {};
80      
81     eventHandlers.sourceEditor = {
82         ready: function() {
83             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
84             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
85         }
86     };
87     
88     eventHandlers.data = {
89         ready: function() {
90             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
91             
92             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
93                 sandbox.getModule(moduleName).start();
94             });
95             
96             wlxmlDocument = sandbox.getModule('data').getDocument();
97             documentIsDirty = false;
98             wlxmlDocument.on('change', function() {
99                 documentIsDirty = true;
100             });
101             wlxmlDocument.on('contentSet', function() {
102                 documentIsDirty = true;
103             });
104         },
105         savingStarted: function(what) {
106             var msg = {
107                 remote: gettext('Saving document'),
108                 local: gettext('Saving local copy')
109             };
110             sandbox.getModule('mainBar').setCommandEnabled('save', false);
111             sandbox.getModule('indicator').showMessage(msg[what] + '...');
112         },
113         savingEnded: function(status, what, current_version) {
114             void(status);
115             var msg = {
116                 remote: gettext('Document saved'),
117                 local: gettext('Local copy saved')
118             };
119             documentIsDirty = false;
120             sandbox.getModule('mainBar').setCommandEnabled('save', true);
121             sandbox.getModule('indicator').clearMessage({message: msg[what]});
122             sandbox.getModule('mainBar').setVersion(current_version);
123         },
124         restoringStarted: function(event) {
125             sandbox.getModule('mainBar').setCommandEnabled('save', false);
126             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
127         },
128         historyItemAdded: function(item) {
129             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
130         },
131         diffFetched: function(diff) {
132             sandbox.getModule('diffViewer').setDiff(diff);
133         },
134         documentReverted: function(version) {
135             documentIsDirty = false;
136             sandbox.getModule('mainBar').setCommandEnabled('save', true);
137             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
138             sandbox.getModule('mainBar').setVersion(version);
139         }
140     };
141     
142     eventHandlers.mainBar = {
143         ready: function() {
144             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
145             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
146         },
147         'cmd.save': function() {
148             var sourceEditor = sandbox.getModule('sourceEditor');
149             if(!sourceEditor.changesCommited()) {
150                 logger.debug('Source editor has uncommited changes, commiting...');
151                 sourceEditor.commitChanges();
152             }
153             sandbox.getModule('data').saveDocument();
154         }
155     };
156     
157     eventHandlers.indicator = {
158         ready: function() {
159             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
160         }
161     };
162     
163
164     
165     eventHandlers.documentCanvas = {
166         ready: function() {
167             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
168             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
169         },
170         
171         currentTextElementSet: function(textElement) {
172             commands.updateCurrentTextElement(textElement);
173         },
174
175         currentNodeElementSet: function(nodeElement) {
176             commands.updateCurrentNodeElement(nodeElement);
177         },
178         
179         currentNodeElementChanged: function(nodeElement) {
180             commands.updateCurrentNodeElement(nodeElement);
181         },
182
183         nodeHovered: function(canvasNode) {
184             commands.highlightDocumentNode(canvasNode);
185         },
186         
187         nodeBlured: function(canvasNode) {
188             commands.dimDocumentNode(canvasNode);
189         }
190     };
191
192     eventHandlers.nodePane = {
193         ready: function() {
194             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
195         },
196         
197         nodeElementChange: function(attr, value) {
198             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
199         }
200     };
201     
202     eventHandlers.metadataEditor = {
203         ready: function() {
204             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
205             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
206         }
207     };
208     
209     eventHandlers.nodeFamilyTree = {
210         ready: function() {
211             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
212         },
213         nodeEntered: function(node) {
214             commands.highlightDocumentElement(node, 'nodeFamilyTree');
215         },
216         nodeLeft: function(node) {
217             commands.dimDocumentElement(node, 'nodeFamilyTree');
218         },
219         nodeClicked: function(node) {
220             commands.jumpToDocumentElement(node);
221         }
222     };
223     
224     eventHandlers.documentToolbar = {
225         ready: function() {
226             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
227         },
228         command: function(cmd, params) {
229             sandbox.getModule('documentCanvas').command(cmd, params);
230         }
231     };
232     
233     eventHandlers.nodeBreadCrumbs = {
234         ready: function() {
235             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
236         },
237         elementEntered: function(element) {
238             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
239         },
240         elementLeft: function(element) {
241             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
242         },
243         elementClicked: function(element) {
244             commands.jumpToDocumentElement(element);
245         }
246     };
247     
248     eventHandlers.documentHistory = {
249         ready: function() {
250             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
251             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
252         },
253         compare: function(ver1, ver2) {
254             sandbox.getModule('data').fetchDiff(ver1, ver2);
255         },
256         restoreVersion: function(version) {
257             sandbox.getModule('data').restoreVersion(version);
258         },
259         displayVersion: function(event) {
260             /* globals window */
261             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
262         }
263     };
264     
265     eventHandlers.diffViewer = {
266         ready: function() {
267             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
268         }
269     };
270
271     window.addEventListener('beforeunload', function(event) {
272         var txt = gettext('Do you really want to exit?');
273         if(documentIsDirty) {
274             txt += ' ' + gettext('Document contains unsaved changes!');
275         }
276         event.returnValue = txt; // FF
277         return txt; // Chrome
278     });
279     
280     /* api */
281     
282     return {
283         start: function() {
284             sandbox.getModule('data').start();
285         },
286         handleEvent: function(moduleName, eventName, args) {
287             var eventRepr = moduleName + '.' + eventName;
288             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
289                 logger.debug('Handling event ' + eventRepr);
290                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
291             } else {
292                 logger.debug('No event handler for ' + eventRepr);
293             }
294
295         }
296     };
297 };
298
299 });