4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html'
6 ], function($, _, mainTemplate, itemTemplate) {
10 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() {
23 currentNode.getMetadata().add('','');
26 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
27 $(e.target).closest('tr').data('row').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();
45 this.metaTable.on('keyup', '[contenteditable]', _.throttle(function(e) {
47 var editable = $(e.target),
48 toSet = editable.text(),
49 row = editable.parents('tr').data('row'),
50 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
51 method = isKey ? 'setKey' : 'setValue';
56 setMetadata: function(node) {
58 metadata = node.getMetadata();
59 this.metaTable.find('tr').remove();
60 metadata.forEach(function(row) {
61 view.addMetadataRow(row);
64 addMetadataRow: function(row) {
65 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
66 newRow.appendTo(this.metaTable);
67 newRow.data('row', row);
69 $(newRow.find('td div')[0]).focus();
74 updateMetadataRow: function(row) {
75 this._getRowTr(row, function(tr) {
76 var tds = tr.find('td > div'),
80 if(keyTd.text() !== row.getKey()) {
81 keyTd.text(row.getKey());
83 if(valueTd.text() !== row.getValue()) {
84 valueTd.text(row.getValue());
88 removeMetadataRow: function(row) {
89 this._getRowTr(row, function(tr) {
93 _getRowTr: function(row, callback) {
94 this.metaTable.find('tr').each(function() {
96 if(tr.data('row') === row) {
108 sandbox.publish('ready');
110 setDocument: function(document) {
111 document.on('change', function(event) {
112 if(event.type === 'metadataAdded' && event.meta.node.sameNode(currentNode)) {
113 view.addMetadataRow(event.meta.row);
115 if(event.type === 'metadataChanged' && event.meta.node.sameNode(currentNode)) {
116 view.updateMetadataRow(event.meta.row);
118 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(currentNode)) {
119 view.removeMetadataRow(event.meta.row);
123 setNodeElement: function(node) {
124 if(currentNode && currentNode.sameNode(node)) {
128 view.setMetadata(node);
130 getView: function() {