diff --git a/application/controllers/api/frontend/v1/dashboard/Widget.php b/application/controllers/api/frontend/v1/dashboard/Widget.php index 42dcb63bf..0b1c3649d 100644 --- a/application/controllers/api/frontend/v1/dashboard/Widget.php +++ b/application/controllers/api/frontend/v1/dashboard/Widget.php @@ -38,6 +38,7 @@ class Widget extends FHCAPI_Controller 'setAllowed' => 'dashboard/admin:rw', 'create' => 'dashboard/admin:rw', 'update' => 'dashboard/admin:rw', + 'remove' => 'dashboard/admin:rw', 'generators' => 'dashboard/admin:rw', ]); @@ -257,6 +258,40 @@ class Widget extends FHCAPI_Controller $this->terminateWithSuccess(); } + public function remove() + { + $this->loadPhrases([ 'dashboard' ]); + $this->load->library('form_validation'); + + $this->form_validation->set_rules( + 'widget_id', + $this->p->t('dashboard', 'widget_id'), + 'required' + ); + + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); + + $widget_id = $this->input->post('widget_id'); + + $result = $this->WidgetModel->load($widget_id); + $widget = $this->getDataOrTerminateWithError($result); + $widget = current($widget); + if (!$widget) + show_404(); + + $setup = json_decode($widget->setup); + + if (!property_exists($setup, 'generator')) + $this->terminateWithError($this->p->t('dashboard', 'widget_error_delete_native'), null, REST_Controller::HTTP_METHOD_NOT_ALLOWED); + + $result = $this->WidgetModel->delete($widget_id); + + $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess(); + } + public function generators() { $result = $this->getGeneratorsRecursive(); diff --git a/public/js/api/factory/dashboard/widget.js b/public/js/api/factory/dashboard/widget.js index a8d2d2010..f88e5fd30 100644 --- a/public/js/api/factory/dashboard/widget.js +++ b/public/js/api/factory/dashboard/widget.js @@ -69,6 +69,13 @@ export default { params }; }, + remove(widget_id) { + return { + method: 'post', + url: '/api/frontend/v1/dashboard/widget/remove', + params: { widget_id } + }; + }, generators() { return { method: 'get', diff --git a/public/js/components/Dashboard/WidgetAdmin.js b/public/js/components/Dashboard/WidgetAdmin.js index 23a26bc61..6fa55f53c 100644 --- a/public/js/components/Dashboard/WidgetAdmin.js +++ b/public/js/components/Dashboard/WidgetAdmin.js @@ -34,6 +34,10 @@ export default { this.currentData = JSON.parse(JSON.stringify(data)); this.save(); }, + remove(widget) { + this.$refs.list.$refs.table.tabulator.deleteRow(widget.widget_id); + this.originalData = this.currentData = null; + }, save() { this.select(this.currentData); this.$nextTick(() => { @@ -65,6 +69,7 @@ export default { v-model="currentData" :original-data="originalData" :unsaved-progress="unsavedProgress" + @remove="remove" @saved="save" />
diff --git a/public/js/components/Dashboard/WidgetAdmin/Edit.js b/public/js/components/Dashboard/WidgetAdmin/Edit.js index 972de1d89..28e1f1957 100644 --- a/public/js/components/Dashboard/WidgetAdmin/Edit.js +++ b/public/js/components/Dashboard/WidgetAdmin/Edit.js @@ -18,6 +18,7 @@ export default { }, emits: [ "saved", + "remove", "update:modelValue", ], data() { @@ -54,6 +55,22 @@ export default { .catch(this.$fhcAlert.handleSystemError) .finally(() => this.saving = false); }, + remove() { + this.$fhcAlert.confirmDelete() + .then(result => { + if (!result) + return Promise.reject({ handled: true }); + + this.saving = true; + return this.$api + .call(ApiWidget.remove(this.modelValue.widget_id)); + }) + .then(() => { + this.$emit('remove', this.modelValue); + }) + .catch(this.$fhcAlert.handleSystemError) + .finally(() => this.saving = false); + }, }, template: /* html */`
+