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