UNPKG

11.6 kBJavaScriptView Raw
1/*
2Copyright (c) 2020-present NAVER Corp.
3name: @egjs/imready
4license: MIT
5author: NAVER Corp.
6repository: https://github.com/naver/egjs-imready
7version: 1.1.4
8*/
9!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((t=t||self).eg=t.eg||{},t.eg.ImReady=e())}(this,function(){"use strict";var r=function(t,e){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])})(t,e)};function o(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var a=function(){return(a=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);return t}).apply(this,arguments)};function d(t){return void 0===t}var t=function(){function t(){this.options={},this._eventHandler={}}var e=t.prototype;return e.trigger=function(t){for(var e=this,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];if(!(0<(a=this._eventHandler[t]||[]).length))return!0;var i=n[0]||{},o=n.slice(1),a=a.concat(),s=!1;i.eventType=t,i.stop=function(){s=!0},i.currentTarget=this;var u=[i];return 1<=o.length&&(u=u.concat(o)),a.forEach(function(t){t.apply(e,u)}),!s},e.once=function(n,r){var i,o=this;if("object"==typeof n&&d(r)){var t,e=n;for(t in e)this.once(t,e[t]);return this}return"string"==typeof n&&"function"==typeof r&&(i=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];r.apply(o,t),o.off(n,i)},this.on(n,i)),this},e.hasOn=function(t){return!!this._eventHandler[t]},e.on=function(t,e){if("object"==typeof t&&d(e)){var n,r=t;for(n in r)this.on(n,r[n]);return this}var i;return"string"==typeof t&&"function"==typeof e&&(d(i=this._eventHandler[t])&&(this._eventHandler[t]=[],i=this._eventHandler[t]),i.push(e)),this},e.off=function(t,e){var n,r;if(d(t))return this._eventHandler={},this;if(d(e)){if("string"==typeof t)return delete this._eventHandler[t],this;var i,o=t;for(i in o)this.off(i,o[i]);return this}var a=this._eventHandler[t];if(a){var s=0;try{for(var u=function(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(a),h=u.next();!h.done;h=u.next()){if(h.value===e){a.splice(s,1);break}s++}}catch(t){n={error:t}}finally{try{h&&!h.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}return this},t.VERSION="2.2.2",t}(),e="undefined"!=typeof window,n=e?window.navigator.userAgent:"",i=e&&!!("getComputedStyle"in window),s=/MSIE|Trident|Windows Phone|Edge/.test(n),u=e&&!!("addEventListener"in document),h="width",f="height";function c(t,e){return t.getAttribute(e)||""}function l(t){return[].slice.call(t)}function y(t){return"loading"in t&&"lazy"===t.getAttribute("loading")}function p(t,e,n){u?t.addEventListener(e,n,!1):t.attachEvent?t.attachEvent("on"+e,n):t["on"+e]=n}function g(t,e,n){t.removeEventListener?t.removeEventListener(e,n,!1):t.detachEvent?t.detachEvent("on"+e,n):t["on"+e]=null}function v(t,e){var n=t["client"+e]||t["offset"+e];return parseFloat(n||(t=t,((i?window.getComputedStyle(t):t.currentStyle)||{})[e.toLowerCase()]))||0}function R(t,e,n){var r=l(t.querySelectorAll(function(){for(var t=0,e=0,n=arguments.length;e<n;e++)t+=arguments[e].length;for(var r=Array(t),i=0,e=0;e<n;e++)for(var o=arguments[e],a=0,s=o.length;a<s;a++,i++)r[i]=o[a];return r}(["["+n+"skip] ["+n+"width]"],e.map(function(t){return["["+n+"skip] "+t,t+"["+n+"skip]","["+n+"width] "+t].join(", ")})).join(", ")));return l(t.querySelectorAll("["+n+"width], "+e.join(", "))).filter(function(t){return-1===r.indexOf(t)})}var m=[];function E(t,e){m.length||p(window,"resize",P),t.__PREFIX__=e,m.push(t),C(t)}function C(t,e){void 0===e&&(e="data-");var n,r=t.__PREFIX__||e,i=parseInt(c(t,""+r+h),10)||0,e=parseInt(c(t,""+r+f),10)||0;c(t,r+"fixed")===f?(n=v(t,"Height")||e,t.style[h]=i/e*n+"px"):(n=v(t,"Width")||i,t.style[f]=e/i*n+"px")}function P(){m.forEach(function(t){C(t)})}var S,e=function(i){function t(t,e){void 0===e&&(e={});var n=i.call(this)||this;n.isReady=!1,n.isPreReady=!1,n.hasDataSize=!1,n.hasLoading=!1,n.isSkip=!1,n.onCheck=function(t){n.clear(),t&&"error"===t.type&&n.onError(n.element);t=!n.hasDataSize&&!n.hasLoading;n.onReady(t)},n.options=a({prefix:"data-"},e),n.element=t;var r=n.options.prefix;return n.hasDataSize=(void 0===(e=r)&&(e="data-"),!!t.getAttribute(e+"width")),n.isSkip=(void 0===(r=r)&&(r="data-"),!!t.getAttribute(r+"skip")),n.hasLoading=y(t),n}o(t,i);var e=t.prototype;return e.check=function(){return this.isSkip||!this.checkElement()?(this.onAlreadyReady(!0),!1):(this.hasDataSize&&E(this.element,this.options.prefix),(this.hasDataSize||this.hasLoading)&&this.onAlreadyPreReady(),!0)},e.addEvents=function(){var e=this,n=this.element;this.constructor.EVENTS.forEach(function(t){p(n,t,e.onCheck)})},e.clear=function(){var e=this,n=this.element;this.constructor.EVENTS.forEach(function(t){g(n,t,e.onCheck)}),this.removeAutoSizer()},e.destroy=function(){this.clear(),this.off()},e.removeAutoSizer=function(){var t,e,n;this.hasDataSize&&(n=this.options.prefix,t=this.element,e=n,(n=m.indexOf(t))<0||(e=c(t,e+"fixed"),delete t.__PREFIX__,t.style[e===f?h:f]="",m.splice(n,1),m.length||g(window,"resize",P)))},e.onError=function(t){this.trigger("error",{element:this.element,target:t})},e.onPreReady=function(){this.isPreReady||(this.isPreReady=!0,this.trigger("preReady",{element:this.element,hasLoading:this.hasLoading,isSkip:this.isSkip}))},e.onReady=function(t){this.isReady||((t=!this.isPreReady&&t)&&(this.isPreReady=!0),this.removeAutoSizer(),this.isReady=!0,this.trigger("ready",{element:this.element,withPreReady:t,hasLoading:this.hasLoading,isSkip:this.isSkip}))},e.onAlreadyError=function(t){var e=this;setTimeout(function(){e.onError(t)})},e.onAlreadyPreReady=function(){var t=this;setTimeout(function(){t.onPreReady()})},e.onAlreadyReady=function(t){var e=this;setTimeout(function(){e.onReady(t)})},t.EVENTS=[],t}(t),k=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}o(e,t);var n=e.prototype;return n.setHasLoading=function(t){this.hasLoading=t},n.check=function(){return this.isSkip?(this.onAlreadyReady(!0),!1):(this.hasDataSize?(E(this.element,this.options.prefix),this.onAlreadyPreReady()):this.trigger("requestChildren"),!0)},n.checkElement=function(){return!0},n.destroy=function(){this.clear(),this.trigger("requestDestroy"),this.off()},n.onAlreadyPreReady=function(){t.prototype.onAlreadyPreReady.call(this),this.trigger("reqeustReadyChildren")},e.EVENTS=[],e}(e),t=function(n){function t(t){void 0===t&&(t={});var e=n.call(this)||this;return e.readyCount=0,e.preReadyCount=0,e.totalCount=0,e.totalErrorCount=0,e.isPreReadyOver=!0,e.elementInfos=[],e.options=a({loaders:{},prefix:"data-"},t),e}o(t,n);var e=t.prototype;return e.check=function(t){var o=this,n=this.options.prefix;this.clear(),this.elementInfos=l(t).map(function(t,i){var e=o.getLoader(t,{prefix:n});return e.check(),e.on("error",function(t){o.onError(i,t.target)}).on("preReady",function(t){var e=o.elementInfos[i];e.hasLoading=t.hasLoading,e.isSkip=t.isSkip;t=o.checkPreReady(i);o.onPreReadyElement(i),t&&o.onPreReady()}).on("ready",function(t){var e=t.withPreReady,n=t.hasLoading,r=t.isSkip,t=o.elementInfos[i];t.hasLoading=n,t.isSkip=r;t=e&&o.checkPreReady(i),r=o.checkReady(i);e&&o.onPreReadyElement(i),o.onReadyElement(i),t&&o.onPreReady(),r&&o.onReady()}),{loader:e,element:t,hasLoading:!1,hasError:!1,isPreReady:!1,isReady:!1,isSkip:!1}});t=this.elementInfos.length;return(this.totalCount=t)||setTimeout(function(){o.onPreReady(),o.onReady()}),this},e.getTotalCount=function(){return this.totalCount},e.isPreReady=function(){return this.elementInfos.every(function(t){return t.isPreReady})},e.isReady=function(){return this.elementInfos.every(function(t){return t.isReady})},e.hasError=function(){return 0<this.totalErrorCount},e.clear=function(){this.isPreReadyOver=!1,this.totalCount=0,this.preReadyCount=0,this.readyCount=0,this.totalErrorCount=0,this.elementInfos.forEach(function(t){!t.isReady&&t.loader&&t.loader.destroy()}),this.elementInfos=[]},e.destroy=function(){this.clear(),this.off()},e.getLoader=function(e,t){var n=this,r=e.tagName.toLowerCase(),i=this.options.loaders,o=Object.keys(i);if(i[r])return new i[r](e,t);var a=new k(e,t),s=l(e.querySelectorAll(o.join(", ")));a.setHasLoading(s.some(y));var u=!1,h=this.clone().on("error",function(t){a.onError(t.target)}).on("ready",function(){a.onReady(u)});return a.on("requestChildren",function(){var t=R(e,o,n.options.prefix);h.check(t).on("preReady",function(t){(u=t.isReady)||a.onPreReady()})}).on("reqeustReadyChildren",function(){h.check(s)}).on("requestDestroy",function(){h.destroy()}),a},e.clone=function(){return new t(a({},this.options))},e.checkPreReady=function(t){return this.elementInfos[t].isPreReady=!0,++this.preReadyCount,!(this.preReadyCount<this.totalCount)},e.checkReady=function(t){return this.elementInfos[t].isReady=!0,++this.readyCount,!(this.readyCount<this.totalCount)},e.onError=function(t,e){var n=this.elementInfos[t];n.hasError=!0,this.trigger("error",{element:n.element,index:t,target:e,errorCount:this.getErrorCount(),totalErrorCount:++this.totalErrorCount})},e.onPreReadyElement=function(t){var e=this.elementInfos[t];this.trigger("preReadyElement",{element:e.element,index:t,preReadyCount:this.preReadyCount,readyCount:this.readyCount,totalCount:this.totalCount,isPreReady:this.isPreReady(),isReady:this.isReady(),hasLoading:e.hasLoading,isSkip:e.isSkip})},e.onPreReady=function(){this.isPreReadyOver=!0,this.trigger("preReady",{readyCount:this.readyCount,totalCount:this.totalCount,isReady:this.isReady(),hasLoading:this.hasLoading()})},e.onReadyElement=function(t){var e=this.elementInfos[t];this.trigger("readyElement",{index:t,element:e.element,hasError:e.hasError,errorCount:this.getErrorCount(),totalErrorCount:this.totalErrorCount,preReadyCount:this.preReadyCount,readyCount:this.readyCount,totalCount:this.totalCount,isPreReady:this.isPreReady(),isReady:this.isReady(),hasLoading:e.hasLoading,isPreReadyOver:this.isPreReadyOver,isSkip:e.isSkip})},e.onReady=function(){this.trigger("ready",{errorCount:this.getErrorCount(),totalErrorCount:this.totalErrorCount,totalCount:this.totalCount})},e.getErrorCount=function(){return this.elementInfos.filter(function(t){return t.hasError}).length},e.hasLoading=function(){return this.elementInfos.some(function(t){return t.hasLoading})},t}(t),L=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.checkElement=function(){var t=this.element,e=t.getAttribute("src");if(t.complete){if(e)return t.naturalWidth||this.onAlreadyError(t),!1;this.onAlreadyPreReady()}return this.addEvents(),s&&t.setAttribute("src",e),!0},e.EVENTS=["load","error"],e}(e),_=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.checkElement=function(){var t=this.element;return!(1<=t.readyState)&&(t.error?(this.onAlreadyError(t),!1):(this.addEvents(),!0))},e.EVENTS=["loadedmetadata","error"],e}(e),w=function(e){function t(t){return void 0===t&&(t={}),e.call(this,a({loaders:{img:L,video:_}},t))||this}return o(t,e),t}(t),A={__proto__:null,default:w,Manager:t,VideoLoader:_,ImageLoader:L,Loader:e};for(S in A)w[S]=A[S];return w});
10//# sourceMappingURL=imready.min.js.map