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) {
32 /* globals document */
34 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
35 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
37 var input = $('<input>');
38 input.appendTo('body').focus();
39 view.node.find('.rng-module-metadataEditor-addBtn').focus();
47 var onKeyUp = function(e) {
49 sandbox.publish('metadataChanged', view.getMetadata());
52 this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
54 getMetadata: function() {
56 this.node.find('tr').each(function() {
57 var inputs = $(this).find('td [contenteditable]');
58 var key = $(inputs[0]).text();
59 var value = $(inputs[1]).text();
64 setMetadata: function(metadata) {
66 this.metaTable.find('tr').remove();
67 _.each(_.keys(metadata), function(key) {
68 view._addMetaRow(key, metadata[key]);
71 _addMetaRow: function(key, value) {
72 var newRow = $(_.template(itemTemplate)({key: key || '', value: value || ''}));
73 newRow.appendTo(this.metaTable);
82 sandbox.publish('ready');
84 setDocument: function(xml) {
85 view.setMetadata(transformations.getMetadata(xml));
86 sandbox.publish('metadataSet');
88 getMetadata: function() {
89 return transformations.getXML(view.getMetadata());
94 attachMetadata: function(document) {
95 var toret = $('<div>');
96 toret.append($(document));
97 var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));
99 var metadata = toret.find('metadata');
100 if(metadata.length === 0) {
101 var section = toret.find('section');
102 section = section.length ? $(section[0]) : null;
104 section.prepend(meta);
107 metadata.replaceWith(meta);