xdolla
Version:
193 lines • 6.42 kB
JavaScript
/*
XDOLLA - Client-side JavaScript Framework
By Xzlash - (c) Copyright 2018 All Rights Reserved.
Contact: xzl4sh <at-symbol> google mail dot com
v0.0.3
*/
window.xdolla = function xdolla(options = {
debug: true,
disableMessage: false
}) {
// +++++++++++++++++++++++++++++ element manipulation
class ElementHook {
constructor(element) {
this.element = element
this.__oldDisplay
}
addClass(...classNames) {
for (let i of classNames) this.element.classList.add(i)
}
removeClass(...classNames) {
for (let i of classNames) this.element.classList.remove(className)
}
hasClass(className) {
return this.element.classList.contains(className)
}
text(newText) {
if (typeof newText === 'undefined') return this.element.innerHTML
return this.element.innerHTML = newText
}
hide() {
this.__oldDisplay = this.element.style.display
this.element.style.display = 'none'
}
unhide() {
if (this.element.style.display !== 'none') {
return false
} else {
this.element.style.display = this.__oldDisplay
}
}
show() {
this.element.style.display = 'block'
}
}
let XDOLLA = function XDOLLA(sel) {
switch (sel.slice(0, 1)) {
case '#':
return [new ElementHook(window.document.getElementById(sel.slice(1)))]
case '.':
return Array.from(window.document.getElementsByClassName(sel.slice(1))).map(v => new ElementHook(v))
default:
return Array.from(window.document.getElementsByTagName(sel)).map(v => new ElementHook(v))
}
}
// +++++++++++++++++++++++++++ console
XDOLLA.console = {}
XDOLLA.console.debug = function debug(...args) {
if (options.debug) return console.log(`%c[DEBUG] ${args.join(' ')}`, 'color: blue; display: block;')
}
XDOLLA.console.style = function style(text, css = '') {
return css ? console.log(`%c${text}`, css) : console.log(text)
}
XDOLLA.console.announce = function announce(...args) {
return XDOLLA.console.style(`${args.join(' ')}`, 'background: yellow; color: blue; display: block; font-size: 36pt;')
}
// +++++++++++++++++++++++++++ request
XDOLLA.request = {}
XDOLLA.request.get = function get(uri, parsejson = false, customHeaders = {}) {
if (typeof parsejson === 'object') {
// they forgot to pass a bool for parsejson lol.
customHeaders = parsejson
parsejson = false
}
return new Promise((ful, rej) => {
let xhr = new XMLHttpRequest()
xhr.open('GET', uri, true)
for (let i in customHeaders) {
xhr.setRequestHeader(i, customHeaders[i])
}
xhr.send(null)
xhr.onload = function () {
if (xhr.status < 200 || xhr.status >= 300) {
return rej(xhr.status)
}
return parsejson ? ful(JSON.parse(xhr.responseText)) : ful(xhr.responseText)
}
xhr.onerror = function () {
return rej(xhr.status)
}
})
}
XDOLLA.request.getSync = function getSync(uri, parsejson = false, customHeaders = {}) {
if (typeof parsejson === 'object') {
// they forgot to pass a bool for parsejson lol.
customHeaders = parsejson
parsejson = false
}
let xhr = new XMLHttpRequest()
xhr.open('GET', uri, false)
for (let i in customHeaders) {
xhr.setRequestHeader(i, customHeaders[i])
}
xhr.send(null)
if (xhr.status >= 200 && xhr.status < 300) {
return parsejson ? JSON.parse(xhr.responseText) : xhr.responseText
} else {
throw new Error(`$.request error: server replied with ${xhr.status}, expected 2xx`)
}
}
XDOLLA.request.post = function post(uri, body, parsejson = false, customHeaders = {}) {
if (typeof parsejson === 'object') {
// they forgot to pass a bool for parsejson lol.
customHeaders = parsejson
parsejson = false
}
return new Promise((ful, rej) => {
let xhr = new XMLHttpRequest()
xhr.open('POST', uri, true)
xhr.setRequestHeader('Content-Type', 'application/json')
for (let i in customHeaders) {
xhr.setRequestHeader(i, customHeaders[i])
}
xhr.send(JSON.stringify(body))
xhr.onload = function () {
if (xhr.status < 200 || xhr.status >= 300) {
return rej(xhr.status)
}
return parsejson ? ful(JSON.parse(xhr.responseText)) : ful(xhr.responseText)
}
xhr.onerror = function () {
return rej(xhr.status)
}
})
}
XDOLLA.request.postSync = function postSync(uri, body, parsejson = false, customHeaders = {}) {
if (typeof parsejson === 'object') {
// they forgot to pass a bool for parsejson lol.
customHeaders = parsejson
parsejson = false
}
let xhr = new XMLHttpRequest()
xhr.open('POST', uri, false)
xhr.setRequestHeader('Content-Type', 'application/json')
for (let i in customHeaders) {
xhr.setRequestHeader(i, customHeaders[i])
}
xhr.send(JSON.stringify(body))
if (xhr.status >= 200 && xhr.status < 300) {
return parsejson ? JSON.parsejson(xhr.responseText) : xhr.responseText
} else {
throw new Error(`$.request error: server replied with ${xhr.status}, expected 2xx`)
}
}
// ++++++++++++++++++++++++++++ require
XDOLLA.require = async function require(uri) {
let module = {
exports: undefined
}
let data
try {
data = await XDOLLA.request.get(uri, false, {})
} catch (err) {
throw new Error(`MODULE_NOT_FOUND ${uri}`)
}
try {
eval(`(async function (module) {${data}})`)(module)
} catch (err) {
throw new Error(`Syntax Error in ${uri}`)
}
return module.exports
}
XDOLLA.requireSync = function require(uri) {
let module = {
exports: undefined
}
let data
try {
data = XDOLLA.request.getSync(uri, false, {})
} catch (err) {
throw new Error(`MODULE_NOT_FOUND ${uri}`)
}
try {
eval(`(function (module) {${data}})`)(module)
} catch (err) {
throw new Error(`Syntax Error in ${uri} - ${err}`)
}
return module.exports
}
// ++++++++++++++++++++++++++++ show small copyright thing in console
if (!options.disableMessage) XDOLLA.console.style('Powered by Xdolla JavaScript Framework < https://github.com/XDOLLA/XDOLLA >', 'color: purple; background-color: lightgreen; font-size: 11pt;')
// ++++++ END
return XDOLLA
}