iwmlib/lib/card/theme.js

44 lines
1.4 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-unused-vars */
/**
* The Theme class helps manage the card themes.
* Usually it overrides or enhances aspects of the card.js.
*
* @class Theme
*/
2019-07-16 09:27:48 +02:00
export default class Theme {
/**
* Loads a config file and parses it to JSON.
*
* @static
* @param {string} [path=null] - Optional: A path where the config file is located at (including the filename and extension).
* @returns {Promise} - Returns a promise, that returns the parsed json file when resolved.
* @memberof Theme
*/
static loadConfig(path = null) {
return new Promise((resolve, reject) => {
2019-07-18 12:26:39 +02:00
path = path ? path : './config.json'
let xhttp = new XMLHttpRequest()
2022-10-04 10:51:35 +02:00
xhttp.onreadystatechange = function () {
if (this.readyState == 4) {
if (this.status == 200 || Theme._isLocal()) {
try {
const json = JSON.parse(this.responseText)
resolve(json)
} catch (e) {
reject(e)
}
} else reject('Invalid request : ' + this.status)
}
}
xhttp.open('GET', path, true)
xhttp.send()
})
}
static _isLocal() {
2019-07-18 12:26:39 +02:00
return window.location.protocol == 'file:'
}
}