From c240a8404cf57518c79c963db621575cc5a26745 Mon Sep 17 00:00:00 2001 From: Kelani Tolulope Date: Wed, 4 Oct 2023 17:13:23 +0100 Subject: [PATCH] Add save as copy to module and chart resources --- .../compose/src/views/Admin/Charts/Edit.vue | 23 +++++++++++++++-- .../compose/src/views/Admin/Modules/Edit.vue | 25 +++++++++++++++---- lib/js/src/compose/types/chart/base.ts | 4 +++ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/client/web/compose/src/views/Admin/Charts/Edit.vue b/client/web/compose/src/views/Admin/Charts/Edit.vue index 60304cbe9a..7f4472db36 100644 --- a/client/web/compose/src/views/Admin/Charts/Edit.vue +++ b/client/web/compose/src/views/Admin/Charts/Edit.vue @@ -300,10 +300,11 @@ :processing="processing" :hide-delete="hideDelete" :hide-save="hideSave" - hide-clone + :hide-clone="!isEdit" :disable-save="disableSave" @delete="handleDelete()" @save="handleSave()" + @clone="handleClone()" @saveAndClose="handleSave({ closeOnSuccess: true })" @back="$router.push(previousPage || { name: 'admin.charts' })" /> @@ -527,7 +528,9 @@ export default { const { namespaceID } = this.namespace if (chartID === NoID) { - let c = new compose.Chart({ namespaceID: this.namespace.namespaceID }) + const chartClone = this.$attrs.chart || {} + + let c = new compose.Chart({ namespaceID: this.namespace.namespaceID, ...chartClone }) switch (this.category) { case 'gauge': c = new compose.GaugeChart(c) @@ -651,6 +654,22 @@ export default { }).catch(this.toastErrorHandler(this.$t('notification:chart.deleteFailed'))) }, + handleClone () { + const chart = this.chart.clone() + chart.chartID = NoID + chart.name = `${this.chart.name} (copy)` + chart.handle = `${this.chart.handle}_copy` + + this.$router.push({ + name: 'admin.charts.create', + params: { + chartID: NoID, + category: this.category || 'generic', + chart, + }, + }) + }, + redirect () { this.$router.push(this.previousPage || { name: 'admin.charts' }) }, diff --git a/client/web/compose/src/views/Admin/Modules/Edit.vue b/client/web/compose/src/views/Admin/Modules/Edit.vue index a16c17f7d4..a25ac816fe 100644 --- a/client/web/compose/src/views/Admin/Modules/Edit.vue +++ b/client/web/compose/src/views/Admin/Modules/Edit.vue @@ -440,12 +440,13 @@ @@ -657,10 +658,15 @@ export default { this.activeTab = 0 if (moduleID === NoID) { - this.module = new compose.Module( - { fields: [new compose.ModuleFieldString({ fieldID: NoID, name: this.$t('general.placeholder.sample') })] }, - this.namespace, - ) + if (this.$attrs.module) { + this.module = new compose.Module(this.$attrs.module, this.namespace) + } else { + this.module = new compose.Module( + { fields: [new compose.ModuleFieldString({ fieldID: NoID, name: this.$t('general.placeholder.sample') })] }, + this.namespace, + ) + } + this.initialModuleState = this.module.clone() } else { const params = { @@ -840,6 +846,15 @@ export default { }) }, + handleClone () { + const module = this.module.clone() + module.moduleID = NoID + module.name = `${this.module.name} (copy)` + module.handle = `${this.module.handle}_copy` + + this.$router.push({ name: 'admin.modules.create', params: { module, moduleID: NoID } }) + }, + async fetchConnection (connectionID) { if (connectionID && connectionID !== NoID) { this.$SystemAPI.dalConnectionRead({ connectionID }) diff --git a/lib/js/src/compose/types/chart/base.ts b/lib/js/src/compose/types/chart/base.ts index c423b565f4..0389b3d9ea 100644 --- a/lib/js/src/compose/types/chart/base.ts +++ b/lib/js/src/compose/types/chart/base.ts @@ -389,6 +389,10 @@ export class BaseChart { get resourceType (): string { return 'compose:chart' } + + clone (): BaseChart { + return new BaseChart(JSON.parse(JSON.stringify(this))) + } } export { chartUtil } from './util'