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';
|
|
|
|
import $ from 'jquery';
|
|
|
|
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-09-25 16:28:26 +00:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
$(document).on('keyup.keyEdit', this.onEscape.bind(this));
|
|
|
|
},
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
willDestroyElement() {
|
2018-09-25 16:28:26 +00:00
|
|
|
this._super(...arguments);
|
2018-04-03 14:16:57 +00:00
|
|
|
const model = this.get('model');
|
|
|
|
if (get(model, 'isError')) {
|
|
|
|
model.rollbackAttributes();
|
|
|
|
}
|
2018-09-25 16:28:26 +00:00
|
|
|
$(document).off('keyup.keyEdit');
|
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: {
|
|
|
|
handleKeyDown(_, e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
if (!(e.keyCode === keys.ENTER && e.metaKey)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let $form = this.$('form');
|
|
|
|
if ($form.length) {
|
|
|
|
$form.submit();
|
|
|
|
}
|
|
|
|
$form = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
handleChange() {
|
|
|
|
this.hasDataChanges();
|
|
|
|
},
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|