5 'fnpjs/logging/logging',
7 'libs/text!./mainLayout.html',
8 'libs/text!./editingLayout.html',
9 'libs/text!./diffLayout.html',
10 ], function(_, layout, vbox, logging, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
14 return function(sandbox) {
18 var logger = logging.getLogger('editor.modules.rng');
20 function addMainTab(title, slug, view) {
21 views.mainTabs.addTab(title, slug, view);
25 highlightDocumentElement: function(element, origin) {
26 ///'nodeBreadCrumbs', 'nodeFamilyTree'
27 ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
28 if(!origin || moduleName !== origin) {
29 sandbox.getModule(moduleName).highlightElement(element);
33 dimDocumentElement: function(element, origin) {
34 //'nodeBreadCrumbs', 'nodeFamilyTree'
35 ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
36 if(!origin || moduleName !== origin) {
37 sandbox.getModule(moduleName).dimElement(element);
41 jumpToDocumentElement: function(element) {
42 sandbox.getModule('documentCanvas').jumpToElement(element);
44 updateCurrentNodeElement: function(nodeElement) {
45 sandbox.getModule('nodePane').setNodeElement(nodeElement);
46 sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
47 sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
48 sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
49 sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
51 updateCurrentTextElement: function(textElement) {
52 sandbox.getModule('nodeFamilyTree').setElement(textElement);
58 mainLayout: new layout.Layout(mainLayoutTemplate),
59 mainTabs: (new tabs.View()).render(),
60 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
61 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
62 currentNodePaneLayout: new vbox.VBox(),
63 diffLayout: new layout.Layout(diffLayoutTemplate)
66 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
67 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
68 addMainTab(gettext('Source'), 'sourceEditor', '');
69 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
71 sandbox.getDOM().append(views.mainLayout.getAsView());
73 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
75 var wlxmlDocument, documentIsDirty;
79 var eventHandlers = {};
81 eventHandlers.sourceEditor = {
83 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
84 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
88 eventHandlers.data = {
90 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
92 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
93 sandbox.getModule(moduleName).start();
96 wlxmlDocument = sandbox.getModule('data').getDocument();
97 documentIsDirty = false;
98 wlxmlDocument.on('change', function() {
99 documentIsDirty = true;
101 wlxmlDocument.on('contentSet', function() {
102 documentIsDirty = true;
105 savingStarted: function(what) {
107 remote: gettext('Saving document'),
108 local: gettext('Saving local copy')
110 sandbox.getModule('mainBar').setCommandEnabled('save', false);
111 sandbox.getModule('indicator').showMessage(msg[what] + '...');
113 savingEnded: function(status, what, data) {
116 remote: gettext('Document saved'),
117 local: gettext('Local copy saved')
119 documentIsDirty = false;
120 sandbox.getModule('mainBar').setCommandEnabled('save', true);
121 sandbox.getModule('indicator').clearMessage({message: msg[what]});
122 sandbox.getModule('mainBar').setVersion(data.version);
124 restoringStarted: function(event) {
125 sandbox.getModule('mainBar').setCommandEnabled('save', false);
126 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
128 historyItemAdded: function(item) {
129 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
131 diffFetched: function(diff) {
132 sandbox.getModule('diffViewer').setDiff(diff);
134 documentReverted: function(version) {
135 documentIsDirty = false;
136 sandbox.getModule('mainBar').setCommandEnabled('save', true);
137 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
138 sandbox.getModule('mainBar').setVersion(version);
142 eventHandlers.mainBar = {
144 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
145 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
147 'cmd.save': function() {
148 var sourceEditor = sandbox.getModule('sourceEditor');
149 if(!sourceEditor.changesCommited()) {
150 logger.debug('Source editor has uncommited changes, commiting...');
151 sourceEditor.commitChanges();
153 sandbox.getModule('data').saveDocument();
155 'cmd.drop-draft': function() {
156 sandbox.getModule('data').dropDraft();
160 eventHandlers.indicator = {
162 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
168 eventHandlers.documentCanvas = {
170 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
171 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
174 currentTextElementSet: function(textElement) {
175 commands.updateCurrentTextElement(textElement);
178 currentNodeElementSet: function(nodeElement) {
179 commands.updateCurrentNodeElement(nodeElement);
182 currentNodeElementChanged: function(nodeElement) {
183 commands.updateCurrentNodeElement(nodeElement);
186 nodeHovered: function(canvasNode) {
187 commands.highlightDocumentNode(canvasNode);
190 nodeBlured: function(canvasNode) {
191 commands.dimDocumentNode(canvasNode);
195 eventHandlers.nodePane = {
197 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
200 nodeElementChange: function(attr, value) {
201 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
205 eventHandlers.metadataEditor = {
207 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
208 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
212 eventHandlers.nodeFamilyTree = {
214 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
216 nodeEntered: function(node) {
217 commands.highlightDocumentElement(node, 'nodeFamilyTree');
219 nodeLeft: function(node) {
220 commands.dimDocumentElement(node, 'nodeFamilyTree');
222 nodeClicked: function(node) {
223 commands.jumpToDocumentElement(node);
227 eventHandlers.documentToolbar = {
229 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
231 command: function(cmd, params) {
232 sandbox.getModule('documentCanvas').command(cmd, params);
236 eventHandlers.nodeBreadCrumbs = {
238 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
240 elementEntered: function(element) {
241 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
243 elementLeft: function(element) {
244 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
246 elementClicked: function(element) {
247 commands.jumpToDocumentElement(element);
251 eventHandlers.documentHistory = {
253 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
254 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
256 compare: function(ver1, ver2) {
257 sandbox.getModule('data').fetchDiff(ver1, ver2);
259 restoreVersion: function(version) {
260 sandbox.getModule('data').restoreVersion(version);
262 displayVersion: function(event) {
264 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
268 eventHandlers.diffViewer = {
270 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
274 window.addEventListener('beforeunload', function(event) {
275 var txt = gettext('Do you really want to exit?');
276 if(documentIsDirty) {
277 txt += ' ' + gettext('Document contains unsaved changes!');
279 event.returnValue = txt; // FF
280 return txt; // Chrome
287 sandbox.getModule('data').start();
289 handleEvent: function(moduleName, eventName, args) {
290 var eventRepr = moduleName + '.' + eventName;
291 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
292 logger.debug('Handling event ' + eventRepr);
293 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
295 logger.warning('No event handler for ' + eventRepr);