This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
web/js/lib.main.js

198 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-05-08 21:31:47 +01:00
/* eslint-disable no-unused-vars */
/* All Page Init */
2023-05-23 10:56:40 +01:00
const version = '2023.5.3'
2023-02-09 20:29:07 +00:00
/* Feature Detectors */
/* Valid values for ${type}: localstorage, sessionstorage */
async function storageAvailable(type) { // Currently not used
try {
let storage = window[type]
let x = '__storage_test__'
storage.setItem(x, 'test')
storage.getItem(x)
storage.removeItem(x)
log(`lib.main.storageAvailable: ${type} is available`, 'INFO')
return true
} catch (err) {
log(`lib.main.storageAvailable: ${type} is not available`, 'ERR')
return false
}
}
async function versionDisplay() { // Outputs version string on to any page with a tag with id="ver_str"
localStorage.setItem('version', version)
document.getElementById('ver_str').textContent = version
return
2023-02-09 20:29:07 +00:00
}
/* Array Converter
Converts a string to a single item array */
async function makeArray(data) {
if (!Array.isArray(data)) {
var array = []
array.push(data)
return array
}
return data
2023-02-09 20:29:07 +00:00
}
/* Timeouts */
/* Usage: '' */
const delay = ms => new Promise(res => setTimeout(res, ms))
2023-02-09 20:29:07 +00:00
/* Log Helper */
/* Maintains backwards compatibility for previous
implementation of log helper */
async function log(msg, type) {
const mode = 'tst'
if (mode === 'prod' && type != 'ERR') {
return
}
var time = new Date().toISOString()
switch (type) {
case 'ERR':
console.error(`${time} - ${msg}`)
break
case 'WARN':
console.warn(`${time} - ${msg}`)
break
case 'INFO':
console.info(`${time} - ${msg}`)
break
default:
console.log(`${time} - ${msg}`)
break
}
}
2023-02-09 20:29:07 +00:00
/* Show/Hide - Menu Control */
async function sidebarOpen() {
document.getElementById('sidebar').style.width = '50%'
document.getElementById('sidebar_open_short').style.display = 'none'
document.getElementById('sidebar_close_short').style.display = 'block'
}
2023-02-09 20:29:07 +00:00
async function sidebarClose() {
document.getElementById('sidebar').style.width = '0%'
document.getElementById('sidebar_open_short').style.display = 'block'
document.getElementById('sidebar_close_short').style.display = 'none'
}
2023-02-09 20:29:07 +00:00
/* Loading Box Control */
async function hideLoading() {
document.getElementById('loading').style = 'display: none;'
2023-02-09 20:29:07 +00:00
}
/* DEPRECIATED: Alias for hideLoading() - Marked for removal*/
async function clearLoading() {
log('Depreciated function called - clearLoading() - Alias to hideLoading()', 'WARN')
hideLoading()
}
2023-02-09 20:29:07 +00:00
async function showLoading() {
document.getElementById('loading').style = 'display: block;'
2023-02-09 20:29:07 +00:00
}
async function setLoadingDesc(desc) {
document.getElementById('loading_desc').textContent = `${desc}`
2023-02-09 20:29:07 +00:00
}
/* Fetch User Settings */
async function getQuickLinks() {
var defaults =
['bri','lwh','srd','mtp','rda','cfn',
'sml','shh','pri','avn','sar','svb']
try {
if (localStorage.getItem('qlOpt')) {
var data = JSON.parse(localStorage.getItem('qlOpt'))
} else {
data = defaults
2023-02-09 20:29:07 +00:00
}
} catch (err) {
data = defaults
}
return data.sort()
2023-02-09 20:29:07 +00:00
}
/* Fetch a known query parameter from the pages URL */
async function getQuery(param) {
var params = new URLSearchParams(window.location.search)
var query = params.get(param)
if (query) {
return query
} else {
return 'false'
}
2023-02-09 20:29:07 +00:00
}
async function getApi(path,auth = false) {
let apiVer = 'v1'
let url = `${window.location.origin}/api/${apiVer}/${path}`
log(`getApi: Fetching from endpoint: ${url}, Auth=${auth}`)
2023-05-08 21:31:47 +01:00
let options
if (auth) {
let key = localStorage.getItem('uuid')
2023-05-08 21:31:47 +01:00
options = {
method: 'GET',
redirect: 'follow',
headers: {
'uuid': key
}
2023-02-09 20:29:07 +00:00
}
} else {
2023-05-08 21:31:47 +01:00
options = {
method: 'GET',
redirect: 'follow'
2023-02-09 20:29:07 +00:00
}
}
try {
var resp = await fetch(url, options)
var json = await resp.json()
log(`resp.status: ${resp.status}`)
log(`resp.json: ${json}`)
if (resp.status != 200) {
log(`lib.main: getApi: Response status: ${resp.status}`)
return resp.status
}
if (!resp.ok) {
log('lib.main: getApi: Fetch error')
return false
}
return json
} catch(err) {
log(`lib.main: getApi: Caught fetch error. Status: ${resp.status}`)
return resp.status
}
}
async function showHideAuthNotice() {
let uuid = localStorage.getItem('uuid')
if (uuid) {
document.getElementById('auth-required').style = 'display:none'
}
}
async function vibe(type) {
let canVibrate = 'vibrate' in navigator || 'mozVibrate' in navigator
if (canVibrate && !('vibrate' in navigator)){
navigator.vibrate = navigator.mozVibrate
}
switch (type) {
case 'err':
navigator.vibrate([300])
break
case 'ok':
navigator.vibrate([50,50,50])
break
default:
navigator.vibrate(30)
}
}
async function convertUnixLocal(unix) { // Convert unix time string to local
var jsTime = unix*1000
var dt = new Date(jsTime)
return dt.toLocaleString()
2023-02-09 20:29:07 +00:00
}