2022-02-07 20:07:53 +00:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { isNone } from '@ember/utils';
|
|
|
|
import { assert } from '@ember/debug';
|
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { guidFor } from '@ember/object/internals';
|
|
|
|
import KVObject from 'vault/lib/kv-object';
|
|
|
|
|
2021-08-31 15:41:41 +00:00
|
|
|
/**
|
|
|
|
* @module KvObjectEditor
|
|
|
|
* KvObjectEditor components are called in FormFields when the editType on the model is kv. They are used to show a key-value input field.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* <KvObjectEditor
|
|
|
|
* @value={{get model valuePath}}
|
|
|
|
* @onChange={{action "setAndBroadcast" valuePath }}
|
|
|
|
* @label="some label"
|
|
|
|
/>
|
|
|
|
* ```
|
|
|
|
* @param {string} value - the value is captured from the model.
|
|
|
|
* @param {function} onChange - function that captures the value on change
|
2022-02-07 20:07:53 +00:00
|
|
|
* @param {function} [onKeyUp] - function passed in that handles the dom keyup event. Used for validation on the kv custom metadata.
|
2021-08-31 15:41:41 +00:00
|
|
|
* @param {string} [label] - label displayed over key value inputs
|
2022-02-07 20:07:53 +00:00
|
|
|
* @param {string} [labelClass] - override default label class in FormFieldLabel component
|
2021-08-31 15:41:41 +00:00
|
|
|
* @param {string} [warning] - warning that is displayed
|
|
|
|
* @param {string} [helpText] - helper text. In tooltip.
|
|
|
|
* @param {string} [subText] - placed under label.
|
2022-02-07 20:07:53 +00:00
|
|
|
* @param {string} [keyPlaceholder] - placeholder for key input
|
|
|
|
* @param {string} [valuePlaceholder] - placeholder for value input
|
2021-08-31 15:41:41 +00:00
|
|
|
*/
|
|
|
|
|
2022-02-07 20:07:53 +00:00
|
|
|
export default class KvObjectEditor extends Component {
|
|
|
|
@tracked kvData;
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-02-07 20:07:53 +00:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.kvData = KVObject.create({ content: [] }).fromJSON(this.args.value);
|
2018-04-03 14:16:57 +00:00
|
|
|
this.addRow();
|
2022-02-07 20:07:53 +00:00
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-02-07 20:07:53 +00:00
|
|
|
get placeholders() {
|
|
|
|
return {
|
|
|
|
key: this.args.keyPlaceholder || 'key',
|
|
|
|
value: this.args.valuePlaceholder || 'value',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
get hasDuplicateKeys() {
|
|
|
|
return this.kvData.uniqBy('name').length !== this.kvData.get('length');
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2022-02-07 20:07:53 +00:00
|
|
|
@action
|
2018-04-03 14:16:57 +00:00
|
|
|
addRow() {
|
2022-02-07 20:07:53 +00:00
|
|
|
if (!isNone(this.kvData.findBy('name', ''))) {
|
2018-04-03 14:16:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-07 20:07:53 +00:00
|
|
|
const newObj = { name: '', value: '' };
|
2018-04-03 14:16:57 +00:00
|
|
|
guidFor(newObj);
|
2022-02-07 20:07:53 +00:00
|
|
|
this.kvData.addObject(newObj);
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
updateRow() {
|
|
|
|
this.args.onChange(this.kvData.toJSON());
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
deleteRow(object, index) {
|
|
|
|
const oldObj = this.kvData.objectAt(index);
|
|
|
|
assert('object guids match', guidFor(oldObj) === guidFor(object));
|
|
|
|
this.kvData.removeAt(index);
|
|
|
|
this.args.onChange(this.kvData.toJSON());
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
handleKeyUp(event) {
|
|
|
|
if (this.args.onKeyUp) {
|
|
|
|
this.args.onKeyUp(event.target.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|