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 highlightDocumentNode: function(canvasNode, origin) {
54 ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
55 if(!origin || moduleName != origin)
56 sandbox.getModule(moduleName).highlightNode(canvasNode);
59 dimDocumentNode: function(canvasNode, origin) {
60 ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
61 if(!origin || moduleName != origin)
62 sandbox.getModule(moduleName).dimNode(canvasNode);
65 selectNode: function(canvasNode, origin) {
66 sandbox.getModule('documentCanvas').selectNode(canvasNode);
67 sandbox.getModule('nodePane').setNode(canvasNode);
68 sandbox.getModule('nodeFamilyTree').setNode(canvasNode);
69 sandbox.getModule('nodeBreadCrumbs').setNode(canvasNode);
72 resetDocument: function(document, reason) {
74 if(reason === 'source_edit')
75 modules = ['documentCanvas', 'metadataEditor'];
76 else if (reason === 'edit')
77 modules = ['sourceEditor'];
78 else if (reason === 'revert')
79 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];
81 modules.forEach(function(moduleName) {
82 sandbox.getModule(moduleName).setDocument(document);
89 mainLayout: new layout.Layout(mainLayoutTemplate),
90 mainTabs: (new tabs.View()).render(),
91 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
92 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
93 currentNodePaneLayout: new vbox.VBox(),
94 diffLayout: new layout.Layout(diffLayoutTemplate)
97 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
98 addMainTab('Edytor', 'editor', views.visualEditing.getAsView());
99 addMainTab(gettext('Source'), 'sourceEditor', '');
100 addMainTab('Historia', 'history', views.diffLayout.getAsView());
102 sandbox.getDOM().append(views.mainLayout.getAsView());
104 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
106 views.mainTabs.on('tabSelected', function(event) {
108 synchronizeTab(event.prevSlug);
112 /* Events handling */
114 var eventHandlers = {};
116 eventHandlers.sourceEditor = {
118 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
119 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
121 xmlChanged: function() {
122 dirty.sourceEditor = true;
124 documentSet: function() {
125 dirty.sourceEditor = false;
129 eventHandlers.data = {
131 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
133 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
134 sandbox.getModule(moduleName).start();
137 documentChanged: function(document, reason) {
138 commands.resetDocument(document, reason);
140 savingStarted: function() {
141 sandbox.getModule('mainBar').setCommandEnabled('save', false);
142 sandbox.getModule('indicator').showMessage(gettext('Saving...'));
144 savingEnded: function(status) {
145 sandbox.getModule('mainBar').setCommandEnabled('save', true);
146 sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
148 restoringStarted: function(event) {
149 sandbox.getModule('mainBar').setCommandEnabled('save', false);
150 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
152 historyItemAdded: function(item) {
153 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
155 diffFetched: function(diff) {
156 sandbox.getModule('diffViewer').setDiff(diff);
158 documentReverted: function(event) {
159 commands.resetDocument(event.document, 'revert');
160 sandbox.getModule('mainBar').setCommandEnabled('save', true);
161 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
162 sandbox.getModule('mainBar').setVersion(event.current_version);
166 eventHandlers.mainBar = {
168 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
169 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
171 'cmd.save': function() {
172 synchronizeTab(views.mainTabs.getCurrentSlug());
173 sandbox.getModule('data').saveDocument();
177 eventHandlers.indicator = {
179 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
185 eventHandlers.documentCanvas = {
187 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
188 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
190 documentSet: function() {
191 dirty.documentCanvas = false;
194 nodeSelected: function(canvasNode) {
195 commands.selectNode(canvasNode);
198 contentChanged: function() {
199 dirty.documentCanvas = true;
202 nodeHovered: function(canvasNode) {
203 commands.highlightDocumentNode(canvasNode);
206 nodeBlured: function(canvasNode) {
207 commands.dimDocumentNode(canvasNode);
211 eventHandlers.nodePane = {
213 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
216 nodeChanged: function(attr, value) {
217 sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);
221 eventHandlers.metadataEditor = {
223 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
224 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
226 metadataChanged: function(metadata) {
227 dirty.metadataEditor = true;
229 metadataSet: function() {
230 dirty.metadataEditor = false;
234 eventHandlers.nodeFamilyTree = {
236 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
238 nodeEntered: function(canvasNode) {
239 commands.highlightDocumentNode(canvasNode, 'nodeFamilyTree');
241 nodeLeft: function(canvasNode) {
242 commands.dimDocumentNode(canvasNode, 'nodeFamilyTree');
244 nodeSelected: function(canvasNode) {
245 commands.selectNode(canvasNode);
249 eventHandlers.documentToolbar = {
251 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
253 toggleGrid: function(toggle) {
254 sandbox.getModule('documentCanvas').toggleGrid(toggle);
256 newNodeRequested: function(wlxmlTag, wlxmlClass) {
257 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);
259 command: function(cmd, meta) {
260 sandbox.getModule('documentCanvas').command(cmd, meta);
264 eventHandlers.nodeBreadCrumbs = {
266 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
268 nodeHighlighted: function(canvasNode) {
269 commands.highlightDocumentNode(canvasNode, 'nodeBreadCrumbs');
271 nodeDimmed: function(canvasNode) {
272 commands.dimDocumentNode(canvasNode, 'nodeBreadCrumbs');
274 nodeSelected: function(canvasNode) {
275 commands.selectNode(canvasNode);
279 eventHandlers.documentHistory = {
281 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
282 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
284 compare: function(ver1, ver2) {
285 sandbox.getModule('data').fetchDiff(ver1, ver2);
287 restoreVersion: function(event) {
288 sandbox.getModule('data').restoreVersion(event);
290 displayVersion: function(event) {
291 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
295 eventHandlers.diffViewer = {
297 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
305 sandbox.getModule('data').start();
307 handleEvent: function(moduleName, eventName, args) {
309 wysiwigHandler.handleEvent(moduleName, eventName, args);
310 else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
311 eventHandlers[moduleName][eventName].apply(eventHandlers, args);