2 lines
16 KiB
JavaScript
2 lines
16 KiB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),e=require("primereact/utils"),i=require("primereact/ripple"),n=require("primereact/api");function s(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var a=s(t),r=s(n);function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var i=0,n=new Array(e);i<e;i++)n[i]=t[i];return n}function l(t){if(Array.isArray(t))return o(t)}function u(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}function c(t,e){if(t){if("string"==typeof t)return o(t,e);var i=Object.prototype.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?o(t,e):void 0}}function p(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function h(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function m(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function d(t,e){for(var i=0;i<e.length;i++){var n=e[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function f(t,e,i){return e&&d(t.prototype,e),i&&d(t,i),Object.defineProperty(t,"prototype",{writable:!1}),t}function v(t,e){return v=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},v(t,e)}function y(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&v(t,e)}function g(t){return g="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},g(t)}function b(t,e){if(e&&("object"===g(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return h(t)}function S(t){return S=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},S(t)}function I(t,e,i){return e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i,t}function k(t,e){var i=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),i.push.apply(i,n)}return i}function w(t){for(var e=1;e<arguments.length;e++){var i=null!=arguments[e]?arguments[e]:{};e%2?k(Object(i),!0).forEach((function(e){I(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):k(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}function P(t){var e=O();return function(){var i,n=S(t);if(e){var s=S(this).constructor;i=Reflect.construct(n,arguments,s)}else i=n.apply(this,arguments);return b(this,i)}}function O(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}var C=function(i){y(s,t.Component);var n=P(s);function s(){return m(this,s),n.apply(this,arguments)}return f(s,[{key:"render",value:function(){var t=this.props.template(this.props.item),i=e.classNames(this.props.className,"p-carousel-item",{"p-carousel-item-active":this.props.active,"p-carousel-item-start":this.props.start,"p-carousel-item-end":this.props.end});return a.default.createElement("div",{className:i},t)}}]),s}();I(C,"defaultProps",{template:null,item:null,active:!1,start:!1,end:!1,className:null});var V=function(n){y(o,t.Component);var s=P(o);function o(t){var i;return m(this,o),(i=s.call(this,t)).state={numVisible:t.numVisible,numScroll:t.numScroll,totalShiftedItems:t.page*t.numScroll*-1},i.props.onPageChange||(i.state=w(w({},i.state),{},{page:t.page})),i.navBackward=i.navBackward.bind(h(i)),i.navForward=i.navForward.bind(h(i)),i.onTransitionEnd=i.onTransitionEnd.bind(h(i)),i.onTouchStart=i.onTouchStart.bind(h(i)),i.onTouchMove=i.onTouchMove.bind(h(i)),i.onTouchEnd=i.onTouchEnd.bind(h(i)),i.totalIndicators=0,i.remainingItems=0,i.allowAutoplay=!!i.props.autoplayInterval,i.circular=i.props.circular||i.allowAutoplay,i.attributeSelector=e.UniqueComponentId(),i.swipeThreshold=20,i}return f(o,[{key:"step",value:function(t,i){var n=this.state.totalShiftedItems,s=this.isCircular();null!=i?(n=this.state.numScroll*i*-1,s&&(n-=this.state.numVisible),this.isRemainingItemsAdded=!1):(n+=this.state.numScroll*t,this.isRemainingItemsAdded&&(n+=this.remainingItems-this.state.numScroll*t,this.isRemainingItemsAdded=!1),i=Math.abs(Math.floor((s?n+this.state.numVisible:n)/this.state.numScroll)));s&&this.state.page===this.totalIndicators-1&&-1===t?(n=-1*(this.props.value.length+this.state.numVisible),i=0):s&&0===this.state.page&&1===t?(n=0,i=this.totalIndicators-1):i===this.totalIndicators-1&&this.remainingItems>0&&(n+=-1*this.remainingItems-this.state.numScroll*t,this.isRemainingItemsAdded=!0),this.itemsContainer&&(e.DomHandler.removeClass(this.itemsContainer,"p-items-hidden"),this.changePosition(n),this.itemsContainer.style.transition="transform 500ms ease 0s"),this.props.onPageChange?(this.setState({totalShiftedItems:n}),this.props.onPageChange({page:i})):this.setState({page:i,totalShiftedItems:n})}},{key:"calculatePosition",value:function(){if(this.itemsContainer&&this.responsiveOptions){for(var t=window.innerWidth,e={numVisible:this.props.numVisible,numScroll:this.props.numScroll},i=0;i<this.responsiveOptions.length;i++){var n=this.responsiveOptions[i];parseInt(n.breakpoint,10)>=t&&(e=n)}var s={};if(this.state.numScroll!==e.numScroll){var a=this.getPage();a=Math.floor(a*this.state.numScroll/e.numScroll);var r=e.numScroll*a*-1;this.isCircular()&&(r-=e.numVisible),s={totalShiftedItems:r,numScroll:e.numScroll},this.props.onPageChange?this.props.onPageChange({page:a}):s=w(w({},s),{},{page:a})}this.state.numVisible!==e.numVisible&&(s=w(w({},s),{},{numVisible:e.numVisible})),Object.keys(s).length&&this.setState(s)}}},{key:"navBackward",value:function(t,e){(this.circular||0!==this.getPage())&&this.step(1,e),this.allowAutoplay=!1,t.cancelable&&t.preventDefault()}},{key:"navForward",value:function(t,e){(this.circular||this.getPage()<this.totalIndicators-1)&&this.step(-1,e),this.allowAutoplay=!1,t.cancelable&&t.preventDefault()}},{key:"onDotClick",value:function(t,e){var i=this.getPage();e>i?this.navForward(t,e):e<i&&this.navBackward(t,e)}},{key:"onTransitionEnd",value:function(t){this.itemsContainer&&"transform"===t.propertyName&&(e.DomHandler.addClass(this.itemsContainer,"p-items-hidden"),this.itemsContainer.style.transition="",0!==this.state.page&&this.state.page!==this.totalIndicators-1||!this.isCircular()||this.changePosition(this.state.totalShiftedItems))}},{key:"onTouchStart",value:function(t){var e=t.changedTouches[0];this.startPos={x:e.pageX,y:e.pageY}}},{key:"onTouchMove",value:function(t){t.cancelable&&t.preventDefault()}},{key:"onTouchEnd",value:function(t){var e=t.changedTouches[0];this.isVertical()?this.changePageOnTouch(t,e.pageY-this.startPos.y):this.changePageOnTouch(t,e.pageX-this.startPos.x)}},{key:"changePageOnTouch",value:function(t,e){Math.abs(e)>this.swipeThreshold&&(e<0?this.navForward(t):this.navBackward(t))}},{key:"bindDocumentListeners",value:function(){var t=this;this.documentResizeListener||(this.documentResizeListener=function(){t.calculatePosition()},window.addEventListener("resize",this.documentResizeListener))}},{key:"unbindDocumentListeners",value:function(){this.documentResizeListener&&(window.removeEventListener("resize",this.documentResizeListener),this.documentResizeListener=null)}},{key:"isVertical",value:function(){return"vertical"===this.props.orientation}},{key:"isCircular",value:function(){return this.circular&&this.props.value.length>=this.state.numVisible}},{key:"getPage",value:function(){return this.props.onPageChange?this.props.page:this.state.page}},{key:"getTotalIndicators",value:function(){return this.props.value?Math.ceil((this.props.value.length-this.state.numVisible)/this.state.numScroll)+1:0}},{key:"isAutoplay",value:function(){return this.props.autoplayInterval&&this.allowAutoplay}},{key:"startAutoplay",value:function(){var t=this;this.interval=setInterval((function(){t.step(-1,t.state.page===t.totalIndicators-1?0:t.state.page+1)}),this.props.autoplayInterval)}},{key:"stopAutoplay",value:function(){this.interval&&clearInterval(this.interval)}},{key:"createStyle",value:function(){this.carouselStyle||(this.carouselStyle=e.DomHandler.createInlineStyle(r.default.nonce));var t,i="\n .p-carousel[".concat(this.attributeSelector,"] .p-carousel-item {\n flex: 1 0 ").concat(100/this.state.numVisible,"%\n }\n ");if(this.props.responsiveOptions){this.responsiveOptions=l(t=this.props.responsiveOptions)||u(t)||c(t)||p(),this.responsiveOptions.sort((function(t,e){var i=t.breakpoint,n=e.breakpoint;return-1*(null==i&&null!=n?-1:null!=i&&null==n?1:null==i&&null==n?0:"string"==typeof i&&"string"==typeof n?i.localeCompare(n,void 0,{numeric:!0}):i<n?-1:i>n?1:0)}));for(var n=0;n<this.responsiveOptions.length;n++){var s=this.responsiveOptions[n];i+="\n @media screen and (max-width: ".concat(s.breakpoint,") {\n .p-carousel[").concat(this.attributeSelector,"] .p-carousel-item {\n flex: 1 0 ").concat(100/s.numVisible,"%\n }\n }\n ")}}this.carouselStyle.innerHTML=i}},{key:"changePosition",value:function(t){this.itemsContainer&&(this.itemsContainer.style.transform=this.isVertical()?"translate3d(0, ".concat(t*(100/this.state.numVisible),"%, 0)"):"translate3d(".concat(t*(100/this.state.numVisible),"%, 0, 0)"))}},{key:"componentDidMount",value:function(){this.container&&this.container.setAttribute(this.attributeSelector,""),this.createStyle(),this.calculatePosition(),this.changePosition(this.state.totalShiftedItems),this.props.responsiveOptions&&this.bindDocumentListeners()}},{key:"componentDidUpdate",value:function(t,e){var i=this.isCircular(),n=!1,s=this.state.totalShiftedItems;if(this.props.autoplayInterval&&this.stopAutoplay(),e.numScroll!==this.state.numScroll||e.numVisible!==this.state.numVisible||this.props.value&&t.value&&t.value.length!==this.props.value.length){this.remainingItems=(this.props.value.length-this.state.numVisible)%this.state.numScroll;var a=this.getPage();0!==this.totalIndicators&&a>=this.totalIndicators&&(a=this.totalIndicators-1,this.props.onPageChange?this.props.onPageChange({page:a}):this.setState({page:a}),n=!0),s=a*this.state.numScroll*-1,i&&(s-=this.state.numVisible),a===this.totalIndicators-1&&this.remainingItems>0?(s+=-1*this.remainingItems+this.state.numScroll,this.isRemainingItemsAdded=!0):this.isRemainingItemsAdded=!1,s!==this.state.totalShiftedItems&&(this.setState({totalShiftedItems:s}),n=!0),this.changePosition(s)}i&&(0===this.state.page?s=-1*this.state.numVisible:0===s&&(s=-1*this.props.value.length,this.remainingItems>0&&(this.isRemainingItemsAdded=!0)),s!==this.state.totalShiftedItems&&(this.setState({totalShiftedItems:s}),n=!0)),t.page!==this.props.page&&(this.props.page>t.page&&this.props.page<=this.totalIndicators-1?this.step(-1,this.props.page):this.props.page<t.page&&this.step(1,this.props.page)),!n&&this.isAutoplay()&&this.startAutoplay()}},{key:"componentWillUnmount",value:function(){this.props.responsiveOptions&&this.unbindDocumentListeners(),this.props.autoplayInterval&&this.stopAutoplay()}},{key:"renderItems",value:function(){var t=this;if(this.props.value&&this.props.value.length){var e=this.isCircular(),i=null,n=null;if(e){var s=null;i=(s=this.props.value.slice(-1*this.state.numVisible)).map((function(e,i){return a.default.createElement(C,{key:i+"_scloned",className:"p-carousel-item-cloned",template:t.props.itemTemplate,item:e,active:-1*t.state.totalShiftedItems===t.props.value.length+t.state.numVisible,start:0===i,end:i===s.length-1})})),n=(s=this.props.value.slice(0,this.state.numVisible)).map((function(e,i){return a.default.createElement(C,{key:i+"_fcloned",className:"p-carousel-item-cloned",template:t.props.itemTemplate,item:e,active:0===t.state.totalShiftedItems,start:0===i,end:i===s.length-1})}))}var r=this.props.value.map((function(i,n){var s=e?-1*(t.state.totalShiftedItems+t.state.numVisible):-1*t.state.totalShiftedItems,r=s+t.state.numVisible-1;return a.default.createElement(C,{key:n,template:t.props.itemTemplate,item:i,active:s<=n&&r>=n,start:s===n,end:r===n})}));return a.default.createElement(a.default.Fragment,null,i,r,n)}}},{key:"renderHeader",value:function(){return this.props.header?a.default.createElement("div",{className:"p-carousel-header"},this.props.header):null}},{key:"renderFooter",value:function(){return this.props.footer?a.default.createElement("div",{className:"p-carousel-footer"},this.props.footer):null}},{key:"renderContent",value:function(){var t=this,i=this.renderItems(),n=this.isVertical()?this.props.verticalViewPortHeight:"auto",s=this.renderBackwardNavigator(),r=this.renderForwardNavigator(),o=e.classNames("p-carousel-container",this.props.containerClassName);return a.default.createElement("div",{className:o},s,a.default.createElement("div",{className:"p-carousel-items-content",style:{height:n},onTouchStart:this.onTouchStart,onTouchMove:this.onTouchMove,onTouchEnd:this.onTouchEnd},a.default.createElement("div",{ref:function(e){return t.itemsContainer=e},className:"p-carousel-items-container",onTransitionEnd:this.onTransitionEnd},i)),r)}},{key:"renderBackwardNavigator",value:function(){var t=(!this.circular||this.props.value&&this.props.value.length<this.state.numVisible)&&0===this.getPage(),n=e.classNames("p-carousel-prev p-link",{"p-disabled":t}),s=e.classNames("p-carousel-prev-icon pi",{"pi-chevron-left":!this.isVertical(),"pi-chevron-up":this.isVertical()});return a.default.createElement("button",{type:"button",className:n,onClick:this.navBackward,disabled:t},a.default.createElement("span",{className:s}),a.default.createElement(i.Ripple,null))}},{key:"renderForwardNavigator",value:function(){var t=(!this.circular||this.props.value&&this.props.value.length<this.state.numVisible)&&(this.getPage()===this.totalIndicators-1||0===this.totalIndicators),n=e.classNames("p-carousel-next p-link",{"p-disabled":t}),s=e.classNames("p-carousel-next-icon pi",{"pi-chevron-right":!this.isVertical(),"pi-chevron-down":this.isVertical()});return a.default.createElement("button",{type:"button",className:n,onClick:this.navForward,disabled:t},a.default.createElement("span",{className:s}),a.default.createElement(i.Ripple,null))}},{key:"renderIndicator",value:function(t){var n=this,s=this.getPage()===t,r=e.classNames("p-carousel-indicator",{"p-highlight":s});return a.default.createElement("li",{className:r,key:"p-carousel-indicator-"+t},a.default.createElement("button",{type:"button",className:"p-link",onClick:function(e){return n.onDotClick(e,t)}},a.default.createElement(i.Ripple,null)))}},{key:"renderIndicators",value:function(){for(var t=e.classNames("p-carousel-indicators p-reset",this.props.indicatorsContentClassName),i=[],n=0;n<this.totalIndicators;n++)i.push(this.renderIndicator(n));return a.default.createElement("ul",{className:t},i)}},{key:"render",value:function(){var t=this,i=e.classNames("p-carousel p-component",{"p-carousel-vertical":this.isVertical(),"p-carousel-horizontal":!this.isVertical()},this.props.className),n=e.classNames("p-carousel-content",this.props.contentClassName);this.totalIndicators=this.getTotalIndicators();var s=this.renderContent(),r=this.renderIndicators(),o=this.renderHeader(),l=this.renderFooter();return a.default.createElement("div",{ref:function(e){return t.container=e},id:this.props.id,className:i,style:this.props.style},o,a.default.createElement("div",{className:n},s,r),l)}}]),o}();I(V,"defaultProps",{id:null,value:null,page:0,header:null,footer:null,style:null,className:null,itemTemplate:null,circular:!1,autoplayInterval:0,numVisible:1,numScroll:1,responsiveOptions:null,orientation:"horizontal",verticalViewPortHeight:"300px",contentClassName:null,containerClassName:null,indicatorsContentClassName:null,onPageChange:null}),exports.Carousel=V;
|