Converting end of lines from crlf to lf
[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         highlightDocumentNode: function(canvasNode, origin) {
54             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
55                 if(!origin || moduleName != origin)
56                     sandbox.getModule(moduleName).highlightNode(canvasNode);
57             });
58         },
59         dimDocumentNode: function(canvasNode, origin) {
60             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
61                 if(!origin || moduleName != origin)
62                     sandbox.getModule(moduleName).dimNode(canvasNode);
63             });
64         },
65         selectNode: function(canvasNode, origin) {
66             sandbox.getModule('documentCanvas').selectNode(canvasNode);
67             sandbox.getModule('nodePane').setNode(canvasNode);
68             sandbox.getModule('nodeFamilyTree').setNode(canvasNode);
69             sandbox.getModule('nodeBreadCrumbs').setNode(canvasNode);
70             
71         },
72         resetDocument: function(document, reason) {
73             var modules = [];
74             if(reason === 'source_edit')
75                 modules = ['documentCanvas', 'metadataEditor'];
76             else if (reason === 'edit')
77                 modules = ['sourceEditor'];
78             else if (reason === 'revert')
79                 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];
80                 
81             modules.forEach(function(moduleName) {
82                 sandbox.getModule(moduleName).setDocument(document);
83             });
84         }
85     };
86     
87
88     var views = {
89         mainLayout: new layout.Layout(mainLayoutTemplate),
90         mainTabs: (new tabs.View()).render(),
91         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
92         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
93         currentNodePaneLayout: new vbox.VBox(),
94         diffLayout: new layout.Layout(diffLayoutTemplate)
95     };
96     
97     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
98     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());
99     addMainTab(gettext('Source'), 'sourceEditor',  '');
100     addMainTab('Historia', 'history', views.diffLayout.getAsView());
101     
102     sandbox.getDOM().append(views.mainLayout.getAsView());
103     
104     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
105
106     views.mainTabs.on('tabSelected', function(event) {
107         if(event.prevSlug) {
108             synchronizeTab(event.prevSlug);
109         }
110     });
111     
112     /* Events handling */
113     
114     var eventHandlers = {};
115      
116     eventHandlers.sourceEditor = {
117         ready: function() {
118             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
119             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
120         },
121         xmlChanged: function() {
122             dirty.sourceEditor = true;
123         },
124         documentSet: function() {
125             dirty.sourceEditor = false;
126         }
127     };
128     
129     eventHandlers.data = {
130         ready: function() {
131             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
132             
133             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
134                 sandbox.getModule(moduleName).start();
135             });
136         },
137         documentChanged: function(document, reason) {
138             commands.resetDocument(document, reason);
139         },
140         savingStarted: function() {
141             sandbox.getModule('mainBar').setCommandEnabled('save', false);
142             sandbox.getModule('indicator').showMessage(gettext('Saving...'));
143         },
144         savingEnded: function(status) {
145             sandbox.getModule('mainBar').setCommandEnabled('save', true);
146             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
147         },
148         restoringStarted: function(event) {
149             sandbox.getModule('mainBar').setCommandEnabled('save', false);
150             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
151         },
152         historyItemAdded: function(item) {
153             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
154         },
155         diffFetched: function(diff) {
156             sandbox.getModule('diffViewer').setDiff(diff);
157         },
158         documentReverted: function(event) {
159             commands.resetDocument(event.document, 'revert');
160             sandbox.getModule('mainBar').setCommandEnabled('save', true);
161             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
162             sandbox.getModule('mainBar').setVersion(event.current_version);
163         }
164     };
165     
166     eventHandlers.mainBar = {
167         ready: function() {
168             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
169             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
170         },
171         'cmd.save': function() {
172             synchronizeTab(views.mainTabs.getCurrentSlug());
173             sandbox.getModule('data').saveDocument();
174         }
175     };
176     
177     eventHandlers.indicator = {
178         ready: function() {
179             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
180         }
181     };
182     
183
184     
185     eventHandlers.documentCanvas = {
186         ready: function() {
187             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
188             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
189         },
190         documentSet: function() {
191             dirty.documentCanvas = false;
192         },
193         
194         nodeSelected: function(canvasNode) {
195             commands.selectNode(canvasNode);
196         },
197         
198         contentChanged: function() {
199             dirty.documentCanvas = true;
200         },
201         
202         nodeHovered: function(canvasNode) {
203             commands.highlightDocumentNode(canvasNode);
204         },
205         
206         nodeBlured: function(canvasNode) {
207             commands.dimDocumentNode(canvasNode);
208         }
209     };
210
211     eventHandlers.nodePane = {
212         ready: function() {
213             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
214         },
215         
216         nodeChanged: function(attr, value) {
217             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);
218         }
219     };
220     
221     eventHandlers.metadataEditor = {
222         ready: function() {
223             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
224             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
225         },
226         metadataChanged: function(metadata) {
227             dirty.metadataEditor = true;
228         },
229         metadataSet: function() {
230             dirty.metadataEditor = false;
231         },
232     };
233     
234     eventHandlers.nodeFamilyTree = {
235         ready: function() {
236             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
237         },
238         nodeEntered: function(canvasNode) {
239             commands.highlightDocumentNode(canvasNode, 'nodeFamilyTree');
240         },
241         nodeLeft: function(canvasNode) {
242             commands.dimDocumentNode(canvasNode, 'nodeFamilyTree');
243         },
244         nodeSelected: function(canvasNode) {
245             commands.selectNode(canvasNode);
246         }
247     };
248     
249     eventHandlers.documentToolbar = {
250         ready: function() {
251             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
252         },
253         toggleGrid: function(toggle) {
254             sandbox.getModule('documentCanvas').toggleGrid(toggle);
255         },
256         newNodeRequested: function(wlxmlTag, wlxmlClass) {
257                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);
258         },
259         command: function(cmd, meta) {
260             sandbox.getModule('documentCanvas').command(cmd, meta);
261         }
262     };
263     
264     eventHandlers.nodeBreadCrumbs = {
265         ready: function() {
266             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
267         },
268         nodeHighlighted: function(canvasNode) {
269             commands.highlightDocumentNode(canvasNode, 'nodeBreadCrumbs');
270         },
271         nodeDimmed: function(canvasNode) {
272             commands.dimDocumentNode(canvasNode, 'nodeBreadCrumbs');
273         },
274         nodeSelected: function(canvasNode) {
275             commands.selectNode(canvasNode);
276         }        
277     };
278     
279     eventHandlers.documentHistory = {
280         ready: function() {
281             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
282             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
283         },
284         compare: function(ver1, ver2) {
285             sandbox.getModule('data').fetchDiff(ver1, ver2);
286         },
287         restoreVersion: function(event) {
288             sandbox.getModule('data').restoreVersion(event);
289         },
290         displayVersion: function(event) {
291             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
292         }
293     };
294     
295     eventHandlers.diffViewer = {
296         ready: function() {
297             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
298         }
299     };
300     
301     /* api */
302     
303     return {
304         start: function() {
305             sandbox.getModule('data').start();
306         },
307         handleEvent: function(moduleName, eventName, args) {
308             if('')
309                 wysiwigHandler.handleEvent(moduleName, eventName, args);
310             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
311                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
312             }
313         }
314     };
315 };
316
317 });