5 'fnpjs/logging/logging',
 
   7 'libs/text!./mainLayout.html',
 
   8 'libs/text!./editingLayout.html',
 
   9 'libs/text!./diffLayout.html',
 
  10 ], function(_, layout, vbox, 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         highlightDocumentElement: function(element, origin) {
 
  26             ///'nodeBreadCrumbs', 'nodeFamilyTree'
 
  27             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
 
  28                 if(!origin || moduleName !== origin) {
 
  29                     sandbox.getModule(moduleName).highlightElement(element);
 
  33         dimDocumentElement: function(element, origin) {
 
  34             //'nodeBreadCrumbs', 'nodeFamilyTree'
 
  35             ['documentCanvas', 'nodeFamilyTree'].forEach(function(moduleName) {
 
  36                 if(!origin || moduleName !== origin) {
 
  37                     sandbox.getModule(moduleName).dimElement(element);
 
  41         jumpToDocumentElement: function(element) {
 
  42             sandbox.getModule('documentCanvas').jumpToElement(element);
 
  44         updateCurrentNodeElement: function(nodeElement) {
 
  45             sandbox.getModule('nodePane').setNodeElement(nodeElement);
 
  46             sandbox.getModule('nodeFamilyTree').setElement(nodeElement);
 
  47             sandbox.getModule('nodeBreadCrumbs').setNodeElement(nodeElement);
 
  48             sandbox.getModule('documentToolbar').setNodeElement(nodeElement);
 
  49             sandbox.getModule('metadataEditor').setNodeElement(nodeElement);
 
  51         updateCurrentTextElement: function(textElement) {
 
  52             sandbox.getModule('nodeFamilyTree').setElement(textElement);
 
  58         mainLayout: new layout.Layout(mainLayoutTemplate),
 
  59         mainTabs: (new tabs.View()).render(),
 
  60         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
 
  61         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
 
  62         currentNodePaneLayout: new vbox.VBox(),
 
  63         diffLayout: new layout.Layout(diffLayoutTemplate)
 
  66     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
 
  67     addMainTab(gettext('Editor'), 'editor', views.visualEditing.getAsView());
 
  68     addMainTab(gettext('Source'), 'sourceEditor',  '');
 
  69     addMainTab(gettext('History'), 'history', views.diffLayout.getAsView());
 
  71     sandbox.getDOM().append(views.mainLayout.getAsView());
 
  73     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
 
  75     var wlxmlDocument, documentIsDirty;
 
  79     var eventHandlers = {};
 
  81     eventHandlers.sourceEditor = {
 
  83             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
 
  84             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
 
  88     eventHandlers.data = {
 
  90             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
 
  92             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
 
  93                 sandbox.getModule(moduleName).start();
 
  96             wlxmlDocument = sandbox.getModule('data').getDocument();
 
  97             documentIsDirty = false;
 
  98             wlxmlDocument.on('change', function() {
 
  99                 documentIsDirty = true;
 
 101             wlxmlDocument.on('contentSet', function() {
 
 102                 documentIsDirty = true;
 
 105         savingStarted: function(what) {
 
 107                 remote: gettext('Saving document'),
 
 108                 local: gettext('Saving local copy')
 
 110             sandbox.getModule('mainBar').setCommandEnabled('save', false);
 
 111             sandbox.getModule('indicator').showMessage(msg[what] + '...');
 
 113         savingEnded: function(status, what, current_version) {
 
 116                 remote: gettext('Document saved'),
 
 117                 local: gettext('Local copy saved')
 
 119             documentIsDirty = false;
 
 120             sandbox.getModule('mainBar').setCommandEnabled('save', true);
 
 121             sandbox.getModule('indicator').clearMessage({message: msg[what]});
 
 122             sandbox.getModule('mainBar').setVersion(current_version);
 
 124         restoringStarted: function(event) {
 
 125             sandbox.getModule('mainBar').setCommandEnabled('save', false);
 
 126             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
 
 128         historyItemAdded: function(item) {
 
 129             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
 
 131         diffFetched: function(diff) {
 
 132             sandbox.getModule('diffViewer').setDiff(diff);
 
 134         documentReverted: function(version) {
 
 135             documentIsDirty = false;
 
 136             sandbox.getModule('mainBar').setCommandEnabled('save', true);
 
 137             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + version + ' przywrócona'});
 
 138             sandbox.getModule('mainBar').setVersion(version);
 
 142     eventHandlers.mainBar = {
 
 144             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
 
 145             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
 
 147         'cmd.save': function() {
 
 148             var sourceEditor = sandbox.getModule('sourceEditor');
 
 149             if(!sourceEditor.changesCommited()) {
 
 150                 logger.debug('Source editor has uncommited changes, commiting...');
 
 151                 sourceEditor.commitChanges();
 
 153             sandbox.getModule('data').saveDocument();
 
 157     eventHandlers.indicator = {
 
 159             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
 
 165     eventHandlers.documentCanvas = {
 
 167             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
 
 168             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
 
 171         currentTextElementSet: function(textElement) {
 
 172             commands.updateCurrentTextElement(textElement);
 
 175         currentNodeElementSet: function(nodeElement) {
 
 176             commands.updateCurrentNodeElement(nodeElement);
 
 179         currentNodeElementChanged: function(nodeElement) {
 
 180             commands.updateCurrentNodeElement(nodeElement);
 
 183         nodeHovered: function(canvasNode) {
 
 184             commands.highlightDocumentNode(canvasNode);
 
 187         nodeBlured: function(canvasNode) {
 
 188             commands.dimDocumentNode(canvasNode);
 
 192     eventHandlers.nodePane = {
 
 194             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
 
 197         nodeElementChange: function(attr, value) {
 
 198             sandbox.getModule('documentCanvas').modifyCurrentNodeElement(attr, value);
 
 202     eventHandlers.metadataEditor = {
 
 204             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
 
 205             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
 
 209     eventHandlers.nodeFamilyTree = {
 
 211             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
 
 213         nodeEntered: function(node) {
 
 214             commands.highlightDocumentElement(node, 'nodeFamilyTree');
 
 216         nodeLeft: function(node) {
 
 217             commands.dimDocumentElement(node, 'nodeFamilyTree');
 
 219         nodeClicked: function(node) {
 
 220             commands.jumpToDocumentElement(node);
 
 224     eventHandlers.documentToolbar = {
 
 226             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
 
 228         command: function(cmd, params) {
 
 229             sandbox.getModule('documentCanvas').command(cmd, params);
 
 233     eventHandlers.nodeBreadCrumbs = {
 
 235             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
 
 237         elementEntered: function(element) {
 
 238             commands.highlightDocumentElement(element, 'nodeBreadCrumbs');
 
 240         elementLeft: function(element) {
 
 241             commands.dimDocumentElement(element, 'nodeBreadCrumbs');
 
 243         elementClicked: function(element) {
 
 244             commands.jumpToDocumentElement(element);
 
 248     eventHandlers.documentHistory = {
 
 250             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
 
 251             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
 
 253         compare: function(ver1, ver2) {
 
 254             sandbox.getModule('data').fetchDiff(ver1, ver2);
 
 256         restoreVersion: function(version) {
 
 257             sandbox.getModule('data').restoreVersion(version);
 
 259         displayVersion: function(event) {
 
 261             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
 
 265     eventHandlers.diffViewer = {
 
 267             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
 
 271     window.addEventListener('beforeunload', function(event) {
 
 272         var txt = gettext('Do you really want to exit?');
 
 273         if(documentIsDirty) {
 
 274             txt += ' ' + gettext('Document contains unsaved changes!');
 
 276         event.returnValue = txt; // FF
 
 277         return txt; // Chrome
 
 284             sandbox.getModule('data').start();
 
 286         handleEvent: function(moduleName, eventName, args) {
 
 287             var eventRepr = moduleName + '.' + eventName;
 
 288             if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
 
 289                 logger.debug('Handling event ' + eventRepr);
 
 290                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);
 
 292                 logger.debug('No event handler for ' + eventRepr);