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