5 'libs/text!./mainLayout.html',
 
   6 'libs/text!./editingLayout.html',
 
   7 'libs/text!./diffLayout.html',
 
   8 ], function(layout, vbox, tabs, mainLayoutTemplate, visualEditingLayoutTemplate, diffLayoutTemplate) {
 
  12 return function(sandbox) {
 
  14     function addMainTab(title, slug, view) {
 
  15         views.mainTabs.addTab(title, slug, view);
 
  20         documentCanvas: false,
 
  21         metadataEditor: false,
 
  24     var synchronizeTab = function(slug) {
 
  25         function tabIsDirty(slug) {
 
  26             if(slug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor))
 
  28             if(slug === 'sourceEditor' && dirty.sourceEditor)
 
  33         if(tabIsDirty(slug)) {
 
  35             if(slug === 'sourceEditor') {
 
  36                 doc = sandbox.getModule('sourceEditor').getDocument();
 
  37                 reason = 'source_edit';
 
  38                 dirty.sourceEditor = false;
 
  40             if(slug === 'editor') {
 
  41                 doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();
 
  42                 if(dirty.metadataEditor) {
 
  43                     doc = sandbox.getModule('metadataEditor').attachMetadata(doc);
 
  46                 dirty.documentCanvas = dirty.metadataEditor = false;
 
  48             sandbox.getModule('data').commitDocument(doc, reason);
 
  53         highlightDocumentNode: function(canvasNode, origin) {
 
  54             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
 
  55                 if(!origin || moduleName != origin)
 
  56                     sandbox.getModule(moduleName).highlightNode(canvasNode);
 
  59         dimDocumentNode: function(canvasNode, origin) {
 
  60             ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {
 
  61                 if(!origin || moduleName != origin)
 
  62                     sandbox.getModule(moduleName).dimNode(canvasNode);
 
  65         selectNode: function(canvasNode, origin) {
 
  66             sandbox.getModule('documentCanvas').selectNode(canvasNode);
 
  67             this.updateNodesModules(canvasNode);           
 
  69         updateNodesModules: function(canvasNode) {
 
  70             sandbox.getModule('nodePane').setNode(canvasNode);
 
  71             sandbox.getModule('nodeFamilyTree').setNode(canvasNode);
 
  72             sandbox.getModule('nodeBreadCrumbs').setNode(canvasNode);
 
  74         resetDocument: function(document, reason) {
 
  76             if(reason === 'source_edit')
 
  77                 modules = ['documentCanvas', 'metadataEditor'];
 
  78             else if (reason === 'edit')
 
  79                 modules = ['sourceEditor'];
 
  80             else if (reason === 'revert')
 
  81                 modules = ['documentCanvas', 'metadataEditor', 'sourceEditor'];
 
  83             modules.forEach(function(moduleName) {
 
  84                 sandbox.getModule(moduleName).setDocument(document);
 
  91         mainLayout: new layout.Layout(mainLayoutTemplate),
 
  92         mainTabs: (new tabs.View()).render(),
 
  93         visualEditing: new layout.Layout(visualEditingLayoutTemplate),
 
  94         visualEditingSidebar: (new tabs.View({stacked: true})).render(),
 
  95         currentNodePaneLayout: new vbox.VBox(),
 
  96         diffLayout: new layout.Layout(diffLayoutTemplate)
 
  99     views.visualEditing.setView('rightColumn', views.visualEditingSidebar.getAsView());
 
 100     addMainTab('Edytor', 'editor', views.visualEditing.getAsView());
 
 101     addMainTab(gettext('Source'), 'sourceEditor',  '');
 
 102     addMainTab('Historia', 'history', views.diffLayout.getAsView());
 
 104     sandbox.getDOM().append(views.mainLayout.getAsView());
 
 106     views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());
 
 108     views.mainTabs.on('tabSelected', function(event) {
 
 110             synchronizeTab(event.prevSlug);
 
 114     /* Events handling */
 
 116     var eventHandlers = {};
 
 118     eventHandlers.sourceEditor = {
 
 120             addMainTab(gettext('Source'), 'sourceEditor',  sandbox.getModule('sourceEditor').getView());
 
 121             sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());
 
 123         xmlChanged: function() {
 
 124             dirty.sourceEditor = true;
 
 126         documentSet: function() {
 
 127             dirty.sourceEditor = false;
 
 131     eventHandlers.data = {
 
 133             views.mainLayout.setView('mainView', views.mainTabs.getAsView());
 
 135             _.each(['sourceEditor', 'documentCanvas', 'documentToolbar', 'nodePane', 'metadataEditor', 'nodeFamilyTree', 'nodeBreadCrumbs', 'mainBar', 'indicator', 'documentHistory', 'diffViewer'], function(moduleName) {
 
 136                 sandbox.getModule(moduleName).start();
 
 139         documentChanged: function(document, reason) {
 
 140             commands.resetDocument(document, reason);
 
 142         savingStarted: function() {
 
 143             sandbox.getModule('mainBar').setCommandEnabled('save', false);
 
 144             sandbox.getModule('indicator').showMessage(gettext('Saving...'));
 
 146         savingEnded: function(status) {
 
 147             sandbox.getModule('mainBar').setCommandEnabled('save', true);
 
 148             sandbox.getModule('indicator').clearMessage({message:'Dokument zapisany'});
 
 150         restoringStarted: function(event) {
 
 151             sandbox.getModule('mainBar').setCommandEnabled('save', false);
 
 152             sandbox.getModule('indicator').showMessage(gettext('Restoring version ') + event.version + '...');
 
 154         historyItemAdded: function(item) {
 
 155             sandbox.getModule('documentHistory').addHistory([item], {animate: true});
 
 157         diffFetched: function(diff) {
 
 158             sandbox.getModule('diffViewer').setDiff(diff);
 
 160         documentReverted: function(event) {
 
 161             commands.resetDocument(event.document, 'revert');
 
 162             sandbox.getModule('mainBar').setCommandEnabled('save', true);
 
 163             sandbox.getModule('indicator').clearMessage({message:'Wersja ' + event.reverted_version + ' przywrócona'});
 
 164             sandbox.getModule('mainBar').setVersion(event.current_version);
 
 168     eventHandlers.mainBar = {
 
 170             sandbox.getModule('mainBar').setVersion(sandbox.getModule('data').getDocumentVersion());
 
 171             views.mainLayout.setView('topPanel', sandbox.getModule('mainBar').getView());
 
 173         'cmd.save': function() {
 
 174             synchronizeTab(views.mainTabs.getCurrentSlug());
 
 175             sandbox.getModule('data').saveDocument();
 
 179     eventHandlers.indicator = {
 
 181             views.mainLayout.setView('messages', sandbox.getModule('indicator').getView());
 
 187     eventHandlers.documentCanvas = {
 
 189             sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());
 
 190             views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());
 
 192         documentSet: function() {
 
 193             dirty.documentCanvas = false;
 
 196         nodeSelected: function(canvasNode) {
 
 197             commands.selectNode(canvasNode);
 
 200         contentChanged: function() {
 
 201             dirty.documentCanvas = true;
 
 204         currentNodeChanged: function(canvasNode) {
 
 205             commands.updateNodesModules(canvasNode);
 
 208         nodeHovered: function(canvasNode) {
 
 209             commands.highlightDocumentNode(canvasNode);
 
 212         nodeBlured: function(canvasNode) {
 
 213             commands.dimDocumentNode(canvasNode);
 
 217     eventHandlers.nodePane = {
 
 219             views.currentNodePaneLayout.appendView(sandbox.getModule('nodePane').getView());
 
 222         nodeChanged: function(attr, value) {
 
 223             sandbox.getModule('documentCanvas').modifyCurrentNode(attr, value);
 
 227     eventHandlers.metadataEditor = {
 
 229             sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());
 
 230             views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());
 
 232         metadataChanged: function(metadata) {
 
 233             dirty.metadataEditor = true;
 
 235         metadataSet: function() {
 
 236             dirty.metadataEditor = false;
 
 240     eventHandlers.nodeFamilyTree = {
 
 242             views.currentNodePaneLayout.appendView(sandbox.getModule('nodeFamilyTree').getView());
 
 244         nodeEntered: function(canvasNode) {
 
 245             commands.highlightDocumentNode(canvasNode, 'nodeFamilyTree');
 
 247         nodeLeft: function(canvasNode) {
 
 248             commands.dimDocumentNode(canvasNode, 'nodeFamilyTree');
 
 250         nodeSelected: function(canvasNode) {
 
 251             commands.selectNode(canvasNode);
 
 255     eventHandlers.documentToolbar = {
 
 257             views.visualEditing.setView('toolbar', sandbox.getModule('documentToolbar').getView());
 
 259         toggleGrid: function(toggle) {
 
 260             sandbox.getModule('documentCanvas').toggleGrid(toggle);
 
 262         newNodeRequested: function(wlxmlTag, wlxmlClass) {
 
 263                 sandbox.getModule('documentCanvas').insertNewNode(wlxmlTag, wlxmlClass);
 
 265         command: function(cmd, meta) {
 
 266             sandbox.getModule('documentCanvas').command(cmd, meta);
 
 270     eventHandlers.nodeBreadCrumbs = {
 
 272             views.visualEditing.setView('statusBar', sandbox.getModule('nodeBreadCrumbs').getView());
 
 274         nodeHighlighted: function(canvasNode) {
 
 275             commands.highlightDocumentNode(canvasNode, 'nodeBreadCrumbs');
 
 277         nodeDimmed: function(canvasNode) {
 
 278             commands.dimDocumentNode(canvasNode, 'nodeBreadCrumbs');
 
 280         nodeSelected: function(canvasNode) {
 
 281             commands.selectNode(canvasNode);
 
 285     eventHandlers.documentHistory = {
 
 287             sandbox.getModule('documentHistory').addHistory(sandbox.getModule('data').getHistory());
 
 288             views.diffLayout.setView('left', sandbox.getModule('documentHistory').getView());
 
 290         compare: function(ver1, ver2) {
 
 291             sandbox.getModule('data').fetchDiff(ver1, ver2);
 
 293         restoreVersion: function(event) {
 
 294             sandbox.getModule('data').restoreVersion(event);
 
 296         displayVersion: function(event) {
 
 297             window.open('/' + gettext('editor') + '/' + sandbox.getModule('data').getDocumentId() + '?version=' + event.version, _.uniqueId());
 
 301     eventHandlers.diffViewer = {
 
 303             views.diffLayout.setView('right', sandbox.getModule('diffViewer').getView());
 
 311             sandbox.getModule('data').start();
 
 313         handleEvent: function(moduleName, eventName, args) {
 
 315                 wysiwigHandler.handleEvent(moduleName, eventName, args);
 
 316             else if(eventHandlers[moduleName] && eventHandlers[moduleName][eventName]) {
 
 317                 eventHandlers[moduleName][eventName].apply(eventHandlers, args);