X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/9385033bf213d7d00f0646c1ee3b3aa8a463cf02..06f085a7b35932bcf2c83b3279d5adc687de908c:/modules/visualEditor.js?ds=sidebyside diff --git a/modules/visualEditor.js b/modules/visualEditor.js index 2c3b6d3..38f3302 100644 --- a/modules/visualEditor.js +++ b/modules/visualEditor.js @@ -1,52 +1,83 @@ rng.modules.visualEditor = function(sandbox) { var transformations = rng.modules.visualEditor.transformations; - var view = $(sandbox.getTemplate('main')()); - var isDirty = false; - - var getMetaData = function() { - var toret = {}; - view.find('#rng-visualEditor-meta table tr').each(function() { - var tr = $(this); - var key = $(tr.find('td')[0]).html(); - var value = $(tr.find('td input')[0]).val(); - toret[key] = value; - }); - console.log(toret); - return toret; - }; - - var setMetaData = function(metadata) { - var table = view.find('#rng-visualEditor-meta table'); - table.empty(); - _.each(_.keys(metadata), function(key) { - $(sandbox.getTemplate('metaItem')({key: key, value: metadata[key]})).appendTo(table); - }); + var view = { + node: $(sandbox.getTemplate('main')()), + setup: function() { + var view = this; + + this.node.find('#rng-visualEditor-content').on('keyup', function() { + isDirty = true; + }); + + this.node.find('#rng-visualEditor-meta').on('keyup', function() { + isDirty = true; + }); + + this.node.on('mouseover', '.rng', function(e) { $(e.target).addClass('rng-hover')}); + this.node.on('mouseout', '.rng', function(e) { $(e.target).removeClass('rng-hover')}); + this.node.on('click', '.rng', function(e) { + view._markSelected($(e.target)); + }); + + this.node.on('keyup', function(e) { + var anchor = $(window.getSelection().anchorNode); + if(anchor[0].nodeType === Node.TEXT_NODE) + anchor = anchor.parent(); + if(!anchor.hasClass('rng')) + return; + view._markSelected(anchor); + }); + }, + getMetaData: function() { + var toret = {}; + this.node.find('#rng-visualEditor-meta table tr').each(function() { + var tr = $(this); + var key = $(tr.find('td')[0]).html(); + var value = $(tr.find('td input')[0]).val(); + toret[key] = value; + }); + console.log(toret); + return toret; + }, + setMetaData: function(metadata) { + var table = this.node.find('#rng-visualEditor-meta table'); + table.empty(); + _.each(_.keys(metadata), function(key) { + $(sandbox.getTemplate('metaItem')({key: key, value: metadata[key]})).appendTo(table); + }); + }, + setBody: function(HTMLTree) { + this.node.find('#rng-visualEditor-content').html(HTMLTree); + }, + getBody: function() { + return this.node.find('#rng-visualEditor-content').html(); + }, + _markSelected: function(node) { + this.node.find('.rng').removeClass('rng-current'); + node.addClass('rng-current'); + } }; + view.setup(); - $('#rng-visualEditor-content', view).on('keyup', function() { - isDirty = true; - }); + var isDirty = false; - $('#rng-visualEditor-meta', view).on('keyup', function() { - isDirty = true; - }); return { start: function() { sandbox.publish('ready'); }, getView: function() { - return view; + return view.node; }, setDocument: function(xml) { var transformed = transformations.fromXML.getDocumentDescription(xml); - $('#rng-visualEditor-content', view).html(transformed.HTMLTree); - setMetaData(transformed.metadata); + view.setBody(transformed.HTMLTree); + view.setMetaData(transformed.metadata); isDirty = false; }, getDocument: function() { - return transformations.toXML.getXML({HTMLTree: $('#rng-visualEditor-content').html(), metadata: getMetaData()}); + return transformations.toXML.getXML({HTMLTree: view.getBody(), metadata: view.getMetaData()}); }, isDirty: function() { return isDirty;