5 'fnpjs/logging/logging',
7 'libs/text!./mainLayout.html',
8 'libs/text!./editingLayout.html',
9 'libs/text!./diffLayout.html',
10 ], function(documentSummary, _, layout, logging, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
14 return function(sandbox) {
18 var logger = logging.getLogger('editor.modules.rng');
20 function addMainTab(title, slug, view) {
21 views.mainTabs.addTab(title, slug, view);
25 refreshCanvasSelection: function(selection) {
26 var fragment = selection.toDocumentFragment();
27 sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
33 mainLayout: new layout.Layout(mainLayoutTemplate),
34 mainTabs: (new tabs.View()).render(),
35 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
36 diffLayout: new layout.Layout(diffLayoutTemplate)
39 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
40 addMainTab(gettext('Source'), 'sourceEditor', '');
41 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
43 sandbox.getDOM().append(views.mainLayout.getAsView());
45 var wlxmlDocument, documentIsDirty;
49 var eventHandlers = {};
51 eventHandlers.sourceEditor = {
53 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
54 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
58 eventHandlers.data = {
59 ready: function(usingDraft, draftTimestamp, xmlValid) {
60 wlxmlDocument = sandbox.getModule('data').getDocument();
62 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
64 documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
65 documentSummary.render();
66 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
67 sandbox.getModule('mainBar').setSummaryView(documentSummary.dom);
69 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
70 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
73 var toStart = ['sourceEditor', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'];
75 toStart.push('documentCanvas');
77 _.each(toStart, function(moduleName) {
78 sandbox.getModule(moduleName).start();
81 documentIsDirty = false;
82 wlxmlDocument.on('change', function() {
83 documentIsDirty = true;
84 sandbox.getModule('mainBar').setCommandEnabled('save', true);
86 wlxmlDocument.on('contentSet', function() {
87 documentIsDirty = true;
90 draftDropped: function() {
91 documentSummary.setDraftField('-');
92 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
93 sandbox.getModule('mainBar').setCommandEnabled('save', false);
95 savingStarted: function(what) {
97 remote: gettext('Saving document'),
98 local: gettext('Saving local copy')
100 sandbox.getModule('mainBar').setCommandEnabled('save', false);
101 sandbox.getModule('indicator').showMessage(msg[what] + '...');
103 savingEnded: function(status, what, data) {
106 remote: gettext('Document saved'),
107 local: gettext('Local copy saved')
109 documentIsDirty = false;
111 sandbox.getModule('indicator').clearMessage({message: msg[what]});
112 if(status === 'success' && what === 'remote') {
113 documentSummary.setDraftField('-');
114 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
115 sandbox.getModule('mainBar').setCommandEnabled('save', false);
117 if(what === 'local') {
118 documentSummary.setDraftField(data.timestamp);
119 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
120 sandbox.getModule('mainBar').setCommandEnabled('save', true);
123 restoringStarted: function(event) {
124 sandbox.getModule('mainBar').setCommandEnabled('save', false);
125 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
127 historyItemAdded: function(item) {
128 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
130 diffFetched: function(diff) {
131 sandbox.getModule('diffViewer').setDiff(diff);
133 documentReverted: function(version) {
134 documentIsDirty = false;
135 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
139 eventHandlers.mainBar = {
141 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
143 'cmd.save': function() {
144 var sourceEditor = sandbox.getModule('sourceEditor');
145 if(!sourceEditor.changesCommited()) {
146 logger.debug('Source editor has uncommited changes, commiting...');
147 sourceEditor.commitChanges();
149 sandbox.getModule('data').saveDocument();
151 'cmd.drop-draft': function() {
152 sandbox.getModule('data').dropDraft();
156 eventHandlers.indicator = {
158 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
164 eventHandlers.documentCanvas = {
166 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
167 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
170 nodeHovered: function(canvasNode) {
171 commands.highlightDocumentNode(canvasNode);
174 nodeBlured: function(canvasNode) {
175 commands.dimDocumentNode(canvasNode);
178 selectionChanged: function(selection) {
179 commands.refreshCanvasSelection(selection);
183 eventHandlers.documentToolbar = {
185 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
186 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
188 actionExecuted: function(action, ret) {
189 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
193 eventHandlers.documentHistory = {
195 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
196 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
198 compare: function(ver1, ver2) {
199 sandbox.getModule('data').fetchDiff(ver1, ver2);
201 restoreVersion: function(version) {
202 sandbox.getModule('data').restoreVersion(version);
204 displayVersion: function(event) {
206 var config = sandbox.getConfig(),
207 doc = sandbox.getModule('data').getDocument();
209 if(config.documentUrl) {
210 window.open(config.documentUrl(doc.properties.document_id, event.version), _.uniqueId());
212 logger.error('Unable to show version ' + event.version + ' of a document - config.documentUrl missing');
217 eventHandlers.diffViewer = {
219 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
223 eventHandlers.statusBar = {
225 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
229 eventHandlers.__all__ = {
230 actionHovered: function(action) {
231 sandbox.getModule('statusBar').showAction(action);
233 actionOff: function() {
234 sandbox.getModule('statusBar').clearAction();
238 window.addEventListener('beforeunload', function(event) {
239 var txt = gettext('Do you really want to exit?');
240 if(documentIsDirty) {
241 txt += ' ' + gettext('Document contains unsaved changes!');
243 event.returnValue = txt; // FF
244 return txt; // Chrome
251 sandbox.registerActionsAppObject({
252 getUser: function() {
253 return sandbox.getConfig().user;
256 sandbox.getModule('data').start();
258 handleEvent: function(moduleName, eventName, args) {
259 var eventRepr = moduleName + '.' + eventName;
260 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
261 logger.debug('Handling event ' + eventRepr);
262 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
266 if(eventHandlers.__all__[eventName]) {
267 logger.debug('Handling event ' + eventRepr);
268 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
272 logger.warning('No event handler for ' + eventRepr);