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