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 jumpToDocumentElement: function(element) {
27 sandbox.getModule('documentCanvas').jumpToElement(element);
29 refreshCanvasSelection: function(selection) {
30 var fragment = selection.toDocumentFragment(),
33 sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
35 if(fragment && fragment.node) {
36 elementParent = fragment.node.getNearestElementNode();
37 sandbox.getModule('nodeBreadCrumbs').setNodeElement(elementParent);
38 sandbox.getModule('metadataEditor').setNodeElement(elementParent);
40 sandbox.getModule('nodeBreadCrumbs').setNodeElement(null);
41 sandbox.getModule('metadataEditor').setNodeElement(null);
48 mainLayout: new layout.Layout(mainLayoutTemplate),
49 mainTabs: (new tabs.View()).render(),
50 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
51 visualEditingSidebar: (new tabs.View({stacked: true})).render(),
52 currentNodePaneLayout: new vbox.VBox(),
53 diffLayout: new layout.Layout(diffLayoutTemplate)
56 views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
57 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
58 addMainTab(gettext('Source'), 'sourceEditor', '');
59 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
61 sandbox.getDOM().append(views.mainLayout.getAsView());
63 views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
65 var wlxmlDocument, documentIsDirty;
69 var eventHandlers = {};
71 eventHandlers.sourceEditor = {
73 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
74 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
78 eventHandlers.data = {
79 ready: function(usingDraft, draftTimestamp) {
80 wlxmlDocument = sandbox.getModule('data').getDocument();
82 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
84 documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
85 documentSummary.render();
86 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
87 sandbox.getModule('mainBar').setSummaryView(documentSummary.dom);
89 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
90 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
92 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'metadataEditor', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) {
93 sandbox.getModule(moduleName).start();
96 documentIsDirty = false;
97 wlxmlDocument.on('change', function() {
98 documentIsDirty = true;
99 sandbox.getModule('mainBar').setCommandEnabled('save', true);
101 wlxmlDocument.on('contentSet', function() {
102 documentIsDirty = true;
105 draftDropped: function() {
106 documentSummary.setDraftField('-');
107 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
108 sandbox.getModule('mainBar').setCommandEnabled('save', false);
110 savingStarted: function(what) {
112 remote: gettext('Saving document'),
113 local: gettext('Saving local copy')
115 sandbox.getModule('mainBar').setCommandEnabled('save', false);
116 sandbox.getModule('indicator').showMessage(msg[what] + '...');
118 savingEnded: function(status, what, data) {
121 remote: gettext('Document saved'),
122 local: gettext('Local copy saved')
124 documentIsDirty = false;
126 sandbox.getModule('indicator').clearMessage({message: msg[what]});
127 if(status === 'success' && what === 'remote') {
128 documentSummary.setDraftField('-');
129 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
130 sandbox.getModule('mainBar').setCommandEnabled('save', false);
132 if(what === 'local') {
133 documentSummary.setDraftField(data.timestamp);
134 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
135 sandbox.getModule('mainBar').setCommandEnabled('save', true);
138 restoringStarted: function(event) {
139 sandbox.getModule('mainBar').setCommandEnabled('save', false);
140 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
142 historyItemAdded: function(item) {
143 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
145 diffFetched: function(diff) {
146 sandbox.getModule('diffViewer').setDiff(diff);
148 documentReverted: function(version) {
149 documentIsDirty = false;
150 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
154 eventHandlers.mainBar = {
156 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
158 'cmd.save': function() {
159 var sourceEditor = sandbox.getModule('sourceEditor');
160 if(!sourceEditor.changesCommited()) {
161 logger.debug('Source editor has uncommited changes, commiting...');
162 sourceEditor.commitChanges();
164 sandbox.getModule('data').saveDocument();
166 'cmd.drop-draft': function() {
167 sandbox.getModule('data').dropDraft();
171 eventHandlers.indicator = {
173 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
179 eventHandlers.documentCanvas = {
181 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
182 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
185 nodeHovered: function(canvasNode) {
186 commands.highlightDocumentNode(canvasNode);
189 nodeBlured: function(canvasNode) {
190 commands.dimDocumentNode(canvasNode);
193 selectionChanged: function(selection) {
194 commands.refreshCanvasSelection(selection);
198 eventHandlers.metadataEditor = {
200 sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
201 views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
205 eventHandlers.documentToolbar = {
207 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
208 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
210 actionExecuted: function(action, ret) {
211 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
215 eventHandlers.nodeBreadCrumbs = {
217 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
219 elementClicked: function(element) {
220 commands.jumpToDocumentElement(element);
224 eventHandlers.documentHistory = {
226 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
227 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
229 compare: function(ver1, ver2) {
230 sandbox.getModule('data').fetchDiff(ver1, ver2);
232 restoreVersion: function(version) {
233 sandbox.getModule('data').restoreVersion(version);
235 displayVersion: function(event) {
237 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
241 eventHandlers.diffViewer = {
243 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
247 eventHandlers.statusBar = {
249 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
253 eventHandlers.__all__ = {
254 actionHovered: function(action) {
255 sandbox.getModule('statusBar').showAction(action);
257 actionOff: function() {
258 sandbox.getModule('statusBar').clearAction();
262 window.addEventListener('beforeunload', function(event) {
263 var txt = gettext('Do you really want to exit?');
264 if(documentIsDirty) {
265 txt += ' ' + gettext('Document contains unsaved changes!');
267 event.returnValue = txt; // FF
268 return txt; // Chrome
275 sandbox.registerActionsAppObject({
276 getUser: function() {
277 return sandbox.getConfig().user;
280 sandbox.getModule('data').start();
282 handleEvent: function(moduleName, eventName, args) {
283 var eventRepr = moduleName + '.' + eventName;
284 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
285 logger.debug('Handling event ' + eventRepr);
286 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
290 if(eventHandlers.__all__[eventName]) {
291 logger.debug('Handling event ' + eventRepr);
292 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
296 logger.warning('No event handler for ' + eventRepr);