rava
Version:
The Rava core library
1 lines • 2.33 kB
JavaScript
!function(){"use strict";var e={},t=new Map,n=Element.prototype.matches||Element.prototype.msMatchesSelector,a=NodeList.prototype.forEach||Array.prototype.forEach,o=String.prototype.startsWith||function(e,t){return t=t||0,this.substr(t,e.length)===e};e.bind=function(e,n){var o=t.get(e);o||(o=new Set,t.set(e,o)),o.add(n),a.call(document.querySelectorAll(e),function(t){f(t,e,n)})},e.find=function(e,t){return c(e,function(e){if(n.call(e,t))return e}),null},e.findAll=function(e,t){var a=[];return c(e,function(e){n.call(e,t)&&a.push(e)}),a},new MutationObserver(function(e){e.forEach(function(e){r(e.addedNodes,i),r(e.removedNodes,d)})}).observe(document,{attributes:!1,childList:!0,subtree:!0,characterData:!1});var r=function(e,t){a.call(e,function(e){e.nodeType==Node.ELEMENT_NODE&&c(e,t)})},c=function(e,t){var n=new Set;n.add(e),n.forEach(function(e){t(e);for(var a=0;a<e.children.length;a++)n.add(e.children[a]);n.delete(e)})},i=function(e){t.forEach(function(t,a){n.call(e,a)&&t.forEach(function(t){n.call(e,a)&&f(e,a,t)})})},d=function(e){e["x-rava"]&&e["x-rava"].forEach(function(t){v(e,"removed",t)})},f=function(e,t,n){if(!n.scoped||n.target.contains(e)){var a=n.data||{},o=e["x-rava"];o||(o=new Set,e["x-rava"]=o),o.has(n)?v(e,"added",n,a):(o.add(n),"function"==typeof a&&(a=a.call(e)),Object.getOwnPropertyNames(n).forEach(function(o){"events"===o&&u(e,n,t,a),"methods"===o&&l(e,n[o]),"callbacks"===o&&s(e,n,t,a)}),n.callbacks&&(v(e,"created",n,a),v(e,"added",n,a)))}},l=function(e,t){for(var n in t)e[n]=t[n].bind(e)},u=function(t,n,a,r){var c=n.events,i=n.target||t;for(var d in c){var f=c[d];if("function"==typeof f)t.addEventListener(d,p(f,i,r));else{var l={};l.scoped=o.call(d.trim(),":scope"),l.target=t,l.events=f,l.data=r;var u=d.replace(":scope",a).trim();e.bind(u,l)}}},s=function(t,n,a,r){var c=n.callbacks;for(var i in c){var d=c[i];if("object"==typeof d){var f={};f.scoped=o.call(i.trim(),":scope"),f.target=n.target||t,f.callbacks=d,f.data=r;var l=i.replace(":scope",a).trim();e.bind(l,f)}}},v=function(e,t,n,a){n.callbacks&&n.callbacks[t]&&n.callbacks[t].call(n.target||e,a,e)},p=function(e,t,n){return function(a){e.call(t,a,n)}};"function"==typeof define&&define.amd?define(e):"undefined"!=typeof module&&module.exports&&(module.exports={rava:e}),"undefined"!=typeof window&&(window.rava=e)}();