From: Aleksander Ɓukasz Date: Wed, 12 Jun 2013 10:16:30 +0000 (+0200) Subject: Synchronizing changes X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/b7d40f4f02e71505150c8b33ad3ad7d7acde0ae2 Synchronizing changes --- diff --git a/modules/documentCanvas/documentCanvas.js b/modules/documentCanvas/documentCanvas.js index 8f3a3c6..df11ea0 100644 --- a/modules/documentCanvas/documentCanvas.js +++ b/modules/documentCanvas/documentCanvas.js @@ -232,11 +232,16 @@ return function(sandbox) { var transformed = transformations.fromXML.getDocumentDescription(xml); view.setBody(transformed.HTMLTree); view.selectFirstNode(); - //isDirty = false; + sandbox.publish('documentSet'); + }, + getDocument: function() { + return transformations.toXML.getXML(view.getBody()); }, modifyCurrentNode: function(attr, value) { - if(view.currentNode) + if(view.currentNode) { view.currentNode.attr('wlxml-'+attr, value); + sandbox.publish('contentEdited'); + } }, highlightNode: function(wlxmlNode) { view.highlightNode(wlxmlNode); diff --git a/modules/documentCanvas/transformations.js b/modules/documentCanvas/transformations.js index cd20d2b..de72efd 100644 --- a/modules/documentCanvas/transformations.js +++ b/modules/documentCanvas/transformations.js @@ -54,9 +54,9 @@ define(['libs/jquery-1.9.1.min'], function($) { } transformations.toXML = { - getXML: function(documentDescription) { + getXML: function(body) { - var inner = $(documentDescription.HTMLTree); + var inner = $(body); var toret = $('
'); toret.append(inner); @@ -80,25 +80,7 @@ define(['libs/jquery-1.9.1.min'], function($) { toret.append(div.contents()); return toret; }); - - var meta = $('\n'); - _.each(_.keys(documentDescription.metadata), function(key) { - meta.append('\n\t' + documentDescription.metadata[key] + ''); - }); - meta.append('\n'); - - var metadata = toret.find('metadata'); - if(metadata.length === 0) { - var section = toret.find('section'); - section = section.length ? $(section[0]) : null; - if(section) { - section.prepend(meta) - } - } else { - metadata.replaceWith(meta); - } - - + return vkbeautify.xml(toret.html()); } } diff --git a/modules/metadataEditor/metadataEditor.js b/modules/metadataEditor/metadataEditor.js index 5c3e4fc..79302bb 100644 --- a/modules/metadataEditor/metadataEditor.js +++ b/modules/metadataEditor/metadataEditor.js @@ -14,17 +14,18 @@ return function(sandbox) { var view = { node: $(_.template(mainTemplate)()), setup: function() { + var view = this; var metaTable = this.metaTable = this.node.find('table'); this.node.find('.rng-module-metadataEditor-addBtn').click(function() { var newRow = view._addMetaRow('', ''); $(newRow.find('td div')[0]).focus(); - //isDirty = true; + sandbox.publish('metadataChanged', view.getMetadata()); }); this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) { $(e.target).closest('tr').remove(); - //isDirty = true; + sandbox.publish('metadataChanged', view.getMetadata()); }); this.metaTable.on('keydown', '[contenteditable]', function(e) { @@ -40,10 +41,16 @@ return function(sandbox) { } e.preventDefault(); } - }); + + + var onKeyUp = function(e) { + if(e.which !== 13) + sandbox.publish('metadataChanged', view.getMetadata()); + } + this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500)); }, - getMetaData: function() { + getMetadata: function() { var toret = {}; this.node.find('tr').each(function() { var tr = $(this); @@ -74,14 +81,32 @@ return function(sandbox) { start: function() { sandbox.publish('ready'); }, - setMetadata: function(xml) { + setDocument: function(xml) { view.setMetadata(transformations.getMetadata(xml)); + sandbox.publish('metadataSet'); }, getMetadata: function() { - return view.getMetadata(); + return transformations.getXML(view.getMetadata()); }, getView: function() { return view.node; + }, + attachMetadata: function(document) { + var toret = $('
'); + toret.append($(document)); + var meta = $('\n').append(transformations.getXML(view.getMetadata())); + + var metadata = toret.find('metadata'); + if(metadata.length === 0) { + var section = toret.find('section'); + section = section.length ? $(section[0]) : null; + if(section) { + section.prepend(meta) + } + } else { + metadata.replaceWith(meta); + } + return toret.html(); } }; diff --git a/modules/rng/rng.js b/modules/rng/rng.js index 6f65c19..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) { @@ -51,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 */ @@ -58,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; } }; @@ -72,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); @@ -107,13 +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(wlxmlNode) { commands.selectNode(wlxmlNode); }, contentChanged: function() { - + dirty.documentCanvas = true; }, nodeHovered: function(wlxmlNode) { @@ -137,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 = { diff --git a/modules/sourceEditor/sourceEditor.js b/modules/sourceEditor/sourceEditor.js index 248c02d..bd56c43 100644 --- a/modules/sourceEditor/sourceEditor.js +++ b/modules/sourceEditor/sourceEditor.js @@ -3,17 +3,16 @@ define(function() { return function(sandbox) { var view = $(sandbox.getTemplate('main')()); - var isDirty = false; var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]); editor.setTheme("ace/theme/chrome"); editor.getSession().setMode("ace/mode/xml"); $('textarea', view).on('keyup', function() { - isDirty = true; + sandbox.publish('xmlChanged'); }); editor.getSession().on('change', function() { - isDirty = true; + sandbox.publish('xmlChanged'); }) return { start: function() { @@ -25,18 +24,11 @@ return function(sandbox) { setDocument: function(document) { editor.setValue(document); editor.gotoLine(0) - isDirty = false; + sandbox.publish('documentSet'); }, getDocument: function() { return editor.getValue(); - }, - isDirty: function() { - return isDirty; - }, - setDirty: function(dirty) { - isDirty = dirty; } - } }; diff --git a/views/tabs/tabs.js b/views/tabs/tabs.js index 52c8160..f533785 100644 --- a/views/tabs/tabs.js +++ b/views/tabs/tabs.js @@ -57,8 +57,9 @@ define([ this.nodes.tabBar.find('.active').removeClass('active'); this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active'); + var prevSlug = this.selectedTab; this.selectedTab = slug; - this.trigger('tabSelected', slug); + this.trigger('tabSelected', {slug: slug, prevSlug: prevSlug}); } },