2021-09-01 20:07:11 -05:00
|
|
|
import Vue from 'vue'
|
2023-03-06 10:58:08 -06:00
|
|
|
import vClickOutside from 'v-click-outside'
|
2021-12-10 16:31:54 -06:00
|
|
|
import { App } from '@capacitor/app'
|
|
|
|
import { Dialog } from '@capacitor/dialog'
|
2023-06-04 14:59:55 -05:00
|
|
|
import { AbsFileSystem } from '@/plugins/capacitor'
|
2021-12-31 16:57:53 -06:00
|
|
|
import { StatusBar, Style } from '@capacitor/status-bar';
|
2022-08-03 22:17:11 -04:00
|
|
|
import { formatDistance, format, addDays, isDate } from 'date-fns'
|
2023-03-06 10:58:08 -06:00
|
|
|
import { Capacitor } from '@capacitor/core'
|
|
|
|
|
|
|
|
Vue.directive('click-outside', vClickOutside.directive)
|
2021-11-02 19:44:42 -05:00
|
|
|
|
2022-04-03 14:37:44 -05:00
|
|
|
if (Capacitor.getPlatform() != 'web') {
|
|
|
|
const setStatusBarStyleDark = async () => {
|
|
|
|
await StatusBar.setStyle({ style: Style.Dark })
|
|
|
|
}
|
|
|
|
setStatusBarStyleDark()
|
2021-12-31 16:57:53 -06:00
|
|
|
}
|
|
|
|
|
2021-09-01 20:07:11 -05:00
|
|
|
Vue.prototype.$isDev = process.env.NODE_ENV !== 'production'
|
|
|
|
|
2023-06-04 14:59:55 -05:00
|
|
|
Vue.prototype.$getAndroidSDKVersion = async () => {
|
|
|
|
if (Capacitor.getPlatform() !== 'android') return null
|
|
|
|
const data = await AbsFileSystem.getSDKVersion()
|
|
|
|
if (isNaN(data?.version)) return null
|
|
|
|
return Number(data.version)
|
|
|
|
}
|
|
|
|
|
2022-12-06 16:21:12 -06:00
|
|
|
Vue.prototype.$encodeUriPath = (path) => {
|
|
|
|
return path.replace(/\\/g, '/').replace(/%/g, '%25').replace(/#/g, '%23')
|
|
|
|
}
|
2021-11-02 19:44:42 -05: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)
|
|
|
|
}
|
2022-08-03 22:17:11 -04: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
|
|
|
|
return date
|
|
|
|
}
|
|
|
|
Vue.prototype.$addDaysToDate = (jsdate, daysToAdd) => {
|
|
|
|
var date = addDays(jsdate, daysToAdd)
|
|
|
|
if (!date || !isDate(date)) return null
|
|
|
|
return date
|
|
|
|
}
|
2021-09-01 20:07:11 -05:00
|
|
|
Vue.prototype.$bytesPretty = (bytes, decimals = 2) => {
|
2021-09-19 18:44:10 -05:00
|
|
|
if (isNaN(bytes) || bytes === null) return 'Invalid Bytes'
|
2021-09-01 20:07:11 -05:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2022-07-04 14:59:56 -05:00
|
|
|
Vue.prototype.$elapsedPretty = (seconds, useFullNames = false) => {
|
|
|
|
if (seconds < 60) {
|
|
|
|
return `${Math.floor(seconds)} sec${useFullNames ? 'onds' : ''}`
|
|
|
|
}
|
2021-09-04 12:31:00 -05:00
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
if (minutes < 70) {
|
2022-07-04 14:59:56 -05:00
|
|
|
return `${minutes} min${useFullNames ? `ute${minutes === 1 ? '' : 's'}` : ''}`
|
2021-09-04 12:31:00 -05:00
|
|
|
}
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
if (!minutes) {
|
2022-07-04 14:59:56 -05:00
|
|
|
return `${hours} ${useFullNames ? 'hours' : 'hr'}`
|
2021-09-04 12:31:00 -05:00
|
|
|
}
|
2022-07-04 14:59:56 -05:00
|
|
|
return `${hours} ${useFullNames ? `hour${hours === 1 ? '' : 's'}` : 'hr'} ${minutes} ${useFullNames ? `minute${minutes === 1 ? '' : 's'}` : 'min'}`
|
2021-09-04 12:31:00 -05:00
|
|
|
}
|
|
|
|
|
2022-08-04 18:23:09 -05:00
|
|
|
Vue.prototype.$elapsedPrettyExtended = (seconds, useDays = true) => {
|
|
|
|
if (isNaN(seconds) || seconds === null) return ''
|
|
|
|
seconds = Math.round(seconds)
|
|
|
|
|
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
seconds -= minutes * 60
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
minutes -= hours * 60
|
|
|
|
|
|
|
|
var days = 0
|
|
|
|
if (useDays || Math.floor(hours / 24) >= 100) {
|
|
|
|
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-01 20:07:11 -05:00
|
|
|
Vue.prototype.$secondsToTimestamp = (seconds) => {
|
2023-01-14 18:01:12 -06:00
|
|
|
let _seconds = seconds
|
|
|
|
let _minutes = Math.floor(seconds / 60)
|
2021-09-01 20:07:11 -05:00
|
|
|
_seconds -= _minutes * 60
|
2023-01-14 18:01:12 -06:00
|
|
|
let _hours = Math.floor(_minutes / 60)
|
2021-09-01 20:07:11 -05:00
|
|
|
_minutes -= _hours * 60
|
2021-10-28 09:37:31 -05:00
|
|
|
_seconds = Math.floor(_seconds)
|
2021-09-01 20:07:11 -05:00
|
|
|
if (!_hours) {
|
|
|
|
return `${_minutes}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
return `${_hours}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
|
2023-01-14 18:01:12 -06:00
|
|
|
Vue.prototype.$secondsToTimestampFull = (seconds) => {
|
2023-01-15 14:58:26 -06:00
|
|
|
let _seconds = Math.round(seconds)
|
2023-01-14 18:01:12 -06:00
|
|
|
let _minutes = Math.floor(seconds / 60)
|
|
|
|
_seconds -= _minutes * 60
|
|
|
|
let _hours = Math.floor(_minutes / 60)
|
|
|
|
_minutes -= _hours * 60
|
|
|
|
_seconds = Math.floor(_seconds)
|
|
|
|
return `${_hours.toString().padStart(2, '0')}:${_minutes.toString().padStart(2, '0')}:${_seconds.toString().padStart(2, '0')}`
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:54:56 -06:00
|
|
|
Vue.prototype.$sanitizeFilename = (input, colonReplacement = ' - ') => {
|
|
|
|
if (typeof input !== 'string') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Max is actually 255-260 for windows but this leaves padding incase ext wasnt put on yet
|
|
|
|
const MAX_FILENAME_LEN = 240
|
|
|
|
|
|
|
|
var replacement = ''
|
|
|
|
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
|
|
|
|
|
|
|
|
var sanitized = input
|
|
|
|
.replace(':', colonReplacement) // Replace first occurrence of a colon
|
|
|
|
.replace(illegalRe, replacement)
|
|
|
|
.replace(controlRe, replacement)
|
|
|
|
.replace(reservedRe, replacement)
|
|
|
|
.replace(lineBreaks, replacement)
|
|
|
|
.replace(windowsReservedRe, replacement)
|
|
|
|
.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
|
|
|
|
}
|
|
|
|
|
|
|
|
return sanitized
|
|
|
|
}
|
|
|
|
|
2021-10-17 20:20:00 -05: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 15:22:30 -05: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
|
|
|
|
|
2022-12-04 10:41:09 -06:00
|
|
|
Vue.prototype.$setOrientationLock = (orientationLockSetting) => {
|
|
|
|
if (orientationLockSetting == 'PORTRAIT') {
|
|
|
|
window.screen.orientation.lock('portrait')
|
|
|
|
} else if (orientationLockSetting == 'LANDSCAPE') {
|
|
|
|
window.screen.orientation.lock('landscape')
|
|
|
|
} else {
|
|
|
|
window.screen.orientation.unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 14:48:56 -06:00
|
|
|
export default ({ store, app }, inject) => {
|
2023-01-31 14:50:26 -06:00
|
|
|
const eventBus = new Vue()
|
|
|
|
inject('eventBus', eventBus)
|
2022-12-17 14:48:56 -06:00
|
|
|
|
2022-07-04 16:55:34 -05:00
|
|
|
// iOS Only
|
|
|
|
// backButton event does not work with iOS swipe navigation so use this workaround
|
|
|
|
if (app.router && Capacitor.getPlatform() === 'ios') {
|
|
|
|
app.router.beforeEach((to, from, next) => {
|
|
|
|
if (store.state.globals.isModalOpen) {
|
2023-01-31 14:50:26 -06:00
|
|
|
eventBus.$emit('close-modal')
|
2022-07-04 16:55:34 -05:00
|
|
|
}
|
|
|
|
if (store.state.playerIsFullscreen) {
|
2023-01-31 14:50:26 -06:00
|
|
|
eventBus.$emit('minimize-player')
|
2022-07-04 16:55:34 -05:00
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Android only
|
2022-07-04 15:42:59 -05:00
|
|
|
App.addListener('backButton', async ({ canGoBack }) => {
|
|
|
|
if (store.state.globals.isModalOpen) {
|
2023-01-31 14:50:26 -06:00
|
|
|
eventBus.$emit('close-modal')
|
2022-07-04 15:42:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (store.state.playerIsFullscreen) {
|
2023-01-31 14:50:26 -06:00
|
|
|
eventBus.$emit('minimize-player')
|
2022-07-04 15:42:59 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!canGoBack) {
|
|
|
|
const { value } = await Dialog.confirm({
|
|
|
|
title: 'Confirm',
|
|
|
|
message: `Did you want to exit the app?`,
|
|
|
|
})
|
|
|
|
if (value) {
|
|
|
|
App.exitApp()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
window.history.back()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-05 15:22:30 -05:00
|
|
|
export {
|
|
|
|
encode,
|
|
|
|
decode
|
2022-08-03 22:17:11 -04:00
|
|
|
}
|