Added JSDoc for src/mixins/*

Signed-off-by: Matthew Nickson <mnickson@sidingsmedia.com>
This commit is contained in:
Matthew Nickson 2022-06-02 10:38:17 +01:00
parent 2b42c3c828
commit 213aca4fc3
No known key found for this signature in database
GPG Key ID: BF229DCFD4748E05
5 changed files with 141 additions and 0 deletions

View File

@ -18,14 +18,31 @@ export default {
}, },
methods: { methods: {
/**
* Return a given value in the format YYYY-MM-DD HH:mm:ss
* @param {any} value Value to format as date time
* @returns {string}
*/
datetime(value) { datetime(value) {
return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss"); return this.datetimeFormat(value, "YYYY-MM-DD HH:mm:ss");
}, },
/**
* Return a given value in the format YYYY-MM-DD
* @param {any} value Value to format as date
* @returns {string}
*/
date(value) { date(value) {
return this.datetimeFormat(value, "YYYY-MM-DD"); return this.datetimeFormat(value, "YYYY-MM-DD");
}, },
/**
* Return a given value in the format HH:mm or if second is set
* to true, HH:mm:ss
* @param {any} value Value to format
* @param {boolean} second Should seconds be included?
* @returns {string}
*/
time(value, second = true) { time(value, second = true) {
let secondString; let secondString;
if (second) { if (second) {
@ -36,6 +53,12 @@ export default {
return this.datetimeFormat(value, "HH:mm" + secondString); return this.datetimeFormat(value, "HH:mm" + secondString);
}, },
/**
* Return a value in a custom format
* @param {any} value Value to format
* @param {any} format Format to return value in
* @returns {string}
*/
datetimeFormat(value, format) { datetimeFormat(value, format) {
if (value !== undefined && value !== "") { if (value !== undefined && value !== "") {
return dayjs.utc(value).tz(this.timezone).format(format); return dayjs.utc(value).tz(this.timezone).format(format);

View File

@ -22,6 +22,7 @@ export default {
}, },
methods: { methods: {
/** Change the application language */
async changeLang(lang) { async changeLang(lang) {
let message = (await langModules["../languages/" + lang + ".js"]()).default; let message = (await langModules["../languages/" + lang + ".js"]()).default;
this.$i18n.setLocaleMessage(lang, message); this.$i18n.setLocaleMessage(lang, message);

View File

@ -12,11 +12,13 @@ export default {
}, },
methods: { methods: {
/** Called when the screen changes size */
onResize() { onResize() {
this.windowWidth = window.innerWidth; this.windowWidth = window.innerWidth;
this.updateBody(); this.updateBody();
}, },
/** Update the document body */
updateBody() { updateBody() {
if (this.isMobile) { if (this.isMobile) {
document.body.classList.add("mobile"); document.body.classList.add("mobile");

View File

@ -62,6 +62,12 @@ export default {
methods: { methods: {
/**
* Initialize connection to socket server
* @param {boolean} [bypass = false] Should the check for if we
* are on a status page be bypassed?
* @returns {(void|null)}
*/
initSocketIO(bypass = false) { initSocketIO(bypass = false) {
// No need to re-init // No need to re-init
if (this.socket.initedSocketIO) { if (this.socket.initedSocketIO) {
@ -258,10 +264,18 @@ export default {
socket.on("cloudflared_token", (res) => this.cloudflared.cloudflareTunnelToken = res); socket.on("cloudflared_token", (res) => this.cloudflared.cloudflareTunnelToken = res);
}, },
/**
* The storage currently in use
* @returns {Storage}
*/
storage() { storage() {
return (this.remember) ? localStorage : sessionStorage; return (this.remember) ? localStorage : sessionStorage;
}, },
/**
* Get payload of JWT cookie
* @returns {(Object|undefined)}
*/
getJWTPayload() { getJWTPayload() {
const jwtToken = this.$root.storage().token; const jwtToken = this.$root.storage().token;
@ -271,10 +285,18 @@ export default {
return undefined; return undefined;
}, },
/**
* Get current socket
* @returns {Socket}
*/
getSocket() { getSocket() {
return socket; return socket;
}, },
/**
* Show success or error toast dependant on response status code
* @param {Object} res Response object
*/
toastRes(res) { toastRes(res) {
if (res.ok) { if (res.ok) {
toast.success(res.msg); toast.success(res.msg);
@ -283,14 +305,35 @@ export default {
} }
}, },
/**
* Show a success toast
* @param {string} msg Message to show
*/
toastSuccess(msg) { toastSuccess(msg) {
toast.success(msg); toast.success(msg);
}, },
/**
* Show an error toast
* @param {string} msg Message to show
*/
toastError(msg) { toastError(msg) {
toast.error(msg); toast.error(msg);
}, },
/**
* Callback for login
* @callback loginCB
* @param {Object} res Response object
*/
/**
* Send request to log user in
* @param {string} username Username to log in with
* @param {string} password Password to log in with
* @param {string} token User token
* @param {loginCB} callback Callback to call with result
*/
login(username, password, token, callback) { login(username, password, token, callback) {
socket.emit("login", { socket.emit("login", {
username, username,
@ -315,6 +358,10 @@ export default {
}); });
}, },
/**
* Log in using a token
* @param {string} token Token to log in with
*/
loginByToken(token) { loginByToken(token) {
socket.emit("loginByToken", token, (res) => { socket.emit("loginByToken", token, (res) => {
this.allowLoginDialog = true; this.allowLoginDialog = true;
@ -328,6 +375,7 @@ export default {
}); });
}, },
/** Log out of the web application */
logout() { logout() {
socket.emit("logout", () => { }); socket.emit("logout", () => { });
this.storage().removeItem("token"); this.storage().removeItem("token");
@ -337,26 +385,54 @@ export default {
this.clearData(); this.clearData();
}, },
/**
* Callback for general socket requests
* @callback socketCB
* @param {Object} res Result of operation
*/
/** Prepare 2FA configuration */
prepare2FA(callback) { prepare2FA(callback) {
socket.emit("prepare2FA", callback); socket.emit("prepare2FA", callback);
}, },
/**
* Save the current 2FA configuration
* @param {any} secret Unused
* @param {socketCB} callback
*/
save2FA(secret, callback) { save2FA(secret, callback) {
socket.emit("save2FA", callback); socket.emit("save2FA", callback);
}, },
/**
* Disable 2FA for this user
* @param {socketCB} callback
*/
disable2FA(callback) { disable2FA(callback) {
socket.emit("disable2FA", callback); socket.emit("disable2FA", callback);
}, },
/**
* Verify the provided 2FA token
* @param {string} token Token to verify
* @param {socketCB} callback
*/
verifyToken(token, callback) { verifyToken(token, callback) {
socket.emit("verifyToken", token, callback); socket.emit("verifyToken", token, callback);
}, },
/**
* Get current 2FA status
* @param {socketCB} callback
*/
twoFAStatus(callback) { twoFAStatus(callback) {
socket.emit("twoFAStatus", callback); socket.emit("twoFAStatus", callback);
}, },
/**
* Get list of monitors
* @param {socketCB} callback
*/
getMonitorList(callback) { getMonitorList(callback) {
if (! callback) { if (! callback) {
callback = () => { }; callback = () => { };
@ -364,36 +440,74 @@ export default {
socket.emit("getMonitorList", callback); socket.emit("getMonitorList", callback);
}, },
/**
* Add a monitor
* @param {Object} monitor Object representing monitor to add
* @param {socketCB} callback
*/
add(monitor, callback) { add(monitor, callback) {
socket.emit("add", monitor, callback); socket.emit("add", monitor, callback);
}, },
/**
* Delete monitor by ID
* @param {number} monitorID ID of monitor to delete
* @param {socketCB} callback
*/
deleteMonitor(monitorID, callback) { deleteMonitor(monitorID, callback) {
socket.emit("deleteMonitor", monitorID, callback); socket.emit("deleteMonitor", monitorID, callback);
}, },
/** Clear the hearbeat list */
clearData() { clearData() {
console.log("reset heartbeat list"); console.log("reset heartbeat list");
this.heartbeatList = {}; this.heartbeatList = {};
this.importantHeartbeatList = {}; this.importantHeartbeatList = {};
}, },
/**
* Upload the provided backup
* @param {string} uploadedJSON JSON to upload
* @param {string} importHandle Type of import. If set to
* most data in database will be replaced
* @param {socketCB} callback
*/
uploadBackup(uploadedJSON, importHandle, callback) { uploadBackup(uploadedJSON, importHandle, callback) {
socket.emit("uploadBackup", uploadedJSON, importHandle, callback); socket.emit("uploadBackup", uploadedJSON, importHandle, callback);
}, },
/**
* Clear events for a specified monitor
* @param {number} monitorID ID of monitor to clear
* @param {socketCB} callback
*/
clearEvents(monitorID, callback) { clearEvents(monitorID, callback) {
socket.emit("clearEvents", monitorID, callback); socket.emit("clearEvents", monitorID, callback);
}, },
/**
* Clear the heartbeats of a specified monitor
* @param {number} monitorID Id of monitor to clear
* @param {socketCB} callback
*/
clearHeartbeats(monitorID, callback) { clearHeartbeats(monitorID, callback) {
socket.emit("clearHeartbeats", monitorID, callback); socket.emit("clearHeartbeats", monitorID, callback);
}, },
/**
* Clear all statistics
* @param {socketCB} callback
*/
clearStatistics(callback) { clearStatistics(callback) {
socket.emit("clearStatistics", callback); socket.emit("clearStatistics", callback);
}, },
/**
* Get monitor beats for a specific monitor in a time range
* @param {number} monitorID ID of monitor to fetch
* @param {number} period Time in hours from now
* @param {socketCB} callback
*/
getMonitorBeats(monitorID, period, callback) { getMonitorBeats(monitorID, period, callback) {
socket.emit("getMonitorBeats", monitorID, period, callback); socket.emit("getMonitorBeats", monitorID, period, callback);
} }

View File

@ -75,6 +75,7 @@ export default {
}, },
methods: { methods: {
/** Update the theme color meta tag */
updateThemeColorMeta() { updateThemeColorMeta() {
if (this.theme === "dark") { if (this.theme === "dark") {
document.querySelector("#theme-color").setAttribute("content", "#161B22"); document.querySelector("#theme-color").setAttribute("content", "#161B22");