2021-08-18 00:01:11 +02:00
|
|
|
import Vue from 'vue'
|
2022-06-02 03:14:10 +02:00
|
|
|
import Path from 'path'
|
2021-11-10 03:30:44 +01:00
|
|
|
import vClickOutside from 'v-click-outside'
|
2021-11-14 02:17:33 +01:00
|
|
|
import { formatDistance, format, addDays, isDate } from 'date-fns'
|
2021-10-13 03:07:42 +02:00
|
|
|
|
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-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-11-14 02:17:33 +01:00
|
|
|
Vue.prototype.$formatJsDate = (jsdate, fnsFormat = 'MM/dd/yyyy HH:mm') => {
|
|
|
|
if (!jsdate || !isDate(jsdate)) return ''
|
|
|
|
return format(jsdate, fnsFormat)
|
|
|
|
}
|
|
|
|
Vue.prototype.$addDaysToToday = (daysToAdd) => {
|
|
|
|
var date = addDays(new Date(), daysToAdd)
|
|
|
|
if (!date || !isDate(date)) return null
|
2022-05-11 23:27:40 +02:00
|
|
|
return date
|
|
|
|
}
|
|
|
|
Vue.prototype.$addDaysToDate = (jsdate, daysToAdd) => {
|
|
|
|
var date = addDays(jsdate, daysToAdd)
|
|
|
|
if (!date || !isDate(date)) return null
|
2021-11-14 02:17:33 +01:00
|
|
|
return date
|
|
|
|
}
|
2021-10-13 03:07:42 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
Vue.prototype.$bytesPretty = (bytes, decimals = 2) => {
|
2022-03-27 22:37:04 +02:00
|
|
|
if (isNaN(bytes) || bytes == 0) {
|
2021-08-18 00:01:11 +02:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2022-05-12 00:35:04 +02:00
|
|
|
Vue.prototype.$elapsedPretty = (seconds, useFullNames = false) => {
|
2022-05-12 00:20:32 +02:00
|
|
|
if (seconds < 60) {
|
2022-05-12 00:35:04 +02:00
|
|
|
return `${Math.floor(seconds)} sec${useFullNames ? 'onds' : ''}`
|
2022-05-12 00:20:32 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
if (minutes < 70) {
|
2022-05-12 00:35:04 +02:00
|
|
|
return `${minutes} min${useFullNames ? `ute${minutes === 1 ? '' : 's'}` : ''}`
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
if (!minutes) {
|
2022-05-12 00:35:04 +02:00
|
|
|
return `${hours} ${useFullNames ? 'hours' : 'hr'}`
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2022-05-12 00:35:04 +02:00
|
|
|
return `${hours} ${useFullNames ? `hour${hours === 1 ? '' : 's'}` : 'hr'} ${minutes} ${useFullNames ? `minute${minutes === 1 ? '' : 's'}` : 'min'}`
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vue.prototype.$secondsToTimestamp = (seconds) => {
|
2022-05-27 02:09:46 +02:00
|
|
|
if (!seconds) return '0:00'
|
2021-08-18 00:01:11 +02:00
|
|
|
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')}`
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:01:53 +01:00
|
|
|
Vue.prototype.$elapsedPrettyExtended = (seconds, useDays = true) => {
|
|
|
|
if (isNaN(seconds) || seconds === null) return ''
|
|
|
|
seconds = Math.round(seconds)
|
|
|
|
|
2021-11-13 02:43:16 +01:00
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
seconds -= minutes * 60
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
2022-02-14 23:01:53 +01:00
|
|
|
|
|
|
|
var days = 0
|
|
|
|
if (useDays || Math.floor(hours / 24) >= 100) {
|
|
|
|
days = Math.floor(hours / 24)
|
|
|
|
hours -= days * 24
|
|
|
|
}
|
2021-11-13 02:43:16 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-14 00:13:58 +02:00
|
|
|
Vue.prototype.$sanitizeFilename = (input, colonReplacement = ' - ') => {
|
2021-08-18 00:01:11 +02:00
|
|
|
if (typeof input !== 'string') {
|
|
|
|
return false
|
|
|
|
}
|
2022-06-02 03:14:10 +02:00
|
|
|
|
|
|
|
// Max is actually 255-260 for windows but this leaves padding incase ext wasnt put on yet
|
|
|
|
const MAX_FILENAME_LEN = 240
|
|
|
|
|
2022-05-14 00:13:58 +02:00
|
|
|
var replacement = ''
|
2022-06-02 03:14:10 +02:00
|
|
|
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 lineBreaks = /[\n\r]/g
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
var sanitized = input
|
2022-05-14 00:13:58 +02:00
|
|
|
.replace(':', colonReplacement) // Replace first occurrence of a colon
|
2021-08-18 00:01:11 +02:00
|
|
|
.replace(illegalRe, replacement)
|
|
|
|
.replace(controlRe, replacement)
|
|
|
|
.replace(reservedRe, replacement)
|
2022-06-02 03:14:10 +02:00
|
|
|
.replace(lineBreaks, replacement)
|
2021-08-18 00:01:11 +02:00
|
|
|
.replace(windowsReservedRe, replacement)
|
2022-06-02 03:14:10 +02:00
|
|
|
.replace(windowsTrailingRe, replacement)
|
|
|
|
|
|
|
|
|
|
|
|
if (sanitized.length > MAX_FILENAME_LEN) {
|
|
|
|
var lenToRemove = sanitized.length - MAX_FILENAME_LEN
|
|
|
|
var ext = Path.extname(sanitized)
|
|
|
|
var basename = Path.basename(sanitized, ext)
|
|
|
|
basename = basename.slice(0, basename.length - lenToRemove)
|
|
|
|
sanitized = basename + ext
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
return sanitized
|
|
|
|
}
|
2021-09-05 20:21:02 +02:00
|
|
|
|
2022-05-04 01:52:34 +02:00
|
|
|
// SOURCE: https://gist.github.com/spyesx/561b1d65d4afb595f295
|
|
|
|
// modified: allowed underscores
|
|
|
|
Vue.prototype.$sanitizeSlug = (str) => {
|
|
|
|
if (!str) return ''
|
|
|
|
|
|
|
|
str = str.replace(/^\s+|\s+$/g, '') // trim
|
|
|
|
str = str.toLowerCase()
|
|
|
|
|
|
|
|
// remove accents, swap ñ for n, etc
|
|
|
|
var from = "àáäâèéëêìíïîòóöôùúüûñçěščřžýúůďťň·/,:;"
|
|
|
|
var to = "aaaaeeeeiiiioooouuuuncescrzyuudtn-----"
|
|
|
|
|
|
|
|
for (var i = 0, l = from.length; i < l; i++) {
|
|
|
|
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i))
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:26:10 +02:00
|
|
|
str = str.replace('.', '-') // replace a dot by a dash
|
2022-05-04 01:52:34 +02:00
|
|
|
.replace(/[^a-z0-9 -_]/g, '') // remove invalid chars
|
|
|
|
.replace(/\s+/g, '-') // collapse whitespace and replace by a dash
|
|
|
|
.replace(/-+/g, '-') // collapse dashes
|
|
|
|
.replace(/\//g, '') // collapse all forward-slashes
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2022-03-17 15:28:31 +01:00
|
|
|
Vue.prototype.$copyToClipboard = (str, ctx) => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
if (!navigator.clipboard) {
|
2022-05-22 15:05:39 +02:00
|
|
|
navigator.clipboard.writeText(str).then(() => {
|
|
|
|
if (ctx) ctx.$toast.success('Copied to clipboard')
|
|
|
|
resolve(true)
|
|
|
|
}, (err) => {
|
|
|
|
console.error('Clipboard copy failed', str, err)
|
|
|
|
resolve(false)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
const el = document.createElement('textarea')
|
|
|
|
el.value = str
|
|
|
|
el.setAttribute('readonly', '')
|
|
|
|
el.style.position = 'absolute'
|
|
|
|
el.style.left = '-9999px'
|
|
|
|
document.body.appendChild(el)
|
|
|
|
el.select()
|
|
|
|
document.execCommand('copy')
|
|
|
|
document.body.removeChild(el)
|
|
|
|
|
|
|
|
if (ctx) ctx.$toast.success('Copied to clipboard')
|
2022-03-17 15:28:31 +01: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
|
|
|
|
|
2022-03-06 00:28:15 +01:00
|
|
|
Vue.prototype.$encodeUriPath = (path) => {
|
|
|
|
return path.replace(/\\/g, '/').replace(/%/g, '%25').replace(/#/g, '%23')
|
|
|
|
}
|
|
|
|
|
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-12-02 02:07:03 +01:00
|
|
|
app.$encode = encode
|
2021-12-18 03:52:11 +01:00
|
|
|
inject('isDev', process.env.NODE_ENV !== 'production')
|
2021-09-05 20:21:02 +02:00
|
|
|
}
|