2018-09-25 16:28:26 +00:00
|
|
|
import { inject as service } from '@ember/service';
|
|
|
|
import { or } from '@ember/object/computed';
|
|
|
|
import { isBlank } from '@ember/utils';
|
2019-06-20 13:37:27 +00:00
|
|
|
import { task, waitForEvent } from 'ember-concurrency';
|
2018-09-25 16:28:26 +00:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import { set, get } from '@ember/object';
|
2018-04-03 14:16:57 +00:00
|
|
|
import FocusOnInsertMixin from 'vault/mixins/focus-on-insert';
|
|
|
|
import keys from 'vault/lib/keycodes';
|
|
|
|
|
|
|
|
const LIST_ROOT_ROUTE = 'vault.cluster.secrets.backend.list-root';
|
|
|
|
const SHOW_ROUTE = 'vault.cluster.secrets.backend.show';
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
export default Component.extend(FocusOnInsertMixin, {
|
|
|
|
router: service(),
|
|
|
|
wizard: service(),
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
mode: null,
|
|
|
|
emptyData: '{\n}',
|
2018-09-25 16:28:26 +00:00
|
|
|
onDataChange() {},
|
|
|
|
onRefresh() {},
|
2018-04-03 14:16:57 +00:00
|
|
|
model: null,
|
2018-09-25 16:28:26 +00:00
|
|
|
requestInFlight: or('model.isLoading', 'model.isReloading', 'model.isSaving'),
|
2018-08-28 05:03:55 +00:00
|
|
|
|
|
|
|
didReceiveAttrs() {
|
|
|
|
this._super(...arguments);
|
|
|
|
if (
|
|
|
|
(this.get('wizard.featureState') === 'details' && this.get('mode') === 'create') ||
|
|
|
|
(this.get('wizard.featureState') === 'role' && this.get('mode') === 'show')
|
|
|
|
) {
|
|
|
|
this.get('wizard').transitionFeatureMachine(
|
|
|
|
this.get('wizard.featureState'),
|
|
|
|
'CONTINUE',
|
|
|
|
this.get('backendType')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.get('wizard.featureState') === 'displayRole') {
|
|
|
|
this.get('wizard').transitionFeatureMachine(
|
|
|
|
this.get('wizard.featureState'),
|
|
|
|
'NOOP',
|
|
|
|
this.get('backendType')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
willDestroyElement() {
|
2018-09-25 16:28:26 +00:00
|
|
|
this._super(...arguments);
|
2019-06-20 13:37:27 +00:00
|
|
|
if (this.model && this.model.isError) {
|
|
|
|
this.model.rollbackAttributes();
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-06-20 13:37:27 +00:00
|
|
|
waitForKeyUp: task(function*() {
|
|
|
|
while (true) {
|
|
|
|
let event = yield waitForEvent(document.body, 'keyup');
|
|
|
|
this.onEscape(event);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on('didInsertElement')
|
|
|
|
.cancelOn('willDestroyElement'),
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
transitionToRoute() {
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('router').transitionTo(...arguments);
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
onEscape(e) {
|
|
|
|
if (e.keyCode !== keys.ESC || this.get('mode') !== 'show') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.transitionToRoute(LIST_ROOT_ROUTE);
|
|
|
|
},
|
|
|
|
|
|
|
|
hasDataChanges() {
|
|
|
|
get(this, 'onDataChange')(get(this, 'model.hasDirtyAttributes'));
|
|
|
|
},
|
|
|
|
|
|
|
|
persist(method, successCallback) {
|
|
|
|
const model = get(this, 'model');
|
2018-05-29 19:48:55 +00:00
|
|
|
return model[method]().then(() => {
|
2018-09-25 16:28:26 +00:00
|
|
|
if (!get(model, 'isError')) {
|
2018-08-28 05:03:55 +00:00
|
|
|
if (this.get('wizard.featureState') === 'role') {
|
|
|
|
this.get('wizard').transitionFeatureMachine('role', 'CONTINUE', this.get('backendType'));
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
successCallback(model);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
createOrUpdate(type, event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
const modelId = this.get('model.id');
|
|
|
|
// prevent from submitting if there's no key
|
|
|
|
// maybe do something fancier later
|
2018-09-25 16:28:26 +00:00
|
|
|
if (type === 'create' && isBlank(modelId)) {
|
2018-04-03 14:16:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.persist('save', () => {
|
|
|
|
this.hasDataChanges();
|
|
|
|
this.transitionToRoute(SHOW_ROUTE, modelId);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
setValue(key, event) {
|
|
|
|
set(get(this, 'model'), key, event.target.checked);
|
|
|
|
},
|
|
|
|
|
|
|
|
refresh() {
|
2018-09-25 16:28:26 +00:00
|
|
|
this.get('onRefresh')();
|
2018-04-03 14:16:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
delete() {
|
|
|
|
this.persist('destroyRecord', () => {
|
|
|
|
this.hasDataChanges();
|
|
|
|
this.transitionToRoute(LIST_ROOT_ROUTE);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
codemirrorUpdated(attr, val, codemirror) {
|
|
|
|
codemirror.performLint();
|
|
|
|
const hasErrors = codemirror.state.lint.marked.length > 0;
|
|
|
|
|
|
|
|
if (!hasErrors) {
|
|
|
|
set(this.get('model'), attr, JSON.parse(val));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|