open-vault/ui/app/components/role-edit.js

130 lines
3.4 KiB
JavaScript
Raw Normal View History

import { inject as service } from '@ember/service';
import { or } from '@ember/object/computed';
import { isBlank } from '@ember/utils';
import { task, waitForEvent } from 'ember-concurrency';
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';
export default Component.extend(FocusOnInsertMixin, {
router: service(),
wizard: service(),
2018-04-03 14:16:57 +00:00
mode: null,
emptyData: '{\n}',
onDataChange() {},
onRefresh() {},
2018-04-03 14:16:57 +00:00
model: null,
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() {
this._super(...arguments);
if (this.model && this.model.isError) {
this.model.rollbackAttributes();
2018-04-03 14:16:57 +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() {
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');
return model[method]().then(() => {
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
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() {
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));
}
},
},
});