2 'libs/jquery-1.9.1.min',
\r
3 'libs/underscore-min',
\r
5 'libs/text!./templates/main.html',
\r
6 'libs/text!./templates/item.html'
\r
7 ], function($, _, transformations, mainTemplate, itemTemplate) {
\r
11 return function(sandbox) {
\r
15 node: $(_.template(mainTemplate)()),
\r
18 var metaTable = this.metaTable = this.node.find('table');
\r
20 this.node.find('.rng-module-metadataEditor-addBtn').click(function() {
\r
21 var newRow = view._addMetaRow('', '');
\r
22 $(newRow.find('td div')[0]).focus();
\r
23 sandbox.publish('metadataChanged', view.getMetadata());
\r
26 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
\r
27 $(e.target).closest('tr').remove();
\r
28 sandbox.publish('metadataChanged', view.getMetadata());
\r
31 this.metaTable.on('keydown', '[contenteditable]', function(e) {
\r
32 console.log(e.which);
\r
33 if(e.which === 13) {
\r
34 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
\r
35 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
\r
37 var input = $('<input>');
\r
38 input.appendTo('body').focus();
\r
39 view.node.find('.rng-module-metadataEditor-addBtn').focus();
\r
47 var onKeyUp = function(e) {
\r
49 sandbox.publish('metadataChanged', view.getMetadata());
\r
51 this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
\r
53 getMetadata: function() {
\r
55 this.node.find('tr').each(function() {
\r
57 var inputs = $(this).find('td [contenteditable]');
\r
58 var key = $(inputs[0]).text();
\r
59 var value = $(inputs[1]).text();
\r
64 setMetadata: function(metadata) {
\r
66 this.metaTable.find('tr').remove();
\r
67 _.each(_.keys(metadata), function(key) {
\r
68 view._addMetaRow(key, metadata[key]);
\r
71 _addMetaRow: function(key, value) {
\r
72 var newRow = $(_.template(itemTemplate)({key: key || '', value: value || ''}));
\r
73 newRow.appendTo(this.metaTable);
\r
82 sandbox.publish('ready');
\r
84 setDocument: function(xml) {
\r
85 view.setMetadata(transformations.getMetadata(xml));
\r
86 sandbox.publish('metadataSet');
\r
88 getMetadata: function() {
\r
89 return transformations.getXML(view.getMetadata());
\r
91 getView: function() {
\r
94 attachMetadata: function(document) {
\r
95 var toret = $('<div>');
\r
96 toret.append($(document));
\r
97 var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));
\r
99 var metadata = toret.find('metadata');
\r
100 if(metadata.length === 0) {
\r
101 var section = toret.find('section');
\r
102 section = section.length ? $(section[0]) : null;
\r
104 section.prepend(meta);
\r
107 metadata.replaceWith(meta);
\r
109 return toret.html();
\r