UNPKG

image360

Version:
1 lines 5.75 kB
!function(e){var t={};function n(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,n),r.l=!0,r.exports}n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var r in e)n.d(o,r,function(t){return e[t]}.bind(null,r));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){"use strict";var o,r=this&&this.__assign||function(){return(r=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)},i=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))((function(r,i){function s(e){try{u(o.next(e))}catch(e){i(e)}}function a(e){try{u(o.throw(e))}catch(e){i(e)}}function u(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,a)}u((o=o.apply(e,t||[])).next())}))},s=this&&this.__generator||function(e,t){var n,o,r,i,s={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return i={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function a(i){return function(a){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;s;)try{if(n=1,o&&(r=2&i[0]?o.return:i[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,i[1])).done)return r;switch(o=0,r&&(i=[2&i[0],r.value]),i[0]){case 0:case 1:r=i;break;case 4:return s.label++,{value:i[1],done:!1};case 5:s.label++,o=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(r=s.trys,(r=r.length>0&&r[r.length-1])||6!==i[0]&&2!==i[0])){s=0;continue}if(3===i[0]&&(!r||i[1]>r[0]&&i[1]<r[3])){s.label=i[1];break}if(6===i[0]&&s.label<r[1]){s.label=r[1],r=i;break}if(r&&s.label<r[2]){s.label=r[2],s.ops.push(i);break}r[2]&&s.ops.pop(),s.trys.pop();continue}i=t.call(e,s)}catch(e){i=[6,e],o=0}finally{n=r=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,a])}}};Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.LEFT=0]="LEFT",e[e.RIGHT=1]="RIGHT"}(o=t.DIRECTION||(t.DIRECTION={})),t.DEFAULT_CONFIG={preload:!0,isBackground:!1,loopCount:10,imagesUrls:[]};var a=function(){function e(e,n){var i=this;void 0===n&&(n={}),this.config=t.DEFAULT_CONFIG,this.direction=o.LEFT,this.currentPosition=0,this.currentImageIndex=0,this.lastPositionPercents=0,this.dragInProgress=!1,this.setDirection=function(e){i.direction=e<i.currentPosition?o.LEFT:o.RIGHT},this.handleClickOrTouch=function(e){switch(e.preventDefault(),e.type){case"mousedown":case"touchstart":i.dragInProgress=!0,i.currentPosition=i.getEventPosition(e);break;case"touchmove":if(!(i.dragInProgress&&e instanceof TouchEvent))break;i.setDirection(e.touches[0].pageX),i.currentPosition=i.getEventPosition(e),i.animateImage(e.touches[0].pageX-i.element.getBoundingClientRect().x);break;case"touchend":i.dragInProgress=!1}},this.handleMouseMove=function(e){i.dragInProgress&&(i.setDirection(e.pageX),i.currentPosition=i.getEventPosition(e),i.animateImage(e.pageX-i.element.getBoundingClientRect().x))},this.handleMouseUp=function(e){i.dragInProgress=!1},this.element=e,this.config=r(r({},this.config),n),this.init()}return e.prototype.init=function(){return i(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return 0===this.config.imagesUrls.length?[2]:(this.setInitialImage(),this.shouldPreloadImages()?[4,this.preloadImages()]:[3,2]);case 1:e.sent(),e.label=2;case 2:return this.setHandlers(),[2]}}))}))},e.prototype.setInitialImage=function(){var e=this.config.imagesUrls[0];this.element instanceof HTMLImageElement?this.element.src=e:this.element.style.backgroundImage='url("'+e+'")'},e.prototype.shouldPreloadImages=function(){return this.config.preload},e.prototype.preloadImages=function(){var e=this.config.imagesUrls.map((function(e){return new Promise((function(t){var n=new Image;n.src=e,n.onload=n.onerror=t}))}));return Promise.all(e)},e.prototype.setImageToElement=function(e){this.element instanceof HTMLImageElement?this.element.src=e:this.element.style.backgroundImage='url("'+e+'")'},e.prototype.setHandlers=function(){this.element.onmousedown=this.element.ontouchstart=this.element.ontouchmove=this.element.ontouchend=this.element.onclick=this.handleClickOrTouch,this.element.onmousemove=this.handleMouseMove,document.onmouseup=this.handleMouseUp},e.prototype.getEventPosition=function(e){return e instanceof TouchEvent?e.touches[0].pageX:e.pageX},e.prototype.animateImage=function(e){var t=this.element.getBoundingClientRect().width,n=(e<0?0:e>t?t:e)/t*100,r=100/(this.config.loopCount*this.config.imagesUrls.length);Math.abs(n-this.lastPositionPercents)<=r||(this.lastPositionPercents=n,this.currentImageIndex=this.direction===o.LEFT?this.currentImageIndex-1:this.currentImageIndex+1,this.currentImageIndex<0?this.currentImageIndex=this.config.imagesUrls.length-1:this.currentImageIndex>this.config.imagesUrls.length-1&&(this.currentImageIndex=0),this.setImageToElement(this.config.imagesUrls[this.currentImageIndex]))},e}();t.default=a},function(e,t,n){"use strict";n.r(t);var o=n(0),r=n.n(o);window.Image360=r.a}]);