X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/5be499ab63f91b65065fe34f3a6d645efc9ae7b6..b7d40f4f02e71505150c8b33ad3ad7d7acde0ae2:/modules/rng/rng.js diff --git a/modules/rng/rng.js b/modules/rng/rng.js index 5edc7ac..6029e7c 100644 --- a/modules/rng/rng.js +++ b/modules/rng/rng.js @@ -13,6 +13,12 @@ return function(sandbox) { views.mainTabs.addTab(title, slug, view); } + var dirty = { + sourceEditor: false, + documentCanvas: false, + metadataEditor: false, + }; + var commands = { highlightDocumentNode: function(wlxmlNode, origin) { ['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) { @@ -25,6 +31,13 @@ return function(sandbox) { if(!origin || moduleName != origin) sandbox.getModule(moduleName).dimNode(wlxmlNode) }); + }, + selectNode: function(wlxmlNode, origin) { + sandbox.getModule('documentCanvas').selectNode(wlxmlNode); + sandbox.getModule('nodePane').setNode(wlxmlNode); + sandbox.getModule('nodeFamilyTree').setNode(wlxmlNode); + sandbox.getModule('nodeBreadCrumbs').setNode(wlxmlNode); + } } @@ -44,6 +57,21 @@ return function(sandbox) { views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView()); + views.mainTabs.on('tabSelected', function(event) { + if(event.prevSlug) { + if(event.prevSlug === 'sourceEditor' && dirty.sourceEditor) { + sandbox.getModule('data').commitDocument(sandbox.getModule('sourceEditor').getDocument(), 'source_edit'); + } + if(event.prevSlug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor)) { + var doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument(); + if(dirty.metadataEditor) { + doc = sandbox.getModule('metadataEditor').attachMetadata(doc); + } + sandbox.getModule('data').commitDocument(doc, 'edit'); + } + + } + }); /* Events handling */ @@ -51,8 +79,14 @@ return function(sandbox) { eventHandlers.sourceEditor = { ready: function() { - addMainTab(gettext('Source'), 'source', sandbox.getModule('sourceEditor').getView()); + addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView()); sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument()); + }, + xmlChanged: function() { + dirty.sourceEditor = true; + }, + documentSet: function() { + dirty.sourceEditor = false; } }; @@ -65,8 +99,15 @@ return function(sandbox) { }); }, documentChanged: function(document, reason) { - var slug = (reason === 'visual_edit' ? 'source' : 'visual'); - sandbox.getModule(slug+'Editor').setDocument(document); + var modules = []; + if(reason === 'source_edit') + modules = ['documentCanvas', 'metadataEditor']; + else if (reason === 'edit') + modules = ['sourceEditor']; + + modules.forEach(function(moduleName) { + sandbox.getModule(moduleName).setDocument(document); + }); }, savingStarted: function() { sandbox.getModule('mainBar').setCommandEnabled('save', false); @@ -100,15 +141,16 @@ return function(sandbox) { sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument()); views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView()); }, + documentSet: function() { + dirty.documentCanvas = false; + }, - nodeSelected: function(node) { - sandbox.getModule('nodePane').setNode(node); - sandbox.getModule('nodeFamilyTree').setNode(node); - sandbox.getModule('nodeBreadCrumbs').setNode(node); + nodeSelected: function(wlxmlNode) { + commands.selectNode(wlxmlNode); }, contentChanged: function() { - + dirty.documentCanvas = true; }, nodeHovered: function(wlxmlNode) { @@ -132,9 +174,15 @@ return function(sandbox) { eventHandlers.metadataEditor = { ready: function() { - sandbox.getModule('metadataEditor').setMetadata(sandbox.getModule('data').getDocument()); + sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument()); views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView()); - } + }, + metadataChanged: function(metadata) { + dirty.metadataEditor = true; + }, + metadataSet: function() { + dirty.metadataEditor = false; + }, }; eventHandlers.nodeFamilyTree = { @@ -148,7 +196,7 @@ return function(sandbox) { commands.dimDocumentNode(wlxmlNode, 'nodeFamilyTree'); }, nodeSelected: function(wlxmlNode) { - sandbox.getModule('documentCanvas').selectNode(wlxmlNode); + commands.selectNode(wlxmlNode); } }; @@ -179,7 +227,7 @@ return function(sandbox) { commands.dimDocumentNode(wlxmlNode, 'nodeBreadCrumbs'); }, nodeSelected: function(wlxmlNode) { - sandbox.getModule('documentCanvas').selectNode(wlxmlNode); + commands.selectNode(wlxmlNode); } }