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 jumpToDocumentElement: function(element) {
26 sandbox.getModule('documentCanvas').jumpToElement(element);
28 refreshCanvasSelection: function(selection) {
29 var fragment = selection.toDocumentFragment();
30 sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
36 mainLayout: new layout.Layout(mainLayoutTemplate),
37 mainTabs: (new tabs.View()).render(),
38 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
39 diffLayout: new layout.Layout(diffLayoutTemplate)
42 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
43 addMainTab(gettext('Source'), 'sourceEditor', '');
44 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
46 sandbox.getDOM().append(views.mainLayout.getAsView());
48 var wlxmlDocument, documentIsDirty;
52 var eventHandlers = {};
54 eventHandlers.sourceEditor = {
56 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
57 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
61 eventHandlers.data = {
62 ready: function(usingDraft, draftTimestamp, xmlValid) {
63 wlxmlDocument = sandbox.getModule('data').getDocument();
65 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
67 documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
68 documentSummary.render();
69 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
70 sandbox.getModule('mainBar').setSummaryView(documentSummary.dom);
72 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
73 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
76 var toStart = ['sourceEditor', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'];
78 toStart.push('documentCanvas');
80 _.each(toStart, function(moduleName) {
81 sandbox.getModule(moduleName).start();
84 documentIsDirty = false;
85 wlxmlDocument.on('change', function() {
86 documentIsDirty = true;
87 sandbox.getModule('mainBar').setCommandEnabled('save', true);
89 wlxmlDocument.on('contentSet', function() {
90 documentIsDirty = true;
93 draftDropped: function() {
94 documentSummary.setDraftField('-');
95 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
96 sandbox.getModule('mainBar').setCommandEnabled('save', false);
98 savingStarted: function(what) {
100 remote: gettext('Saving document'),
101 local: gettext('Saving local copy')
103 sandbox.getModule('mainBar').setCommandEnabled('save', false);
104 sandbox.getModule('indicator').showMessage(msg[what] + '...');
106 savingEnded: function(status, what, data) {
109 remote: gettext('Document saved'),
110 local: gettext('Local copy saved')
112 documentIsDirty = false;
114 sandbox.getModule('indicator').clearMessage({message: msg[what]});
115 if(status === 'success' && what === 'remote') {
116 documentSummary.setDraftField('-');
117 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
118 sandbox.getModule('mainBar').setCommandEnabled('save', false);
120 if(what === 'local') {
121 documentSummary.setDraftField(data.timestamp);
122 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
123 sandbox.getModule('mainBar').setCommandEnabled('save', true);
126 restoringStarted: function(event) {
127 sandbox.getModule('mainBar').setCommandEnabled('save', false);
128 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
130 historyItemAdded: function(item) {
131 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
133 diffFetched: function(diff) {
134 sandbox.getModule('diffViewer').setDiff(diff);
136 documentReverted: function(version) {
137 documentIsDirty = false;
138 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
142 eventHandlers.mainBar = {
144 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
146 'cmd.save': function() {
147 var sourceEditor = sandbox.getModule('sourceEditor');
148 if(!sourceEditor.changesCommited()) {
149 logger.debug('Source editor has uncommited changes, commiting...');
150 sourceEditor.commitChanges();
152 sandbox.getModule('data').saveDocument();
154 'cmd.drop-draft': function() {
155 sandbox.getModule('data').dropDraft();
159 eventHandlers.indicator = {
161 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
167 eventHandlers.documentCanvas = {
169 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
170 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
173 nodeHovered: function(canvasNode) {
174 commands.highlightDocumentNode(canvasNode);
177 nodeBlured: function(canvasNode) {
178 commands.dimDocumentNode(canvasNode);
181 selectionChanged: function(selection) {
182 commands.refreshCanvasSelection(selection);
186 eventHandlers.documentToolbar = {
188 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
189 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
191 actionExecuted: function(action, ret) {
192 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
196 eventHandlers.documentHistory = {
198 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
199 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
201 compare: function(ver1, ver2) {
202 sandbox.getModule('data').fetchDiff(ver1, ver2);
204 restoreVersion: function(version) {
205 sandbox.getModule('data').restoreVersion(version);
207 displayVersion: function(event) {
209 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
213 eventHandlers.diffViewer = {
215 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
219 eventHandlers.statusBar = {
221 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
225 eventHandlers.__all__ = {
226 actionHovered: function(action) {
227 sandbox.getModule('statusBar').showAction(action);
229 actionOff: function() {
230 sandbox.getModule('statusBar').clearAction();
234 window.addEventListener('beforeunload', function(event) {
235 var txt = gettext('Do you really want to exit?');
236 if(documentIsDirty) {
237 txt += ' ' + gettext('Document contains unsaved changes!');
239 event.returnValue = txt; // FF
240 return txt; // Chrome
247 sandbox.registerActionsAppObject({
248 getUser: function() {
249 return sandbox.getConfig().user;
252 sandbox.getModule('data').start();
254 handleEvent: function(moduleName, eventName, args) {
255 var eventRepr = moduleName + '.' + eventName;
256 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
257 logger.debug('Handling event ' + eventRepr);
258 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
262 if(eventHandlers.__all__[eventName]) {
263 logger.debug('Handling event ' + eventRepr);
264 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
268 logger.warning('No event handler for ' + eventRepr);