4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html',
6 'views/openSelect/openSelect',
7 'views/attachments/attachments'
8 ], function($, _, mainTemplate, itemTemplate, OpenSelectView, attachments) {
14 var View = function(node, metadataConfig) {
16 this.metadataConfig = metadataConfig;
17 this.dom = $(_.template(mainTemplate)());
20 var metaTable = this.metaTable = this.dom.find('table');
22 this.dom.find('.rng-module-metadataEditor-addBtn').click(function() {
24 this.node.document.transaction(function() {
25 this.node.getMetadata().add('','');
28 description: gettext('Add metadata row')
33 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
34 this.node.document.transaction(function() {
35 $(e.target).closest('tr').data('row').remove();
38 description: gettext('Remove metadata row')
43 this.metaTable.on('keydown', '[contenteditable]', function(e) {
44 /* globals document */
46 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
47 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
49 var input = $('<input>');
50 input.appendTo('body').focus();
51 this.dom.find('.rng-module-metadataEditor-addBtn').focus();
58 this.metaTable.on('keyup', '[contenteditable]', _.throttle(function(e) {
60 var editable = $(e.target),
61 toSet = editable.text(),
62 row = editable.parents('tr').data('row'),
63 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
64 method = isKey ? 'setKey' : 'setValue';
65 row.metadata.node.document.transaction(function() {
69 description: gettext('Metadata edit')
75 this.setMetadata(this.node); //
77 this.node.document.on('change', this.handleEvent, this);
80 _.extend(View.prototype, {
82 this.node.document.off('change', this.handleEvent);
84 handleEvent: function(event) {
85 if(event.type === 'metadataAdded' && event.meta.node.sameNode(this.node)) {
86 this.addMetadataRow(event.meta.row);
88 if(event.type === 'metadataChanged' && event.meta.node.sameNode(this.node)) {
89 this.updateMetadataRow(event.meta.row);
91 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(this.node)) {
92 this.removeMetadataRow(event.meta.row);
94 if(event.type === 'nodeDetached' && event.meta.node.containsNode(this.node)) {
95 this.setMetadata(null);
98 getValuesForKey: function(key) {
100 this.metadataConfig.some(function(configRow) {
101 if(configRow.key === key) {
102 toret = configRow.values || [];
108 getIsFileForKey: function(key) {
110 this.metadataConfig.some(function(configRow) {
111 if (configRow.key == key) {
112 toret = configRow.isFile || false;
118 setMetadata: function(node) {
119 this.dom.find('.rng-module-metadataEditor-addBtn').attr('disabled', !node);
121 this.metaTable.html('');
125 metadata = node.getMetadata();
126 this.metaTable.find('tr').remove();
127 metadata.forEach(function(row) {
128 view.addMetadataRow(row);
131 addMetadataRow: function(row) {
133 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
134 newRow.appendTo(this.metaTable);
135 newRow.data('row', row);
137 var keySelectView = new OpenSelectView({
138 value: row.getKey() || '',
139 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemKey" contentEditable="true"><%= value %></div>')({value: row.getKey() || '' }),
140 setInput: function(inputDOM, value) {
141 if(inputDOM.text() !== value) {
142 inputDOM.text(value);
145 valueSelectView.clearItems();
146 this.getValuesForKey(value).forEach(function(value) {
147 valueSelectView.addItem(value);
152 newRow.find('td:first').append(keySelectView.el).data('view', keySelectView);
155 var valueSelectView = new OpenSelectView({
156 value: row.getValue(),
157 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemValue" contentEditable="true"><%= value %></div>')({value: row.getValue() || '' }),
160 setInput: function(inputDOM, value) {
161 if(inputDOM.text() !== value) {
162 inputDOM.text(value);
167 newRow.find('td:nth-child(2)').append(valueSelectView.el).data('view', valueSelectView);
169 if (this.getIsFileForKey(row.getKey())) {
170 var el = $("<a href='#-' class='attachment-library' style='float: right'>attachments</a>");
171 el.on('click', function() {
172 attachments.select(function(v) {
173 valueSelectView.setInput(v);
177 newRow.find('td:nth-child(2)').append(el);
181 this.metadataConfig.forEach(function(configRow) {
182 keySelectView.addItem(configRow.key);
183 if(row.getKey() === configRow.key) {
184 (configRow.values || []).forEach(function(value) {
185 valueSelectView.addItem(value);
191 $(newRow.find('td div')[0]).focus();
196 updateMetadataRow: function(row) {
197 this._getRowTr(row, function(tr) {
198 var tds = tr.find('td'),
202 keyTd.data('view').setInput(row.getKey());
203 valueTd.data('view').setInput(row.getValue());
206 removeMetadataRow: function(row) {
207 this._getRowTr(row, function(tr) {
211 _getRowTr: function(row, callback) {
212 this.metaTable.find('tr').each(function() {
214 if(tr.data('row') === row) {