2025-06-20 23:26:06 +12:00
|
|
|
import axios from "axios";
|
|
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
import ColorHash from "color-hash";
|
|
|
|
|
import { Modal, Offcanvas } from "bootstrap";
|
2024-06-22 13:27:00 +12:00
|
|
|
import { limitOptions } from "../stores/pagination";
|
2022-10-08 23:23:30 +13:00
|
|
|
|
2023-09-22 15:06:03 +12:00
|
|
|
// BootstrapElement is used to return a fake Bootstrap element
|
2023-06-14 22:18:51 +12:00
|
|
|
// if the ID returns nothing to prevent errors.
|
2023-09-22 15:06:03 +12:00
|
|
|
class BootstrapElement {
|
2025-06-20 23:26:06 +12:00
|
|
|
hide() {}
|
|
|
|
|
show() {}
|
2023-09-22 15:06:03 +12:00
|
|
|
}
|
2023-06-14 22:18:51 +12:00
|
|
|
|
|
|
|
|
// Set up the color hash generator lightness and hue to ensure darker colors
|
2025-06-20 23:26:06 +12:00
|
|
|
const colorHash = new ColorHash({ lightness: 0.3, saturation: [0.35, 0.5, 0.65] });
|
2022-07-29 23:23:08 +12:00
|
|
|
|
|
|
|
|
/* Common mixin functions used in apps */
|
2023-09-14 22:30:20 +12:00
|
|
|
export default {
|
2022-09-12 22:11:51 +12:00
|
|
|
data() {
|
|
|
|
|
return {
|
2023-06-14 22:18:51 +12:00
|
|
|
loading: 0,
|
|
|
|
|
tagColorCache: {},
|
2025-06-20 23:26:06 +12:00
|
|
|
};
|
2023-06-14 22:18:51 +12:00
|
|
|
},
|
|
|
|
|
|
2022-09-12 22:11:51 +12:00
|
|
|
methods: {
|
2024-06-22 13:27:00 +12:00
|
|
|
resolve(u) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return this.$router.resolve(u).href;
|
2023-09-22 15:06:03 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
searchURI(s) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return this.resolve("/search") + "?q=" + encodeURIComponent(s);
|
2023-09-23 11:48:06 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
getFileSize(bytes) {
|
2025-06-20 23:26:06 +12:00
|
|
|
if (bytes === 0) {
|
|
|
|
|
return "0B";
|
2024-01-02 13:23:16 +13:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
|
|
|
return (bytes / Math.pow(1024, i)).toFixed(1) * 1 + " " + ["B", "kB", "MB", "GB", "TB"][i];
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
formatNumber(nr) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return new Intl.NumberFormat().format(nr);
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
messageDate(d) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return dayjs(d).format("ddd, D MMM YYYY, h:mm a");
|
2022-11-13 16:45:54 +13:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
secondsToRelative(d) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return dayjs().subtract(d, "seconds").fromNow();
|
2024-01-02 13:23:16 +13:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
tagEncodeURI(tag) {
|
2023-09-14 22:30:20 +12:00
|
|
|
if (tag.match(/ /)) {
|
2025-06-20 23:26:06 +12:00
|
|
|
tag = `"${tag}"`;
|
2023-09-14 22:30:20 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
return encodeURIComponent(`tag:${tag}`);
|
2023-09-22 15:06:03 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
getSearch() {
|
2023-09-22 15:06:03 +12:00
|
|
|
if (!window.location.search) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return false;
|
2023-09-22 15:06:03 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const q = urlParams.get("q")?.trim();
|
2024-06-01 20:32:11 +09:00
|
|
|
if (!q) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return false;
|
2023-09-22 15:06:03 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
return q;
|
2023-09-14 22:30:20 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
getPaginationParams() {
|
2024-06-01 20:32:11 +09:00
|
|
|
if (!window.location.search) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return null;
|
2024-06-01 20:32:11 +09:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
const start = parseInt(urlParams.get("start")?.trim(), 10);
|
|
|
|
|
const limit = parseInt(urlParams.get("limit")?.trim(), 10);
|
2024-06-01 20:32:11 +09:00
|
|
|
return {
|
|
|
|
|
start: Number.isInteger(start) && start >= 0 ? start : null,
|
|
|
|
|
limit: limitOptions.includes(limit) ? limit : null,
|
2025-06-20 23:26:06 +12:00
|
|
|
};
|
2024-06-01 20:32:11 +09:00
|
|
|
},
|
|
|
|
|
|
2022-09-12 22:11:51 +12:00
|
|
|
// generic modal get/set function
|
2024-06-22 13:27:00 +12:00
|
|
|
modal(id) {
|
2025-06-20 23:26:06 +12:00
|
|
|
const e = document.getElementById(id);
|
2022-09-12 22:11:51 +12:00
|
|
|
if (e) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return Modal.getOrCreateInstance(e);
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
|
|
|
|
// in case there are open/close actions
|
2025-06-20 23:26:06 +12:00
|
|
|
return new BootstrapElement();
|
2023-09-22 15:06:03 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// close mobile navigation
|
2024-06-22 13:27:00 +12:00
|
|
|
hideNav() {
|
2025-06-20 23:26:06 +12:00
|
|
|
const e = document.getElementById("offcanvas");
|
2023-09-22 15:06:03 +12:00
|
|
|
if (e) {
|
2025-06-20 23:26:06 +12:00
|
|
|
Offcanvas.getOrCreateInstance(e).hide();
|
2023-09-22 15:06:03 +12:00
|
|
|
}
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Axios GET request
|
|
|
|
|
*
|
|
|
|
|
* @params string url
|
|
|
|
|
* @params array array parameters Object/array
|
|
|
|
|
* @params function callback function
|
2023-09-23 15:49:43 +12:00
|
|
|
* @params function error callback function
|
2022-09-12 22:11:51 +12:00
|
|
|
*/
|
2024-08-04 17:04:14 +12:00
|
|
|
get(url, values, callback, errorCallback, hideLoader) {
|
|
|
|
|
if (!hideLoader) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading++;
|
2024-08-04 17:04:14 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
axios
|
|
|
|
|
.get(url, { params: values })
|
2022-09-12 22:11:51 +12:00
|
|
|
.then(callback)
|
2024-06-22 13:27:00 +12:00
|
|
|
.catch((err) => {
|
2025-06-20 23:26:06 +12:00
|
|
|
if (typeof errorCallback === "function") {
|
|
|
|
|
return errorCallback(err);
|
2023-09-23 15:49:43 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
this.handleError(err);
|
2023-09-23 15:49:43 +12:00
|
|
|
})
|
2024-06-22 13:27:00 +12:00
|
|
|
.then(() => {
|
2022-09-12 22:11:51 +12:00
|
|
|
// always executed
|
2024-08-04 17:04:14 +12:00
|
|
|
if (!hideLoader && this.loading > 0) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading--;
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
});
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2022-10-07 19:47:41 +13:00
|
|
|
* Axios POST request
|
2022-09-12 22:11:51 +12:00
|
|
|
*
|
|
|
|
|
* @params string url
|
2022-10-07 19:47:41 +13:00
|
|
|
* @params array object/array values
|
2022-09-12 22:11:51 +12:00
|
|
|
* @params function callback function
|
|
|
|
|
*/
|
2024-06-22 13:27:00 +12:00
|
|
|
post(url, data, callback) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading++;
|
|
|
|
|
axios
|
|
|
|
|
.post(url, data)
|
2022-09-12 22:11:51 +12:00
|
|
|
.then(callback)
|
2024-06-22 13:27:00 +12:00
|
|
|
.catch(this.handleError)
|
|
|
|
|
.then(() => {
|
2022-09-12 22:11:51 +12:00
|
|
|
// always executed
|
2024-06-22 13:27:00 +12:00
|
|
|
if (this.loading > 0) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading--;
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
});
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Axios DELETE request (REST only)
|
|
|
|
|
*
|
|
|
|
|
* @params string url
|
2022-10-07 19:47:41 +13:00
|
|
|
* @params array object/array values
|
|
|
|
|
* @params function callback function
|
|
|
|
|
*/
|
2024-06-22 13:27:00 +12:00
|
|
|
delete(url, data, callback) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading++;
|
|
|
|
|
axios
|
|
|
|
|
.delete(url, { data })
|
2022-10-07 19:47:41 +13:00
|
|
|
.then(callback)
|
2024-06-22 13:27:00 +12:00
|
|
|
.catch(this.handleError)
|
|
|
|
|
.then(() => {
|
2022-10-07 19:47:41 +13:00
|
|
|
// always executed
|
2024-06-22 13:27:00 +12:00
|
|
|
if (this.loading > 0) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading--;
|
2022-10-07 19:47:41 +13:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
});
|
2022-10-07 19:47:41 +13:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Axios PUT request (REST only)
|
|
|
|
|
*
|
|
|
|
|
* @params string url
|
|
|
|
|
* @params array object/array values
|
2022-09-12 22:11:51 +12:00
|
|
|
* @params function callback function
|
|
|
|
|
*/
|
2024-06-22 13:27:00 +12:00
|
|
|
put(url, data, callback) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading++;
|
|
|
|
|
axios
|
|
|
|
|
.put(url, data)
|
2022-09-12 22:11:51 +12:00
|
|
|
.then(callback)
|
2024-06-22 13:27:00 +12:00
|
|
|
.catch(this.handleError)
|
|
|
|
|
.then(() => {
|
2022-09-12 22:11:51 +12:00
|
|
|
// always executed
|
2024-06-22 13:27:00 +12:00
|
|
|
if (this.loading > 0) {
|
2025-06-20 23:26:06 +12:00
|
|
|
this.loading--;
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
});
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
2023-09-23 15:49:43 +12:00
|
|
|
// Ajax error message
|
2024-06-22 13:27:00 +12:00
|
|
|
handleError(error) {
|
2023-09-23 15:49:43 +12:00
|
|
|
// handle error
|
|
|
|
|
if (error.response && error.response.data) {
|
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
|
if (error.response.data.Error) {
|
2025-06-20 23:26:06 +12:00
|
|
|
alert(error.response.data.Error);
|
2023-09-23 15:49:43 +12:00
|
|
|
} else {
|
2025-06-20 23:26:06 +12:00
|
|
|
alert(error.response.data);
|
2023-09-23 15:49:43 +12:00
|
|
|
}
|
|
|
|
|
} else if (error.request) {
|
|
|
|
|
// The request was made but no response was received
|
2025-06-20 23:26:06 +12:00
|
|
|
alert("Error sending data to the server. Please try again.");
|
2023-09-23 15:49:43 +12:00
|
|
|
} else {
|
|
|
|
|
// Something happened in setting up the request that triggered an Error
|
2025-06-20 23:26:06 +12:00
|
|
|
alert(error.message);
|
2023-09-23 15:49:43 +12:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
allAttachments(message) {
|
2025-06-20 23:26:06 +12:00
|
|
|
const a = [];
|
|
|
|
|
for (const i in message.Attachments) {
|
|
|
|
|
a.push(message.Attachments[i]);
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
for (const i in message.OtherParts) {
|
|
|
|
|
a.push(message.OtherParts[i]);
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
for (const i in message.Inline) {
|
|
|
|
|
a.push(message.Inline[i]);
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
return a.length ? a : false;
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
isImage(a) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return a.ContentType.match(/^image\//);
|
2022-09-12 22:11:51 +12:00
|
|
|
},
|
|
|
|
|
|
2024-06-22 13:27:00 +12:00
|
|
|
attachmentIcon(a) {
|
2025-06-20 23:26:06 +12:00
|
|
|
const ext = a.FileName.split(".").pop().toLowerCase();
|
2022-09-12 22:11:51 +12:00
|
|
|
|
|
|
|
|
if (a.ContentType.match(/^image\//)) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return "bi-file-image-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (a.ContentType.match(/\/pdf$/) || ext === "pdf") {
|
|
|
|
|
return "bi-file-pdf-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (["doc", "docx", "odt", "rtf"].includes(ext)) {
|
|
|
|
|
return "bi-file-word-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (["xls", "xlsx", "ods"].includes(ext)) {
|
|
|
|
|
return "bi-file-spreadsheet-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (["ppt", "pptx", "key", "ppt", "odp"].includes(ext)) {
|
|
|
|
|
return "bi-file-slides-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (["zip", "tar", "rar", "bz2", "gz", "xz"].includes(ext)) {
|
|
|
|
|
return "bi-file-zip-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (["ics"].includes(ext)) {
|
|
|
|
|
return "bi-calendar-event";
|
2024-05-18 23:42:06 +12:00
|
|
|
}
|
2022-09-12 22:11:51 +12:00
|
|
|
if (a.ContentType.match(/^audio\//)) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return "bi-file-music-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
|
|
|
|
if (a.ContentType.match(/^video\//)) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return "bi-file-play-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
|
|
|
|
if (a.ContentType.match(/\/calendar$/)) {
|
2025-06-20 23:26:06 +12:00
|
|
|
return "bi-file-check-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
if (a.ContentType.match(/^text\//) || ["txt", "sh", "log"].includes(ext)) {
|
|
|
|
|
return "bi-file-text-fill";
|
2022-09-12 22:11:51 +12:00
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
return "bi-file-arrow-down-fill";
|
2023-06-14 22:18:51 +12:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Returns a hex color based on a string.
|
|
|
|
|
// Values are stored in an array for faster lookup / processing.
|
2024-06-22 13:27:00 +12:00
|
|
|
colorHash(s) {
|
2025-06-20 23:26:06 +12:00
|
|
|
if (this.tagColorCache[s] !== undefined) {
|
|
|
|
|
return this.tagColorCache[s];
|
2023-06-14 22:18:51 +12:00
|
|
|
}
|
2025-06-20 23:26:06 +12:00
|
|
|
this.tagColorCache[s] = colorHash.hex(s);
|
2023-06-14 22:18:51 +12:00
|
|
|
|
2025-06-20 23:26:06 +12:00
|
|
|
return this.tagColorCache[s];
|
2023-06-14 22:18:51 +12:00
|
|
|
},
|
2025-06-20 23:26:06 +12:00
|
|
|
},
|
|
|
|
|
};
|