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(),
32 sandbox.getModule('documentToolbar').setDocumentFragment(fragment);
34 if(fragment && fragment.node) {
35 elementParent = fragment.node.getNearestElementNode();
36 sandbox.getModule('nodeBreadCrumbs').setNodeElement(elementParent);
38 sandbox.getModule('nodeBreadCrumbs').setNodeElement(null);
45 mainLayout: new layout.Layout(mainLayoutTemplate),
46 mainTabs: (new tabs.View()).render(),
47 visualEditing: new layout.Layout(visualEditingLayoutTemplate),
48 diffLayout: new layout.Layout(diffLayoutTemplate)
51 addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
52 addMainTab(gettext('Source'), 'sourceEditor', '');
53 addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
55 sandbox.getDOM().append(views.mainLayout.getAsView());
57 var wlxmlDocument, documentIsDirty;
61 var eventHandlers = {};
63 eventHandlers.sourceEditor = {
65 addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());
66 sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
70 eventHandlers.data = {
71 ready: function(usingDraft, draftTimestamp) {
72 wlxmlDocument = sandbox.getModule('data').getDocument();
74 views.mainLayout.setView('mainView', views.mainTabs.getAsView());
76 documentSummary.init(sandbox.getConfig().documentSummaryView, wlxmlDocument);
77 documentSummary.render();
78 documentSummary.setDraftField(usingDraft ? (draftTimestamp || '???') : '-');
79 sandbox.getModule('mainBar').setSummaryView(documentSummary.dom);
81 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', usingDraft);
82 sandbox.getModule('mainBar').setCommandEnabled('save', usingDraft);
84 _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer', 'statusBar'], function(moduleName) {
85 sandbox.getModule(moduleName).start();
88 documentIsDirty = false;
89 wlxmlDocument.on('change', function() {
90 documentIsDirty = true;
91 sandbox.getModule('mainBar').setCommandEnabled('save', true);
93 wlxmlDocument.on('contentSet', function() {
94 documentIsDirty = true;
97 draftDropped: function() {
98 documentSummary.setDraftField('-');
99 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
100 sandbox.getModule('mainBar').setCommandEnabled('save', false);
102 savingStarted: function(what) {
104 remote: gettext('Saving document'),
105 local: gettext('Saving local copy')
107 sandbox.getModule('mainBar').setCommandEnabled('save', false);
108 sandbox.getModule('indicator').showMessage(msg[what] + '...');
110 savingEnded: function(status, what, data) {
113 remote: gettext('Document saved'),
114 local: gettext('Local copy saved')
116 documentIsDirty = false;
118 sandbox.getModule('indicator').clearMessage({message: msg[what]});
119 if(status === 'success' && what === 'remote') {
120 documentSummary.setDraftField('-');
121 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', false);
122 sandbox.getModule('mainBar').setCommandEnabled('save', false);
124 if(what === 'local') {
125 documentSummary.setDraftField(data.timestamp);
126 sandbox.getModule('mainBar').setCommandEnabled('drop-draft', true);
127 sandbox.getModule('mainBar').setCommandEnabled('save', true);
130 restoringStarted: function(event) {
131 sandbox.getModule('mainBar').setCommandEnabled('save', false);
132 sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
134 historyItemAdded: function(item) {
135 sandbox.getModule('documentHistory').addHistory([item], {animate: true});
137 diffFetched: function(diff) {
138 sandbox.getModule('diffViewer').setDiff(diff);
140 documentReverted: function(version) {
141 documentIsDirty = false;
142 sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
146 eventHandlers.mainBar = {
148 views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
150 'cmd.save': function() {
151 var sourceEditor = sandbox.getModule('sourceEditor');
152 if(!sourceEditor.changesCommited()) {
153 logger.debug('Source editor has uncommited changes, commiting...');
154 sourceEditor.commitChanges();
156 sandbox.getModule('data').saveDocument();
158 'cmd.drop-draft': function() {
159 sandbox.getModule('data').dropDraft();
163 eventHandlers.indicator = {
165 views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
171 eventHandlers.documentCanvas = {
173 sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
174 views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
177 nodeHovered: function(canvasNode) {
178 commands.highlightDocumentNode(canvasNode);
181 nodeBlured: function(canvasNode) {
182 commands.dimDocumentNode(canvasNode);
185 selectionChanged: function(selection) {
186 commands.refreshCanvasSelection(selection);
190 eventHandlers.documentToolbar = {
192 views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
193 sandbox.getModule('documentToolbar').setCanvas(sandbox.getModule('documentCanvas').getCanvas());
195 actionExecuted: function(action, ret) {
196 sandbox.getModule('documentCanvas').onAfterActionExecuted(action, ret);
200 eventHandlers.nodeBreadCrumbs = {
202 views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
204 elementClicked: function(element) {
205 commands.jumpToDocumentElement(element);
209 eventHandlers.documentHistory = {
211 sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
212 views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
214 compare: function(ver1, ver2) {
215 sandbox.getModule('data').fetchDiff(ver1, ver2);
217 restoreVersion: function(version) {
218 sandbox.getModule('data').restoreVersion(version);
220 displayVersion: function(event) {
222 window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
226 eventHandlers.diffViewer = {
228 views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
232 eventHandlers.statusBar = {
234 views.mainLayout.setView('bottomPanel', sandbox.getModule('statusBar').getView());
238 eventHandlers.__all__ = {
239 actionHovered: function(action) {
240 sandbox.getModule('statusBar').showAction(action);
242 actionOff: function() {
243 sandbox.getModule('statusBar').clearAction();
247 window.addEventListener('beforeunload', function(event) {
248 var txt = gettext('Do you really want to exit?');
249 if(documentIsDirty) {
250 txt += ' ' + gettext('Document contains unsaved changes!');
252 event.returnValue = txt; // FF
253 return txt; // Chrome
260 sandbox.registerActionsAppObject({
261 getUser: function() {
262 return sandbox.getConfig().user;
265 sandbox.getModule('data').start();
267 handleEvent: function(moduleName, eventName, args) {
268 var eventRepr = moduleName + '.' + eventName;
269 if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
270 logger.debug('Handling event ' + eventRepr);
271 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
275 if(eventHandlers.__all__[eventName]) {
276 logger.debug('Handling event ' + eventRepr);
277 eventHandlers.__all__[eventName].apply(eventHandlers.__all__, args);
281 logger.warning('No event handler for ' + eventRepr);