6 'libs/text!./mainLayout.html',
7 'libs/text!./editingLayout.html',
8 'libs/text!./diffLayout.html',
9 ], function(_, layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
13 return function(sandbox) {
17 function addMainTab(title, slug, view) {
18 views.mainTabs.addTab(title, slug, view);
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);
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);
38 jumpToDocumentElement: function(element) {
39 sandbox.getModule('documentCanvas').jumpToElement(element);
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);
47 updateCurrentTextElement: function(textElement) {
48 sandbox.getModule('nodeFamilyTree').setElement(textElement);
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)
62 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
63 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
64 addMainTab(gettext('Source'), 'sourceEditor', '');
65 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
67 sandbox.getDOM().append(views.mainLayout.getAsView());
69 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
71 var wlxmlDocument, documentIsDirty;
75 var eventHandlers = {};
77 eventHandlers.sourceEditor = {
79 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
80 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
84 eventHandlers.data = {
86 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
88 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
89 sandbox.getModule(moduleName).start();
92 wlxmlDocument = sandbox.getModule('data').getDocument();
93 documentIsDirty = false;
94 wlxmlDocument.on('change', function() {
95 documentIsDirty = true;
97 wlxmlDocument.on('contentSet', function() {
98 documentIsDirty = true;
101 savingStarted: function() {
102 sandbox.getModule('mainBar').setCommandEnabled('save', false);
103 sandbox.getModule('indicator').showMessage(gettext('Saving...'));
105 savingEnded: function(status, current_version) {
107 documentIsDirty = false;
108 sandbox.getModule('mainBar').setCommandEnabled('save', true);
109 sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
110 sandbox.getModule('mainBar').setVersion(current_version);
112 restoringStarted: function(event) {
113 sandbox.getModule('mainBar').setCommandEnabled('save', false);
114 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
116 historyItemAdded: function(item) {
117 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
119 diffFetched: function(diff) {
120 sandbox.getModule('diffViewer').setDiff(diff);
122 documentReverted: function(version) {
123 documentIsDirty = false;
124 sandbox.getModule('mainBar').setCommandEnabled('save', true);
125 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
126 sandbox.getModule('mainBar').setVersion(version);
130 eventHandlers.mainBar = {
132 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
133 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
135 'cmd.save': function() {
136 sandbox.getModule('data').saveDocument();
140 eventHandlers.indicator = {
142 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
148 eventHandlers.documentCanvas = {
150 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
151 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
154 currentTextElementSet: function(textElement) {
155 commands.updateCurrentTextElement(textElement);
158 currentNodeElementSet: function(nodeElement) {
159 commands.updateCurrentNodeElement(nodeElement);
162 currentNodeElementChanged: function(nodeElement) {
163 commands.updateCurrentNodeElement(nodeElement);
166 nodeHovered: function(canvasNode) {
167 commands.highlightDocumentNode(canvasNode);
170 nodeBlured: function(canvasNode) {
171 commands.dimDocumentNode(canvasNode);
175 eventHandlers.nodePane = {
177 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
180 nodeElementChange: function(attr, value) {
181 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
185 eventHandlers.metadataEditor = {
187 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
188 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
192 eventHandlers.nodeFamilyTree = {
194 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
196 nodeEntered: function(node) {
197 commands.highlightDocumentElement(node, 'nodeFamilyTree');
199 nodeLeft: function(node) {
200 commands.dimDocumentElement(node, 'nodeFamilyTree');
202 nodeClicked: function(node) {
203 commands.jumpToDocumentElement(node);
207 eventHandlers.documentToolbar = {
209 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
211 command: function(cmd, params) {
212 sandbox.getModule('documentCanvas').command(cmd, params);
216 eventHandlers.nodeBreadCrumbs = {
218 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
220 elementEntered: function(element) {
221 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
223 elementLeft: function(element) {
224 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
226 elementClicked: function(element) {
227 commands.jumpToDocumentElement(element);
231 eventHandlers.documentHistory = {
233 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
234 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
236 compare: function(ver1, ver2) {
237 sandbox.getModule('data').fetchDiff(ver1, ver2);
239 restoreVersion: function(version) {
240 sandbox.getModule('data').restoreVersion(version);
242 displayVersion: function(event) {
244 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
248 eventHandlers.diffViewer = {
250 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
254 window.addEventListener('beforeunload', function(event) {
255 var txt = gettext('Do you really want to exit?');
256 if(documentIsDirty) {
257 txt += ' ' + gettext('Document contains unsaved changes!');
259 event.returnValue = txt; // FF
260 return txt; // Chrome
267 sandbox.getModule('data').start();
269 handleEvent: function(moduleName, eventName, args) {
270 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
271 eventHandlers[moduleName][eventName].apply(eventHandlers, args);