6 'fnpjs/logging/logging',
8 'libs/text!./mainLayout.html',
9 'libs/text!./editingLayout.html',
10 'libs/text!./diffLayout.html',
11 ], function(documentSummary, _, layout, vbox, logging, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
15 return function(sandbox) {
19 var logger = logging.getLogger('editor.modules.rng');
21 function addMainTab(title, slug, view) {
22 views.mainTabs.addTab(title, slug, view);
26 highlightDocumentElement: function(element, origin) {
27 ///'nodeBreadCrumbs', 'nodeFamilyTree'
28 ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
29 if(!origin || moduleName !== origin) {
30 sandbox.getModule(moduleName).highlightElement(element);
34 dimDocumentElement: function(element, origin) {
35 //'nodeBreadCrumbs', 'nodeFamilyTree'
36 ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
37 if(!origin || moduleName !== origin) {
38 sandbox.getModule(moduleName).dimElement(element);
42 jumpToDocumentElement: function(element) {
43 sandbox.getModule('documentCanvas').jumpToElement(element);
45 updateCurrentNodeElement: function(nodeElement) {
46 sandbox.getModule('nodePane').setNodeElement(nodeElement);
47 sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
48 sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
49 sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
50 sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
52 updateCurrentTextElement: function(textElement) {
53 sandbox.getModule('nodeFamilyTree').setElement(textElement);
59 mainLayout: new layout.Layout(mainLayoutTemplate),
60 mainTabs: (new tabs.View()).render(),
61 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
62 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
63 currentNodePaneLayout: new vbox.VBox(),
64 diffLayout: new layout.Layout(diffLayoutTemplate)
67 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
68 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
69 addMainTab(gettext('Source'), 'sourceEditor', '');
70 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
72 sandbox.getDOM().append(views.mainLayout.getAsView());
74 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
76 var wlxmlDocument, documentIsDirty;
80 var eventHandlers = {};
82 eventHandlers.sourceEditor = {
84 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
85 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
89 eventHandlers.data = {
90 ready: function(usingDraft, draftTimestamp) {
91 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
93 documentSummary.init(sandbox.getConfig().documentSummaryView);
94 documentSummary.render(sandbox.getModule('data').getDocumentProperties());
95 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
96 views.currentNodePaneLayout.appendView(documentSummary.dom);
98 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
99 sandbox.getModule(moduleName).start();
102 wlxmlDocument = sandbox.getModule('data').getDocument();
103 documentIsDirty = false;
104 wlxmlDocument.on('change', function() {
105 documentIsDirty = true;
107 wlxmlDocument.on('contentSet', function() {
108 documentIsDirty = true;
111 draftDropped: function() {
112 documentSummary.setDraftField('-');
114 savingStarted: function(what) {
116 remote: gettext('Saving document'),
117 local: gettext('Saving local copy')
119 sandbox.getModule('mainBar').setCommandEnabled('save', false);
120 sandbox.getModule('indicator').showMessage(msg[what] + '...');
122 savingEnded: function(status, what, data) {
125 remote: gettext('Document saved'),
126 local: gettext('Local copy saved')
128 documentIsDirty = false;
129 sandbox.getModule('mainBar').setCommandEnabled('save', true);
130 sandbox.getModule('indicator').clearMessage({message: msg[what]});
131 if(status === 'success' && what === 'remote') {
132 sandbox.getModule('mainBar').setVersion(data.version);
133 documentSummary.render(data);
134 documentSummary.setDraftField('-');
136 if(what === 'local') {
137 documentSummary.setDraftField(data.timestamp);
140 restoringStarted: function(event) {
141 sandbox.getModule('mainBar').setCommandEnabled('save', false);
142 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
144 historyItemAdded: function(item) {
145 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
147 diffFetched: function(diff) {
148 sandbox.getModule('diffViewer').setDiff(diff);
150 documentReverted: function(version) {
151 documentIsDirty = false;
152 sandbox.getModule('mainBar').setCommandEnabled('save', true);
153 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
154 sandbox.getModule('mainBar').setVersion(version);
158 eventHandlers.mainBar = {
160 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
161 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
163 'cmd.save': function() {
164 var sourceEditor = sandbox.getModule('sourceEditor');
165 if(!sourceEditor.changesCommited()) {
166 logger.debug('Source editor has uncommited changes, commiting...');
167 sourceEditor.commitChanges();
169 sandbox.getModule('data').saveDocument();
171 'cmd.drop-draft': function() {
172 sandbox.getModule('data').dropDraft();
176 eventHandlers.indicator = {
178 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
184 eventHandlers.documentCanvas = {
186 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
187 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
190 currentTextElementSet: function(textElement) {
191 commands.updateCurrentTextElement(textElement);
194 currentNodeElementSet: function(nodeElement) {
195 commands.updateCurrentNodeElement(nodeElement);
198 currentNodeElementChanged: function(nodeElement) {
199 commands.updateCurrentNodeElement(nodeElement);
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 nodeElementChange: function(attr, value) {
217 sandbox.getModule('documentCanvas').modifyCurrentNodeElement(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());
228 eventHandlers.nodeFamilyTree = {
230 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
232 nodeEntered: function(node) {
233 commands.highlightDocumentElement(node, 'nodeFamilyTree');
235 nodeLeft: function(node) {
236 commands.dimDocumentElement(node, 'nodeFamilyTree');
238 nodeClicked: function(node) {
239 commands.jumpToDocumentElement(node);
243 eventHandlers.documentToolbar = {
245 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
247 command: function(cmd, params) {
248 sandbox.getModule('documentCanvas').command(cmd, params);
252 eventHandlers.nodeBreadCrumbs = {
254 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
256 elementEntered: function(element) {
257 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
259 elementLeft: function(element) {
260 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
262 elementClicked: function(element) {
263 commands.jumpToDocumentElement(element);
267 eventHandlers.documentHistory = {
269 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
270 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
272 compare: function(ver1, ver2) {
273 sandbox.getModule('data').fetchDiff(ver1, ver2);
275 restoreVersion: function(version) {
276 sandbox.getModule('data').restoreVersion(version);
278 displayVersion: function(event) {
280 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
284 eventHandlers.diffViewer = {
286 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
290 window.addEventListener('beforeunload', function(event) {
291 var txt = gettext('Do you really want to exit?');
292 if(documentIsDirty) {
293 txt += ' ' + gettext('Document contains unsaved changes!');
295 event.returnValue = txt; // FF
296 return txt; // Chrome
303 sandbox.getModule('data').start();
305 handleEvent: function(moduleName, eventName, args) {
306 var eventRepr = moduleName + '.' + eventName;
307 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
308 logger.debug('Handling event ' + eventRepr);
309 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
311 logger.warning('No event handler for ' + eventRepr);