5 'libs/text!./templates/main.html',
6 'libs/text!./templates/item.html'
7 ], function($, _, transformations, mainTemplate, itemTemplate) {
11 return function(sandbox) {
16 node: $(_.template(mainTemplate)()),
19 var metaTable = this.metaTable = this.node.find('table');
21 this.node.find('.rng-module-metadataEditor-addBtn').click(function() {
22 var newRow = view._addMetaRow('', '');
23 $(newRow.find('td div')[0]).focus();
26 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
27 $(e.target).closest('tr').remove();
30 this.metaTable.on('keydown', '[contenteditable]', function(e) {
31 /* globals document */
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 var editable = $(e.target),
49 myIndex = metaTable.find('.'+editable.attr('class')).index(editable),
50 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
52 toSet[isKey ? 'key' : 'value'] = editable.text();
53 currentNode.setMetadataRow(myIndex, toSet);
56 this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
58 getMetadata: function() {
60 this.node.find('tr').each(function() {
61 var inputs = $(this).find('td [contenteditable]');
62 var key = $(inputs[0]).text();
63 var value = $(inputs[1]).text();
64 toret.push({key:key, value: value});
68 setMetadata: function(node) {
70 metadata = node.getMetadata();
71 this.metaTable.find('tr').remove();
72 metadata.forEach(function(row) {
73 view._addMetaRow(row.key, row.value);
76 _addMetaRow: function(key, value) {
77 var newRow = $(_.template(itemTemplate)({key: key || '', value: value || ''}));
78 newRow.appendTo(this.metaTable);
87 sandbox.publish('ready');
89 setDocument: function(document) {
90 document.on('change', function(event) {
91 if(event.type === 'nodeMetadataChange' && event.meta.node.sameNode(currentNode)) {
92 view.setMetadata(currentNode);
95 // view.setMetadata(transformations.getMetadata(xml));
96 // sandbox.publish('metadataSet'); to wywalki
98 setNodeElement: function(node) {
99 if(currentNode && currentNode.sameNode(node)) {
103 view.setMetadata(node);
105 getMetadata: function() {
106 return transformations.getXML(view.getMetadata());
108 getView: function() {
111 attachMetadata: function(document) {
112 var toret = $('<div>');
113 toret.append($(document));
114 var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));
116 var metadata = toret.find('metadata');
117 if(metadata.length === 0) {
118 var section = toret.find('section');
119 section = section.length ? $(section[0]) : null;
121 section.prepend(meta);
124 metadata.replaceWith(meta);