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);
46 sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
48 updateCurrentTextElement: function(textElement) {
49 sandbox.getModule('nodeFamilyTree').setElement(textElement);
55 mainLayout: new layout.Layout(mainLayoutTemplate),
56 mainTabs: (new tabs.View()).render(),
57 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
58 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
59 currentNodePaneLayout: new vbox.VBox(),
60 diffLayout: new layout.Layout(diffLayoutTemplate)
63 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
64 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
65 addMainTab(gettext('Source'), 'sourceEditor', '');
66 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
68 sandbox.getDOM().append(views.mainLayout.getAsView());
70 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
72 var wlxmlDocument, documentIsDirty;
76 var eventHandlers = {};
78 eventHandlers.sourceEditor = {
80 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
81 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
85 eventHandlers.data = {
87 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
89 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
90 sandbox.getModule(moduleName).start();
93 wlxmlDocument = sandbox.getModule('data').getDocument();
94 documentIsDirty = false;
95 wlxmlDocument.on('change', function() {
96 documentIsDirty = true;
98 wlxmlDocument.on('contentSet', function() {
99 documentIsDirty = true;
102 savingStarted: function(what) {
104 remote: gettext('Saving document'),
105 local: gettext('Saving local copy')
107 sandbox.getModule('mainBar').setCommandEnabled('save', false);
108 sandbox.getModule('indicator').showMessage(msg[what] + '...');
110 savingEnded: function(status, what, current_version) {
113 remote: gettext('Document saved'),
114 local: gettext('Local copy saved')
116 documentIsDirty = false;
117 sandbox.getModule('mainBar').setCommandEnabled('save', true);
118 sandbox.getModule('indicator').clearMessage({message: msg[what]});
119 sandbox.getModule('mainBar').setVersion(current_version);
121 restoringStarted: function(event) {
122 sandbox.getModule('mainBar').setCommandEnabled('save', false);
123 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
125 historyItemAdded: function(item) {
126 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
128 diffFetched: function(diff) {
129 sandbox.getModule('diffViewer').setDiff(diff);
131 documentReverted: function(version) {
132 documentIsDirty = false;
133 sandbox.getModule('mainBar').setCommandEnabled('save', true);
134 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
135 sandbox.getModule('mainBar').setVersion(version);
139 eventHandlers.mainBar = {
141 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
142 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
144 'cmd.save': function() {
145 sandbox.getModule('data').saveDocument();
149 eventHandlers.indicator = {
151 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
157 eventHandlers.documentCanvas = {
159 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
160 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
163 currentTextElementSet: function(textElement) {
164 commands.updateCurrentTextElement(textElement);
167 currentNodeElementSet: function(nodeElement) {
168 commands.updateCurrentNodeElement(nodeElement);
171 currentNodeElementChanged: function(nodeElement) {
172 commands.updateCurrentNodeElement(nodeElement);
175 nodeHovered: function(canvasNode) {
176 commands.highlightDocumentNode(canvasNode);
179 nodeBlured: function(canvasNode) {
180 commands.dimDocumentNode(canvasNode);
184 eventHandlers.nodePane = {
186 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
189 nodeElementChange: function(attr, value) {
190 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
194 eventHandlers.metadataEditor = {
196 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
197 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
201 eventHandlers.nodeFamilyTree = {
203 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
205 nodeEntered: function(node) {
206 commands.highlightDocumentElement(node, 'nodeFamilyTree');
208 nodeLeft: function(node) {
209 commands.dimDocumentElement(node, 'nodeFamilyTree');
211 nodeClicked: function(node) {
212 commands.jumpToDocumentElement(node);
216 eventHandlers.documentToolbar = {
218 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
220 command: function(cmd, params) {
221 sandbox.getModule('documentCanvas').command(cmd, params);
225 eventHandlers.nodeBreadCrumbs = {
227 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
229 elementEntered: function(element) {
230 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
232 elementLeft: function(element) {
233 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
235 elementClicked: function(element) {
236 commands.jumpToDocumentElement(element);
240 eventHandlers.documentHistory = {
242 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
243 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
245 compare: function(ver1, ver2) {
246 sandbox.getModule('data').fetchDiff(ver1, ver2);
248 restoreVersion: function(version) {
249 sandbox.getModule('data').restoreVersion(version);
251 displayVersion: function(event) {
253 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
257 eventHandlers.diffViewer = {
259 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
263 window.addEventListener('beforeunload', function(event) {
264 var txt = gettext('Do you really want to exit?');
265 if(documentIsDirty) {
266 txt += ' ' + gettext('Document contains unsaved changes!');
268 event.returnValue = txt; // FF
269 return txt; // Chrome
276 sandbox.getModule('data').start();
278 handleEvent: function(moduleName, eventName, args) {
279 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
280 eventHandlers[moduleName][eventName].apply(eventHandlers, args);