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) {
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);
75 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) {
76 sandbox.getModule(moduleName).start();
79 documentIsDirty = false;
80 wlxmlDocument.on('change', function() {
81 documentIsDirty = true;
82 sandbox.getModule('mainBar').setCommandEnabled('save', true);
84 wlxmlDocument.on('contentSet', function() {
85 documentIsDirty = true;
88 draftDropped: function() {
89 documentSummary.setDraftField('-');
90 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
91 sandbox.getModule('mainBar').setCommandEnabled('save', false);
93 savingStarted: function(what) {
95 remote: gettext('Saving document'),
96 local: gettext('Saving local copy')
98 sandbox.getModule('mainBar').setCommandEnabled('save', false);
99 sandbox.getModule('indicator').showMessage(msg[what] + '...');
101 savingEnded: function(status, what, data) {
104 remote: gettext('Document saved'),
105 local: gettext('Local copy saved')
107 documentIsDirty = false;
109 sandbox.getModule('indicator').clearMessage({message: msg[what]});
110 if(status === 'success' && what === 'remote') {
111 documentSummary.setDraftField('-');
112 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
113 sandbox.getModule('mainBar').setCommandEnabled('save', false);
115 if(what === 'local') {
116 documentSummary.setDraftField(data.timestamp);
117 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
118 sandbox.getModule('mainBar').setCommandEnabled('save', true);
121 restoringStarted: function(event) {
122 sandbox.getModule('mainBar').setCommandEnabled('save', false);
123 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
125 historyItemAdded: function(item) {
126 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
128 diffFetched: function(diff) {
129 sandbox.getModule('diffViewer').setDiff(diff);
131 documentReverted: function(version) {
132 documentIsDirty = false;
133 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
137 eventHandlers.mainBar = {
139 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
141 'cmd.save': function() {
142 var sourceEditor = sandbox.getModule('sourceEditor');
143 if(!sourceEditor.changesCommited()) {
144 logger.debug('Source editor has uncommited changes, commiting...');
145 sourceEditor.commitChanges();
147 sandbox.getModule('data').saveDocument();
149 'cmd.drop-draft': function() {
150 sandbox.getModule('data').dropDraft();
154 eventHandlers.indicator = {
156 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
162 eventHandlers.documentCanvas = {
164 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
165 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
168 nodeHovered: function(canvasNode) {
169 commands.highlightDocumentNode(canvasNode);
172 nodeBlured: function(canvasNode) {
173 commands.dimDocumentNode(canvasNode);
176 selectionChanged: function(selection) {
177 commands.refreshCanvasSelection(selection);
181 eventHandlers.documentToolbar = {
183 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
184 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
186 actionExecuted: function(action, ret) {
187 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
191 eventHandlers.documentHistory = {
193 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
194 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
196 compare: function(ver1, ver2) {
197 sandbox.getModule('data').fetchDiff(ver1, ver2);
199 restoreVersion: function(version) {
200 sandbox.getModule('data').restoreVersion(version);
202 displayVersion: function(event) {
204 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
208 eventHandlers.diffViewer = {
210 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
214 eventHandlers.statusBar = {
216 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
220 eventHandlers.__all__ = {
221 actionHovered: function(action) {
222 sandbox.getModule('statusBar').showAction(action);
224 actionOff: function() {
225 sandbox.getModule('statusBar').clearAction();
229 window.addEventListener('beforeunload', function(event) {
230 var txt = gettext('Do you really want to exit?');
231 if(documentIsDirty) {
232 txt += ' ' + gettext('Document contains unsaved changes!');
234 event.returnValue = txt; // FF
235 return txt; // Chrome
242 sandbox.registerActionsAppObject({
243 getUser: function() {
244 return sandbox.getConfig().user;
247 sandbox.getModule('data').start();
249 handleEvent: function(moduleName, eventName, args) {
250 var eventRepr = moduleName + '.' + eventName;
251 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
252 logger.debug('Handling event ' + eventRepr);
253 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
257 if(eventHandlers.__all__[eventName]) {
258 logger.debug('Handling event ' + eventRepr);
259 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
263 logger.warning('No event handler for ' + eventRepr);