editor: fix - removing redundant endTransaction call
[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         updateCurrentNodeElement: function(nodeElement) {
46             sandbox.getModule('nodePane').setNodeElement(nodeElement);
47             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
48             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
49             sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
50             sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
51         },
52         updateCurrentTextElement: function(textElement) {
53             sandbox.getModule('nodeFamilyTree').setElement(textElement);
54         }
55     };
56     
57
58     var views = {
59         mainLayout: new layout.Layout(mainLayoutTemplate),
60         mainTabs: (new tabs.View()).render(),
61         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
62         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
63         currentNodePaneLayout: new vbox.VBox(),
64         diffLayout: new layout.Layout(diffLayoutTemplate)
65     };
66     
67     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
68     addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
69     addMainTab(gettext('Source'), 'sourceEditor',  '');
70     addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
71     
72     sandbox.getDOM().append(views.mainLayout.getAsView());
73     
74     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
75
76     var wlxmlDocument, documentIsDirty;
77     
78     /* Events handling */
79     
80     var eventHandlers = {};
81      
82     eventHandlers.sourceEditor = {
83         ready: function() {
84             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
85             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
86         }
87     };
88     
89     eventHandlers.data = {
90         ready: function(usingDraft, draftTimestamp) {
91             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
92             
93             documentSummary.init(sandbox.getConfig().documentSummaryView);
94             documentSummary.render(sandbox.getModule('data').getDocumentProperties());
95             documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
96             views.currentNodePaneLayout.appendView(documentSummary.dom);
97
98             sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
99             sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
100
101             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'metadataEditor', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
102                 sandbox.getModule(moduleName).start();
103             });
104             
105             wlxmlDocument = sandbox.getModule('data').getDocument();
106             documentIsDirty = false;
107             wlxmlDocument.on('change', function() {
108                 documentIsDirty = true;
109                 sandbox.getModule('mainBar').setCommandEnabled('save', true);
110             });
111             wlxmlDocument.on('contentSet', function() {
112                 documentIsDirty = true;
113             });
114         },
115         draftDropped: function() {
116             documentSummary.setDraftField('-');
117             sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
118             sandbox.getModule('mainBar').setCommandEnabled('save', false);
119         },
120         savingStarted: function(what) {
121             var msg = {
122                 remote: gettext('Saving document'),
123                 local: gettext('Saving local copy')
124             };
125             sandbox.getModule('mainBar').setCommandEnabled('save', false);
126             sandbox.getModule('indicator').showMessage(msg[what] + '...');
127         },
128         savingEnded: function(status, what, data) {
129             void(status);
130             var msg = {
131                 remote: gettext('Document saved'),
132                 local: gettext('Local copy saved')
133             };
134             documentIsDirty = false;
135             
136             sandbox.getModule('indicator').clearMessage({message: msg[what]});
137             if(status === 'success' && what === 'remote') {
138                 sandbox.getModule('mainBar').setVersion(data.version);
139                 documentSummary.render(data);
140                 documentSummary.setDraftField('-');
141                 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
142                 sandbox.getModule('mainBar').setCommandEnabled('save', false);
143             }
144             if(what === 'local') {
145                 documentSummary.setDraftField(data.timestamp);
146                 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
147                 sandbox.getModule('mainBar').setCommandEnabled('save', true);
148             }
149         },
150         restoringStarted: function(event) {
151             sandbox.getModule('mainBar').setCommandEnabled('save', false);
152             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
153         },
154         historyItemAdded: function(item) {
155             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
156         },
157         diffFetched: function(diff) {
158             sandbox.getModule('diffViewer').setDiff(diff);
159         },
160         documentReverted: function(version) {
161             documentIsDirty = false;
162             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
163             sandbox.getModule('mainBar').setVersion(version);
164         }
165     };
166     
167     eventHandlers.mainBar = {
168         ready: function() {
169             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
170             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
171         },
172         'cmd.save': function() {
173             var sourceEditor = sandbox.getModule('sourceEditor');
174             if(!sourceEditor.changesCommited()) {
175                 logger.debug('Source editor has uncommited changes, commiting...');
176                 sourceEditor.commitChanges();
177             }
178             sandbox.getModule('data').saveDocument();
179         },
180         'cmd.drop-draft': function() {
181             sandbox.getModule('data').dropDraft();
182         }
183     };
184     
185     eventHandlers.indicator = {
186         ready: function() {
187             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
188         }
189     };
190     
191
192     
193     eventHandlers.documentCanvas = {
194         ready: function() {
195             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
196             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
197         },
198         
199         currentTextElementSet: function(textElement) {
200             commands.updateCurrentTextElement(textElement);
201         },
202
203         currentNodeElementSet: function(nodeElement) {
204             commands.updateCurrentNodeElement(nodeElement);
205         },
206         
207         currentNodeElementChanged: function(nodeElement) {
208             commands.updateCurrentNodeElement(nodeElement);
209         },
210
211         nodeHovered: function(canvasNode) {
212             commands.highlightDocumentNode(canvasNode);
213         },
214         
215         nodeBlured: function(canvasNode) {
216             commands.dimDocumentNode(canvasNode);
217         }
218     };
219
220     eventHandlers.nodePane = {
221         ready: function() {
222             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
223         },
224         
225         nodeElementChange: function(attr, value) {
226             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
227         }
228     };
229     
230     eventHandlers.metadataEditor = {
231         ready: function() {
232             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
233             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
234         }
235     };
236     
237     eventHandlers.nodeFamilyTree = {
238         ready: function() {
239             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
240         },
241         nodeEntered: function(node) {
242             commands.highlightDocumentElement(node, 'nodeFamilyTree');
243         },
244         nodeLeft: function(node) {
245             commands.dimDocumentElement(node, 'nodeFamilyTree');
246         },
247         nodeClicked: function(node) {
248             commands.jumpToDocumentElement(node);
249         }
250     };
251     
252     eventHandlers.documentToolbar = {
253         ready: function() {
254             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
255         },
256         command: function(cmd, params) {
257             sandbox.getModule('documentCanvas').command(cmd, params);
258         }
259     };
260     
261     eventHandlers.nodeBreadCrumbs = {
262         ready: function() {
263             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
264         },
265         elementEntered: function(element) {
266             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
267         },
268         elementLeft: function(element) {
269             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
270         },
271         elementClicked: function(element) {
272             commands.jumpToDocumentElement(element);
273         }
274     };
275     
276     eventHandlers.documentHistory = {
277         ready: function() {
278             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
279             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
280         },
281         compare: function(ver1, ver2) {
282             sandbox.getModule('data').fetchDiff(ver1, ver2);
283         },
284         restoreVersion: function(version) {
285             sandbox.getModule('data').restoreVersion(version);
286         },
287         displayVersion: function(event) {
288             /* globals window */
289             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
290         }
291     };
292     
293     eventHandlers.diffViewer = {
294         ready: function() {
295             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
296         }
297     };
298
299     window.addEventListener('beforeunload', function(event) {
300         var txt = gettext('Do you really want to exit?');
301         if(documentIsDirty) {
302             txt += ' ' + gettext('Document contains unsaved changes!');
303         }
304         event.returnValue = txt; // FF
305         return txt; // Chrome
306     });
307     
308     /* api */
309     
310     return {
311         start: function() {
312             sandbox.getModule('data').start();
313         },
314         handleEvent: function(moduleName, eventName, args) {
315             var eventRepr = moduleName + '.' + eventName;
316             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
317                 logger.debug('Handling event ' + eventRepr);
318                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
319             } else {
320                 logger.warning('No event handler for ' + eventRepr);
321             }
322
323         }
324     };
325 };
326
327 });