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