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 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
99 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
101 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'metadataEditor', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
102 sandbox.getModule(moduleName).start();
105 wlxmlDocument = sandbox.getModule('data').getDocument();
106 documentIsDirty = false;
107 wlxmlDocument.on('change', function() {
108 documentIsDirty = true;
109 sandbox.getModule('mainBar').setCommandEnabled('save', true);
111 wlxmlDocument.on('contentSet', function() {
112 documentIsDirty = true;
115 draftDropped: function() {
116 documentSummary.setDraftField('-');
117 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
118 sandbox.getModule('mainBar').setCommandEnabled('save', false);
120 savingStarted: function(what) {
122 remote: gettext('Saving document'),
123 local: gettext('Saving local copy')
125 sandbox.getModule('mainBar').setCommandEnabled('save', false);
126 sandbox.getModule('indicator').showMessage(msg[what] + '...');
128 savingEnded: function(status, what, data) {
131 remote: gettext('Document saved'),
132 local: gettext('Local copy saved')
134 documentIsDirty = false;
136 sandbox.getModule('indicator').clearMessage({message: msg[what]});
137 if(status === 'success' && what === 'remote') {
138 sandbox.getModule('mainBar').setVersion(data.version);
139 documentSummary.render(data);
140 documentSummary.setDraftField('-');
141 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
142 sandbox.getModule('mainBar').setCommandEnabled('save', false);
144 if(what === 'local') {
145 documentSummary.setDraftField(data.timestamp);
146 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
147 sandbox.getModule('mainBar').setCommandEnabled('save', true);
150 restoringStarted: function(event) {
151 sandbox.getModule('mainBar').setCommandEnabled('save', false);
152 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
154 historyItemAdded: function(item) {
155 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
157 diffFetched: function(diff) {
158 sandbox.getModule('diffViewer').setDiff(diff);
160 documentReverted: function(version) {
161 documentIsDirty = false;
162 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
163 sandbox.getModule('mainBar').setVersion(version);
167 eventHandlers.mainBar = {
169 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
170 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
172 'cmd.save': function() {
173 var sourceEditor = sandbox.getModule('sourceEditor');
174 if(!sourceEditor.changesCommited()) {
175 logger.debug('Source editor has uncommited changes, commiting...');
176 sourceEditor.commitChanges();
178 sandbox.getModule('data').saveDocument();
180 'cmd.drop-draft': function() {
181 sandbox.getModule('data').dropDraft();
185 eventHandlers.indicator = {
187 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
193 eventHandlers.documentCanvas = {
195 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
196 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
199 currentTextElementSet: function(textElement) {
200 commands.updateCurrentTextElement(textElement);
203 currentNodeElementSet: function(nodeElement) {
204 commands.updateCurrentNodeElement(nodeElement);
207 currentNodeElementChanged: function(nodeElement) {
208 commands.updateCurrentNodeElement(nodeElement);
211 nodeHovered: function(canvasNode) {
212 commands.highlightDocumentNode(canvasNode);
215 nodeBlured: function(canvasNode) {
216 commands.dimDocumentNode(canvasNode);
220 eventHandlers.nodePane = {
222 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
226 eventHandlers.metadataEditor = {
228 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
229 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
233 eventHandlers.nodeFamilyTree = {
235 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
237 nodeEntered: function(node) {
238 commands.highlightDocumentElement(node, 'nodeFamilyTree');
240 nodeLeft: function(node) {
241 commands.dimDocumentElement(node, 'nodeFamilyTree');
243 nodeClicked: function(node) {
244 commands.jumpToDocumentElement(node);
248 eventHandlers.documentToolbar = {
250 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
252 command: function(cmd, params) {
253 sandbox.getModule('documentCanvas').command(cmd, params);
257 eventHandlers.nodeBreadCrumbs = {
259 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
261 elementEntered: function(element) {
262 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
264 elementLeft: function(element) {
265 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
267 elementClicked: function(element) {
268 commands.jumpToDocumentElement(element);
272 eventHandlers.documentHistory = {
274 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
275 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
277 compare: function(ver1, ver2) {
278 sandbox.getModule('data').fetchDiff(ver1, ver2);
280 restoreVersion: function(version) {
281 sandbox.getModule('data').restoreVersion(version);
283 displayVersion: function(event) {
285 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
289 eventHandlers.diffViewer = {
291 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
295 window.addEventListener('beforeunload', function(event) {
296 var txt = gettext('Do you really want to exit?');
297 if(documentIsDirty) {
298 txt += ' ' + gettext('Document contains unsaved changes!');
300 event.returnValue = txt; // FF
301 return txt; // Chrome
308 sandbox.getModule('data').start();
310 handleEvent: function(moduleName, eventName, args) {
311 var eventRepr = moduleName + '.' + eventName;
312 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
313 logger.debug('Handling event ' + eventRepr);
314 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
316 logger.warning('No event handler for ' + eventRepr);