5 'libs/text!./mainLayout.html',
6 'libs/text!./editingLayout.html',
7 'libs/text!./diffLayout.html',
8 ], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
12 return function(sandbox) {
14 function addMainTab(title, slug, view) {
15 views.mainTabs.addTab(title, slug, view);
20 documentCanvas: false,
21 metadataEditor: false,
24 var synchronizeTab = function(slug) {
25 function tabIsDirty(slug) {
26 if(slug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor))
28 if(slug === 'sourceEditor' && dirty.sourceEditor)
33 if(tabIsDirty(slug)) {
35 if(slug === 'sourceEditor') {
36 doc = sandbox.getModule('sourceEditor').getDocument();
37 reason = 'source_edit';
38 dirty.sourceEditor = false;
40 if(slug === 'editor') {
41 doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();
42 if(dirty.metadataEditor) {
43 doc = sandbox.getModule('metadataEditor').attachMetadata(doc);
46 dirty.documentCanvas = dirty.metadataEditor = false;
48 sandbox.getModule('data').commitDocument(doc, reason);
53 highlightDocumentElement: function(element, origin) {
54 ///'nodeBreadCrumbs', 'nodeFamilyTree'
55 ['documentCanvas', ].forEach(function(moduleName) {
56 if(!origin || moduleName != origin)
57 sandbox.getModule(moduleName).highlightElement(element);
60 dimDocumentElement: function(element, origin) {
61 //'nodeBreadCrumbs', 'nodeFamilyTree'
62 ['documentCanvas'].forEach(function(moduleName) {
63 if(!origin || moduleName != origin)
64 sandbox.getModule(moduleName).dimElement(element);
67 jumpToDocumentElement: function(element) {
68 sandbox.getModule('documentCanvas').jumpToElement(element);
70 updateCurrentNodeElement: function(nodeElement) {
71 sandbox.getModule('nodePane').setNodeElement(nodeElement);
72 sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
73 sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
75 updateCurrentTextElement: function(textElement) {
76 sandbox.getModule('nodeFamilyTree').setElement(textElement);
78 resetDocument: function(document, reason) {
80 if(reason === 'source_edit')
81 modules = ['documentCanvas', 'metadataEditor'];
82 else if (reason === 'edit')
83 modules = ['sourceEditor'];
84 else if (reason === 'revert')
85 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];
87 modules.forEach(function(moduleName) {
88 sandbox.getModule(moduleName).setDocument(document);
95 mainLayout: new layout.Layout(mainLayoutTemplate),
96 mainTabs: (new tabs.View()).render(),
97 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
98 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
99 currentNodePaneLayout: new vbox.VBox(),
100 diffLayout: new layout.Layout(diffLayoutTemplate)
103 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
104 addMainTab('Edytor', 'editor', views.visualEditing.getAsView());
105 addMainTab(gettext('Source'), 'sourceEditor', '');
106 addMainTab('Historia', 'history', views.diffLayout.getAsView());
108 sandbox.getDOM().append(views.mainLayout.getAsView());
110 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
112 views.mainTabs.on('tabSelected', function(event) {
114 synchronizeTab(event.prevSlug);
118 /* Events handling */
120 var eventHandlers = {};
122 eventHandlers.sourceEditor = {
124 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
125 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
127 xmlChanged: function() {
128 dirty.sourceEditor = true;
130 documentSet: function() {
131 dirty.sourceEditor = false;
135 eventHandlers.data = {
137 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
139 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
140 sandbox.getModule(moduleName).start();
143 documentChanged: function(document, reason) {
144 commands.resetDocument(document, reason);
146 savingStarted: function() {
147 sandbox.getModule('mainBar').setCommandEnabled('save', false);
148 sandbox.getModule('indicator').showMessage(gettext('Saving...'));
150 savingEnded: function(status) {
151 sandbox.getModule('mainBar').setCommandEnabled('save', true);
152 sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
154 restoringStarted: function(event) {
155 sandbox.getModule('mainBar').setCommandEnabled('save', false);
156 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
158 historyItemAdded: function(item) {
159 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
161 diffFetched: function(diff) {
162 sandbox.getModule('diffViewer').setDiff(diff);
164 documentReverted: function(event) {
165 commands.resetDocument(event.document, 'revert');
166 sandbox.getModule('mainBar').setCommandEnabled('save', true);
167 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
168 sandbox.getModule('mainBar').setVersion(event.current_version);
172 eventHandlers.mainBar = {
174 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
175 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
177 'cmd.save': function() {
178 synchronizeTab(views.mainTabs.getCurrentSlug());
179 sandbox.getModule('data').saveDocument();
183 eventHandlers.indicator = {
185 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
191 eventHandlers.documentCanvas = {
193 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
194 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
196 documentSet: function() {
197 dirty.documentCanvas = false;
200 currentTextElementSet: function(textElement) {
201 commands.updateCurrentTextElement(textElement);
204 currentNodeElementSet: function(nodeElement) {
205 commands.updateCurrentNodeElement(nodeElement);
208 contentChanged: function() {
209 dirty.documentCanvas = true;
212 nodeHovered: function(canvasNode) {
213 commands.highlightDocumentNode(canvasNode);
216 nodeBlured: function(canvasNode) {
217 commands.dimDocumentNode(canvasNode);
221 eventHandlers.nodePane = {
223 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
226 nodeElementChange: function(attr, value) {
227 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
231 eventHandlers.metadataEditor = {
233 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
234 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
236 metadataChanged: function(metadata) {
237 dirty.metadataEditor = true;
239 metadataSet: function() {
240 dirty.metadataEditor = false;
244 eventHandlers.nodeFamilyTree = {
246 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
248 elementEntered: function(element) {
249 commands.highlightDocumentElement(element, 'nodeFamilyTree');
251 elementLeft: function(element) {
252 commands.dimDocumentElement(element, 'nodeFamilyTree');
254 elementClicked: function(element) {
255 commands.jumpToDocumentElement(element);
259 eventHandlers.documentToolbar = {
261 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
263 command: function(cmd, params) {
264 sandbox.getModule('documentCanvas').command(cmd, params);
268 eventHandlers.nodeBreadCrumbs = {
270 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
272 elementEntered: function(element) {
273 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
275 elementLeft: function(element) {
276 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
278 elementClicked: function(element) {
279 commands.jumpToDocumentElement(element);
283 eventHandlers.documentHistory = {
285 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
286 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
288 compare: function(ver1, ver2) {
289 sandbox.getModule('data').fetchDiff(ver1, ver2);
291 restoreVersion: function(event) {
292 sandbox.getModule('data').restoreVersion(event);
294 displayVersion: function(event) {
295 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
299 eventHandlers.diffViewer = {
301 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
309 sandbox.getModule('data').start();
311 handleEvent: function(moduleName, eventName, args) {
313 wysiwigHandler.handleEvent(moduleName, eventName, args);
314 else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
315 eventHandlers[moduleName][eventName].apply(eventHandlers, args);