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