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