Breaking node at beginning/end
[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         contentChanged: function() {
209             dirty.documentCanvas = true;
210         },
211
212         nodeHovered: function(canvasNode) {
213             commands.highlightDocumentNode(canvasNode);
214         },
215         
216         nodeBlured: function(canvasNode) {
217             commands.dimDocumentNode(canvasNode);
218         }
219     };
220
221     eventHandlers.nodePane = {
222         ready: function() {
223             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
224         },
225         
226         nodeElementChange: function(attr, value) {
227             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
228         }
229     };
230     
231     eventHandlers.metadataEditor = {
232         ready: function() {
233             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
234             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
235         },
236         metadataChanged: function(metadata) {
237             dirty.metadataEditor = true;
238         },
239         metadataSet: function() {
240             dirty.metadataEditor = false;
241         },
242     };
243     
244     eventHandlers.nodeFamilyTree = {
245         ready: function() {
246             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
247         },
248         elementEntered: function(element) {
249             commands.highlightDocumentElement(element, 'nodeFamilyTree');
250         },
251         elementLeft: function(element) {
252             commands.dimDocumentElement(element, 'nodeFamilyTree');
253         },
254         elementClicked: function(element) {
255             commands.jumpToDocumentElement(element);
256         }
257     };
258     
259     eventHandlers.documentToolbar = {
260         ready: function() {
261             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
262         },
263         command: function(cmd, params) {
264             sandbox.getModule('documentCanvas').command(cmd, params);
265         }
266     };
267     
268     eventHandlers.nodeBreadCrumbs = {
269         ready: function() {
270             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
271         },
272         elementEntered: function(element) {
273             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
274         },
275         elementLeft: function(element) {
276             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
277         },
278         elementClicked: function(element) {
279             commands.jumpToDocumentElement(element);
280         }        
281     };
282     
283     eventHandlers.documentHistory = {
284         ready: function() {
285             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
286             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
287         },
288         compare: function(ver1, ver2) {
289             sandbox.getModule('data').fetchDiff(ver1, ver2);
290         },
291         restoreVersion: function(event) {
292             sandbox.getModule('data').restoreVersion(event);
293         },
294         displayVersion: function(event) {
295             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
296         }
297     };
298     
299     eventHandlers.diffViewer = {
300         ready: function() {
301             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
302         }
303     };
304     
305     /* api */
306     
307     return {
308         start: function() {
309             sandbox.getModule('data').start();
310         },
311         handleEvent: function(moduleName, eventName, args) {
312             if('')
313                 wysiwigHandler.handleEvent(moduleName, eventName, args);
314             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
315                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
316             }
317         }
318     };
319 };
320
321 });