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').setDocument2(sandbox.getModule('data').getDocument2());
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 currentNodeElementChanged: function(nodeElement) {
209 commands.updateCurrentNodeElement(nodeElement);
210 dirty.documentCanvas = true;
213 contentChanged: function() {
214 dirty.documentCanvas = true;
217 nodeHovered: function(canvasNode) {
218 commands.highlightDocumentNode(canvasNode);
221 nodeBlured: function(canvasNode) {
222 commands.dimDocumentNode(canvasNode);
226 eventHandlers.nodePane = {
228 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
231 nodeElementChange: function(attr, value) {
232 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
236 eventHandlers.metadataEditor = {
238 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
239 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
241 metadataChanged: function(metadata) {
242 dirty.metadataEditor = true;
244 metadataSet: function() {
245 dirty.metadataEditor = false;
249 eventHandlers.nodeFamilyTree = {
251 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
253 elementEntered: function(element) {
254 commands.highlightDocumentElement(element, 'nodeFamilyTree');
256 elementLeft: function(element) {
257 commands.dimDocumentElement(element, 'nodeFamilyTree');
259 elementClicked: function(element) {
260 commands.jumpToDocumentElement(element);
264 eventHandlers.documentToolbar = {
266 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
268 command: function(cmd, params) {
269 sandbox.getModule('documentCanvas').command(cmd, params);
273 eventHandlers.nodeBreadCrumbs = {
275 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
277 elementEntered: function(element) {
278 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
280 elementLeft: function(element) {
281 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
283 elementClicked: function(element) {
284 commands.jumpToDocumentElement(element);
288 eventHandlers.documentHistory = {
290 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
291 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
293 compare: function(ver1, ver2) {
294 sandbox.getModule('data').fetchDiff(ver1, ver2);
296 restoreVersion: function(event) {
297 sandbox.getModule('data').restoreVersion(event);
299 displayVersion: function(event) {
300 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
304 eventHandlers.diffViewer = {
306 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
314 sandbox.getModule('data').start();
316 handleEvent: function(moduleName, eventName, args) {
318 wysiwigHandler.handleEvent(moduleName, eventName, args);
319 else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
320 eventHandlers[moduleName][eventName].apply(eventHandlers, args);