2021-08-18 00:01:11 +02:00
|
|
|
import Vue from 'vue'
|
2021-11-10 03:30:44 +01:00
|
|
|
import vClickOutside from 'v-click-outside'
|
2021-10-13 03:07:42 +02:00
|
|
|
import { formatDistance, format } from 'date-fns'
|
|
|
|
|
2021-11-10 03:30:44 +01:00
|
|
|
Vue.directive('click-outside', vClickOutside.directive)
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
Vue.prototype.$eventBus = new Vue()
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
Vue.prototype.$isDev = process.env.NODE_ENV !== 'production'
|
|
|
|
|
2021-10-13 03:07:42 +02:00
|
|
|
Vue.prototype.$dateDistanceFromNow = (unixms) => {
|
|
|
|
if (!unixms) return ''
|
|
|
|
return formatDistance(unixms, Date.now(), { addSuffix: true })
|
|
|
|
}
|
|
|
|
Vue.prototype.$formatDate = (unixms, fnsFormat = 'MM/dd/yyyy HH:mm') => {
|
|
|
|
if (!unixms) return ''
|
|
|
|
return format(unixms, fnsFormat)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
Vue.prototype.$bytesPretty = (bytes, decimals = 2) => {
|
|
|
|
if (bytes === 0) {
|
|
|
|
return '0 Bytes'
|
|
|
|
}
|
|
|
|
const k = 1024
|
|
|
|
const dm = decimals < 0 ? 0 : decimals
|
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
Vue.prototype.$elapsedPretty = (seconds) => {
|
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
if (minutes < 70) {
|
|
|
|
return `${minutes} min`
|
|
|
|
}
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
if (!minutes) {
|
|
|
|
return `${hours} hr`
|
|
|
|
}
|
|
|
|
return `${hours} hr ${minutes} min`
|
|
|
|
}
|
|
|
|
|
|
|
|
Vue.prototype.$secondsToTimestamp = (seconds) => {
|
|
|
|
var _seconds = seconds
|
|
|
|
var _minutes = Math.floor(seconds / 60)
|
|
|
|
_seconds -= _minutes * 60
|
|
|
|
var _hours = Math.floor(_minutes / 60)
|
|
|
|
_minutes -= _hours * 60
|
2021-10-27 00:55:48 +02:00
|
|
|
_seconds = Math.floor(_seconds)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (!_hours) {
|
|
|
|
return `${_minutes}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
return `${_hours}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
|
2021-11-13 02:43:16 +01:00
|
|
|
Vue.prototype.$elapsedPrettyExtended = (seconds) => {
|
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
seconds -= minutes * 60
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
var days = Math.floor(hours / 24)
|
|
|
|
hours -= days * 24
|
|
|
|
|
|
|
|
var strs = []
|
|
|
|
if (days) strs.push(`${days}d`)
|
|
|
|
if (hours) strs.push(`${hours}h`)
|
|
|
|
if (minutes) strs.push(`${minutes}m`)
|
|
|
|
if (seconds) strs.push(`${seconds}s`)
|
|
|
|
return strs.join(' ')
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:37:09 +02:00
|
|
|
Vue.prototype.$calculateTextSize = (text, styles = {}) => {
|
|
|
|
const el = document.createElement('p')
|
2021-08-22 15:52:37 +02:00
|
|
|
|
2021-09-16 15:37:09 +02:00
|
|
|
let attr = 'margin:0px;opacity:1;position:absolute;top:100px;left:100px;z-index:99;'
|
|
|
|
for (const key in styles) {
|
|
|
|
if (styles[key] && String(styles[key]).length > 0) {
|
|
|
|
attr += `${key}:${styles[key]};`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
el.setAttribute('style', attr)
|
|
|
|
el.innerText = text
|
|
|
|
|
|
|
|
document.body.appendChild(el)
|
|
|
|
const boundingBox = el.getBoundingClientRect()
|
|
|
|
el.remove()
|
|
|
|
return {
|
|
|
|
height: boundingBox.height,
|
|
|
|
width: boundingBox.width
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-11-10 03:30:44 +01:00
|
|
|
// function isClickedOutsideEl(clickEvent, elToCheckOutside, ignoreSelectors = [], ignoreElems = []) {
|
|
|
|
// const isDOMElement = (element) => {
|
|
|
|
// return element instanceof Element || element instanceof HTMLDocument
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const clickedEl = clickEvent.srcElement
|
|
|
|
// const didClickOnIgnoredEl = ignoreElems.filter((el) => el).some((element) => element.contains(clickedEl) || element.isEqualNode(clickedEl))
|
|
|
|
// const didClickOnIgnoredSelector = ignoreSelectors.length ? ignoreSelectors.map((selector) => clickedEl.closest(selector)).reduce((curr, accumulator) => curr && accumulator, true) : false
|
|
|
|
|
|
|
|
// if (isDOMElement(elToCheckOutside) && !elToCheckOutside.contains(clickedEl) && !didClickOnIgnoredEl && !didClickOnIgnoredSelector) {
|
|
|
|
// return true
|
|
|
|
// }
|
|
|
|
// return false
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Vue.directive('click-outside', {
|
|
|
|
// bind: function (el, binding, vnode) {
|
|
|
|
// let vm = vnode.context;
|
|
|
|
// let callback = binding.value;
|
|
|
|
// if (typeof callback !== 'function') {
|
|
|
|
// console.error('Invalid callback', binding)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// el['__click_outside__'] = (ev) => {
|
|
|
|
// if (isClickedOutsideEl(ev, el)) {
|
|
|
|
// callback.call(vm, ev)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// document.addEventListener('click', el['__click_outside__'], false)
|
|
|
|
// },
|
|
|
|
// unbind: function (el, binding, vnode) {
|
|
|
|
// document.removeEventListener('click', el['__click_outside__'], false)
|
|
|
|
// delete el['__click_outside__']
|
|
|
|
// }
|
|
|
|
// })
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
Vue.prototype.$sanitizeFilename = (input, replacement = '') => {
|
|
|
|
if (typeof input !== 'string') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var illegalRe = /[\/\?<>\\:\*\|"]/g;
|
|
|
|
var controlRe = /[\x00-\x1f\x80-\x9f]/g;
|
|
|
|
var reservedRe = /^\.+$/;
|
|
|
|
var windowsReservedRe = /^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i;
|
|
|
|
var windowsTrailingRe = /[\. ]+$/;
|
|
|
|
|
|
|
|
var sanitized = input
|
|
|
|
.replace(illegalRe, replacement)
|
|
|
|
.replace(controlRe, replacement)
|
|
|
|
.replace(reservedRe, replacement)
|
|
|
|
.replace(windowsReservedRe, replacement)
|
|
|
|
.replace(windowsTrailingRe, replacement);
|
|
|
|
return sanitized
|
|
|
|
}
|
2021-09-05 20:21:02 +02:00
|
|
|
|
2021-10-18 01:05:43 +02:00
|
|
|
|
|
|
|
function xmlToJson(xml) {
|
|
|
|
const json = {};
|
|
|
|
for (const res of xml.matchAll(/(?:<(\w*)(?:\s[^>]*)*>)((?:(?!<\1).)*)(?:<\/\1>)|<(\w*)(?:\s*)*\/>/gm)) {
|
|
|
|
const key = res[1] || res[3];
|
|
|
|
const value = res[2] && xmlToJson(res[2]);
|
|
|
|
json[key] = ((value && Object.keys(value).length) ? value : res[2]) || null;
|
|
|
|
|
|
|
|
}
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
Vue.prototype.$xmlToJson = xmlToJson
|
|
|
|
|
2021-09-05 21:30:33 +02:00
|
|
|
const encode = (text) => encodeURIComponent(Buffer.from(text).toString('base64'))
|
|
|
|
Vue.prototype.$encode = encode
|
|
|
|
const decode = (text) => Buffer.from(decodeURIComponent(text), 'base64').toString()
|
|
|
|
Vue.prototype.$decode = decode
|
|
|
|
|
2021-09-05 20:21:02 +02:00
|
|
|
export {
|
|
|
|
encode,
|
|
|
|
decode
|
2021-09-24 14:32:38 +02:00
|
|
|
}
|
|
|
|
export default ({ app }, inject) => {
|
|
|
|
app.$decode = decode
|
2021-09-05 20:21:02 +02:00
|
|
|
}
|