4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html',
6 'views/openSelect/openSelect'
7 ], function($, _, mainTemplate, itemTemplate, OpenSelectView) {
13 var View = function(node, metadataConfig) {
15 this.metadataConfig = metadataConfig;
16 this.dom = $(_.template(mainTemplate)());
19 var metaTable = this.metaTable = this.dom.find('table');
21 this.dom.find('.rng-module-metadataEditor-addBtn').click(function() {
23 this.node.document.transaction(function() {
24 this.node.getMetadata().add('','');
27 description: gettext('Add metadata row')
32 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
33 this.node.document.transaction(function() {
34 $(e.target).closest('tr').data('row').remove();
37 description: gettext('Remove metadata row')
42 this.metaTable.on('keydown', '[contenteditable]', function(e) {
43 /* globals document */
45 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
46 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
48 var input = $('<input>');
49 input.appendTo('body').focus();
50 this.dom.find('.rng-module-metadataEditor-addBtn').focus();
57 this.metaTable.on('keyup', '[contenteditable]', _.throttle(function(e) {
59 var editable = $(e.target),
60 toSet = editable.text(),
61 row = editable.parents('tr').data('row'),
62 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
63 method = isKey ? 'setKey' : 'setValue';
64 row.metadata.node.document.transaction(function() {
68 description: gettext('Metadata edit')
74 this.setMetadata(this.node); //
76 this.node.document.on('change', this.handleEvent, this);
79 _.extend(View.prototype, {
81 this.node.document.off('change', this.handleEvent);
83 handleEvent: function(event) {
84 if(event.type === 'metadataAdded' && event.meta.node.sameNode(this.node)) {
85 this.addMetadataRow(event.meta.row);
87 if(event.type === 'metadataChanged' && event.meta.node.sameNode(this.node)) {
88 this.updateMetadataRow(event.meta.row);
90 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(this.node)) {
91 this.removeMetadataRow(event.meta.row);
93 if(event.type === 'nodeDetached' && event.meta.node.containsNode(this.node)) {
94 this.setMetadata(null);
97 getValuesForKey: function(key) {
99 this.metadataConfig.some(function(configRow) {
100 if(configRow.key === key) {
101 toret = configRow.values || [];
107 setMetadata: function(node) {
108 this.dom.find('.rng-module-metadataEditor-addBtn').attr('disabled', !node);
110 this.metaTable.html('');
114 metadata = node.getMetadata();
115 this.metaTable.find('tr').remove();
116 metadata.forEach(function(row) {
117 view.addMetadataRow(row);
120 addMetadataRow: function(row) {
121 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
122 newRow.appendTo(this.metaTable);
123 newRow.data('row', row);
125 var keySelectView = new OpenSelectView({
126 value: row.getKey() || '',
127 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemKey" contentEditable="true"><%= value %></div>')({value: row.getKey() || '' }),
128 setInput: function(inputDOM, value) {
129 if(inputDOM.text() !== value) {
130 inputDOM.text(value);
133 valueSelectView.clearItems();
134 this.getValuesForKey(value).forEach(function(value) {
135 valueSelectView.addItem(value);
139 newRow.find('td:first').append(keySelectView.el).data('view', keySelectView);
142 var valueSelectView = new OpenSelectView({
143 value: row.getValue(),
144 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemValue" contentEditable="true"><%= value %></div>')({value: row.getValue() || '' }),
147 setInput: function(inputDOM, value) {
148 if(inputDOM.text() !== value) {
149 inputDOM.text(value);
154 newRow.find('td:nth-child(2)').append(valueSelectView.el).data('view', valueSelectView);
157 this.metadataConfig.forEach(function(configRow) {
158 keySelectView.addItem(configRow.key);
159 if(row.getKey() === configRow.key) {
160 (configRow.values || []).forEach(function(value) {
161 valueSelectView.addItem(value);
167 $(newRow.find('td div')[0]).focus();
172 updateMetadataRow: function(row) {
173 this._getRowTr(row, function(tr) {
174 var tds = tr.find('td'),
178 keyTd.data('view').setInput(row.getKey());
179 valueTd.data('view').setInput(row.getValue());
182 removeMetadataRow: function(row) {
183 this._getRowTr(row, function(tr) {
187 _getRowTr: function(row, callback) {
188 this.metaTable.find('tr').each(function() {
190 if(tr.data('row') === row) {