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();
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';
51 //toSet[isKey ? 'key' : 'value'] = editable.text();
52 //currentNode.setMetadataRow(myIndex, toSet);
54 var row = editable.parents('tr').data('row'),
55 toSet = editable.text();
64 this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));
66 setMetadata: function(node) {
68 metadata = node.getMetadata();
69 this.metaTable.find('tr').remove();
70 metadata.forEach(function(row) {
71 view.addMetadataRow(row);
74 addMetadataRow: function(row) {
75 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
76 newRow.appendTo(this.metaTable);
77 newRow.data('row', row);
79 $(newRow.find('td div')[0]).focus();
84 updateMetadataRow: function(row) {
85 this._getRowTr(row, function(tr) {
86 var tds = tr.find('td'),
90 if(keyTd.text() !== row.getKey()) {
91 keyTd.text(row.getKey());
93 if(valueTd.text() !== row.getValue()) {
94 valueTd.text(row.getValue());
98 removeMetadataRow: function(row) {
99 this._getRowTr(row, function(tr) {
103 _getRowTr: function(row, callback) {
104 this.metaTable.find('tr').each(function() {
106 if(tr.data('row') === row) {
118 sandbox.publish('ready');
120 setDocument: function(document) {
121 document.on('change', function(event) {
122 if(event.type === 'metadataAdded' && event.meta.node.sameNode(currentNode)) {
123 view.addMetadataRow(event.meta.row);
125 if(event.type === 'metadataChanged' && event.meta.node.sameNode(currentNode)) {
126 view.updateMetadataRow(event.meta.row);
128 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(currentNode)) {
129 view.removeMetadataRow(event.meta.row);
133 setNodeElement: function(node) {
134 if(currentNode && currentNode.sameNode(node)) {
138 view.setMetadata(node);
140 getView: function() {