-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.min.js
10 lines (9 loc) · 20.1 KB
/
block.min.js
1
2
3
4
5
6
7
8
9
10
/*
block.js v3.3
[http://github.anuv.me/block.js]
File: block.min.js (block.js minified)
Source: [https://github.com/anuvgupta/block.js]
License: MIT [https://github.com/anuvgupta/block.js/blob/v3/LICENSE.md]
Copyright: (c) 2020 Anuv Gupta
*/
var Block, Block = function () { var block, addblock, __addblock, element, events = {}, customEvents = {}, type = arguments[0], marking = arguments[1], parent, __parent, children = {}, __children = {}, keys = {}, blockdata = { count: 0 }, dataBindings = {}, mediaQueries = {}, resizeQuery; if (null != marking && null != marking && "function" == typeof marking) return Block.blocks[type] = {}, Block.blocks[type].create = arguments[1], null != arguments[2] && null != arguments[2] && "function" == typeof arguments[2] && (Block.blocks[type].load = arguments[2]), !0; var Is = Block.is, content; if (Is.unset(type) && (type = "block"), 1 == marking) marking = type; else if (Is.unset(marking) || 0 == marking) for (; marking = "_" + Math.floor(1e3 * Math.random() + 1).toString(), Is.obj(children[marking]);); return block = { block: !0, add: function () { var e = [].slice.call(arguments), t = null; return e = Is.set(e[0]) && !0 !== e[0].block ? (3 <= e.length && (t = e[2], e = e.slice(0, 2)), Block.apply(Block, e)) : (2 <= e.length && (t = e[1], e = e.slice(0, 1)), e[0]), (children[e.mark()] = e).parent(this), Is.obj(addblock) && addblock.block ? addblock.add(e, t) : Is.str(t) && Is.obj(children[t]) && children[t].block ? element.insertBefore(e.node(), children[t].node()) : element.appendChild(e.node()), this }, __add: function () { var e = [].slice.call(arguments), t = null; return e = Is.set(e[0]) && !0 !== e[0].block ? (3 <= e.length && (t = e[2], e = e.slice(0, 2)), Block.apply(Block, e)) : (2 <= e.length && (t = e[1], e = e.slice(0, 1)), e[0]), (__children[e.mark()] = e).__parent(this), Is.obj(__addblock) && __addblock.block ? addblock.__add(e, t) : Is.str(t) && Is.obj(__children[t]) && __children[t].block ? element.insertBefore(e.node(), __children[t].node()) : element.appendChild(e.node()), this }, setAdd: function (e) { return Is.obj(e) && e.block && (addblock = e), this }, removeAddBlock: function () { return addblock = null, this }, __setAdd: function (e) { return Is.obj(e) && e.block && (__addblock = e), this }, remove: function () { return $marking = arguments[0], Is.null($marking) && Is.undef($marking) && !Is.null(parent) && !Is.undef(parent) ? parent.remove(marking) : Is.str($marking) && !Is.null(children[$marking]) && ((Is.obj(addblock) && addblock.block ? addblock.node() : element).removeChild(children[$marking].node()), delete children[$marking]), this }, __remove: function () { return $marking = arguments[0], Is.null($marking) && Is.undef($marking) && !Is.null(__parent) && !Is.undef(__parent) ? __parent.__remove(marking) : Is.str($marking) && !Is.null(__children[$marking]) && ((Is.obj(__addblock) && __addblock.block ? __addblock.node() : element).removeChild(__children[$marking].node()), delete __children[$marking]), this }, empty: function () { for (var e in children) this.remove(e); return this }, __empty: function () { for (var e in children) this.remove(e); return this }, type: function () { var e = arguments[0]; return Is.str(e) ? (Is.set(Block.blocks[e]) && (type = e), this) : type }, class: function () { return Is.str(arguments[0]) ? (element.className += " " + arguments[0], this) : element.className }, id: function () { return Is.str(arguments[0]) ? (element.id = arguments[0], this) : element.id }, mark: function () { var e = arguments[0]; return Is.str(e) ? ("css" == e ? console.warn("[BLOCK] cannot mark as '" + e + "' (reserved)") : marking = e, this) : marking }, attribute: function (e) { var t = arguments[1]; return Is.str(t) ? (element.setAttribute(e, t), this) : element.getAttribute(e) }, css: function () { var e = arguments[0]; if (Is.obj(e)) for (var t in e) e.hasOwnProperty(t) && Is.str(e[t]) && (element.style[t] = e[t]); else { if (!Is.str(e)) { var n, r = {}, a = element.style; for (n in a) !a.hasOwnProperty(n) || "" == a[n] || !isNaN(parseFloat(n)) && isFinite(n) || (r[n] = a[n]); return r } var s = arguments[1]; if (!Is.str(s)) return element.style[e]; element.style[e] = s } return this }, key: function (e) { var t = arguments[1]; if (Is.unset(t)) { if (Is.str(e) || !Is.obj(e)) return Is.unset(keys[e]) ? null : keys[e]; for ($subkey in e) e.hasOwnProperty($subkey) && (keys[$subkey] = e[$subkey]) } else keys[e] = t; return this }, blockdata: function (e) { return Is.unset(blockdata[e]) ? null : blockdata[e] }, on: function (n, t) { var r, a, e, s, l, i = this; return Is.str(n) && (Is.func(t) ? (r = function (e) { e.detail; Is.unset(e.detail) ? t(e, i, {}) : t(e, i, e.detail) }, Is.str(arguments[2]) ? (e = n + "_" + (a = arguments[2]), Is.obj(events[n]) || (events[n] = {}), events[n][a] = function (e) { r(e) }, element.addEventListener(e, events[n][a], !1), events[e] = function (e) { var t = e.detail; Is.unset(e.detail) ? i.on(n, a, {}) : i.on(n, a, t) }, element.addEventListener(n, events[e], !1)) : element.addEventListener(n, r, !1)) : (e = Is.str(t) ? n + "_" + t : n, s = arguments[arguments.length - 1], !Is.unset(s) && Is.obj(s) || (s = {}), window.CustomEvent ? (l = new CustomEvent(e, { detail: s, bubbles: !0, cancelable: !0 }), element.dispatchEvent(l)) : document.createEvent ? ((l = document.createEvent("Event")).initEvent(e, !0, !0, s), element.dispatchEvent(l)) : document.createEventObject ? ((l = document.createEventObject()).eventType = e, l.detail = s, element.fireEvent("on" + e, l), console.warn("Event bubbling and data not fully supported")) : console.warn("Events not fully supported"))), this }, off: function (e, t) { var n = events[e + "_" + t], r = null; return Is.set(events[e]) && (r = events[e][t]), Is.func(n) && (events[e + "_" + t] = null, element.removeEventListener(e, n)), Is.func(r) && (events[e][t] = null, element.removeEventListener(e + "_" + t, r)), this }, child: function (e) { if (Is.str(e)) { if (!e.includes("/")) return Is.set(children[e]) ? children[e] : null; var t = e.substring(0, e.indexOf("/")); if (Is.set(children[t])) return children[t].child(e.substring(e.indexOf("/") + 1)) } return null }, children: function () { return children }, childCount: function () { var e, t = 0; for (e in children) t++; return t }, __child: function (e) { return Is.str(e) ? e.includes("/") ? __children[e.substring(0, e.indexOf("/"))].__child(e.substring(e.indexOf("/") + 1)) : Is.set(__children[e]) ? __children[e] : null : null }, __children: function () { return __children }, __childCount: function () { var e, t = 0; for (e in __children) t++; return t }, sibling: function (e) { return null != parent && null != parent ? parent.child(e) : null }, siblings: function () { return null != parent && null != parent ? parent.children() : null }, siblingCount: function () { return null != parent && null != parent ? parent.childCount() : null }, __sibling: function (e) { return null != __parent && null != __parent ? __parent.__child(e) : null }, __siblings: function () { return null != __parent && null != __parent ? __parent.__children() : null }, __siblingCount: function () { return null != __parent && null != __parent ? __parent.__childCount() : null }, parent: function () { var e = arguments[0]; return Is.obj(e) && e.block && Is.unset(parent) ? (parent = e, this) : Is.int(e) && Is.set(e) ? 0 == e ? parent : parent.parent(e - 1) : Is.unset(parent) ? null : parent }, __parent: function () { var e = arguments[0]; return Is.obj(e) && e.block && Is.unset(__parent) ? (__parent = e, this) : Is.int(e) && Is.set(__parent) ? 0 == e ? __parent : __parent.__parent(e - 1) : Is.unset(__parent) ? null : __parent }, html: function () { var e = arguments[0], t = arguments[1]; return Is.str(e) ? (Is.set(t) && !0 === t ? element.innerHTML += e : element.innerHTML = e, this) : element.innerHTML }, node: function () { return element }, fill: function (e) { return Is.obj(e) && e.block ? (e.empty(), e = e.node()) : Is.str(e) && (e = document.querySelector(e)), Is.elem(e) && (e.innerHTML = "", e.appendChild(element)), this }, jQuery: function () { if (Block.jQuery) { var e = this; return jQuery.extend(jQuery(element), { block: function () { return e } }) } return console.warn("jQuery not detected"), null }, $: function () { return this.jQuery() }, query: function (e, t) { if (Is.str(e)) { if (Is.func(t)) r = t.toString().match(/function[^{]+\{([\s\S]*)\}$/)[1]; else { if (!Is.str(t)) return this; r = t } var n = e, t = n.substring(n.search(/ /) + 1), e = t.substring(t.search(/ /) + 1), n = n.substring(0, n.search(/ /)).trim(), t = t.substring(0, t.search(/ /)).trim(); "window" == n && ("height" == t ? t = "innerHeight" : "width" == t && (t = "innerWidth")); var r = "if (" + n + "." + t + " " + e.trim() + ") { " + r + " }"; Is.obj(mediaQueries[n]) ? Is.arr(mediaQueries[n][t]) || (mediaQueries[n][t] = []) : (mediaQueries[n] = {}, mediaQueries[n][t] = []), mediaQueries[n][t].unshift(r) } else resizeQuery(new CustomEvent(marking + "_query_" + Math.floor(Date.now() / 1e3).toString())); return this }, data: function ($blockdata) { var $data = {}, $style = {}, $reservedAttributes = [], $iterableKeys, $iterableKeys, $getData, $getStyle, $property, $key, $key; if (Is.obj($blockdata)) { $reservedAttributes.push("__keys"), $iterableKeys = Is.arr($blockdata.__keys) ? $blockdata.__keys : Object.keys($blockdata); for (var $j = 0; $j < $iterableKeys.length; $j++) { var $key = $iterableKeys[$j], $midspace, $dataToLoad, $eventTypes, $eventCallback, $callbackJS, $query, $type, $initJS, $callbacks, $dataJS, $name, callbackJS, $childtype, $childmarking, $childblock; $blockdata.hasOwnProperty($key) && ($midspace = $key.search(/ /), "css" == $key ? ($style = $blockdata.css, $reservedAttributes.push("css")) : "html" == $key ? (this.html($blockdata[$key]), $reservedAttributes.push($key)) : "__js" == $key ? (eval("/* block.js auto-generated JS clause\n (temporary event callback)\n block type = " + type + "\n block marking = " + marking + "\n*/\nvar $callback = function (event, block, data) {\n\n" + $blockdata.__js + "\n\n};"), this.on("__temp_event", $callback, "__rand"), this.on("__temp_event", "__rand"), this.off("__temp_event", "__rand"), $reservedAttributes.push("__js")) : ":" == $key.substring(0, 1) ? ($reservedAttributes.push($key), $dataToLoad = $blockdata[$key], $eventTypes = $key.substring(1), $eventCallback = "", Is.str($dataToLoad.__js) && ($eventCallback += $dataToLoad.__js, delete $dataToLoad.__js), Is.set($dataToLoad) && ($dataToLoad = JSON.stringify($dataToLoad), Is.str(arguments[1]) ? $eventCallback += " block.data(" + $dataToLoad + ', "' + arguments[1] + '");' : $eventCallback += " block.data(" + $dataToLoad + ");"), eval("/* block.js auto-generated event listener\n (event listener callback)\n block type = " + type + "\n block marking = " + marking + "\n events = " + $eventTypes + "\n*/\nvar $callback = function (event, block, data) {\n\n" + $eventCallback + "\n\n};"), $eventTypes = $eventTypes.split(","), $eventTypes.forEach(function (e) { this.on(e.trim(), $callback) }, this)) : "@" == $key.substring(0, 1) ? ($reservedAttributes.push($key), 6 < $key.length && "query" == $key.substring(1, 6) && ($dataToLoad = $blockdata[$key], $callbackJS = "", Is.str($dataToLoad.__js) && ($callbackJS += $dataToLoad.__js, delete $dataToLoad.__js), Is.set($dataToLoad) && ($dataToLoad = JSON.stringify($dataToLoad), Is.str(arguments[1]) ? $callbackJS += " block.data(" + $dataToLoad + ', "' + arguments[1] + '");' : $callbackJS += " block.data(" + $dataToLoad + ");"), $query = $key.substring(7), this.query($query, $callbackJS))) : "!" == $key.substring(0, 1) ? ($reservedAttributes.push($key), $type = $key.substring(1), $initJS = "", $callbacks = $blockdata[$key], Is.obj($callbacks.init) && Is.str($callbacks.init.__js) && ($initJS = $callbacks.init.__js), eval("/* block.js auto-generated custom block initialization\n (initialization callback)\n custom block type = " + $type + "\n*/\nvar $initCallback = function () {\n\n" + $initJS + "\n\n};"), $dataJS = "", Is.obj($callbacks.load) && Is.str($callbacks.load.__js) && ($dataJS = $callbacks.load.__js), eval("/* block.js auto-generated custom block data loading\n (data loading callback)\n custom block type = " + $type + "\n*/\nvar $dataCallback = function (block, data, style) {\n\n" + $dataJS + "\n\n};"), Block($type, $initCallback, $dataCallback)) : "#" == $key.substring(0, 1) ? ($reservedAttributes.push($key), $name = $key.substring(1), callbackJS = "", Is.obj($blockdata[$key]) && Is.str($blockdata[$key].__js) && ($callbackJS = $blockdata[$key].__js), eval('/* block.js auto-generated data binding\n ("on-the-fly" data binding callback)\n block type = ' + type + "\n block marking = " + marking + "\n data name = " + $name + "\n*/\nvar $callback = function (" + $name + ") {\n" + $callbackJS + "\n};"), this.bind($name, $callback)) : "$" == $key.substring(0, 1) ? ($reservedAttributes.push($key), this.key($key.substring(1), $blockdata[$key])) : 0 < $midspace ? ($childtype = $key.substring(0, $midspace), $childmarking = $key.substring($midspace + 1), $childblock = Block($childtype, $childmarking), Is.obj($blockdata[$key]) && (Is.str(arguments[1]) ? $childblock.data($blockdata[$key], arguments[1]) : $childblock.data($blockdata[$key]), $reservedAttributes.push($key)), this.add($childblock, Is.set($iterableKeys[$j + 1]) ? $iterableKeys[$j + 1] : null), $reservedAttributes.push($key)) : Is.obj($blockdata[$key]) && Is.obj(children[$key]) ? (Is.str(arguments[1]) ? children[$key].data($blockdata[$key], arguments[1]) : children[$key].data($blockdata[$key]), $reservedAttributes.push($key)) : (Is.obj($blockdata[$key]) && $reservedAttributes.push($key), $data[$key] = $blockdata[$key])) } } else if (Is.str($blockdata) || Is.int($blockdata)) $data = { val: $blockdata }; else { if (!Is.null($blockdata) && Is.obj($blockdata)) return this; $data = {} } for ($property in "block" != type && Is.set(Block.blocks[type]) && ($getData = function (e, t) { e = "this" == e ? $data : Is.unset($data[e]) ? null : ($reservedAttributes.push(e), $data[e]); return Is.func(t) && Is.set(e) && t(e), e }, $getStyle = function (e, t) { e = "this" == e ? $style : Is.unset($style[e]) ? null : $style[e]; return Is.func(t, "function") && Is.set(e) && t(e), e }, Block.blocks[type].load(this, $getData, $getStyle)), $style) $style.hasOwnProperty($property) && (element.style[$property] = $style[$property]); for ($key in dataBindings) dataBindings.hasOwnProperty($key) && $data.hasOwnProperty($key) && ($reservedAttributes.push($key), dataBindings[$key]($data[$key], this)); for ($key in $data) $data.hasOwnProperty($key) && !Block.inArr($key, $reservedAttributes) && element.setAttribute($key, $data[$key]); return blockdata.count++, blockdata[Is.str(arguments[1]) ? arguments[1] : "#" + blockdata.count] = { data: $data, css: $style }, this }, bind: function (e, t) { return Is.str(e) && Is.func(t) && (dataBindings[e] = t), this }, parse: function (e, t) { var n = (t = t.replace(/\r\n|\r|\n/g, "\n")).substring(0, t.indexOf("*")), r = Block.parse(t.substring(t.indexOf("*") + 2), n); if (!0 === arguments[arguments.length - 1]) return r; var a, s = {}; for (a in r) r.hasOwnProperty(a) && a != marking && (s[a] = r[a]); return Block("u").data(s), Is.str(arguments[2]) ? this.data(r[marking], arguments[2]) : this.data(r[marking]), Is.func(e) && e(this), this }, load: function (t, n) { function r(e, t) { a.parse.apply(a, [e, t, n]) } var e, a = this, s = arguments[2], l = arguments[3]; return null != l && null != l || (l = !0), Is.str(s) && "jquery" === s.toLowerCase() ? Block.jQuery ? jQuery.ajax({ url: n + ".block", type: "GET", async: l, success: function (e) { r(t, e) } }) : console.warn("jQuery not detected") : !0 === s ? (window.XMLHttpRequest ? e = new XMLHttpRequest : window.ActiveXObject ? e = new ActiveXObject("Microsoft.XMLHTTP") : console.warn("XHR (XMLHttpRequest) not supported"), e.onreadystatechange = function () { 4 == e.readyState && 200 == e.status && r(t, e.responseText) }, e.open("GET", n + ".block", l), e.send()) : r(t, customBlockData[n]), this } }, "block" == type ? (element = Block.node("div"), block.css({ width: "100%", height: "100%", display: "table", textAlign: "center" }), content = Block("div").mark("content").css({ display: "table-cell", textAlign: "center", verticalAlign: "middle", margin: "0 auto" }), block.setAdd(content).__add(content)) : null != Block.blocks[type] ? block = Block.blocks[type].create().type(type).mark(marking) : element = Block.node(type), block.attribute("block", marking), resizeQuery = function ($e) { var $callback = "", $objectName; for ($objectName in mediaQueries) if (mediaQueries.hasOwnProperty($objectName)) { var $object = mediaQueries[$objectName], $propertyName; for ($propertyName in $object) if ($object.hasOwnProperty($propertyName)) { var $property = $object[$propertyName], $i; for ($i in $property) $callback += 0 == $i ? " " + $property[$i] : " else " + $property[$i] } } eval("/* block.js auto-generated media query\n (window resize event callback)\n block type = " + type + "\n block marking = " + marking + "\n*/\n$callback = function (event, block, data) {\n\n" + $callback + "\n\n};"), Is.unset($e.detail) ? $callback($e, block, {}) : $callback($e, block, $e.detail) }, window.addEventListener("blockjs_query", resizeQuery), block }; Block.is = { null: function (e) { return null == e }, eqnull: function (e) { return null === e }, undef: function (e) { return null == e }, set: function (e) { return null != e && null !== e }, unset: function (e) { return null == e || null === e }, str: function (e) { return null !== e && null != e && ("string" == typeof e || e instanceof String) }, func: function (e) { return null !== e && null != e && ("function" == typeof e || e instanceof Function) }, node: function (e) { return null !== e && null != e && ("object" == typeof e || e instanceof Object) && e instanceof Node }, elem: function (e) { return null !== e && null != e && ("object" == typeof e || e instanceof Object) && e instanceof Node && e instanceof Element }, arr: function (e) { return null !== e && null != e && ("array" == typeof e || e instanceof Array) }, obj: function (e) { return null !== e && null != e && ("object" == typeof e || e instanceof Object) }, int: function (e) { return null !== e && null != e && e === parseInt(e, 10) && !isNaN(e) }, type: function (e, t) { return typeof e == t } }, Block.node = function (e) { return document.createElement(e) }, Block.inArr = function (e, t) { return -1 < t.indexOf(e) }, Block.set = function (e, t, n) { return 1 == t.length ? (e[t[0]] = n, e) : 1 < t.length ? Block.set(e[t[0]], t.slice(1), n) : void 0 }, Block.get = function (e, t) { return 1 == t.length ? e[t[0]] : 1 < t.length ? Block.get(e[t[0]], t.slice(1)) : void 0 }, Block.parse = function (e, t) { for (var n = {}, r = [], a = e.split("\n"), s = 0; s < a.length; s++) { var l = (i = a[s]).search(/\S/); if (-1 != l && "//" != i.substring(l, l + 2)) { var i, o = ((i = i.substring(0, l) + i.substring(l).trim()).match(new RegExp(t, "g")) || []).length, c = (i = i.trim()).search(/ /); if (-1 == c || ":" == i.substring(0, 1) || "@" == i.substring(0, 1)) { var u, d = {}; if ("{" == (u = i)) { for (var k = "", $ = s + 1; $ < a.length; $++) { var b = a[$], y = (b.search(/\S/), (b.match(new RegExp(t, "g")) || []).length); if ("}" == (b = b.trim()) && y == o) break; k += b + "\n " } s = $, u = "__js", d = k } else if ("`" == u) { for (var p = "", h = s + 1; h < a.length; h++) { var f = a[h], m = (f.search(/\S/), (f.match(new RegExp(t, "g")) || []).length); if ("`" == (f = f.trim()) && m == o) break; p += f + "\n " } s = h, u = "__str", d = p } } else d = Block.is.str(a[s + 1]) && l < a[s + 1].search(/\S/) ? (u = i, {}) : (u = i.substring(0, c), i.substring(c + 1)); r = r.slice(0, o), "__str" != u && (c = r.concat(["__keys"]), Block.is.arr(Block.get(n, c)) ? Block.get(n, c).push(u) : Block.set(n, c, [u]), r.push(u)), Block.set(n, r, d) } } return n }, Block.jQuery = !1; try { Block.jQuery = "function" == typeof jQuery || "function" == typeof window.jQuery } catch (error) { Block.jQuery = !1 } Block.blocks = {}, Block.queries = function (e) { "on" === e ? window.addEventListener("resize", Block.queryListener) : "off" === e ? window.removeEventListener("resize", Block.queryListener) : window.dispatchEvent(new CustomEvent("blockjs_query")) }, Block.queryListener = function () { Block.queries() }, Block.queries("on");