2021-06-29 01:06:20 -07:00
|
|
|
<template>
|
2021-07-08 23:14:03 -07:00
|
|
|
<form @submit.prevent="submit">
|
2021-07-27 10:47:13 -07:00
|
|
|
<div ref="modal" class="modal fade" tabindex="-1" data-bs-backdrop="static">
|
2021-07-08 23:14:03 -07:00
|
|
|
<div class="modal-dialog">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2021-07-27 10:47:13 -07:00
|
|
|
<h5 id="exampleModalLabel" class="modal-title">
|
2021-08-24 03:26:44 -07:00
|
|
|
{{ $t("Setup Notification") }}
|
2021-07-27 10:47:13 -07:00
|
|
|
</h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" />
|
2021-07-08 23:14:03 -07:00
|
|
|
</div>
|
|
|
|
<div class="modal-body">
|
2021-07-18 03:51:58 -07:00
|
|
|
<div class="mb-3">
|
2021-09-09 06:28:27 -07:00
|
|
|
<label for="notification-type" class="form-label">{{ $t("Notification Type") }}</label>
|
|
|
|
<select id="notification-type" v-model="notification.type" class="form-select">
|
2021-09-17 05:40:57 -07:00
|
|
|
<option v-for="type in notificationTypes" :key="type" :value="type">{{ $t(type) }}</option>
|
2021-07-18 03:51:58 -07:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="mb-3">
|
2021-09-09 06:28:27 -07:00
|
|
|
<label for="notification-name" class="form-label">{{ $t("Friendly Name") }}</label>
|
|
|
|
<input id="notification-name" v-model="notification.name" type="text" class="form-control" required>
|
2021-07-18 03:51:58 -07:00
|
|
|
</div>
|
|
|
|
|
2021-09-17 01:07:03 -07:00
|
|
|
<!-- form body -->
|
|
|
|
<component :is="currentForm" />
|
2021-09-12 10:46:59 -07:00
|
|
|
|
2021-09-08 10:13:09 -07:00
|
|
|
<div class="mb-3 mt-4">
|
|
|
|
<hr class="dropdown-divider mb-4">
|
2021-09-05 14:23:06 -07:00
|
|
|
|
|
|
|
<div class="form-check form-switch">
|
|
|
|
<input v-model="notification.isDefault" class="form-check-input" type="checkbox">
|
|
|
|
<label class="form-check-label">{{ $t("Default enabled") }}</label>
|
|
|
|
</div>
|
|
|
|
<div class="form-text">
|
|
|
|
{{ $t("enableDefaultNotificationDescription") }}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
<div class="form-check form-switch">
|
|
|
|
<input v-model="notification.applyExisting" class="form-check-input" type="checkbox">
|
2021-09-11 16:44:05 -07:00
|
|
|
<label class="form-check-label">{{ $t("Apply on all existing monitors") }}</label>
|
2021-09-05 14:23:06 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-07-08 23:14:03 -07:00
|
|
|
</div>
|
2021-09-08 10:13:09 -07:00
|
|
|
|
2021-07-08 23:14:03 -07:00
|
|
|
<div class="modal-footer">
|
2021-07-27 10:47:13 -07:00
|
|
|
<button v-if="id" type="button" class="btn btn-danger" :disabled="processing" @click="deleteConfirm">
|
2021-08-24 03:26:44 -07:00
|
|
|
{{ $t("Delete") }}
|
2021-07-27 10:47:13 -07:00
|
|
|
</button>
|
|
|
|
<button type="button" class="btn btn-warning" :disabled="processing" @click="test">
|
2021-08-24 03:26:44 -07:00
|
|
|
{{ $t("Test") }}
|
2021-07-27 10:47:13 -07:00
|
|
|
</button>
|
|
|
|
<button type="submit" class="btn btn-primary" :disabled="processing">
|
2021-09-07 16:10:19 -07:00
|
|
|
<div v-if="processing" class="spinner-border spinner-border-sm me-1"></div>
|
2021-08-24 03:26:44 -07:00
|
|
|
{{ $t("Save") }}
|
2021-07-27 10:47:13 -07:00
|
|
|
</button>
|
2021-07-08 23:14:03 -07:00
|
|
|
</div>
|
2021-06-29 01:06:20 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-07-08 23:14:03 -07:00
|
|
|
</form>
|
2021-07-09 02:55:48 -07:00
|
|
|
|
2021-08-25 17:43:26 -07:00
|
|
|
<Confirm ref="confirmDelete" btn-style="btn-danger" :yes-text="$t('Yes')" :no-text="$t('No')" @yes="deleteNotification">
|
|
|
|
{{ $t("deleteNotificationMsg") }}
|
2021-07-27 10:47:13 -07:00
|
|
|
</Confirm>
|
2021-06-29 01:06:20 -07:00
|
|
|
</template>
|
|
|
|
|
2021-07-29 20:23:04 -07:00
|
|
|
<script lang="ts">
|
2021-09-30 04:48:24 -07:00
|
|
|
import { Modal } from "bootstrap";
|
2021-09-07 08:32:05 -07:00
|
|
|
|
2021-07-09 02:55:48 -07:00
|
|
|
import Confirm from "./Confirm.vue";
|
2021-09-30 04:48:24 -07:00
|
|
|
import NotificationFormList from "./notifications";
|
2021-06-29 01:06:20 -07:00
|
|
|
|
|
|
|
export default {
|
2021-07-27 10:47:13 -07:00
|
|
|
components: {
|
|
|
|
Confirm,
|
2021-06-29 01:06:20 -07:00
|
|
|
},
|
2021-07-27 10:47:13 -07:00
|
|
|
props: {},
|
2022-04-17 00:27:35 -07:00
|
|
|
emits: [ "added" ],
|
2021-06-29 01:06:20 -07:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
model: null,
|
2021-07-08 23:14:03 -07:00
|
|
|
processing: false,
|
|
|
|
id: null,
|
2022-03-04 05:48:35 -08:00
|
|
|
notificationTypes: Object.keys(NotificationFormList).sort((a, b) => {
|
|
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
|
|
}),
|
2021-07-08 23:14:03 -07:00
|
|
|
notification: {
|
|
|
|
name: "",
|
2021-09-17 05:40:57 -07:00
|
|
|
/** @type { null | keyof NotificationFormList } */
|
2021-07-08 23:14:03 -07:00
|
|
|
type: null,
|
2021-09-07 16:03:24 -07:00
|
|
|
isDefault: false,
|
2021-09-08 10:13:09 -07:00
|
|
|
// Do not set default value here, please scroll to show()
|
2021-09-17 01:54:50 -07:00
|
|
|
}
|
2021-09-30 04:48:24 -07:00
|
|
|
};
|
2021-06-29 01:06:20 -07:00
|
|
|
},
|
2021-07-27 10:47:13 -07:00
|
|
|
|
2021-09-17 01:07:03 -07:00
|
|
|
computed: {
|
|
|
|
currentForm() {
|
|
|
|
if (!this.notification.type) {
|
2021-09-30 04:48:24 -07:00
|
|
|
return null;
|
2021-09-17 01:07:03 -07:00
|
|
|
}
|
2021-09-30 04:48:24 -07:00
|
|
|
return NotificationFormList[this.notification.type];
|
2021-09-17 01:07:03 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-07-27 10:47:13 -07:00
|
|
|
watch: {
|
|
|
|
"notification.type"(to, from) {
|
|
|
|
let oldName;
|
|
|
|
if (from) {
|
2021-09-21 02:25:54 -07:00
|
|
|
oldName = this.getUniqueDefaultName(from);
|
2021-07-27 10:47:13 -07:00
|
|
|
} else {
|
|
|
|
oldName = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! this.notification.name || this.notification.name === oldName) {
|
2021-09-21 02:25:54 -07:00
|
|
|
this.notification.name = this.getUniqueDefaultName(to);
|
2021-07-27 10:47:13 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-06-29 01:06:20 -07:00
|
|
|
mounted() {
|
2021-09-30 04:48:24 -07:00
|
|
|
this.modal = new Modal(this.$refs.modal);
|
2021-06-29 01:06:20 -07:00
|
|
|
},
|
|
|
|
methods: {
|
2021-07-08 23:14:03 -07:00
|
|
|
|
2022-06-01 16:32:05 -07:00
|
|
|
/** Show dialog to confirm deletion */
|
2021-07-09 02:55:48 -07:00
|
|
|
deleteConfirm() {
|
|
|
|
this.modal.hide();
|
2021-09-30 04:48:24 -07:00
|
|
|
this.$refs.confirmDelete.show();
|
2021-07-09 02:55:48 -07:00
|
|
|
},
|
|
|
|
|
2022-06-01 16:32:05 -07:00
|
|
|
/**
|
|
|
|
* Show settings for specified notification
|
|
|
|
* @param {number} notificationID ID of notification to show
|
|
|
|
*/
|
2021-07-08 23:14:03 -07:00
|
|
|
show(notificationID) {
|
|
|
|
if (notificationID) {
|
|
|
|
this.id = notificationID;
|
|
|
|
|
|
|
|
for (let n of this.$root.notificationList) {
|
|
|
|
if (n.id === notificationID) {
|
|
|
|
this.notification = JSON.parse(n.config);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.id = null;
|
|
|
|
this.notification = {
|
|
|
|
name: "",
|
2022-03-06 23:52:17 -08:00
|
|
|
type: "telegram",
|
2021-09-07 16:03:24 -07:00
|
|
|
isDefault: false,
|
2021-09-30 04:48:24 -07:00
|
|
|
};
|
2021-07-08 23:14:03 -07:00
|
|
|
}
|
|
|
|
|
2021-09-30 04:48:24 -07:00
|
|
|
this.modal.show();
|
2021-06-29 01:06:20 -07:00
|
|
|
},
|
2021-07-08 23:14:03 -07:00
|
|
|
|
2022-06-01 16:32:05 -07:00
|
|
|
/** Submit the form to the server */
|
2021-06-29 01:06:20 -07:00
|
|
|
submit() {
|
2021-07-08 23:14:03 -07:00
|
|
|
this.processing = true;
|
|
|
|
this.$root.getSocket().emit("addNotification", this.notification, this.id, (res) => {
|
2021-09-30 04:48:24 -07:00
|
|
|
this.$root.toastRes(res);
|
2021-07-08 23:14:03 -07:00
|
|
|
this.processing = false;
|
2021-06-29 01:06:20 -07:00
|
|
|
|
2021-07-08 23:14:03 -07:00
|
|
|
if (res.ok) {
|
2021-09-09 06:24:29 -07:00
|
|
|
this.modal.hide();
|
|
|
|
|
|
|
|
// Emit added event, doesn't emit edit.
|
|
|
|
if (! this.id) {
|
|
|
|
this.$emit("added", res.id);
|
|
|
|
}
|
|
|
|
|
2021-07-08 23:14:03 -07:00
|
|
|
}
|
2021-09-30 04:48:24 -07:00
|
|
|
});
|
2021-07-08 23:14:03 -07:00
|
|
|
},
|
|
|
|
|
2022-06-01 16:32:05 -07:00
|
|
|
/** Test the notification endpoint */
|
2021-07-08 23:14:03 -07:00
|
|
|
test() {
|
|
|
|
this.processing = true;
|
|
|
|
this.$root.getSocket().emit("testNotification", this.notification, (res) => {
|
2021-09-30 04:48:24 -07:00
|
|
|
this.$root.toastRes(res);
|
2021-07-08 23:14:03 -07:00
|
|
|
this.processing = false;
|
2021-09-30 04:48:24 -07:00
|
|
|
});
|
2021-07-08 23:14:03 -07:00
|
|
|
},
|
|
|
|
|
2022-06-01 16:32:05 -07:00
|
|
|
/** Delete the notification endpoint */
|
2021-07-08 23:14:03 -07:00
|
|
|
deleteNotification() {
|
|
|
|
this.processing = true;
|
|
|
|
this.$root.getSocket().emit("deleteNotification", this.id, (res) => {
|
2021-09-30 04:48:24 -07:00
|
|
|
this.$root.toastRes(res);
|
2021-07-08 23:14:03 -07:00
|
|
|
this.processing = false;
|
|
|
|
|
|
|
|
if (res.ok) {
|
2021-09-30 04:48:24 -07:00
|
|
|
this.modal.hide();
|
2021-07-08 23:14:03 -07:00
|
|
|
}
|
2021-09-30 04:48:24 -07:00
|
|
|
});
|
2021-07-08 23:14:03 -07:00
|
|
|
},
|
2021-09-21 02:25:54 -07:00
|
|
|
/**
|
2022-06-01 16:32:05 -07:00
|
|
|
* Get a unique default name for the notification
|
2021-09-22 01:13:23 -07:00
|
|
|
* @param {keyof NotificationFormList} notificationKey
|
2021-09-21 02:25:54 -07:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
getUniqueDefaultName(notificationKey) {
|
2021-09-30 04:48:24 -07:00
|
|
|
let index = 1;
|
|
|
|
let name = "";
|
2021-09-21 02:25:54 -07:00
|
|
|
do {
|
2021-09-30 04:48:24 -07:00
|
|
|
name = this.$t("defaultNotificationName", {
|
|
|
|
notification: this.$t(notificationKey).replace(/\(.+\)/, "").trim(),
|
|
|
|
number: index++
|
|
|
|
});
|
|
|
|
} while (this.$root.notificationList.find(it => it.name === name));
|
|
|
|
return name;
|
2021-09-21 02:25:54 -07:00
|
|
|
}
|
2021-07-08 23:14:03 -07:00
|
|
|
},
|
2021-09-30 04:48:24 -07:00
|
|
|
};
|
2021-06-29 01:06:20 -07:00
|
|
|
</script>
|
2021-08-07 22:47:29 -07:00
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
@import "../assets/vars.scss";
|
|
|
|
|
|
|
|
.dark {
|
|
|
|
.modal-dialog .form-text, .modal-dialog p {
|
|
|
|
color: $dark-font-color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|