Bringing back cursor tests
[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             synchronizeTab(views.mainTabs.getCurrentSlug());
134             sandbox.getModule('data').saveDocument();
135         }
136     };
137     
138     eventHandlers.indicator = {
139         ready: function() {
140             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
141         }
142     };
143     
144
145     
146     eventHandlers.documentCanvas = {
147         ready: function() {
148             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
149             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
150         },
151         
152         currentTextElementSet: function(textElement) {
153             commands.updateCurrentTextElement(textElement);
154         },
155
156         currentNodeElementSet: function(nodeElement) {
157             commands.updateCurrentNodeElement(nodeElement);
158         },
159         
160         currentNodeElementChanged: function(nodeElement) {
161             commands.updateCurrentNodeElement(nodeElement);
162         },
163
164         nodeHovered: function(canvasNode) {
165             commands.highlightDocumentNode(canvasNode);
166         },
167         
168         nodeBlured: function(canvasNode) {
169             commands.dimDocumentNode(canvasNode);
170         }
171     };
172
173     eventHandlers.nodePane = {
174         ready: function() {
175             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
176         },
177         
178         nodeElementChange: function(attr, value) {
179             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
180         }
181     };
182     
183     eventHandlers.metadataEditor = {
184         ready: function() {
185             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
186             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
187         }
188     };
189     
190     eventHandlers.nodeFamilyTree = {
191         ready: function() {
192             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
193         },
194         nodeEntered: function(node) {
195             commands.highlightDocumentElement(node, 'nodeFamilyTree');
196         },
197         nodeLeft: function(node) {
198             commands.dimDocumentElement(node, 'nodeFamilyTree');
199         },
200         nodeClicked: function(node) {
201             commands.jumpToDocumentElement(node);
202         }
203     };
204     
205     eventHandlers.documentToolbar = {
206         ready: function() {
207             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
208         },
209         command: function(cmd, params) {
210             sandbox.getModule('documentCanvas').command(cmd, params);
211         }
212     };
213     
214     eventHandlers.nodeBreadCrumbs = {
215         ready: function() {
216             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
217         },
218         elementEntered: function(element) {
219             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
220         },
221         elementLeft: function(element) {
222             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
223         },
224         elementClicked: function(element) {
225             commands.jumpToDocumentElement(element);
226         }        
227     };
228     
229     eventHandlers.documentHistory = {
230         ready: function() {
231             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
232             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
233         },
234         compare: function(ver1, ver2) {
235             sandbox.getModule('data').fetchDiff(ver1, ver2);
236         },
237         restoreVersion: function(event) {
238             sandbox.getModule('data').restoreVersion(event);
239         },
240         displayVersion: function(event) {
241             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
242         }
243     };
244     
245     eventHandlers.diffViewer = {
246         ready: function() {
247             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
248         }
249     };
250     
251     /* api */
252     
253     return {
254         start: function() {
255             sandbox.getModule('data').start();
256         },
257         handleEvent: function(moduleName, eventName, args) {
258             if('')
259                 wysiwigHandler.handleEvent(moduleName, eventName, args);
260             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
261                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
262             }
263         }
264     };
265 };
266
267 });