e4f5c3ec6624957b556179809c14ea79801d42fd
[fnpeditor.git] / src / editor / modules / rng / rng.js
1 define([
2 './documentSummary',
3 'libs/underscore',
4 'fnpjs/layout',
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(documentSummary, _, layout, 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         refreshCanvasSelection: function(selection) {
26             var fragment = selection.toDocumentFragment();
27             sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
28         },
29     };
30     
31
32     var views = {
33         mainLayout: new layout.Layout(mainLayoutTemplate),
34         mainTabs: (new tabs.View()).render(),
35         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
36         diffLayout: new layout.Layout(diffLayoutTemplate)
37     };
38     
39     addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
40     addMainTab(gettext('Source'), 'sourceEditor',  '');
41     addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
42     
43     sandbox.getDOM().append(views.mainLayout.getAsView());
44     
45     var wlxmlDocument, documentIsDirty;
46     
47     /* Events handling */
48     
49     var eventHandlers = {};
50      
51     eventHandlers.sourceEditor = {
52         ready: function() {
53             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
54             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
55         }
56     };
57     
58     eventHandlers.data = {
59         ready: function(usingDraft, draftTimestamp, xmlValid) {
60             wlxmlDocument = sandbox.getModule('data').getDocument();
61
62             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
63             
64             documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
65             documentSummary.render();
66             documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
67             sandbox.getModule('mainBar').setSummaryView(documentSummary.dom);
68
69             sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
70             sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
71
72             
73             var toStart = ['sourceEditor', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'];
74             if(xmlValid) {
75                 toStart.push('documentCanvas');
76             }
77             _.each(toStart, function(moduleName) {
78                 sandbox.getModule(moduleName).start();
79             });
80             
81             documentIsDirty = false;
82             wlxmlDocument.on('change', function() {
83                 documentIsDirty = true;
84                 sandbox.getModule('mainBar').setCommandEnabled('save', true);
85             });
86             wlxmlDocument.on('contentSet', function() {
87                 documentIsDirty = true;
88             });
89         },
90         draftDropped: function() {
91             documentSummary.setDraftField('-');
92             sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
93             sandbox.getModule('mainBar').setCommandEnabled('save', false);
94         },
95         savingStarted: function(what) {
96             var msg = {
97                 remote: gettext('Saving document'),
98                 local: gettext('Saving local copy')
99             };
100             sandbox.getModule('mainBar').setCommandEnabled('save', false);
101             sandbox.getModule('indicator').showMessage(msg[what] + '...');
102         },
103         savingEnded: function(status, what, data) {
104             void(status);
105             var msg = {
106                 remote: gettext('Document saved'),
107                 local: gettext('Local copy saved')
108             };
109             documentIsDirty = false;
110             
111             sandbox.getModule('indicator').clearMessage({message: msg[what]});
112             if(status === 'success' && what === 'remote') {
113                 documentSummary.setDraftField('-');
114                 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
115                 sandbox.getModule('mainBar').setCommandEnabled('save', false);
116             }
117             if(what === 'local') {
118                 documentSummary.setDraftField(data.timestamp);
119                 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
120                 sandbox.getModule('mainBar').setCommandEnabled('save', true);
121             }
122         },
123         restoringStarted: function(event) {
124             sandbox.getModule('mainBar').setCommandEnabled('save', false);
125             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
126         },
127         historyItemAdded: function(item) {
128             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
129         },
130         diffFetched: function(diff) {
131             sandbox.getModule('diffViewer').setDiff(diff);
132         },
133         documentReverted: function(version) {
134             documentIsDirty = false;
135             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
136         }
137     };
138     
139     eventHandlers.mainBar = {
140         ready: function() {
141             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
142         },
143         'cmd.save': function() {
144             var sourceEditor = sandbox.getModule('sourceEditor');
145             if(!sourceEditor.changesCommited()) {
146                 logger.debug('Source editor has uncommited changes, commiting...');
147                 sourceEditor.commitChanges();
148             }
149             sandbox.getModule('data').saveDocument();
150         },
151         'cmd.drop-draft': function() {
152             sandbox.getModule('data').dropDraft();
153         }
154     };
155     
156     eventHandlers.indicator = {
157         ready: function() {
158             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
159         }
160     };
161     
162
163     
164     eventHandlers.documentCanvas = {
165         ready: function() {
166             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
167             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
168         },
169         
170         nodeHovered: function(canvasNode) {
171             commands.highlightDocumentNode(canvasNode);
172         },
173         
174         nodeBlured: function(canvasNode) {
175             commands.dimDocumentNode(canvasNode);
176         },
177
178         selectionChanged: function(selection) {
179             commands.refreshCanvasSelection(selection);
180         }
181     };
182     
183     eventHandlers.documentToolbar = {
184         ready: function() {
185             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
186             sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
187         },
188         actionExecuted: function(action, ret) {
189             sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
190         }
191     };
192     
193     eventHandlers.documentHistory = {
194         ready: function() {
195             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
196             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
197         },
198         compare: function(ver1, ver2) {
199             sandbox.getModule('data').fetchDiff(ver1, ver2);
200         },
201         restoreVersion: function(version) {
202             sandbox.getModule('data').restoreVersion(version);
203         },
204         displayVersion: function(event) {
205             /* globals window */
206             var config = sandbox.getConfig();
207             if(config.documentUrl) {
208                 window.open(config.documentUrl(sandbox.getModule('data').getDocumentId(), event.version), _.uniqueId());
209             } else {
210                 logger.error('Unable to show version ' + event.version + ' of a document - config.documentUrl missing');
211             }
212         }
213     };
214     
215     eventHandlers.diffViewer = {
216         ready: function() {
217             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
218         }
219     };
220
221     eventHandlers.statusBar = {
222         ready: function() {
223             views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
224         }
225     };
226
227     eventHandlers.__all__ = {
228         actionHovered: function(action) {
229             sandbox.getModule('statusBar').showAction(action);
230         },
231         actionOff: function() {
232             sandbox.getModule('statusBar').clearAction();
233         }
234     };
235
236     window.addEventListener('beforeunload', function(event) {
237         var txt = gettext('Do you really want to exit?');
238         if(documentIsDirty) {
239             txt += ' ' + gettext('Document contains unsaved changes!');
240         }
241         event.returnValue = txt; // FF
242         return txt; // Chrome
243     });
244     
245     /* api */
246     
247     return {
248         start: function() {
249             sandbox.registerActionsAppObject({
250                 getUser: function() {
251                     return sandbox.getConfig().user;
252                 }
253             });
254             sandbox.getModule('data').start();
255         },
256         handleEvent: function(moduleName, eventName, args) {
257             var eventRepr = moduleName + '.' + eventName;
258             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
259                 logger.debug('Handling event ' + eventRepr);
260                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
261                 return;
262             }
263
264             if(eventHandlers.__all__[eventName]) {
265                 logger.debug('Handling event ' + eventRepr);
266                 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
267                 return;
268             }
269
270             logger.warning('No event handler for ' + eventRepr);
271         }
272     };
273 };
274
275 });