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