// Welcome to Surreal 1.3.2
// Documentation: https://github.com/gnat/surreal
// Locality of Behavior (LoB): https://htmx.org/essays/locality-of-behaviour/
let surreal = (function () {
let $ = { // Convenience for internals.
$: this, // Convenience for internals.
plugins: [],
// Table of contents and convenient call chaining sugar. For a familiar "jQuery like" syntax. 🙂
// Check before adding new: https://youmightnotneedjquery.com/
sugar(e) {
if (!$.isNode(e) && !$.isNodeList(e)) { console.warn(`Surreal: Not a supported element / node / node list "${e}"`); return e }
if ($.isNodeList(e)) e.forEach(_ => { $.sugar(_) }) // Add Surreal to all nodes from any()
if (e.hasOwnProperty('hasSurreal')) return e // Surreal already added.
// General
e.run = (f) => { return $.run(e, f) }
e.remove = () => { return $.remove(e) }
// Classes and CSS.
e.classAdd = (name) => { return $.classAdd(e, name) }
e.class_add = e.add_class = e.addClass = e.classAdd // Alias
e.classRemove = (name) => { return $.classRemove(e, name) }
e.class_remove = e.remove_class = e.removeClass = e.classRemove // Alias
e.classToggle = (name, force) => { return $.classToggle(e, name, force) }
e.class_toggle = e.toggle_class = e.toggleClass = e.classToggle // Alias
e.styles = (value) => { return $.styles(e, value) }
// Events.
e.on = (name, f) => { return $.on(e, name, f) }
e.off = (name, f) => { return $.off(e, name, f) }
e.offAll = (name) => { return $.offAll(e, name) }
e.off_all = e.offAll // Alias
e.disable = () => { return $.disable(e) }
e.enable = () => { return $.enable(e) }
e.send = (name, detail) => { return $.send(e, name, detail) }
e.trigger = e.send // Alias
e.halt = (ev, keepBubbling, keepDefault) => { return $.halt(ev, keepBubbling, keepDefault) }
// Attributes.
e.attribute = (name, value) => { return $.attribute(e, name, value) }
e.attributes = e.attr = e.attribute // Alias
// Add all plugins.
$.plugins.forEach(function(func) { func(e) })
e.hasSurreal = 1
return e
},
// me() will return a single element or null. Selector not needed if used with inline
//
me(selector=null, start=document, warning=true) {
if (selector == null) return $.sugar(start.currentScript.parentElement) // Just local me() in
// Example:
const onloadAdd = addOnload = onload_add = add_onload = (f) => {
if (typeof window.onload === 'function') { // window.onload already is set, queue functions together (creates a call chain).
let onload_old = window.onload
window.onload = () => {
onload_old()
f()
}
return
}
window.onload = f // window.onload was not set yet.
}
console.log("Surreal: Added shortcuts.")