42ab636cb68560471ba104fa3375f6fcbd24cd00
[fnpeditor.git] / src / editor / modules / rng / rng.js
1 define([
2 'libs/underscore',
3 'fnpjs/layout',
4 'fnpjs/vbox',
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(_, layout, vbox, 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         highlightDocumentElement: function(element, origin) {
26             ///'nodeBreadCrumbs', 'nodeFamilyTree'
27             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
28                 if(!origin || moduleName !== origin) {
29                     sandbox.getModule(moduleName).highlightElement(element);
30                 }
31             });
32         },
33         dimDocumentElement: function(element, origin) {
34             //'nodeBreadCrumbs', 'nodeFamilyTree'
35             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
36                 if(!origin || moduleName !== origin) {
37                     sandbox.getModule(moduleName).dimElement(element);
38                 }
39             });
40         },
41         jumpToDocumentElement: function(element) {
42             sandbox.getModule('documentCanvas').jumpToElement(element);
43         },
44         updateCurrentNodeElement: function(nodeElement) {
45             sandbox.getModule('nodePane').setNodeElement(nodeElement);
46             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
47             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
48             sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
49             sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
50         },
51         updateCurrentTextElement: function(textElement) {
52             sandbox.getModule('nodeFamilyTree').setElement(textElement);
53         }
54     };
55     
56
57     var views = {
58         mainLayout: new layout.Layout(mainLayoutTemplate),
59         mainTabs: (new tabs.View()).render(),
60         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
61         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
62         currentNodePaneLayout: new vbox.VBox(),
63         diffLayout: new layout.Layout(diffLayoutTemplate)
64     };
65     
66     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
67     addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
68     addMainTab(gettext('Source'), 'sourceEditor',  '');
69     addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
70     
71     sandbox.getDOM().append(views.mainLayout.getAsView());
72     
73     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
74
75     var wlxmlDocument, documentIsDirty;
76     
77     /* Events handling */
78     
79     var eventHandlers = {};
80      
81     eventHandlers.sourceEditor = {
82         ready: function() {
83             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
84             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
85         }
86     };
87     
88     eventHandlers.data = {
89         ready: function() {
90             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
91             
92             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
93                 sandbox.getModule(moduleName).start();
94             });
95             
96             wlxmlDocument = sandbox.getModule('data').getDocument();
97             documentIsDirty = false;
98             wlxmlDocument.on('change', function() {
99                 documentIsDirty = true;
100             });
101             wlxmlDocument.on('contentSet', function() {
102                 documentIsDirty = true;
103             });
104         },
105         savingStarted: function(what) {
106             var msg = {
107                 remote: gettext('Saving document'),
108                 local: gettext('Saving local copy')
109             };
110             sandbox.getModule('mainBar').setCommandEnabled('save', false);
111             sandbox.getModule('indicator').showMessage(msg[what] + '...');
112         },
113         savingEnded: function(status, what, data) {
114             void(status);
115             var msg = {
116                 remote: gettext('Document saved'),
117                 local: gettext('Local copy saved')
118             };
119             documentIsDirty = false;
120             sandbox.getModule('mainBar').setCommandEnabled('save', true);
121             sandbox.getModule('indicator').clearMessage({message: msg[what]});
122             sandbox.getModule('mainBar').setVersion(data.version);
123         },
124         restoringStarted: function(event) {
125             sandbox.getModule('mainBar').setCommandEnabled('save', false);
126             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
127         },
128         historyItemAdded: function(item) {
129             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
130         },
131         diffFetched: function(diff) {
132             sandbox.getModule('diffViewer').setDiff(diff);
133         },
134         documentReverted: function(version) {
135             documentIsDirty = false;
136             sandbox.getModule('mainBar').setCommandEnabled('save', true);
137             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
138             sandbox.getModule('mainBar').setVersion(version);
139         }
140     };
141     
142     eventHandlers.mainBar = {
143         ready: function() {
144             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
145             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
146         },
147         'cmd.save': function() {
148             var sourceEditor = sandbox.getModule('sourceEditor');
149             if(!sourceEditor.changesCommited()) {
150                 logger.debug('Source editor has uncommited changes, commiting...');
151                 sourceEditor.commitChanges();
152             }
153             sandbox.getModule('data').saveDocument();
154         },
155         'cmd.drop-draft': function() {
156             sandbox.getModule('data').dropDraft();
157         }
158     };
159     
160     eventHandlers.indicator = {
161         ready: function() {
162             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
163         }
164     };
165     
166
167     
168     eventHandlers.documentCanvas = {
169         ready: function() {
170             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
171             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
172         },
173         
174         currentTextElementSet: function(textElement) {
175             commands.updateCurrentTextElement(textElement);
176         },
177
178         currentNodeElementSet: function(nodeElement) {
179             commands.updateCurrentNodeElement(nodeElement);
180         },
181         
182         currentNodeElementChanged: function(nodeElement) {
183             commands.updateCurrentNodeElement(nodeElement);
184         },
185
186         nodeHovered: function(canvasNode) {
187             commands.highlightDocumentNode(canvasNode);
188         },
189         
190         nodeBlured: function(canvasNode) {
191             commands.dimDocumentNode(canvasNode);
192         }
193     };
194
195     eventHandlers.nodePane = {
196         ready: function() {
197             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
198         },
199         
200         nodeElementChange: function(attr, value) {
201             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
202         }
203     };
204     
205     eventHandlers.metadataEditor = {
206         ready: function() {
207             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
208             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
209         }
210     };
211     
212     eventHandlers.nodeFamilyTree = {
213         ready: function() {
214             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
215         },
216         nodeEntered: function(node) {
217             commands.highlightDocumentElement(node, 'nodeFamilyTree');
218         },
219         nodeLeft: function(node) {
220             commands.dimDocumentElement(node, 'nodeFamilyTree');
221         },
222         nodeClicked: function(node) {
223             commands.jumpToDocumentElement(node);
224         }
225     };
226     
227     eventHandlers.documentToolbar = {
228         ready: function() {
229             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
230         },
231         command: function(cmd, params) {
232             sandbox.getModule('documentCanvas').command(cmd, params);
233         }
234     };
235     
236     eventHandlers.nodeBreadCrumbs = {
237         ready: function() {
238             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
239         },
240         elementEntered: function(element) {
241             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
242         },
243         elementLeft: function(element) {
244             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
245         },
246         elementClicked: function(element) {
247             commands.jumpToDocumentElement(element);
248         }
249     };
250     
251     eventHandlers.documentHistory = {
252         ready: function() {
253             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
254             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
255         },
256         compare: function(ver1, ver2) {
257             sandbox.getModule('data').fetchDiff(ver1, ver2);
258         },
259         restoreVersion: function(version) {
260             sandbox.getModule('data').restoreVersion(version);
261         },
262         displayVersion: function(event) {
263             /* globals window */
264             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
265         }
266     };
267     
268     eventHandlers.diffViewer = {
269         ready: function() {
270             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
271         }
272     };
273
274     window.addEventListener('beforeunload', function(event) {
275         var txt = gettext('Do you really want to exit?');
276         if(documentIsDirty) {
277             txt += ' ' + gettext('Document contains unsaved changes!');
278         }
279         event.returnValue = txt; // FF
280         return txt; // Chrome
281     });
282     
283     /* api */
284     
285     return {
286         start: function() {
287             sandbox.getModule('data').start();
288         },
289         handleEvent: function(moduleName, eventName, args) {
290             var eventRepr = moduleName + '.' + eventName;
291             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
292                 logger.debug('Handling event ' + eventRepr);
293                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
294             } else {
295                 logger.warning('No event handler for ' + eventRepr);
296             }
297
298         }
299     };
300 };
301
302 });