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 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
102 documentSummary.init(sandbox.getConfig().documentSummaryView);
103 documentSummary.render(sandbox.getModule('data').getDocumentProperties());
104 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
105 views.currentNodePaneLayout.appendView(documentSummary.dom);
107 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
108 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
110 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'metadataEditor', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) {
111 sandbox.getModule(moduleName).start();
114 wlxmlDocument = sandbox.getModule('data').getDocument();
115 documentIsDirty = false;
116 wlxmlDocument.on('change', function() {
117 documentIsDirty = true;
118 sandbox.getModule('mainBar').setCommandEnabled('save', true);
120 wlxmlDocument.on('contentSet', function() {
121 documentIsDirty = true;
124 draftDropped: function() {
125 documentSummary.setDraftField('-');
126 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
127 sandbox.getModule('mainBar').setCommandEnabled('save', false);
129 savingStarted: function(what) {
131 remote: gettext('Saving document'),
132 local: gettext('Saving local copy')
134 sandbox.getModule('mainBar').setCommandEnabled('save', false);
135 sandbox.getModule('indicator').showMessage(msg[what] + '...');
137 savingEnded: function(status, what, data) {
140 remote: gettext('Document saved'),
141 local: gettext('Local copy saved')
143 documentIsDirty = false;
145 sandbox.getModule('indicator').clearMessage({message: msg[what]});
146 if(status === 'success' && what === 'remote') {
147 sandbox.getModule('mainBar').setVersion(data.version);
148 documentSummary.render(data);
149 documentSummary.setDraftField('-');
150 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
151 sandbox.getModule('mainBar').setCommandEnabled('save', false);
153 if(what === 'local') {
154 documentSummary.setDraftField(data.timestamp);
155 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
156 sandbox.getModule('mainBar').setCommandEnabled('save', true);
159 restoringStarted: function(event) {
160 sandbox.getModule('mainBar').setCommandEnabled('save', false);
161 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
163 historyItemAdded: function(item) {
164 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
166 diffFetched: function(diff) {
167 sandbox.getModule('diffViewer').setDiff(diff);
169 documentReverted: function(version) {
170 documentIsDirty = false;
171 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
172 sandbox.getModule('mainBar').setVersion(version);
176 eventHandlers.mainBar = {
178 sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentProperties().version);
179 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
181 'cmd.save': function() {
182 var sourceEditor = sandbox.getModule('sourceEditor');
183 if(!sourceEditor.changesCommited()) {
184 logger.debug('Source editor has uncommited changes, commiting...');
185 sourceEditor.commitChanges();
187 sandbox.getModule('data').saveDocument();
189 'cmd.drop-draft': function() {
190 sandbox.getModule('data').dropDraft();
194 eventHandlers.indicator = {
196 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
202 eventHandlers.documentCanvas = {
204 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
205 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
208 nodeHovered: function(canvasNode) {
209 commands.highlightDocumentNode(canvasNode);
212 nodeBlured: function(canvasNode) {
213 commands.dimDocumentNode(canvasNode);
216 selectionChanged: function(selection) {
217 commands.refreshCanvasSelection(selection);
221 eventHandlers.nodePane = {
223 views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
227 eventHandlers.metadataEditor = {
229 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
230 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
234 eventHandlers.nodeFamilyTree = {
236 views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
238 nodeEntered: function(node) {
239 commands.highlightDocumentElement(node, 'nodeFamilyTree');
241 nodeLeft: function(node) {
242 commands.dimDocumentElement(node, 'nodeFamilyTree');
244 nodeClicked: function(node) {
245 commands.jumpToDocumentElement(node);
249 eventHandlers.documentToolbar = {
251 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
252 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
254 actionExecuted: function(action, ret) {
255 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
259 eventHandlers.nodeBreadCrumbs = {
261 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
263 elementEntered: function(element) {
264 commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
266 elementLeft: function(element) {
267 commands.dimDocumentElement(element, 'nodeBreadCrumbs');
269 elementClicked: function(element) {
270 commands.jumpToDocumentElement(element);
274 eventHandlers.documentHistory = {
276 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
277 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
279 compare: function(ver1, ver2) {
280 sandbox.getModule('data').fetchDiff(ver1, ver2);
282 restoreVersion: function(version) {
283 sandbox.getModule('data').restoreVersion(version);
285 displayVersion: function(event) {
287 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
291 eventHandlers.diffViewer = {
293 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
297 eventHandlers.statusBar = {
299 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
303 eventHandlers.__all__ = {
304 actionHovered: function(action) {
305 sandbox.getModule('statusBar').showAction(action);
307 actionOff: function() {
308 sandbox.getModule('statusBar').clearAction();
312 window.addEventListener('beforeunload', function(event) {
313 var txt = gettext('Do you really want to exit?');
314 if(documentIsDirty) {
315 txt += ' ' + gettext('Document contains unsaved changes!');
317 event.returnValue = txt; // FF
318 return txt; // Chrome
325 sandbox.registerActionsAppObject({
326 getUser: function() {
327 return sandbox.getConfig().user;
330 sandbox.getModule('data').start();
332 handleEvent: function(moduleName, eventName, args) {
333 var eventRepr = moduleName + '.' + eventName;
334 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
335 logger.debug('Handling event ' + eventRepr);
336 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
340 if(eventHandlers.__all__[eventName]) {
341 logger.debug('Handling event ' + eventRepr);
342 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
346 logger.warning('No event handler for ' + eventRepr);