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 refreshCanvasSelection: function(selection) {
46 var fragment = selection.toDocumentFragment(),
49 sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
51 if(fragment && fragment.node) {
52 elementParent = fragment.node.getNearestElementNode();
53 sandbox.getModule('nodePane').setNodeElement(elementParent);
54 sandbox.getModule('nodeFamilyTree').setElement(fragment.node);
55 sandbox.getModule('nodeBreadCrumbs').setNodeElement(elementParent);
56 sandbox.getModule('metadataEditor').setNodeElement(elementParent);
58 sandbox.getModule('nodePane').setNodeElement(null);
59 sandbox.getModule('nodeFamilyTree').setElement(null);
60 sandbox.getModule('nodeBreadCrumbs').setNodeElement(null);
61 sandbox.getModule('metadataEditor').setNodeElement(null);
68 mainLayout: new layout.Layout(mainLayoutTemplate),
69 mainTabs: (new tabs.View()).render(),
70 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
71 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
72 currentNodePaneLayout: new vbox.VBox(),
73 diffLayout: new layout.Layout(diffLayoutTemplate)
76 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
77 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
78 addMainTab(gettext('Source'), 'sourceEditor', '');
79 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
81 sandbox.getDOM().append(views.mainLayout.getAsView());
83 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
85 var wlxmlDocument, documentIsDirty;
89 var eventHandlers = {};
91 eventHandlers.sourceEditor = {
93 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
94 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
98 eventHandlers.data = {
99 ready: function(usingDraft, draftTimestamp) {
100 wlxmlDocument = sandbox.getModule('data').getDocument();
102 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
104 documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
105 documentSummary.render();
106 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
107 views.currentNodePaneLayout.appendView(documentSummary.dom);
109 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
110 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
112 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'metadataEditor', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) {
113 sandbox.getModule(moduleName).start();
116 documentIsDirty = false;
117 wlxmlDocument.on('change', function() {
118 documentIsDirty = true;
119 sandbox.getModule('mainBar').setCommandEnabled('save', true);
121 wlxmlDocument.on('contentSet', function() {
122 documentIsDirty = true;
125 draftDropped: function() {
126 documentSummary.setDraftField('-');
127 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
128 sandbox.getModule('mainBar').setCommandEnabled('save', false);
130 savingStarted: function(what) {
132 remote: gettext('Saving document'),
133 local: gettext('Saving local copy')
135 sandbox.getModule('mainBar').setCommandEnabled('save', false);
136 sandbox.getModule('indicator').showMessage(msg[what] + '...');
138 savingEnded: function(status, what, data) {
141 remote: gettext('Document saved'),
142 local: gettext('Local copy saved')
144 documentIsDirty = false;
146 sandbox.getModule('indicator').clearMessage({message: msg[what]});
147 if(status === 'success' && what === 'remote') {
148 documentSummary.setDraftField('-');
149 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
150 sandbox.getModule('mainBar').setCommandEnabled('save', false);
152 if(what === 'local') {
153 documentSummary.setDraftField(data.timestamp);
154 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
155 sandbox.getModule('mainBar').setCommandEnabled('save', true);
158 restoringStarted: function(event) {
159 sandbox.getModule('mainBar').setCommandEnabled('save', false);
160 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
162 historyItemAdded: function(item) {
163 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
165 diffFetched: function(diff) {
166 sandbox.getModule('diffViewer').setDiff(diff);
168 documentReverted: function(version) {
169 documentIsDirty = false;
170 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
174 eventHandlers.mainBar = {
176 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
178 'cmd.save': function() {
179 var sourceEditor = sandbox.getModule('sourceEditor');
180 if(!sourceEditor.changesCommited()) {
181 logger.debug('Source editor has uncommited changes, commiting...');
182 sourceEditor.commitChanges();
184 sandbox.getModule('data').saveDocument();
186 'cmd.drop-draft': function() {
187 sandbox.getModule('data').dropDraft();
191 eventHandlers.indicator = {
193 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
199 eventHandlers.documentCanvas = {
201 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
202 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
205 nodeHovered: function(canvasNode) {
206 commands.highlightDocumentNode(canvasNode);
209 nodeBlured: function(canvasNode) {
210 commands.dimDocumentNode(canvasNode);
213 selectionChanged: function(selection) {
214 commands.refreshCanvasSelection(selection);
218 eventHandlers.nodePane = {
220 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
224 eventHandlers.metadataEditor = {
226 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
227 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
231 eventHandlers.nodeFamilyTree = {
233 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
235 nodeEntered: function(node) {
236 commands.highlightDocumentElement(node, 'nodeFamilyTree');
238 nodeLeft: function(node) {
239 commands.dimDocumentElement(node, 'nodeFamilyTree');
241 nodeClicked: function(node) {
242 commands.jumpToDocumentElement(node);
246 eventHandlers.documentToolbar = {
248 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
249 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
251 actionExecuted: function(action, ret) {
252 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
256 eventHandlers.nodeBreadCrumbs = {
258 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
260 elementEntered: function(element) {
261 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
263 elementLeft: function(element) {
264 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
266 elementClicked: function(element) {
267 commands.jumpToDocumentElement(element);
271 eventHandlers.documentHistory = {
273 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
274 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
276 compare: function(ver1, ver2) {
277 sandbox.getModule('data').fetchDiff(ver1, ver2);
279 restoreVersion: function(version) {
280 sandbox.getModule('data').restoreVersion(version);
282 displayVersion: function(event) {
284 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
288 eventHandlers.diffViewer = {
290 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
294 eventHandlers.statusBar = {
296 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
300 eventHandlers.__all__ = {
301 actionHovered: function(action) {
302 sandbox.getModule('statusBar').showAction(action);
304 actionOff: function() {
305 sandbox.getModule('statusBar').clearAction();
309 window.addEventListener('beforeunload', function(event) {
310 var txt = gettext('Do you really want to exit?');
311 if(documentIsDirty) {
312 txt += ' ' + gettext('Document contains unsaved changes!');
314 event.returnValue = txt; // FF
315 return txt; // Chrome
322 sandbox.registerActionsAppObject({
323 getUser: function() {
324 return sandbox.getConfig().user;
327 sandbox.getModule('data').start();
329 handleEvent: function(moduleName, eventName, args) {
330 var eventRepr = moduleName + '.' + eventName;
331 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
332 logger.debug('Handling event ' + eventRepr);
333 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
337 if(eventHandlers.__all__[eventName]) {
338 logger.debug('Handling event ' + eventRepr);
339 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
343 logger.warning('No event handler for ' + eventRepr);