4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html'
6 ], function($, _, mainTemplate, itemTemplate) {
10 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();
25 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
26 $(e.target).closest('tr').remove();
29 this.metaTable.on('keydown', '[contenteditable]', function(e) {
30 /* globals document */
32 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
33 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
35 var input = $('<input>');
36 input.appendTo('body').focus();
37 view.node.find('.rng-module-metadataEditor-addBtn').focus();
45 var onKeyUp = function(e) {
47 var editable = $(e.target),
48 myIndex = metaTable.find('.'+editable.attr('class')).index(editable),
49 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
51 toSet[isKey ? 'key' : 'value'] = editable.text();
52 currentNode.setMetadataRow(myIndex, toSet);
55 this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
57 setMetadata: function(node) {
59 metadata = node.getMetadata();
60 this.metaTable.find('tr').remove();
61 metadata.forEach(function(row) {
62 view._addMetaRow(row.getKey(), row.getValue());
65 _addMetaRow: function(key, value) {
66 var newRow = $(_.template(itemTemplate)({key: key || '', value: value || ''}));
67 newRow.appendTo(this.metaTable);
76 sandbox.publish('ready');
78 setDocument: function(document) {
79 document.on('change', function(event) {
80 if(event.type === 'nodeMetadataChange' && event.meta.node.sameNode(currentNode)) {
81 view.setMetadata(currentNode);
85 setNodeElement: function(node) {
86 if(currentNode && currentNode.sameNode(node)) {
90 view.setMetadata(node);