arrive
Version:
arrive.js provides events to watch for DOM elements creation and removal. It makes use of Mutation Observers internally.
1 lines • 5.57 kB
JavaScript
var Arrive=((u,e,h)=>{var r,v,n,t,o,l,c;if(u.MutationObserver&&"undefined"!=typeof HTMLElement)return r=0,n=Element.prototype.matches||HTMLElement.prototype.webkitMatchesSelector||HTMLElement.prototype.mozMatchesSelector||HTMLElement.prototype.msMatchesSelector,v={matchesSelector:function(e,t){return e instanceof Element&&n.call(e,t)},addMethod:function(e,t,n){var r=e[t];e[t]=function(){return n.length==arguments.length?n.apply(this,arguments):"function"==typeof r?r.apply(this,arguments):void 0}},callCallbacks:function(e,t,n){if(e.length){t&&t.options.onceOnly&&(e=[e[0]],t.me.unbindEventWithSelectorAndCallback.call(t.target,t.selector,t.callback));for(var r,o=0;r=e[o];o++)r&&r.callback&&r.callback.call(r.elem,r.elem);t&&t.callback&&n&&n.addTimeoutHandler(t.target,t.selector,t.callback,t.options,t.data)}},checkChildNodesRecursively:function(e,t,n,r){for(var o,i=0;o=e[i];i++)n(o,t,r)&&r.push({callback:t.callback,elem:o}),0<o.childNodes.length&&v.checkChildNodesRecursively(o.childNodes,t,n,r)},mergeArrays:function(e,t){var n,r={};for(n in e)e.hasOwnProperty(n)&&(r[n]=e[n]);for(n in t)t.hasOwnProperty(n)&&(r[n]=t[n]);return r},toElementsArray:function(e){return e=void 0===e||"number"==typeof e.length&&e!==u?e:[e]}},i.prototype.addEvent=function(e,t,n,r,o){e={target:e,selector:t,options:n,callback:r,data:o,firedElems:[]};return this._beforeAdding&&this._beforeAdding(e),this._eventsBucket.push(e),e},i.prototype.removeEvent=function(e){for(var t,n=this._eventsBucket.length-1;t=this._eventsBucket[n];n--)e(t)&&(this._beforeRemoving&&this._beforeRemoving(t),t.data&&t.data.timeoutId&&clearTimeout(t.data.timeoutId),t=this._eventsBucket.splice(n,1))&&t.length&&(t[0].callback=null)},i.prototype.beforeAdding=function(e){this._beforeAdding=e},i.prototype.beforeRemoving=function(e){this._beforeRemoving=e},t=i,o=function(o,i){var l=new t,c=this,a={fireOnAttributesModification:!1};return l.beforeAdding(function(t){var e,n=t.target,r=(n!==u.document&&n!==u||(n=document.getElementsByTagName("html")[0]),e=new MutationObserver(function(e){i.call(this,e,t)}),o(t.options));e.observe(n,r),t.observer=e,t.me=c}),l.beforeRemoving(function(e){e.observer.disconnect()}),this.bindEvent=function(e,t,n){t=v.mergeArrays(a,t);for(var r=v.toElementsArray(this),o=0;o<r.length;o++){var i={};c.addTimeoutHandler(r[o],e,n,t,i),l.addEvent(r[o],e,t,n,i)}},this.unbindEvent=function(){var n=v.toElementsArray(this);l.removeEvent(function(e){for(var t=0;t<n.length;t++)if(this===h||e.target===n[t])return!0;return!1})},this.unbindEventWithSelectorOrCallback=function(n){var r=v.toElementsArray(this),o=n,e="function"==typeof n?function(e){for(var t=0;t<r.length;t++)if((this===h||e.target===r[t])&&e.callback===o)return!0;return!1}:function(e){for(var t=0;t<r.length;t++)if((this===h||e.target===r[t])&&e.selector===n)return!0;return!1};l.removeEvent(e)},this.unbindEventWithSelectorAndCallback=function(n,r){var o=v.toElementsArray(this);l.removeEvent(function(e){for(var t=0;t<o.length;t++)if((this===h||e.target===o[t])&&e.selector===n&&e.callback===r)return!0;return!1})},this.addTimeoutHandler=function(e,t,n,r,o){!r.timeout||r.timeout<=0||(o.timeoutId&&clearTimeout(o.timeoutId),o.timeoutId=setTimeout(()=>{c.unbindEventWithSelectorAndCallback.call(e,t,n),n.call(null,null)},r.timeout))},this},l=new function(){var d={fireOnAttributesModification:!1,onceOnly:!1,existing:!1,timeout:0};function i(e,t,n){return!(!v.matchesSelector(e,t.selector)||(e._id===h&&(e._id=r++),-1!=t.firedElems.indexOf(e._id))||(t.firedElems.push(e._id),0))}var f=(l=new o(function(e){var t={attributes:!1,childList:!0,subtree:!0};return e.fireOnAttributesModification&&(t.attributes=!0),t},function(e,o){e.forEach(function(e){var t=e.addedNodes,n=e.target,r=[];null!==t&&0<t.length?v.checkChildNodesRecursively(t,o,i,r):"attributes"===e.type&&i(n,o)&&r.push({callback:o.callback,elem:n}),v.callCallbacks(r,o,l)})})).bindEvent;return l.bindEvent=function(t,e,n){var r,o="object"==typeof e?v.mergeArrays(d,e):{...d},i="function"==typeof n?n:"function"==typeof e?e:h,l=v.toElementsArray(this);if(i||(o.onceOnly=!0),o.existing){for(var c=[],a=0;a<l.length;a++)for(var u=l[a].querySelectorAll(t),s=0;s<u.length;s++)c.push({callback:i,elem:u[s]});if(o.onceOnly&&c.length)return i?i.call(c[0].elem,c[0].elem):Promise.resolve(c[0].elem);setTimeout(v.callCallbacks,1,c)}if(!i)return r=this,new Promise(e=>f.call(r,t,o,e));f.call(this,t,o,i)},l},c=new function(){var i={onceOnly:!1,timeout:0};function r(e,t){return v.matchesSelector(e,t.selector)}var l=(c=new o(function(){return{childList:!0,subtree:!0}},function(e,n){e.forEach(function(e){var e=e.removedNodes,t=[];null!==e&&0<e.length&&v.checkChildNodesRecursively(e,n,r,t),v.callCallbacks(t,n,c)})})).bindEvent;return c.bindEvent=function(t,e,n){var r,o="object"==typeof e?v.mergeArrays(i,e):{...i},n="function"==typeof n?n:"function"==typeof e?e:h;if(!n)return o.onceOnly=!0,r=this,new Promise(e=>l.call(r,t,o,e));l.call(this,t,o,n)},c},e&&s(e.fn),s(HTMLElement.prototype),s(SVGElement.prototype),s(NodeList.prototype),s(HTMLCollection.prototype),s(HTMLDocument.prototype),s(Window.prototype),a(l,e={},"unbindAllArrive"),a(c,e,"unbindAllLeave"),e;function i(){this._eventsBucket=[],this._beforeAdding=null,this._beforeRemoving=null}function a(e,t,n){v.addMethod(t,n,e.unbindEvent),v.addMethod(t,n,e.unbindEventWithSelectorOrCallback),v.addMethod(t,n,e.unbindEventWithSelectorAndCallback)}function s(e){e.arrive=l.bindEvent,a(l,e,"unbindArrive"),e.leave=c.bindEvent,a(c,e,"unbindLeave")}})(window,"undefined"==typeof jQuery?null:jQuery,void 0);