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