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