text: !function(e,t){if("function"==typeof define&&define.amd)define("robojs",["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={};t(n),e.robojs=n}}(this,function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var i=function(){function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(e,t,n){return t&&i(e.prototype,t),n&&i(e,n),e}}(),r=function(){function e(){u(this,e)}return i(e,[{key:"load",value:function(n){var i=this;return new Promise(function(e,t){return i.onComplete(n,e,t)})}},{key:"onComplete",value:function(){}}]),e}(),a=function(e){function t(){return u(this,t),o(this,(t.__proto__||Object.getPrototypeOf(t)).apply(this,arguments))}return s(t,r),i(t,[{key:"onComplete",value:function(e,t,n){window.require([e],t,n)}}]),t}(),t=function(e){function n(e){u(this,n);var t=o(this,(n.__proto__||Object.getPrototypeOf(n)).call(this));return t.fn=e,t}return s(n,r),i(n,[{key:"onComplete",value:function(e,t,n){this.fn(e,t,n)}}]),n}(),l="object"===("undefined"==typeof self?"undefined":n(self))&&self.self===self&&self||"object"===("undefined"==typeof global?"undefined":n(global))&&global.global===global&&global||window||self,h=function(){function e(){u(this,e),this.listeners_={}}return i(e,[{key:"addEventListener",value:function(e,t){this.listeners_[e]||(this.listeners_[e]=[]),this.listeners_[e].includes(t)||this.listeners_[e].push(t)}},{key:"removeEventListener",value:function(e,t){var n=this.listeners_[e];if(void 0!==n){for(var i,r=0;i=n[r];r++)if(i===t){n.splice(r,1);break}n.length||delete this.listeners_[e]}}},{key:"dispatchEvent",value:function(t){var e=this;t.__defineGetter__("target",function(){return e});var n=t.type,i=0,r=this.listeners_[n];return void 0===r||(r.concat().map(function(e){return e.handleEvent?e.handleEvent.bind(e):e}).forEach(function(e){i=!1===e(t)}),!i&&!t.defaultPrevented)}}]),e}(),c=l.EventTarget;try{new c}catch(e){c=h}var d=c,f=function(){function e(){u(this,e),this.listenerBoxes=[],this.listenersNeedCloning=!1}return i(e,[{key:"getNumListeners",value:function(){return this.listenerBoxes.length}},{key:"connect",value:function(e,t){this.registerListener(e,t,!1)}},{key:"connectOnce",value:function(e,t){this.registerListener(e,t,!0)}},{key:"disconnect",value:function(e,t){this.listenersNeedCloning&&(this.listenerBoxes=this.listenerBoxes.slice(),this.listenersNeedCloning=!1);for(var n=this.listenerBoxes.length;n--;)if(this.listenerBoxes[n].listener===e&&this.listenerBoxes[n].scope===t)return void this.listenerBoxes.splice(n,1)}},{key:"disconnectAll",value:function(){for(var e=this.listenerBoxes.length;e--;)this.disconnect(this.listenerBoxes[e].listener,this.listenerBoxes[e].scope)}},{key:"emit",value:function(){for(var i=this,e=arguments.length,r=Array(e),t=0;t<e;t++)r[t]=arguments[t];this.listenersNeedCloning=!0,this.listenerBoxes.forEach(function(e){var t=e.scope,n=e.listener;e.once&&i.disconnect(n,t),n.apply(t,r)}),this.listenersNeedCloning=!1}},{key:"registerListener",value:function(t,n,i){var e=this.listenerBoxes.filter(function(e){return e.listener===t&&e.scope===n});if(e.length){var r=e.find(function(e){return e.once&&!i}),o=e.find(function(e){return i&&!e.once});if(r)throw Error("You cannot addOnce() then try to add() the same listener without removing the relationship first.");if(o)throw Error("You cannot add() then addOnce() the same listener without removing the relationship first.")}else this.listenersNeedCloning&&(this.listenerBoxes=this.listenerBoxes.slice()),this.listenerBoxes.push({listener:t,scope:n,once:i})}}]),e}(),v=function(e){return null!=e&&0<=e.length&&"[object Array]"===Object.prototype.toString.call(e)};function y(e){var t;return!!e&&(!!(Array.isArray||v)(e)||"object"===(void 0===e?"undefined":n(e))&&(t=e,"[object String]"!==Object.prototype.toString.call(t)&&(1===e.nodeType?!!e.length:0===e.length||0<e.length&&(e.hasOwnProperty(0)&&e.hasOwnProperty(e.length-1)))))}function p(e){return Array.from(e).reduce(function(e,t){return y(t)&&(t=Array.from(t)),e.concat(Array.isArray(t)?p(t):t)},[])}var m=function(){function n(e,t){u(this,n),this.onAdded=new f,this.onRemoved=new f,this.root=e,this.handler=t,this.init()}return i(n,[{key:"init",value:function(){this.observer=new MutationObserver(this.handleMutations.bind(this)),this.observer.observe(this.root,{attributes:!1,childList:!0,characterData:!1,subtree:!0})}},{key:"handleMutations",value:function(e){var t=this;e.forEach(function(e){t.updateNodes(e.removedNodes,t.onRemoved),t.updateNodes(e.addedNodes,t.onAdded)})}},{key:"_parseNodes",value:function(e){return e=p(e=(e=p(e)).filter(function(e){return e.querySelectorAll}).map(this.handler.getAllElements.bind(this.handler)).filter(function(e){return 0<e.length})),e=e.filter(function(e,t,n){return n.indexOf(e)===t})}},{key:"updateNodes",value:function(e,t){0<(e=this._parseNodes(e)).length&&t.emit(e)}},{key:"dispose",value:function(){this.observer.disconnect(),this.onAdded.disconnectAll(),this.onRemoved.disconnectAll(),this.observer=null,this.onAdded=null,this.onRemoved=null}}]),n}(),g=function(e){return e},E=/[xy]/g,b=function(){function r(e){u(this,r);var t=e.definitions,n=e.dispatcher,i=void 0===n?new d:n;this.definitions=t,this.dispatcher=i}return i(r,[{key:"getDefinition",value:function(){}},{key:"inCache",value:function(){}},{key:"updateCache",value:function(){}},{key:"hasMediator",value:function(){}},{key:"create",value:function(){}},{key:"getAllElements",value:function(){}},{key:"destroy",value:function(){}},{key:"dispose",value:function(){}}]),r}(),_=function e(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},n=t.mediatorId,i=void 0===n?"":n,r=t.node,o=void 0===r?null:r,s=t.dispose,a=void 0===s?g:s;u(this,e),this.mediatorId=i,this.node=o,this.dispose=a},x=function(e){function r(e){u(this,r);var t=o(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,e));return t.MEDIATORS_CACHE=[],t}return s(r,b),i(r,[{key:"getDefinition",value:function(e){return this.definitions[e.getAttribute(this.selector)]}},{key:"inCache",value:function(t){return!!this.MEDIATORS_CACHE.find(function(e){return e.node===t})}},{key:"updateCache",value:function(e){return this.MEDIATORS_CACHE.push(e),this.MEDIATORS_CACHE}},{key:"hasMediator",value:function(e){return!!this.getDefinition(e)&&!this.inCache(e)}},{key:"create",value:function(e,t){var n="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(E,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)});e.setAttribute("mediatorid",n);var i=g;e.parentNode&&(i=t(e,this.dispatcher)||g);var r=new _({mediatorId:n,node:e,dispose:i});return this.updateCache(r),r}},{key:"getAllElements",value:function(e){var t=Array.from(e.querySelectorAll("["+this.selector+"]")).slice(0);return e.getAttribute(this.selector)&&t.unshift(e),t}},{key:"_destroy",value:function(e){for(var t=this.MEDIATORS_CACHE.length,n=0;n<t;n++){var i=this.MEDIATORS_CACHE[n];!i||i.node&&i.node!==e||(r.disposeMediator(i),this.MEDIATORS_CACHE[n]=null)}return this.MEDIATORS_CACHE.filter(function(e){return e})}},{key:"destroy",value:function(e){return this.MEDIATORS_CACHE=this._destroy(e),this.MEDIATORS_CACHE}},{key:"dispose",value:function(){this.MEDIATORS_CACHE.forEach(r.disposeMediator),this.MEDIATORS_CACHE=null,this.dispatcher=null}},{key:"selector",get:function(){return"data-mediator"}}],[{key:"disposeMediator",value:function(e){e.dispose(),e.node=null}}]),r}(),A=function(){function s(e){u(this,s);var t=e.definitions,n=e.loader,i=void 0===n?new a:n,r=e.root,o=void 0===r?document.body:r;this.definitions=t,this.loader=i,this.root=o,this.handler=e.handler||new x({definitions:t}),this.watcher=e.watcher||new m(o,this.handler),this.watcher.onAdded.connect(this.getMediators.bind(this)),this.watcher.onRemoved.connect(this.removeMediators.bind(this)),this.init()}return i(s,[{key:"init",value:function(){var e=[this.root].map(this.handler.getAllElements.bind(this.handler));this.promise=this.getMediators(e)}},{key:"getMediators",value:function(e){var n=this,t=(e=p(e)).filter(this.handler.hasMediator.bind(this.handler)).map(function(t){var e=n.handler.getDefinition(t);return n.loader.load(e).then(function(e){return n.handler.create(t,e)})});return Promise.all(t)}},{key:"removeMediators",value:function(e){e.forEach(this.handler.destroy.bind(this.handler))}},{key:"dispose",value:function(){this.watcher.dispose(),this.handler.dispose(),this.watcher=null,this.handler=null,this.definitions=null,this.loader=null,this.root=null,this.promise=null}}]),s}(),C=function(e){function n(e){u(this,n);var t=o(this,(n.__proto__||Object.getPrototypeOf(n)).call(this,e));return t.REGISTERED_ELEMENTS={},t}return s(n,b),i(n,[{key:"updateCache",value:function(e){return this.REGISTERED_ELEMENTS[e]=!0,this.REGISTERED_ELEMENTS}},{key:"inCache",value:function(e){return!!this.REGISTERED_ELEMENTS[e]}},{key:"getDefinition",value:function(e){return this.definitions[e.tagName.toLowerCase()]}},{key:"create",value:function(e,n){var t="",i=this.dispatcher;if(!this.inCache(e.tagName.toLowerCase())){if(!(t=e.tagName.toLowerCase()).match(/-/gim))throw Error("The name of a custom element must contain a dash (-). So <x-tags>, <my-element>, and <my-awesome-app> are all valid names, while <tabs> and <foo_bar> are not.");window.customElements.define(t,function(e){function t(){return u(this,t),o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,i))}return s(t,n),t}()),this.updateCache(t)}return new _}},{key:"hasMediator",value:function(e){var t=e.tagName.toLowerCase();return!!this.getDefinition(e)&&!this.inCache(t)}},{key:"getAllElements",value:function(e){var t=Array.from(e.querySelectorAll("*")).filter(function(e){return e.tagName.match(/-/gim)}),n=[];return e.tagName.match(/-/gim)&&(n=[e]),n.concat(t)}}]),n}();e.bootstrap=function(e){return new A(e)},e.Loader=r,e.AMDLoader=a,e.CustomLoader=t,e.EventTarget=d,e.Signal=f,e.DomWatcher=m,e.MediatorHandler=x,e.Robo=A,e.CustomElementHandler=C});