content-change
Version:
Observe and react to changes in distributed nodes in web components, Shadow DOM v0.
2 lines • 5.72 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("ContentChange",[],t):"object"==typeof exports?exports.ContentChange=t():e.ContentChange=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t.default=e,t}function o(e){if(Array.isArray(e)){for(var t=0,n=Array(e.length);t<e.length;t++)n[t]=e[t];return n}return Array.from(e)}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(t,"__esModule",{value:!0});var u=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();n(1),n(2),n(3),n(4);var d=n(5),s=r(d),a=new WeakMap,c=function(){function e(t){i(this,e),this.host=t,this.shadowRoot=this.host.shadowRoot,this.currentObservedElements=[],this.mutationObserver=null,this.setDistributedNodes(),this.observeHost()}return u(e,[{key:"setDistributedNodes",value:function(){var e=this;this.contentElements.forEach(function(t){var n=Array.from(t.getDistributedNodes());e.currentObservedElements.push({content:t,distributedNodes:n})})}},{key:"discoverAddedAndRemovedNodes",value:function(e){var t=this;this.contentElements.forEach(function(n){var r=Array.from(n.getDistributedNodes()),o=t.currentObservedElements.find(function(e){return e.content==n});if(!e||"added"==e){var i=r.filter(function(e){return!o.distributedNodes.includes(e)});i.length&&(o.distributedNodes=r,s.nodesAddedToContentEvent(n,i))}if(!e||"removed"==e){var u=o.distributedNodes.filter(function(e){return!r.includes(e)});u.length&&(o.distributedNodes=r,s.nodesRemovedFromContentEvent(n,u))}})}},{key:"observeHost",value:function(){var e=this;this.mutationObserver=new MutationObserver(function(t){t.forEach(function(t){"childList"===t.type&&(t.addedNodes.length&&e.discoverAddedAndRemovedNodes("added"),t.removedNodes.length&&e.discoverAddedAndRemovedNodes("removed")),t.target&&e.allCurrentDistributedNodes.some(function(e){return e.contains(t.target)})&&e.contentElements.forEach(function(e){var n=Array.from(e.getDistributedNodes()).some(function(e){return e.contains(t.target)});n&&s.contentChangeEvent(e,t)}),"attributes"===t.type&&t.target.parentNode===e.host&&e.discoverAddedAndRemovedNodes()})}),this.mutationObserver.observe(this.host,{childList:!0,attributes:!0,characterData:!0,subtree:!0})}},{key:"unwatch",value:function(){this.mutationObserver.disconnect()}},{key:"allCurrentDistributedNodes",get:function(){var e;return(e=[]).concat.apply(e,o(this.currentObservedElements.map(function(e){return e.distributedNodes})))}},{key:"contentElements",get:function(){return Array.from(this.shadowRoot.querySelectorAll("content"))}}]),e}();t.default={watch:function(e){a.set(e,new c(e))},unwatch:function(e){a.get(e).unwatch(),a.delete(e)}},e.exports=t.default},function(e,t){"use strict";!function(){function e(e,t){t=t||{bubbles:!1,cancelable:!1,detail:void 0};var n=document.createEvent("CustomEvent");return n.initCustomEvent(e,t.bubbles,t.cancelable,t.detail),n}return"function"!=typeof window.CustomEvent&&(e.prototype=window.Event.prototype,void(window.CustomEvent=e))}()},function(e,t){"use strict";Array.prototype.find||Object.defineProperty(Array.prototype,"find",{value:function e(t){if(null==this)throw new TypeError("Array.prototype.find called on null or undefined");if("function"!=typeof t)throw new TypeError("predicate must be a function");for(var e,n=Object(this),r=n.length>>>0,o=arguments[1],i=0;i<r;i++)if(e=n[i],t.call(o,e,i,n))return e}})},function(e,t){"use strict";Array.from||(Array.from=function(){var e=Object.prototype.toString,t=function(t){return"function"==typeof t||"[object Function]"===e.call(t)},n=function(e){var t=Number(e);return isNaN(t)?0:0!==t&&isFinite(t)?(t>0?1:-1)*Math.floor(Math.abs(t)):t},r=Math.pow(2,53)-1,o=function(e){var t=n(e);return Math.min(Math.max(t,0),r)};return function(e){var n=this,r=Object(e);if(null==e)throw new TypeError("Array.from requires an array-like object - not null or undefined");var i,u=arguments.length>1?arguments[1]:void 0;if("undefined"!=typeof u){if(!t(u))throw new TypeError("Array.from: when provided, the second argument must be a function");arguments.length>2&&(i=arguments[2])}for(var d,s=o(r.length),a=t(n)?Object(new n(s)):new Array(s),c=0;c<s;)d=r[c],u?a[c]="undefined"==typeof i?u(d,c):u.call(i,d,c):a[c]=d,c+=1;return a.length=s,a}}())},function(e,t){"use strict";Array.prototype.includes||(Array.prototype.includes=function(e){if(null==this)throw new TypeError("Array.prototype.includes called on null or undefined");var t=Object(this),n=parseInt(t.length,10)||0;if(0===n)return!1;var r,o=parseInt(arguments[1],10)||0;o>=0?r=o:(r=n+o,r<0&&(r=0));for(var i;r<n;){if(i=t[r],e===i||e!==e&&i!==i)return!0;r++}return!1})},function(e,t){"use strict";function n(e,t){var n=new CustomEvent("contentchange",{detail:t});e.dispatchEvent(n)}function r(e,t){n(e,{type:"mutation",mutation:t})}function o(e,t){n(e,{type:"nodesAdded",nodesAdded:t})}function i(e,t){n(e,{type:"nodesRemoved",nodesRemoved:t})}Object.defineProperty(t,"__esModule",{value:!0}),t.contentChangeEvent=r,t.nodesAddedToContentEvent=o,t.nodesRemovedFromContentEvent=i,t.default={}}])});
//# sourceMappingURL=content-change.min.js.map