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