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