Refactoring: Moving commands out of canvasManager
[fnpeditor.git] / modules / rng / rng.js
1 define([
2 'fnpjs/layout',
3 'fnpjs/vbox',
4 'views/tabs/tabs',
5 'libs/text!./mainLayout.html',
6 'libs/text!./editingLayout.html',
7 'libs/text!./diffLayout.html',
8 ], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
9
10 'use strict';
11
12 return function(sandbox) {
13     
14     function addMainTab(title, slug, view) {
15         views.mainTabs.addTab(title, slug, view);
16     }
17     
18     var dirty = {
19         sourceEditor: false,
20         documentCanvas: false,
21         metadataEditor: false,
22     };
23     
24     var synchronizeTab = function(slug) {
25         function tabIsDirty(slug) {
26             if(slug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor))
27                 return true;
28             if(slug === 'sourceEditor' && dirty.sourceEditor)
29                 return true;
30             return false;
31         }
32     
33         if(tabIsDirty(slug)) {
34             var reason, doc;
35             if(slug === 'sourceEditor') {
36                 doc = sandbox.getModule('sourceEditor').getDocument();
37                 reason = 'source_edit';
38                 dirty.sourceEditor = false;
39             }
40             if(slug === 'editor') {
41                 doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();
42                 if(dirty.metadataEditor) {
43                     doc = sandbox.getModule('metadataEditor').attachMetadata(doc);
44                 }
45                 reason = 'edit';
46                 dirty.documentCanvas = dirty.metadataEditor = false;
47             }
48             sandbox.getModule('data').commitDocument(doc, reason);
49         }
50     };
51     
52     var commands = {
53         highlightDocumentElement: function(element, origin) {
54             ///'nodeBreadCrumbs', 'nodeFamilyTree'
55             ['documentCanvas', ].forEach(function(moduleName) {
56                 if(!origin || moduleName != origin)
57                     sandbox.getModule(moduleName).highlightElement(element);
58             });
59         },
60         dimDocumentElement: function(element, origin) {
61             //'nodeBreadCrumbs', 'nodeFamilyTree'
62             ['documentCanvas'].forEach(function(moduleName) {
63                 if(!origin || moduleName != origin)
64                     sandbox.getModule(moduleName).dimElement(element);
65             });
66         },
67         jumpToDocumentElement: function(element) {
68             sandbox.getModule('documentCanvas').jumpToElement(element);
69         },
70         updateCurrentNodeElement: function(nodeElement) {
71             sandbox.getModule('nodePane').setNodeElement(nodeElement);
72             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
73             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
74         },
75         updateCurrentTextElement: function(textElement) {
76             sandbox.getModule('nodeFamilyTree').setElement(textElement);
77         },
78         resetDocument: function(document, reason) {
79             var modules = [];
80             if(reason === 'source_edit')
81                 modules = ['documentCanvas', 'metadataEditor'];
82             else if (reason === 'edit')
83                 modules = ['sourceEditor'];
84             else if (reason === 'revert')
85                 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];
86                 
87             modules.forEach(function(moduleName) {
88                 sandbox.getModule(moduleName).setDocument(document);
89             });
90         }
91     };
92     
93
94     var views = {
95         mainLayout: new layout.Layout(mainLayoutTemplate),
96         mainTabs: (new tabs.View()).render(),
97         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
98         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
99         currentNodePaneLayout: new vbox.VBox(),
100         diffLayout: new layout.Layout(diffLayoutTemplate)
101     };
102     
103     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
104     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());
105     addMainTab(gettext('Source'), 'sourceEditor',  '');
106     addMainTab('Historia', 'history', views.diffLayout.getAsView());
107     
108     sandbox.getDOM().append(views.mainLayout.getAsView());
109     
110     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
111
112     views.mainTabs.on('tabSelected', function(event) {
113         if(event.prevSlug) {
114             synchronizeTab(event.prevSlug);
115         }
116     });
117     
118     /* Events handling */
119     
120     var eventHandlers = {};
121      
122     eventHandlers.sourceEditor = {
123         ready: function() {
124             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
125             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
126         },
127         xmlChanged: function() {
128             dirty.sourceEditor = true;
129         },
130         documentSet: function() {
131             dirty.sourceEditor = false;
132         }
133     };
134     
135     eventHandlers.data = {
136         ready: function() {
137             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
138             
139             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
140                 sandbox.getModule(moduleName).start();
141             });
142         },
143         documentChanged: function(document, reason) {
144             commands.resetDocument(document, reason);
145         },
146         savingStarted: function() {
147             sandbox.getModule('mainBar').setCommandEnabled('save', false);
148             sandbox.getModule('indicator').showMessage(gettext('Saving...'));
149         },
150         savingEnded: function(status) {
151             sandbox.getModule('mainBar').setCommandEnabled('save', true);
152             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
153         },
154         restoringStarted: function(event) {
155             sandbox.getModule('mainBar').setCommandEnabled('save', false);
156             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
157         },
158         historyItemAdded: function(item) {
159             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
160         },
161         diffFetched: function(diff) {
162             sandbox.getModule('diffViewer').setDiff(diff);
163         },
164         documentReverted: function(event) {
165             commands.resetDocument(event.document, 'revert');
166             sandbox.getModule('mainBar').setCommandEnabled('save', true);
167             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
168             sandbox.getModule('mainBar').setVersion(event.current_version);
169         }
170     };
171     
172     eventHandlers.mainBar = {
173         ready: function() {
174             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
175             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
176         },
177         'cmd.save': function() {
178             synchronizeTab(views.mainTabs.getCurrentSlug());
179             sandbox.getModule('data').saveDocument();
180         }
181     };
182     
183     eventHandlers.indicator = {
184         ready: function() {
185             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
186         }
187     };
188     
189
190     
191     eventHandlers.documentCanvas = {
192         ready: function() {
193             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
194             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
195         },
196         documentSet: function() {
197             dirty.documentCanvas = false;
198         },
199         
200         currentTextElementSet: function(textElement) {
201             commands.updateCurrentTextElement(textElement);
202         },
203
204         currentNodeElementSet: function(nodeElement) {
205             commands.updateCurrentNodeElement(nodeElement);
206         },
207         
208         currentNodeElementChanged: function(nodeElement) {
209             commands.updateCurrentNodeElement(nodeElement);
210         },
211
212         contentChanged: function() {
213             dirty.documentCanvas = true;
214         },
215
216         nodeHovered: function(canvasNode) {
217             commands.highlightDocumentNode(canvasNode);
218         },
219         
220         nodeBlured: function(canvasNode) {
221             commands.dimDocumentNode(canvasNode);
222         }
223     };
224
225     eventHandlers.nodePane = {
226         ready: function() {
227             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
228         },
229         
230         nodeElementChange: function(attr, value) {
231             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
232         }
233     };
234     
235     eventHandlers.metadataEditor = {
236         ready: function() {
237             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
238             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
239         },
240         metadataChanged: function(metadata) {
241             dirty.metadataEditor = true;
242         },
243         metadataSet: function() {
244             dirty.metadataEditor = false;
245         },
246     };
247     
248     eventHandlers.nodeFamilyTree = {
249         ready: function() {
250             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
251         },
252         elementEntered: function(element) {
253             commands.highlightDocumentElement(element, 'nodeFamilyTree');
254         },
255         elementLeft: function(element) {
256             commands.dimDocumentElement(element, 'nodeFamilyTree');
257         },
258         elementClicked: function(element) {
259             commands.jumpToDocumentElement(element);
260         }
261     };
262     
263     eventHandlers.documentToolbar = {
264         ready: function() {
265             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
266         },
267         command: function(cmd, params) {
268             sandbox.getModule('documentCanvas').command(cmd, params);
269         }
270     };
271     
272     eventHandlers.nodeBreadCrumbs = {
273         ready: function() {
274             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
275         },
276         elementEntered: function(element) {
277             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
278         },
279         elementLeft: function(element) {
280             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
281         },
282         elementClicked: function(element) {
283             commands.jumpToDocumentElement(element);
284         }        
285     };
286     
287     eventHandlers.documentHistory = {
288         ready: function() {
289             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
290             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
291         },
292         compare: function(ver1, ver2) {
293             sandbox.getModule('data').fetchDiff(ver1, ver2);
294         },
295         restoreVersion: function(event) {
296             sandbox.getModule('data').restoreVersion(event);
297         },
298         displayVersion: function(event) {
299             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
300         }
301     };
302     
303     eventHandlers.diffViewer = {
304         ready: function() {
305             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
306         }
307     };
308     
309     /* api */
310     
311     return {
312         start: function() {
313             sandbox.getModule('data').start();
314         },
315         handleEvent: function(moduleName, eventName, args) {
316             if('')
317                 wysiwigHandler.handleEvent(moduleName, eventName, args);
318             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
319                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
320             }
321         }
322     };
323 };
324
325 });