wip: extracting core transformations continued - detfineDocumentProperties from core
[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) {
95             void(status);
96             sandbox.getModule('mainBar').setCommandEnabled('save', true);
97             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
98         },
99         restoringStarted: function(event) {
100             sandbox.getModule('mainBar').setCommandEnabled('save', false);
101             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
102         },
103         historyItemAdded: function(item) {
104             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
105         },
106         diffFetched: function(diff) {
107             sandbox.getModule('diffViewer').setDiff(diff);
108         },
109         documentReverted: function(event) {
110             sandbox.getModule('mainBar').setCommandEnabled('save', true);
111             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
112             sandbox.getModule('mainBar').setVersion(event.current_version);
113         }
114     };
115     
116     eventHandlers.mainBar = {
117         ready: function() {
118             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
119             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
120         },
121         'cmd.save': function() {
122             sandbox.getModule('data').saveDocument();
123         }
124     };
125     
126     eventHandlers.indicator = {
127         ready: function() {
128             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
129         }
130     };
131     
132
133     
134     eventHandlers.documentCanvas = {
135         ready: function() {
136             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
137             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
138         },
139         
140         currentTextElementSet: function(textElement) {
141             commands.updateCurrentTextElement(textElement);
142         },
143
144         currentNodeElementSet: function(nodeElement) {
145             commands.updateCurrentNodeElement(nodeElement);
146         },
147         
148         currentNodeElementChanged: function(nodeElement) {
149             commands.updateCurrentNodeElement(nodeElement);
150         },
151
152         nodeHovered: function(canvasNode) {
153             commands.highlightDocumentNode(canvasNode);
154         },
155         
156         nodeBlured: function(canvasNode) {
157             commands.dimDocumentNode(canvasNode);
158         }
159     };
160
161     eventHandlers.nodePane = {
162         ready: function() {
163             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
164         },
165         
166         nodeElementChange: function(attr, value) {
167             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
168         }
169     };
170     
171     eventHandlers.metadataEditor = {
172         ready: function() {
173             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
174             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
175         }
176     };
177     
178     eventHandlers.nodeFamilyTree = {
179         ready: function() {
180             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
181         },
182         nodeEntered: function(node) {
183             commands.highlightDocumentElement(node, 'nodeFamilyTree');
184         },
185         nodeLeft: function(node) {
186             commands.dimDocumentElement(node, 'nodeFamilyTree');
187         },
188         nodeClicked: function(node) {
189             commands.jumpToDocumentElement(node);
190         }
191     };
192     
193     eventHandlers.documentToolbar = {
194         ready: function() {
195             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
196         },
197         command: function(cmd, params) {
198             sandbox.getModule('documentCanvas').command(cmd, params);
199         }
200     };
201     
202     eventHandlers.nodeBreadCrumbs = {
203         ready: function() {
204             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
205         },
206         elementEntered: function(element) {
207             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
208         },
209         elementLeft: function(element) {
210             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
211         },
212         elementClicked: function(element) {
213             commands.jumpToDocumentElement(element);
214         }
215     };
216     
217     eventHandlers.documentHistory = {
218         ready: function() {
219             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
220             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
221         },
222         compare: function(ver1, ver2) {
223             sandbox.getModule('data').fetchDiff(ver1, ver2);
224         },
225         restoreVersion: function(event) {
226             sandbox.getModule('data').restoreVersion(event);
227         },
228         displayVersion: function(event) {
229             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
230         }
231     };
232     
233     eventHandlers.diffViewer = {
234         ready: function() {
235             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
236         }
237     };
238     
239     /* api */
240     
241     return {
242         start: function() {
243             sandbox.getModule('data').start();
244         },
245         handleEvent: function(moduleName, eventName, args) {
246             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
247                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
248             }
249         }
250     };
251 };
252
253 });