4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html',
6 'views/openSelect/openSelect'
7 ], function($, _, mainTemplate, itemTemplate, OpenSelectView) {
11 return function(sandbox) {
15 metadataConfig = (sandbox.getConfig().metadata || []).sort(function(configRow1, configRow2) {
16 if(configRow1.key < configRow2.key) {
19 if(configRow1.key > configRow2.key) {
25 var getValuesForKey = function(key) {
27 metadataConfig.some(function(configRow) {
28 if(configRow.key === key) {
29 toret = configRow.values || [];
37 node: $(_.template(mainTemplate)()),
40 var metaTable = this.metaTable = this.node.find('table');
42 this.node.find('.rng-module-metadataEditor-addBtn').click(function() {
44 currentNode.getMetadata().add('','');
47 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
48 $(e.target).closest('tr').data('row').remove();
51 this.metaTable.on('keydown', '[contenteditable]', function(e) {
52 /* globals document */
54 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
55 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
57 var input = $('<input>');
58 input.appendTo('body').focus();
59 view.node.find('.rng-module-metadataEditor-addBtn').focus();
66 this.metaTable.on('keyup', '[contenteditable]', _.throttle(function(e) {
68 var editable = $(e.target),
69 toSet = editable.text(),
70 row = editable.parents('tr').data('row'),
71 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
72 method = isKey ? 'setKey' : 'setValue';
79 setMetadata: function(node) {
81 this.metaTable.html('');
85 metadata = node.getMetadata();
86 this.metaTable.find('tr').remove();
87 metadata.forEach(function(row) {
88 view.addMetadataRow(row);
91 addMetadataRow: function(row) {
92 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
93 newRow.appendTo(this.metaTable);
94 newRow.data('row', row);
96 var keySelectView = new OpenSelectView({
97 value: row.getKey() || '',
98 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemKey" contentEditable="true"><%= value %></div>')({value: row.getKey() || '' }),
99 setInput: function(inputDOM, value) {
100 if(inputDOM.text() !== value) {
101 inputDOM.text(value);
104 valueSelectView.clearItems();
105 getValuesForKey(value).forEach(function(value) {
106 valueSelectView.addItem(value);
110 newRow.find('td:first').append(keySelectView.el).data('view', keySelectView);
113 var valueSelectView = new OpenSelectView({
114 value: row.getValue(),
115 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemValue" contentEditable="true"><%= value %></div>')({value: row.getValue() || '' }),
116 setInput: function(inputDOM, value) {
117 if(inputDOM.text() !== value) {
118 inputDOM.text(value);
123 newRow.find('td:nth-child(2)').append(valueSelectView.el).data('view', valueSelectView);
126 metadataConfig.forEach(function(configRow) {
127 keySelectView.addItem(configRow.key);
128 if(row.getKey() === configRow.key) {
129 (configRow.values || []).forEach(function(value) {
130 valueSelectView.addItem(value);
136 $(newRow.find('td div')[0]).focus();
141 updateMetadataRow: function(row) {
142 this._getRowTr(row, function(tr) {
143 var tds = tr.find('td'),
147 keyTd.data('view').setInput(row.getKey());
148 valueTd.data('view').setInput(row.getValue());
151 removeMetadataRow: function(row) {
152 this._getRowTr(row, function(tr) {
156 _getRowTr: function(row, callback) {
157 this.metaTable.find('tr').each(function() {
159 if(tr.data('row') === row) {
171 sandbox.publish('ready');
173 setDocument: function(document) {
174 document.on('change', function(event) {
175 if(event.type === 'metadataAdded' && event.meta.node.sameNode(currentNode)) {
176 view.addMetadataRow(event.meta.row);
178 if(event.type === 'metadataChanged' && event.meta.node.sameNode(currentNode)) {
179 view.updateMetadataRow(event.meta.row);
181 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(currentNode)) {
182 view.removeMetadataRow(event.meta.row);
184 if(event.type === 'nodeDetached' && event.meta.node.sameNode(currentNode)) {
185 view.setMetadata(null);
189 setNodeElement: function(node) {
190 if(currentNode && currentNode.sameNode(node)) {
194 view.setMetadata(node);
196 getView: function() {