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