text: !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("wafer-fetch",[],t):"object"==typeof exports?exports["wafer-fetch"]=t():(e.wafer=e.wafer||{},e.wafer.wafers=e.wafer.wafers||{},e.wafer.wafers["wafer-fetch"]=t())}("undefined"!=typeof self?self:this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var i=r[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,t),i.l=!0,i.exports}var r={};return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="https://s.yimg.com/aaq/wf/",t(t.s="./src/entry.js")}({"./src/entry.js":function(e,t,r){"use strict";function n(e,t){var r={};for(var n in e)t.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);return r}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}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 a(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 s(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(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 u(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)}Object.defineProperty(t,"__esModule",{value:!0});var l=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},f=function(){function e(e,t){var r=[],n=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done)&&(r.push(a.value),!t||r.length!==t);n=!0);}catch(e){i=!0,o=e}finally{try{!n&&s.return&&s.return()}finally{if(i)throw o}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),h=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),d=function e(t,r,n){null===t&&(t=Function.prototype);var i=Object.getOwnPropertyDescriptor(t,r);if(void 0===i){var o=Object.getPrototypeOf(t);return null===o?void 0:e(o,r,n)}if("value"in i)return i.value;var a=i.get;if(void 0!==a)return a.call(n)},y=window.wafer,v=y.base,p=y.constants,g=y.utils,m=y.WaferBaseClass,b=p.ATTR_PREFIX,_=g.convertNodeListToArray,w=g.fetchWithCache,C=g.findAncestor,A=g.getConfigFromJSONScriptNode,T=g.loadScriptAsync,k=["handleFetchClick","handleMouseenter"],O=["body","cacheKey","target","url"],j=function(e){function t(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=r.errorClass,a=r.selector,s=r.successClass;i(this,t);var c=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e,{errorClass:n,selector:a,successClass:s},{STATE_ATTRS:O})),u=c._util.url||e.getAttribute(b+"url"),h=e.getAttribute(b+"boundary"),d=e.getAttribute(b+"cache"),y=c._util.cacheKey||e.getAttribute(b+"cache-key"),p=e.getAttribute(b+"cache-strategy")||"networkFirst",g=e.getAttribute(b+"cache-ttl"),m=e.getAttribute(b+"delay"),w=e.getAttribute(b+"replace"),T=e.getAttribute(b+"result-selector"),j=e.getAttribute(b+"dependency"),S=j&&document.querySelector(j),E=e.getAttribute(b+"silent"),L=A(e.getElementsByClassName("wafer-fetch-config")[0]),P=e.getAttribute(b+"target"),I=e.getAttribute(b+"timeout"),x=e.getAttribute(b+"type")||"default",N=(e.getAttribute(b+"trigger-offset")||"").split(" "),M=f(N,2),R=M[0],W=M[1],F=e.getAttribute(b+"trigger")||"viewport",D=c._util.body||e.getAttribute(b+"body"),q=e.getAttribute(b+"retry-count"),H=e.getAttribute(b+"session-retry-count"),X=!1,B=e.getAttribute(b+"set-focus");if(B=null===B||void 0===B?0:Number(B),q=null===q||void 0===q?null:Number(q),H=null===H||void 0===H?null:Number(H),v.isIORetrySupported?H&&(H=Math.min(H,3)):("interval"===F&&(F="delay"),H=null),!q)switch(F){case"interval":case"onLoad":case"stateChange":case"viewport":q=2}if("activate"!==F&&"eachClick"!==F&&"interval"!==F&&"stateChange"!==F&&"tabActivate"!==F||(X=!0),!D){var K=_(e.getElementsByClassName("fetch-body")),Y=f(K,1),V=Y[0],z=void 0===V?null:V;z&&(D=z.innerHTML)}var U=P&&(h&&C(e,h)||document.body).querySelector(P)||e;return U&&B&&U.setAttribute("tabIndex","-1"),c._util=l({},c._util,{body:D,cache:null===d||void 0===d?1:Number(d),cacheKey:y,cacheStrategy:p,cacheTtl:null===g||void 0===g?60:Number(g),config:L,delay:null===m||void 0===m?1e4:Number(m),dependencyElem:S,timeout:null===I||void 0===I?6e3:Number(I),elem:e,errorClass:n,isPostWithState:e.getAttribute(b+"[body]")||e.getAttribute(b+"state-body"),offsetX:Number(W)||0,offsetY:Number(R)||0,replace:null===w||void 0===w?0:Number(w),resultSelector:T,retryCount:q,selector:a,sessionRetryCount:H,setFocus:B,shouldAlwaysTrigger:X,targetElem:U,trigger:F,type:x,url:u,silent:null===E||void 0===E?0:Number(E),successClass:s}),c._state={retries:0,status:status},k.forEach(function(e){c[e]=c[e].bind(c)}),"eachClick"===F||"click"===F?c._util.hasClick=!0:"mouseenter"===F&&(c._util.hasMouseenter=!0,c.addEventListeners()),c}return a(t,e),h(t,[{key:"fetchDependency",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,r=this._util.dependencyElem;return new Promise(function(n){if(!r)return void n();setTimeout(function(){var i=void 0;if(window.wafer.base._wafers.some(function(e){var t=e._options;return!(!t||"wafer-fetch"!==t.selector)&&(i=e,!0)}),!i)return n();var o=i._state.elementInstances.get(r)||{},a=o.instance;if(!a)return n();var s=a._state.status;if(s)return n();t<5&&0===s&&setTimeout(function(){e.fetchDependency(t++).then(function(){n()})},100*(t+1)),s||i.trigger(r).then(function(){e._util.dependencyElem=null,setTimeout(function(){n()},100)}).catch(function(){e._util.status=void 0})},0)})}},{key:"fetch",value:function(){var e=this,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=r.disable,o=void 0===i||i,a=r.currentSessionRetryCount,s=void 0===a?0:a,c=this._util,u=c.elem,l=c.shouldAlwaysTrigger,f=c.successClass;if(!l&&u.classList.contains(f))return Promise.resolve(!0);var h=this._util,d=h.errorClass,y=h.replace,p=h.resultSelector,g=h.selector,m=h.sessionRetryCount,b=h.setFocus,_=h.silent,C=h.targetElem,A=h.timeout,T=h.type,k=void 0;if(0!==this._state.status){var O=this._state.retries,j={};return u.classList.add("wafer-fetch-trigger-inprogress"),C.classList.add("wafer-fetch-target-inprogress"),O&&(j.retries=O),this._state.status=0,this.fetchDependency().then(function(){k=e._util.url;var t=e._util,r=t.body;if(t.isPostWithState&&!r)throw new Error("Post without body");var n=e._util,i=n.cache,o=n.cacheKey,a=n.cacheStrategy,s=n.cacheTtl,c=n.config,u=c.headers||{};return w(k,{body:r,cache:i,cacheKey:o,cacheStrategy:a,cacheTtl:s,clientHeaders:u,paramsToNotCache:j,timeout:A})}).then(function(r){var i=T.length?T[0].toUpperCase()+T.slice(1):"",a=e["handle"+i],s=r._fetchMeta,c=void 0===s?{}:s,h=n(r,["_fetchMeta"]),m=c.duration,w=c.source;if(o&&(l||u.classList.remove(g.replace(".","")),u.classList.remove(d),u.classList.remove("wafer-fetch-trigger-inprogress"),u.classList.add(f),C.classList.remove("wafer-fetch-target-inprogress")),_)e._state.status=1;else{v.destroy(C,{destroySelf:!l});var A=void 0,O=r.html,j=void 0===O?r.markup:O;if(p){var S=document.createElement("div");S.innerHTML=j;var E=S.querySelector(p);j=E&&E.innerHTML||j}if(y){var L=C.parentNode;C.insertAdjacentHTML("beforebegin",j),L.removeChild(C),A=L}else C.innerHTML=j,A=C;b&&C&&setTimeout(function(){return C.focus()},5),a&&(a.call(e,A),window.wafer.___debugContent&&window.wafer.___debugContent(r.html||r.markup))}return e._state.retries=0,v.emitLog({name:t.constructor.name,elem:u,meta:{duration:m,source:w,url:k}}),v.emitWaferEvent("fetch:success",{elem:u,meta:{data:h,duration:m,source:w,url:k}}),!0}).catch(function(r){if(k=e._util.url,++s,m&&s<m)return e._state.status=2,u.setAttribute("data-wf-session-retry-counter",s),setTimeout(function(){e.fetch({disable:o,currentSessionRetryCount:s})},A*(s+1)),!1;if("Post without body"===r.message)return u.classList.remove("wafer-fetch-trigger-inprogress"),C.classList.remove("wafer-fetch-target-inprogress"),e._state.status=void 0,!1;var n=e._util.retryCount;return v.emitError({name:t.constructor.name,elem:u,meta:{url:k},stack:r.stack}),u.classList.remove("wafer-fetch-trigger-inprogress"),u.classList.add(d),C.classList.remove("wafer-fetch-target-inprogress"),e._state.status=2,++e._state.retries===n&&(v.destroy(C),!0)})}return Promise.resolve(!1)}},{key:"handleDefault",value:function(e){this._state.status=1,v.sync(e)}},{key:"handleTdv1",value:function(e){this._util.head=this._util.head||document.getElementsByTagName("head")[0];var t=this._util.head;_(e.querySelectorAll("script")).forEach(function(e){var r=e.getAttribute("type")||"text/javascript",n=e.innerText;n&&"text/javascript"===r&&(t.appendChild(e),T({type:r,text:n},function(r){r||t.removeChild(e)}))}),this._state.status=1,v.sync(e)}},{key:"stateDidUpdate",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.stateAttr,r=this._util.trigger;if("target"===t){var n=this._util,i=n.boundary,o=n.elem,a=n.target,s=n.targetElem;this._util.targetElem=a&&(i&&C(o,i)||document.body).querySelector(a)||s}if("stateChange"===r){var c=this._util.retryCount;if(this._state.retries===c)return;return void this.fetch()}}},{key:"destroy",value:function(){var e=this._util,r=e.timeoutInstance,n=e.trigger;r&&(clearTimeout(r),this._util.timeoutInstance=null),"mouseenter"===n&&this.removeEventListeners(),d(t.prototype.__proto__||Object.getPrototypeOf(t.prototype),"destroy",this).call(this)}},{key:"handleFetchClick",value:function(e){this._util.hasClick&&this.fetch()}},{key:"handleMouseenter",value:function(e){this._util.hasMouseenter&&this.fetch()}},{key:"config",get:function(){var e=this._util;return{delay:e.delay,offsetX:e.offsetX,offsetY:e.offsetY,trigger:e.trigger}}}]),t}(m);j.events={click:[["has-fetch-click","handleFetchClick"]],mouseenter:[["_self","handleMouseenter"]]};var S=j,E=function(){function e(e,t){var r=[],n=!0,i=!1,o=void 0;try{for(var a,s=e[Symbol.iterator]();!(n=(a=s.next()).done)&&(r.push(a.value),!t||r.length!==t);n=!0);}catch(e){i=!0,o=e}finally{try{!n&&s.return&&s.return()}finally{if(i)throw o}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),L=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),P=this,I=window.wafer.utils,x=I.elementInView,N=I.throttle,M=window.wafer.base,R=window.wafer.controllers.WaferBaseController,W=function(e,t,r){var n=parseInt(t.getAttribute("data-wf-fetch-iteration-count"),10)||0;return t.setAttribute("data-wf-fetch-iteration-count",n+1),clearTimeout(e._util.timeoutInstance),e._util.timeoutInstance=null,F.call(P,e,t,r)},F=function(e,t,r){!e._util.timeoutInstance&&(e._util.timeoutInstance=setTimeout(function(){e.fetch().then(function(){return W.call(P,e,t,r)}).catch(function(){return W.call(P,e,t,r)})},r))},D=function(e){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.errorClass,n=void 0===r?"wafer-fetch-error":r,i=e.root,o=void 0===i?document:i,a=e.selector,u=e.successClass,l=void 0===u?"wafer-fetch-complete":u,f=e.validateDelay,h=void 0===f?25:f;s(this,t);var d=c(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,{root:o,selector:a,props:{errorClass:n,selector:a,successClass:l},WaferClass:S}));return d._validateWithThrottle=N(function(){d.validate()},h,d),d.sync(),d}return u(t,e),L(t,[{key:"handleScroll",value:function(){this._validateWithThrottle()}},{key:"handleResize",value:function(){this._validateWithThrottle()}},{key:"handleVisibilityChange",value:function(){var e=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],t=this._state.elementInstances,r=!0,n=!1,i=void 0;try{for(var o,a=t.entries()[Symbol.iterator]();!(r=(o=a.next()).done);r=!0){var s=o.value,c=E(s,2),u=c[0],l=c[1],f=l.instance,h=f.config,d=h.delay;switch(h.trigger){case"interval":e?F.call(this,f,u,d):(clearTimeout(f._util.timeoutInstance),f._util.timeoutInstance=null)}}}catch(e){n=!0,i=e}finally{try{!r&&a.return&&a.return()}finally{if(n)throw i}}}},{key:"didSync",value:function(){var e=this._state,t=e.elementInstances,r=e.mounted;0===t.size||r||(this._state.mounted=!0)}},{key:"trigger",value:function(e){var t=this._state.elementInstances,r=t.get(e);return new Promise(function(e){if(r){var t=r.instance;if(!t)return e();t.fetch().then(function(r){if(!r)return e();var n=t.config.trigger;return"viewport"!==n&&"onLoad"!==n||t.destroy(),e()}).catch(function(){return e()})}})}},{key:"willValidate",value:function(e){var t=this,r=this._state.elementInstances;e.forEach(function(e){var n=r.get(e),i=n.instance,o=i.config,a=o.delay,s=o.trigger,c=o.offsetX,u=o.offsetY;switch(s){case"delay":!i._util.timeoutInstance&&(i._util.timeoutInstance=setTimeout(function(){i.fetch().then(function(e){clearTimeout(i._util.timeoutInstance),e&&i.destroy()}).catch(function(){clearTimeout(i._util.timeoutInstance)})},a));break;case"interval":F.call(t,i,e,a);break;case"viewport":x(e,{offsetX:c,offsetY:u},M.viewport)&&i.fetch().then(function(e){e&&i.destroy()});break;case"onLoad":i.fetch().then(function(e){e&&i.destroy()});break;case"click":case"eachClick":e.classList.add("has-fetch-click")}})}}]),t}(R),q=D;t.default=new q({selector:"wafer-fetch"})}})});