2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-04-08 17:03:18 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { set, get, computed } from '@ember/object';
|
|
|
|
|
|
|
|
import { once } from 'consul-ui/utils/dom/event-source';
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: '',
|
|
|
|
|
|
|
|
service: service('data-sink/service'),
|
|
|
|
dom: service('dom'),
|
|
|
|
logger: service('logger'),
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
onchange: function (e) {},
|
|
|
|
onerror: function (e) {},
|
2020-04-08 17:03:18 +00:00
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
state: computed('instance', 'instance.{dirtyType,isSaving}', function () {
|
2020-04-08 17:03:18 +00:00
|
|
|
let id;
|
|
|
|
const isSaving = get(this, 'instance.isSaving');
|
|
|
|
const dirtyType = get(this, 'instance.dirtyType');
|
|
|
|
if (typeof isSaving === 'undefined' && typeof dirtyType === 'undefined') {
|
|
|
|
id = 'idle';
|
|
|
|
} else {
|
|
|
|
switch (dirtyType) {
|
|
|
|
case 'created':
|
|
|
|
id = isSaving ? 'creating' : 'create';
|
|
|
|
break;
|
|
|
|
case 'updated':
|
|
|
|
id = isSaving ? 'updating' : 'update';
|
|
|
|
break;
|
|
|
|
case 'deleted':
|
|
|
|
case undefined:
|
|
|
|
id = isSaving ? 'removing' : 'remove';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
id = `active.${id}`;
|
|
|
|
}
|
|
|
|
return {
|
2022-09-15 08:43:17 +00:00
|
|
|
matches: (name) => id.indexOf(name) !== -1,
|
2020-04-08 17:03:18 +00:00
|
|
|
};
|
|
|
|
}),
|
|
|
|
|
2022-09-15 08:43:17 +00:00
|
|
|
init: function () {
|
2020-04-08 17:03:18 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners = this.dom.listeners();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
willDestroyElement: function () {
|
2020-04-08 17:03:18 +00:00
|
|
|
this._super(...arguments);
|
|
|
|
this._listeners.remove();
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
source: function (cb) {
|
2020-04-08 17:03:18 +00:00
|
|
|
const source = once(cb);
|
2022-09-15 08:43:17 +00:00
|
|
|
const error = (err) => {
|
2020-04-08 17:03:18 +00:00
|
|
|
set(this, 'instance', undefined);
|
|
|
|
try {
|
|
|
|
this.onerror(err);
|
|
|
|
this.logger.execute(err);
|
|
|
|
} catch (err) {
|
|
|
|
this.logger.execute(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._listeners.add(source, {
|
2022-09-15 08:43:17 +00:00
|
|
|
message: (e) => {
|
2020-04-08 17:03:18 +00:00
|
|
|
try {
|
|
|
|
set(this, 'instance', undefined);
|
|
|
|
this.onchange(e);
|
|
|
|
} catch (err) {
|
|
|
|
error(err);
|
|
|
|
}
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
error: (e) => error(e),
|
2020-04-08 17:03:18 +00:00
|
|
|
});
|
|
|
|
return source;
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
didInsertElement: function () {
|
2020-04-08 17:03:18 +00:00
|
|
|
this._super(...arguments);
|
2020-07-09 09:08:47 +00:00
|
|
|
if (typeof this.data !== 'undefined' || typeof this.item !== 'undefined') {
|
|
|
|
this.actions.open.apply(this, [this.data, this.item]);
|
2020-04-08 17:03:18 +00:00
|
|
|
}
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
persist: function (data, instance) {
|
2020-07-09 09:08:47 +00:00
|
|
|
if (typeof data !== 'undefined') {
|
|
|
|
set(this, 'instance', this.service.prepare(this.sink, data, instance));
|
|
|
|
} else {
|
|
|
|
set(this, 'instance', instance);
|
|
|
|
}
|
2020-04-08 17:03:18 +00:00
|
|
|
this.source(() => this.service.persist(this.sink, this.instance));
|
|
|
|
},
|
2022-09-15 08:43:17 +00:00
|
|
|
remove: function (instance) {
|
2020-07-09 09:08:47 +00:00
|
|
|
set(this, 'instance', instance);
|
|
|
|
this.source(() => this.service.remove(this.sink, instance));
|
2020-04-08 17:03:18 +00:00
|
|
|
},
|
|
|
|
actions: {
|
2022-09-15 08:43:17 +00:00
|
|
|
open: function (data, item) {
|
2020-07-09 09:08:47 +00:00
|
|
|
if (item instanceof Event) {
|
|
|
|
item = undefined;
|
2020-04-08 17:03:18 +00:00
|
|
|
}
|
2020-07-09 09:08:47 +00:00
|
|
|
if (typeof data === 'undefined' && typeof item === 'undefined') {
|
2020-04-08 17:03:18 +00:00
|
|
|
throw new Error('You must specify data to save, or null to remove');
|
|
|
|
}
|
|
|
|
// potentially allow {} and "" as 'remove' flags
|
|
|
|
if (data === null || data === '') {
|
2020-07-09 09:08:47 +00:00
|
|
|
this.remove(item);
|
2020-04-08 17:03:18 +00:00
|
|
|
} else {
|
2020-07-09 09:08:47 +00:00
|
|
|
this.persist(data, item);
|
2020-04-08 17:03:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|