mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
325 lines
11 KiB
325 lines
11 KiB
3 years ago
|
<!--eslint-disable-->
|
||
|
|
||
4 years ago
|
<template>
|
||
3 years ago
|
<v-dialog v-model="dialogShow" width="60%">
|
||
|
<v-tabs v-model="activeTab" height="32">
|
||
3 years ago
|
<v-tabs-slider />
|
||
4 years ago
|
|
||
3 years ago
|
<v-tab v-for="env in envs" :key="env" :to="`#${env}`">
|
||
3 years ago
|
{{ env }}
|
||
4 years ago
|
</v-tab>
|
||
3 years ago
|
<div style="padding: 0; margin-top: 3px; margin-left: 20px">
|
||
|
<x-btn v-if="!isDashboard" small tooltip="Add a new environment" outlined @click="addNewEnvDialog = true">
|
||
3 years ago
|
Add Env
|
||
4 years ago
|
</x-btn>
|
||
|
</div>
|
||
|
|
||
3 years ago
|
<v-spacer />
|
||
3 years ago
|
<div style="padding: 0; margin-left: 20px">
|
||
3 years ago
|
<x-btn
|
||
|
v-if="!isDashboard && activeTab !== 'global'"
|
||
|
small
|
||
|
color="error"
|
||
|
tooltip="Deletes environment(without confirmation dialog)"
|
||
|
outlined
|
||
|
@click="deleteEnvironemt(activeTab)"
|
||
|
>
|
||
|
Delete '{{ activeTab }}' Env
|
||
4 years ago
|
</x-btn>
|
||
|
<x-btn
|
||
|
outlined
|
||
3 years ago
|
color="primary"
|
||
|
:tooltip="`Save all the environemtns`"
|
||
|
btn.class="ma-1"
|
||
|
small
|
||
|
@click="saveEnvironment(env)"
|
||
|
>
|
||
3 years ago
|
<v-icon small> save </v-icon> Save All
|
||
4 years ago
|
</x-btn>
|
||
|
</div>
|
||
3 years ago
|
<v-tab-item v-for="env in envs" :key="env" :value="env">
|
||
|
<div class="d-flex" style="height: 100%; width: 100%">
|
||
4 years ago
|
<v-simple-table class="ignore-height-style params-table my-4" style="width: 100%" dense>
|
||
3 years ago
|
<template #default>
|
||
4 years ago
|
<thead>
|
||
3 years ago
|
<tr>
|
||
|
<th class="text-left body-2" width="5%" />
|
||
3 years ago
|
<th class="text-left body-2 grey--text" width="40%">Key</th>
|
||
|
<th class="text-left body-2 grey--text" width="40%">Value</th>
|
||
3 years ago
|
<th class="text-left body-2" width="5%" />
|
||
|
</tr>
|
||
4 years ago
|
</thead>
|
||
|
<draggable v-if="value" v-model="envValues[env].data" tag="tbody">
|
||
3 years ago
|
<tr v-for="(item, i) in envValues[env].data" :key="i">
|
||
4 years ago
|
<td>
|
||
|
<v-checkbox
|
||
3 years ago
|
v-model="item.enabled"
|
||
4 years ago
|
small
|
||
|
class="mt-0"
|
||
|
color="primary lighten-1"
|
||
|
hide-details
|
||
3 years ago
|
dense
|
||
|
/>
|
||
4 years ago
|
</td>
|
||
|
<td>
|
||
|
<v-text-field
|
||
3 years ago
|
v-model="item.key"
|
||
4 years ago
|
class="body-2"
|
||
|
:disabled="!item.enabled"
|
||
|
placeholder="Key"
|
||
|
hide-details
|
||
|
single-line
|
||
|
dense
|
||
3 years ago
|
@input="handleInput(env, i, item.key)"
|
||
3 years ago
|
/>
|
||
4 years ago
|
</td>
|
||
|
<td style="height: auto">
|
||
|
<v-text-field
|
||
|
v-model="item.value"
|
||
3 years ago
|
class="body-2"
|
||
4 years ago
|
:disabled="!item.enabled"
|
||
|
:placeholder="'Value'"
|
||
|
hide-details
|
||
|
single-line
|
||
|
dense
|
||
3 years ago
|
/>
|
||
4 years ago
|
</td>
|
||
|
<td class="">
|
||
3 years ago
|
<x-icon tooltip="Delete environment key" color="error grey" small @click="removeKey(env, i)">
|
||
3 years ago
|
mdi-delete-outline
|
||
4 years ago
|
</x-icon>
|
||
|
</td>
|
||
|
</tr>
|
||
|
</draggable>
|
||
|
</template>
|
||
|
</v-simple-table>
|
||
|
</div>
|
||
|
</v-tab-item>
|
||
|
</v-tabs>
|
||
|
<v-dialog v-model="addNewEnvDialog" max-width="500">
|
||
|
<v-card>
|
||
3 years ago
|
<v-card-title class="headline"> New Environment </v-card-title>
|
||
4 years ago
|
|
||
|
<v-card-text>
|
||
3 years ago
|
<v-text-field v-model="newEnvName" hide-details outlined dense label="Enter environment name" />
|
||
4 years ago
|
</v-card-text>
|
||
|
|
||
|
<v-card-actions>
|
||
3 years ago
|
<v-spacer />
|
||
4 years ago
|
|
||
3 years ago
|
<v-btn color="green darken-1" text @click="addNewEnvDialog = false">
|
||
3 years ago
|
<!-- Cancel -->
|
||
|
{{ $t('general.cancel') }}
|
||
4 years ago
|
</v-btn>
|
||
|
|
||
3 years ago
|
<v-btn color="green darken-1" text @click="addNewEnvironment(newEnvName)"> Create </v-btn>
|
||
4 years ago
|
</v-card-actions>
|
||
|
</v-card>
|
||
|
</v-dialog>
|
||
|
</v-dialog>
|
||
|
</template>
|
||
|
|
||
|
<script>
|
||
3 years ago
|
/* eslint-disable */
|
||
4 years ago
|
|
||
3 years ago
|
import draggable from 'vuedraggable';
|
||
3 years ago
|
export default {
|
||
|
name: 'Environment',
|
||
|
directives: {},
|
||
|
components: {
|
||
3 years ago
|
draggable,
|
||
3 years ago
|
},
|
||
3 years ago
|
validate({ params }) {
|
||
|
return true;
|
||
3 years ago
|
},
|
||
|
props: {
|
||
|
value: Boolean,
|
||
3 years ago
|
env: String,
|
||
3 years ago
|
},
|
||
3 years ago
|
data() {
|
||
3 years ago
|
return {
|
||
|
addNewEnvDialog: false,
|
||
|
newEnvName: '',
|
||
|
activeTab: 'global',
|
||
3 years ago
|
envValues: {},
|
||
|
};
|
||
3 years ago
|
},
|
||
3 years ago
|
head() {
|
||
|
return {};
|
||
3 years ago
|
},
|
||
|
computed: {
|
||
|
dialogShow: {
|
||
3 years ago
|
get() {
|
||
|
return this.value;
|
||
|
},
|
||
|
set(val) {
|
||
|
this.$emit('input', val);
|
||
4 years ago
|
},
|
||
3 years ago
|
},
|
||
3 years ago
|
envs() {
|
||
3 years ago
|
if (this.isDashboard) {
|
||
3 years ago
|
return ['global', ...Object.keys(this.$store.getters['project/GtrProjectJson'].envs)];
|
||
3 years ago
|
} else {
|
||
3 years ago
|
return ['global', ...Object.keys(this.$store.state.project.defaultProject.envs)];
|
||
3 years ago
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
},
|
||
|
watch: {
|
||
3 years ago
|
env(env) {
|
||
|
this.activeTab = env;
|
||
|
},
|
||
3 years ago
|
},
|
||
3 years ago
|
created() {
|
||
|
this.activeTab = this.env;
|
||
|
this.loadEnvironments();
|
||
4 years ago
|
|
||
3 years ago
|
// listen for active project change
|
||
|
this.$store.watch(
|
||
|
state => state.project.unserializedList,
|
||
3 years ago
|
unserializedList => {
|
||
|
if (unserializedList.envs) {
|
||
|
this.loadEnvironments();
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
);
|
||
3 years ago
|
},
|
||
3 years ago
|
mounted() {},
|
||
|
beforeDestroy() {},
|
||
3 years ago
|
methods: {
|
||
3 years ago
|
async addNewEnvironment(name) {
|
||
|
this.addNewEnvDialog = false;
|
||
3 years ago
|
if (!this.isDashboard) {
|
||
3 years ago
|
const projectJsonPath = config.electron.defaultProjectPath;
|
||
|
const freshProjectObj = JSON.parse(JSON.stringify(this.$store.state.project.defaultProject));
|
||
|
freshProjectObj.envs[name] = { apiClient: { data: [] } };
|
||
|
fs.writeFileSync(projectJsonPath, JSON.stringify(freshProjectObj, null, 2), 'utf-8');
|
||
|
this.$store.commit('project/setDefaultProjectJson', JSON.parse(JSON.stringify(freshProjectObj)));
|
||
|
this.loadEnvironments();
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
deleteEnvironemt(env) {
|
||
3 years ago
|
if (!this.isDashboard) {
|
||
3 years ago
|
const projectJsonPath = config.electron.defaultProjectPath;
|
||
|
const freshProjectObj = JSON.parse(JSON.stringify(this.$store.state.project.defaultProject));
|
||
|
delete freshProjectObj.envs[env];
|
||
|
fs.writeFileSync(projectJsonPath, JSON.stringify(freshProjectObj, null, 2), 'utf-8');
|
||
|
this.$store.commit('project/setDefaultProjectJson', JSON.parse(JSON.stringify(freshProjectObj)));
|
||
|
this.loadEnvironments();
|
||
3 years ago
|
}
|
||
4 years ago
|
},
|
||
3 years ago
|
// add extra column if last row is filled
|
||
3 years ago
|
handleInput(env, i, key) {
|
||
3 years ago
|
if (i === this.envValues[env].data.length - 1 && key.length) {
|
||
3 years ago
|
this.envValues[env].data = [...this.envValues[env].data, { key: '', value: '', enabled: true }];
|
||
3 years ago
|
}
|
||
4 years ago
|
},
|
||
3 years ago
|
// remove environment key
|
||
3 years ago
|
removeKey(env, i) {
|
||
|
this.envValues[env].data.splice(i, 1);
|
||
3 years ago
|
if (!this.envValues[env].data.length) {
|
||
3 years ago
|
this.envValues[env].data = [{ key: '', value: '', enabled: true }];
|
||
3 years ago
|
}
|
||
4 years ago
|
},
|
||
3 years ago
|
// filter empty environments and save project JSON
|
||
3 years ago
|
async saveEnvironmentFile(projectJsonPath, freshProject) {
|
||
|
this.envs.forEach(env => {
|
||
|
let envObj = {};
|
||
|
if (!this.envValues[env] || !this.envValues[env].data) {
|
||
|
this.$set(this.envValues, env, { data: [] });
|
||
|
} else {
|
||
|
envObj = this.envValues[env];
|
||
|
}
|
||
3 years ago
|
if (env === 'global') {
|
||
3 years ago
|
freshProject.apiClient.data = envObj.data.filter(o => o.key.trim());
|
||
3 years ago
|
} else {
|
||
3 years ago
|
freshProject.envs[env].apiClient.data = envObj.data.filter(o => o.key.trim());
|
||
4 years ago
|
}
|
||
3 years ago
|
});
|
||
3 years ago
|
|
||
3 years ago
|
fs.writeFileSync(projectJsonPath, JSON.stringify(freshProject, null, 2), 'utf-8');
|
||
3 years ago
|
},
|
||
3 years ago
|
async saveEnvironment(env) {
|
||
3 years ago
|
try {
|
||
3 years ago
|
let projectJsonPath, freshProjectObj;
|
||
3 years ago
|
|
||
|
// make a copy of project json and get it's file path
|
||
|
// save the changes made to project json and update in state
|
||
4 years ago
|
if (this.isDashboard) {
|
||
3 years ago
|
projectJsonPath = path.join(this.$store.getters['project/currentProjectFolder'], 'config.xc.json');
|
||
|
freshProjectObj = JSON.parse(fs.readFileSync(projectJsonPath));
|
||
|
await this.saveEnvironmentFile(projectJsonPath, freshProjectObj);
|
||
|
this.$store.commit(
|
||
|
'project/setProjectJson',
|
||
|
JSON.parse(JSON.stringify(freshProjectObj), (key, value) => {
|
||
|
return typeof value === 'string' ? Handlebars.compile(value, { noEscape: true })(process.env) : value;
|
||
|
})
|
||
|
);
|
||
4 years ago
|
} else {
|
||
3 years ago
|
projectJsonPath = config.electron.defaultProjectPath;
|
||
|
freshProjectObj = JSON.parse(JSON.stringify(this.$store.state.project.defaultProject));
|
||
|
await this.saveEnvironmentFile(projectJsonPath, freshProjectObj);
|
||
|
this.$store.commit('project/setDefaultProjectJson', JSON.parse(JSON.stringify(freshProjectObj)));
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
3 years ago
|
this.$toast.success('Environment saved successfully').goAway(3000);
|
||
3 years ago
|
} catch (e) {
|
||
3 years ago
|
console.log(e);
|
||
|
this.$toast.error('Invalid JSON failed to save environment').goAway(3000);
|
||
4 years ago
|
}
|
||
|
},
|
||
3 years ago
|
// load current environment values for active project from state
|
||
3 years ago
|
loadEnvironments() {
|
||
|
let projectJsonObj;
|
||
3 years ago
|
if (this.isDashboard) {
|
||
3 years ago
|
projectJsonObj = this.$store.getters['project/GtrProjectJson'];
|
||
3 years ago
|
} else {
|
||
3 years ago
|
projectJsonObj = this.$store.state.project.defaultProject;
|
||
3 years ago
|
}
|
||
|
|
||
|
// extract environment object (key-value pair)
|
||
3 years ago
|
this.envValues = JSON.parse(
|
||
|
JSON.stringify({
|
||
|
global: projectJsonObj.apiClient,
|
||
|
...(projectJsonObj.envs
|
||
|
? Object.entries(projectJsonObj.envs).reduce((obj, [name, env]) => ({ [name]: env.apiClient, ...obj }), {})
|
||
|
: []),
|
||
|
})
|
||
|
);
|
||
3 years ago
|
|
||
|
// add extra empty environment to show in form
|
||
|
for (const env in this.envValues) {
|
||
3 years ago
|
this.envValues[env].data.push({ key: '', value: '', enabled: true });
|
||
4 years ago
|
}
|
||
3 years ago
|
},
|
||
3 years ago
|
},
|
||
3 years ago
|
beforeCreated() {},
|
||
|
destroy() {},
|
||
|
};
|
||
4 years ago
|
</script>
|
||
|
|
||
3 years ago
|
<style scoped></style>
|
||
4 years ago
|
<!--
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
-->
|