5 'libs/text!./templates/main.html',
 
   6 'libs/text!./templates/item.html'
 
   7 ], function($, _, transformations, mainTemplate, itemTemplate) {
 
  11 return function(sandbox) {
 
  15         node: $(_.template(mainTemplate)()),
 
  18             var metaTable = this.metaTable = this.node.find('table');
 
  20             this.node.find('.rng-module-metadataEditor-addBtn').click(function() {
 
  21                 var newRow = view._addMetaRow('', '');
 
  22                 $(newRow.find('td div')[0]).focus();
 
  23                 sandbox.publish('metadataChanged', view.getMetadata());
 
  26             this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
 
  27                 $(e.target).closest('tr').remove();
 
  28                 sandbox.publish('metadataChanged', view.getMetadata());
 
  31             this.metaTable.on('keydown', '[contenteditable]', function(e) {
 
  33                     if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
 
  34                         metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
 
  36                         var input = $('<input>');
 
  37                         input.appendTo('body').focus();
 
  38                         view.node.find('.rng-module-metadataEditor-addBtn').focus();
 
  46             var onKeyUp = function(e) {
 
  48                     sandbox.publish('metadataChanged', view.getMetadata());
 
  51             this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
 
  53         getMetadata: function() {
 
  55             this.node.find('tr').each(function() {
 
  56                 var inputs = $(this).find('td [contenteditable]');
 
  57                 var key = $(inputs[0]).text();
 
  58                 var value = $(inputs[1]).text();
 
  63         setMetadata: function(metadata) {
 
  65             this.metaTable.find('tr').remove();
 
  66             _.each(_.keys(metadata), function(key) {
 
  67                 view._addMetaRow(key, metadata[key]);
 
  70         _addMetaRow: function(key, value) {
 
  71             var newRow = $(_.template(itemTemplate)({key: key || '', value: value || ''}));
 
  72             newRow.appendTo(this.metaTable);
 
  81             sandbox.publish('ready');
 
  83         setDocument: function(xml) {
 
  84             view.setMetadata(transformations.getMetadata(xml));
 
  85             sandbox.publish('metadataSet');
 
  87         getMetadata: function() {
 
  88             return transformations.getXML(view.getMetadata());
 
  93         attachMetadata: function(document) {
 
  94             var toret = $('<div>');
 
  95             toret.append($(document));
 
  96             var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));
 
  98             var metadata = toret.find('metadata');
 
  99             if(metadata.length === 0) {
 
 100                 var section = toret.find('section');
 
 101                 section = section.length ? $(section[0]) : null;
 
 103                     section.prepend(meta);
 
 106                 metadata.replaceWith(meta);