4 'libs/text!./templates/main.html',
5 'libs/text!./templates/item.html',
6 'views/openSelect/openSelect'
7 ], function($, _, mainTemplate, itemTemplate, OpenSelectView) {
12 return function(sandbox) {
16 metadataConfig = (sandbox.getConfig().metadata || []).sort(function(configRow1, configRow2) {
17 if(configRow1.key < configRow2.key) {
20 if(configRow1.key > configRow2.key) {
26 var getValuesForKey = function(key) {
28 metadataConfig.some(function(configRow) {
29 if(configRow.key === key) {
30 toret = configRow.values || [];
38 node: $(_.template(mainTemplate)()),
41 var metaTable = this.metaTable = this.node.find('table');
43 this.node.find('.rng-module-metadataEditor-addBtn').click(function() {
45 currentNode.document.transaction(function() {
46 currentNode.getMetadata().add('','');
47 }, this, gettext('Add metadata row'));
50 this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {
51 currentNode.document.transaction(function() {
52 $(e.target).closest('tr').data('row').remove();
53 }, this, gettext('Remove metadata row'));
56 this.metaTable.on('keydown', '[contenteditable]', function(e) {
57 /* globals document */
59 if($(document.activeElement).hasClass('rng-module-metadataEditor-metaItemKey')) {
60 metaTable.find('.rng-module-metadataEditor-metaItemValue').focus();
62 var input = $('<input>');
63 input.appendTo('body').focus();
64 view.node.find('.rng-module-metadataEditor-addBtn').focus();
71 this.metaTable.on('keyup', '[contenteditable]', _.throttle(function(e) {
73 var editable = $(e.target),
74 toSet = editable.text(),
75 row = editable.parents('tr').data('row'),
76 isKey = _.last(editable.attr('class').split('-')) === 'metaItemKey',
77 method = isKey ? 'setKey' : 'setValue';
78 row.metadata.node.document.transaction(function() {
80 }, this, gettext('Metadata edit'));
86 setMetadata: function(node) {
87 this.node.find('.rng-module-metadataEditor-addBtn').attr('disabled', !node);
89 this.metaTable.html('');
93 metadata = node.getMetadata();
94 this.metaTable.find('tr').remove();
95 metadata.forEach(function(row) {
96 view.addMetadataRow(row);
99 addMetadataRow: function(row) {
100 var newRow = $(_.template(itemTemplate)({key: row.getKey() || '', value: row.getValue() || ''}));
101 newRow.appendTo(this.metaTable);
102 newRow.data('row', row);
104 var keySelectView = new OpenSelectView({
105 value: row.getKey() || '',
106 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemKey" contentEditable="true"><%= value %></div>')({value: row.getKey() || '' }),
107 setInput: function(inputDOM, value) {
108 if(inputDOM.text() !== value) {
109 inputDOM.text(value);
112 valueSelectView.clearItems();
113 getValuesForKey(value).forEach(function(value) {
114 valueSelectView.addItem(value);
118 newRow.find('td:first').append(keySelectView.el).data('view', keySelectView);
121 var valueSelectView = new OpenSelectView({
122 value: row.getValue(),
123 inputTemplate: _.template('<div class="openInput rng-module-metadataEditor-metaItemValue" contentEditable="true"><%= value %></div>')({value: row.getValue() || '' }),
126 setInput: function(inputDOM, value) {
127 if(inputDOM.text() !== value) {
128 inputDOM.text(value);
133 newRow.find('td:nth-child(2)').append(valueSelectView.el).data('view', valueSelectView);
136 metadataConfig.forEach(function(configRow) {
137 keySelectView.addItem(configRow.key);
138 if(row.getKey() === configRow.key) {
139 (configRow.values || []).forEach(function(value) {
140 valueSelectView.addItem(value);
146 $(newRow.find('td div')[0]).focus();
151 updateMetadataRow: function(row) {
152 this._getRowTr(row, function(tr) {
153 var tds = tr.find('td'),
157 keyTd.data('view').setInput(row.getKey());
158 valueTd.data('view').setInput(row.getValue());
161 removeMetadataRow: function(row) {
162 this._getRowTr(row, function(tr) {
166 _getRowTr: function(row, callback) {
167 this.metaTable.find('tr').each(function() {
169 if(tr.data('row') === row) {
181 sandbox.publish('ready');
183 setDocument: function(document) {
184 document.on('change', function(event) {
185 if(event.type === 'metadataAdded' && event.meta.node.sameNode(currentNode)) {
186 view.addMetadataRow(event.meta.row);
188 if(event.type === 'metadataChanged' && event.meta.node.sameNode(currentNode)) {
189 view.updateMetadataRow(event.meta.row);
191 if(event.type === 'metadataRemoved' && event.meta.node.sameNode(currentNode)) {
192 view.removeMetadataRow(event.meta.row);
194 if(event.type === 'nodeDetached' && event.meta.node.containsNode(currentNode)) {
195 view.setMetadata(null);
199 setNodeElement: function(node) {
200 if(currentNode && currentNode.sameNode(node)) {
204 view.setMetadata(node);
206 getView: function() {