antares/src/renderer/components/WorkspacePropsTabTrigger.vue

369 lines
13 KiB
Vue
Raw Normal View History

2020-12-31 19:55:02 +01:00
<template>
2021-07-17 13:10:54 +02:00
<div v-show="isSelected" class="workspace-query-tab column col-12 columns col-gapless">
2020-12-31 19:55:02 +01:00
<div class="workspace-query-runner column col-12">
<div class="workspace-query-runner-footer">
<div class="workspace-query-buttons">
<button
class="btn btn-primary btn-sm"
:disabled="!isChanged"
:class="{'loading':isSaving}"
title="CTRL+S"
2020-12-31 19:55:02 +01:00
@click="saveChanges"
>
<i class="mdi mdi-24px mdi-content-save mr-1" />
2020-12-31 19:55:02 +01:00
<span>{{ $t('word.save') }}</span>
</button>
<button
:disabled="!isChanged"
class="btn btn-link btn-sm mr-0"
:title="$t('message.clearChanges')"
@click="clearChanges"
>
<i class="mdi mdi-24px mdi-delete-sweep mr-1" />
2020-12-31 19:55:02 +01:00
<span>{{ $t('word.clear') }}</span>
</button>
</div>
2021-07-17 13:10:54 +02:00
<div class="workspace-query-info">
<div class="d-flex" :title="$t('word.schema')">
<i class="mdi mdi-18px mdi-database mr-1" /><b>{{ schema }}</b>
</div>
</div>
2020-12-31 19:55:02 +01:00
</div>
</div>
<div class="container">
<div class="columns mb-4">
2021-01-25 18:28:22 +01:00
<div class="column col-auto">
2020-12-31 19:55:02 +01:00
<div class="form-group">
<label class="form-label">{{ $t('word.name') }}</label>
<input
v-model="localTrigger.name"
class="form-input"
type="text"
>
</div>
</div>
<div v-if="customizations.definer" class="column col-auto">
2020-12-31 19:55:02 +01:00
<div class="form-group">
<label class="form-label">{{ $t('word.definer') }}</label>
<select
v-if="workspace.users.length"
v-model="localTrigger.definer"
class="form-select"
>
<option value="">
{{ $t('message.currentUser') }}
</option>
<option v-if="!isDefinerInUsers" :value="originalTrigger.definer">
{{ originalTrigger.definer.replaceAll('`', '') }}
</option>
<option
v-for="user in workspace.users"
:key="`${user.name}@${user.host}`"
:value="`\`${user.name}\`@\`${user.host}\``"
>
{{ user.name }}@{{ user.host }}
</option>
</select>
<select v-if="!workspace.users.length" class="form-select">
<option value="">
{{ $t('message.currentUser') }}
</option>
</select>
</div>
</div>
</div>
<fieldset class="columns" :disabled="customizations.triggerOnlyRename">
2021-01-25 18:28:22 +01:00
<div class="column col-auto">
2020-12-31 19:55:02 +01:00
<div class="form-group">
<label class="form-label">{{ $t('word.table') }}</label>
<select v-model="localTrigger.table" class="form-select">
<option v-for="table in schemaTables" :key="table.name">
{{ table.name }}
</option>
</select>
</div>
</div>
2021-01-25 18:28:22 +01:00
<div class="column col-auto">
2020-12-31 19:55:02 +01:00
<div class="form-group">
<label class="form-label">{{ $t('word.event') }}</label>
<div class="input-group">
<select v-model="localTrigger.activation" class="form-select">
2020-12-31 19:55:02 +01:00
<option>BEFORE</option>
<option>AFTER</option>
</select>
<select
v-if="!customizations.triggerMultipleEvents"
v-model="localTrigger.event"
class="form-select"
>
<option v-for="event in Object.keys(localEvents)" :key="event">
{{ event }}
</option>
2020-12-31 19:55:02 +01:00
</select>
<div v-if="customizations.triggerMultipleEvents" class="px-4">
<label
v-for="event in Object.keys(localEvents)"
:key="event"
class="form-checkbox form-inline"
@change.prevent="changeEvents(event)"
>
<input :checked="localEvents[event]" type="checkbox"><i class="form-icon" /> {{ event }}
</label>
</div>
2020-12-31 19:55:02 +01:00
</div>
</div>
</div>
</fieldset>
2020-12-31 19:55:02 +01:00
</div>
<div class="workspace-query-results column col-12 mt-2 p-relative">
<BaseLoader v-if="isLoading" />
2020-12-31 19:55:02 +01:00
<label class="form-label ml-2">{{ $t('message.triggerStatement') }}</label>
<QueryEditor
v-show="isSelected"
2020-12-31 19:55:02 +01:00
ref="queryEditor"
:value.sync="localTrigger.sql"
:workspace="workspace"
:schema="schema"
:height="editorHeight"
/>
</div>
</div>
</template>
<script>
import QueryEditor from '@/components/QueryEditor';
import { mapGetters, mapActions } from 'vuex';
import BaseLoader from '@/components/BaseLoader';
2020-12-31 19:55:02 +01:00
import Triggers from '@/ipc-api/Triggers';
export default {
name: 'WorkspacePropsTabTrigger',
components: {
BaseLoader,
2020-12-31 19:55:02 +01:00
QueryEditor
},
props: {
connection: Object,
2021-07-17 13:10:54 +02:00
trigger: String,
isSelected: Boolean,
schema: String
2020-12-31 19:55:02 +01:00
},
data () {
return {
isLoading: false,
2020-12-31 19:55:02 +01:00
isSaving: false,
originalTrigger: null,
localTrigger: { sql: '' },
lastTrigger: null,
sqlProxy: '',
editorHeight: 300,
localEvents: { INSERT: false, UPDATE: false, DELETE: false }
2020-12-31 19:55:02 +01:00
};
},
computed: {
...mapGetters({
selectedWorkspace: 'workspaces/getSelected',
2020-12-31 19:55:02 +01:00
getWorkspace: 'workspaces/getWorkspace'
}),
workspace () {
return this.getWorkspace(this.connection.uid);
},
2021-07-17 13:10:54 +02:00
tabUid () {
return this.$vnode.key;
},
customizations () {
return this.workspace.customizations;
},
2020-12-31 19:55:02 +01:00
isChanged () {
return JSON.stringify(this.originalTrigger) !== JSON.stringify(this.localTrigger);
},
isDefinerInUsers () {
return this.originalTrigger ? this.workspace.users.some(user => this.originalTrigger.definer === `\`${user.name}\`@\`${user.host}\``) : true;
},
schemaTables () {
const schemaTables = this.workspace.structure
.filter(schema => schema.name === this.schema)
.map(schema => schema.tables);
return schemaTables.length ? schemaTables[0].filter(table => table.type === 'table') : [];
}
},
watch: {
2021-07-17 13:10:54 +02:00
async schema () {
2020-12-31 19:55:02 +01:00
if (this.isSelected) {
await this.getTriggerData();
this.$refs.queryEditor.editor.session.setValue(this.localTrigger.sql);
this.lastTrigger = this.trigger;
}
},
2021-07-17 13:10:54 +02:00
async trigger () {
if (this.isSelected) {
2020-12-31 19:55:02 +01:00
await this.getTriggerData();
this.$refs.queryEditor.editor.session.setValue(this.localTrigger.sql);
this.lastTrigger = this.trigger;
}
},
2021-07-17 13:10:54 +02:00
async isSelected (val) {
if (val) {
this.changeBreadcrumbs({ schema: this.schema, trigger: this.trigger });
if (this.lastTrigger !== this.trigger)
this.getTriggerData();
}
},
2021-07-17 16:09:57 +02:00
isChanged (val) {
2021-07-17 13:10:54 +02:00
this.setUnsavedChanges({ uid: this.connection.uid, tUid: this.tabUid, isChanged: val });
2020-12-31 19:55:02 +01:00
}
},
2021-07-17 13:10:54 +02:00
async created () {
await this.getTriggerData();
this.$refs.queryEditor.editor.session.setValue(this.localTrigger.sql);
window.addEventListener('keydown', this.onKey);
},
2020-12-31 19:55:02 +01:00
mounted () {
window.addEventListener('resize', this.resizeQueryEditor);
2020-12-31 19:55:02 +01:00
},
destroyed () {
window.removeEventListener('resize', this.resizeQueryEditor);
},
beforeDestroy () {
window.removeEventListener('keydown', this.onKey);
},
2020-12-31 19:55:02 +01:00
methods: {
...mapActions({
addNotification: 'notifications/addNotification',
refreshStructure: 'workspaces/refreshStructure',
2021-07-17 13:10:54 +02:00
renameTabs: 'workspaces/renameTabs',
newTab: 'workspaces/newTab',
changeBreadcrumbs: 'workspaces/changeBreadcrumbs',
setUnsavedChanges: 'workspaces/setUnsavedChanges'
2020-12-31 19:55:02 +01:00
}),
async getTriggerData () {
if (!this.trigger) return;
Object.keys(this.localEvents).forEach(event => {
this.localEvents[event] = false;
});
this.localTrigger = { sql: '' };
this.isLoading = true;
2021-07-17 13:10:54 +02:00
this.lastTrigger = this.trigger;
2020-12-31 19:55:02 +01:00
const params = {
uid: this.connection.uid,
schema: this.schema,
trigger: this.workspace.breadcrumbs.trigger
};
try {
const { status, response } = await Triggers.getTriggerInformations(params);
if (status === 'success') {
this.originalTrigger = response;
this.localTrigger = JSON.parse(JSON.stringify(this.originalTrigger));
this.sqlProxy = this.localTrigger.sql;
if (this.customizations.triggerMultipleEvents) {
this.originalTrigger.event.forEach(e => {
this.localEvents[e] = true;
});
}
2020-12-31 19:55:02 +01:00
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.resizeQueryEditor();
this.isLoading = false;
2020-12-31 19:55:02 +01:00
},
changeEvents (event) {
if (this.customizations.triggerMultipleEvents) {
this.localEvents[event] = !this.localEvents[event];
this.localTrigger.event = [];
for (const key in this.localEvents) {
if (this.localEvents[key])
this.localTrigger.event.push(key);
}
}
},
2020-12-31 19:55:02 +01:00
async saveChanges () {
if (this.isSaving) return;
this.isSaving = true;
const params = {
uid: this.connection.uid,
schema: this.schema,
trigger: {
...this.localTrigger,
oldName: this.originalTrigger.name
}
};
try {
const { status, response } = await Triggers.alterTrigger(params);
if (status === 'success') {
await this.refreshStructure(this.connection.uid);
2021-07-17 13:10:54 +02:00
if (this.originalTrigger.name !== this.localTrigger.name) {
const triggerName = this.customizations.triggerTableInName ? `${this.localTrigger.table}.${this.localTrigger.name}` : this.localTrigger.name;
2021-07-17 13:10:54 +02:00
const triggerOldName = this.customizations.triggerTableInName ? `${this.originalTrigger.table}.${this.originalTrigger.name}` : this.originalTrigger.name;
this.renameTabs({
uid: this.connection.uid,
schema: this.schema,
elementName: triggerOldName,
elementNewName: triggerName,
elementType: 'trigger'
});
this.changeBreadcrumbs({ schema: this.schema, trigger: triggerName });
2020-12-31 19:55:02 +01:00
}
this.getTriggerData();
}
else
this.addNotification({ status: 'error', message: response });
}
catch (err) {
this.addNotification({ status: 'error', message: err.stack });
}
this.isSaving = false;
},
clearChanges () {
this.localTrigger = JSON.parse(JSON.stringify(this.originalTrigger));
this.$refs.queryEditor.editor.session.setValue(this.localTrigger.sql);
Object.keys(this.localEvents).forEach(event => {
this.localEvents[event] = false;
});
if (this.customizations.triggerMultipleEvents) {
this.originalTrigger.event.forEach(e => {
this.localEvents[e] = true;
});
}
2020-12-31 19:55:02 +01:00
},
resizeQueryEditor () {
if (this.$refs.queryEditor) {
const footer = document.getElementById('footer');
const size = window.innerHeight - this.$refs.queryEditor.$el.getBoundingClientRect().top - footer.offsetHeight;
this.editorHeight = size;
this.$refs.queryEditor.editor.resize();
}
},
onKey (e) {
if (this.isSelected) {
e.stopPropagation();
if (e.ctrlKey && e.keyCode === 83) { // CTRL + S
if (this.isChanged)
this.saveChanges();
}
}
2020-12-31 19:55:02 +01:00
}
}
};
</script>