Last active
May 1, 2024 09:46
-
-
Save bitsmanent/ea24e1c26d2a56a1da06b87dec991071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* core chaining and context builder */ | |
(() => { | |
"use strict"; | |
const plugins = {}; | |
const ready = { | |
callbacks: [], | |
installed: false | |
}; | |
let oref = null; | |
function noref() { | |
const ref = window.$; | |
window.$ = oref; | |
return ref; | |
} | |
function onready(callback) { | |
if(callback) { | |
if(document.readyState != "loading") | |
return callback instanceof Function ? callback() : null; | |
if(!ready.installed) { | |
document.addEventListener("DOMContentLoaded", () => onready(null)); | |
ready.installed = true; | |
} | |
return ready.callbacks.push(callback); | |
} | |
while(ready.callbacks.length) | |
ready.callbacks.pop()(); | |
} | |
function plug(obj, chained = true) { | |
const target = chained ? plugins : this; | |
Object.assign(target, obj); | |
} | |
function query(arg, ctx = document) { | |
let nodes = []; | |
if(arg instanceof Function) | |
return onready(arg); | |
if(Array.isArray(arg)) | |
nodes = arg; | |
else if(!!arg) { | |
if(typeof arg == "string" && ctx.querySelectorAll) { | |
try { | |
nodes = [...ctx.querySelectorAll(arg)]; | |
} catch(e) { | |
console.log("query(): invalid selector", arg); | |
nodes = []; | |
} | |
} | |
else if(arg.nodeType || arg == window) | |
nodes = [arg]; | |
else | |
console.warn("query(): invalid argument"); | |
} | |
return Object.assign(nodes, plugins); | |
} | |
function init() { | |
const pub = Object.assign(query, {noref,plug}); | |
pub.fn = plugins; | |
window.Pixlib = pub; | |
oref = window.$; | |
window.$ = pub; | |
} | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment