foundation.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { isObject, get } from 'lodash';
  2. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  3. import { numbers } from './constants';
  4. import { throttle } from 'lodash';
  5. export interface CarouselAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  6. notifyChange: (activeIndex: number, preIndex: number) => void;
  7. setNewActiveIndex: (activeIndex: number) => void;
  8. setPreActiveIndex: (activeIndex: number) => void;
  9. setIsReverse: (isReverse: boolean) => void;
  10. setIsInit: (isInit: boolean) => void;
  11. }
  12. class CarouselFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<CarouselAdapter<P, S>, P, S> {
  13. constructor(adapter: CarouselAdapter<P, S>) {
  14. super({ ...adapter });
  15. }
  16. _interval = null;
  17. _forcePlay = false;
  18. setForcePlay(forcePlay: boolean) {
  19. this._forcePlay = forcePlay;
  20. }
  21. play(interval: number): void {
  22. if (this._interval) {
  23. clearInterval(this._interval);
  24. }
  25. this._interval = setInterval(() => {
  26. this.next();
  27. }, interval);
  28. }
  29. stop(): void {
  30. if (this._interval){
  31. clearInterval(this._interval);
  32. }
  33. }
  34. goTo(activeIndex: number): void {
  35. const { activeIndex: stateActiveIndex } = this.getStates();
  36. const targetIndex = this.getValidIndex(activeIndex);
  37. this._adapter.setIsReverse(stateActiveIndex > targetIndex);
  38. if (this.getIsControlledComponent()) {
  39. this._notifyChange(targetIndex);
  40. } else {
  41. this._notifyChange(targetIndex);
  42. this.handleNewActiveIndex(targetIndex);
  43. }
  44. }
  45. next(): void {
  46. this.stop();
  47. const { activeIndex: stateActiveIndex } = this.getStates();
  48. const targetIndex = this.getValidIndex(stateActiveIndex + 1);
  49. this._adapter.setIsReverse(false);
  50. if (this.getIsControlledComponent()) {
  51. this._notifyChange(targetIndex);
  52. } else {
  53. this._notifyChange(targetIndex);
  54. this.handleNewActiveIndex(targetIndex);
  55. }
  56. this.handleAutoPlay();
  57. }
  58. prev(): void {
  59. this.stop();
  60. const { activeIndex: stateActiveIndex } = this.getStates();
  61. const targetIndex = this.getValidIndex(stateActiveIndex - 1);
  62. this._adapter.setIsReverse(true);
  63. if (this.getIsControlledComponent()) {
  64. this._notifyChange(targetIndex);
  65. } else {
  66. this._notifyChange(targetIndex);
  67. this.handleNewActiveIndex(targetIndex);
  68. }
  69. this.handleAutoPlay();
  70. }
  71. destroy(): void {
  72. this._unregisterInterval();
  73. }
  74. _unregisterInterval() {
  75. if (this._interval) {
  76. clearInterval(this._interval);
  77. this._interval = null;
  78. }
  79. }
  80. _notifyChange(activeIndex: number): void {
  81. const { activeIndex: stateActiveIndex, isInit } = this.getStates();
  82. if (isInit){
  83. this._adapter.setIsInit(false);
  84. }
  85. if (stateActiveIndex !== activeIndex) {
  86. this._adapter.setPreActiveIndex(stateActiveIndex);
  87. this._adapter.notifyChange(activeIndex, stateActiveIndex);
  88. }
  89. }
  90. getValidIndex(index: number): number {
  91. const { children } = this.getStates();
  92. return (index + children.length) % children.length;
  93. }
  94. getSwitchingTime(): number {
  95. const { autoPlay, speed } = this.getProps();
  96. const autoPlayType = typeof autoPlay;
  97. if (autoPlayType === 'boolean'){
  98. return numbers.DEFAULT_INTERVAL + speed;
  99. }
  100. if (isObject(autoPlay)){
  101. return get(autoPlay, 'interval', numbers.DEFAULT_INTERVAL) + speed;
  102. }
  103. return speed;
  104. }
  105. getIsControlledComponent(): boolean {
  106. return this._isInProps('activeIndex');
  107. }
  108. handleAutoPlay(): void {
  109. const { autoPlay } = this.getProps();
  110. const autoPlayType = typeof autoPlay;
  111. // when user manually call the play function, force play
  112. if ((autoPlayType === 'boolean' && autoPlay) || isObject(autoPlay) || this._forcePlay){
  113. this.play(this.getSwitchingTime());
  114. }
  115. }
  116. handleKeyDown(event: any): void{
  117. if (event.key === 'ArrowLeft') {
  118. this.prev();
  119. }
  120. if (event.key === 'ArrowRight') {
  121. this.next();
  122. }
  123. }
  124. onIndicatorChange(activeIndex: number): void {
  125. const { activeIndex: stateActiveIndex } = this.getStates();
  126. this._adapter.setIsReverse(stateActiveIndex > activeIndex);
  127. this._notifyChange(activeIndex);
  128. if (!this.getIsControlledComponent()) {
  129. this.handleNewActiveIndex(activeIndex);
  130. }
  131. }
  132. handleNewActiveIndex(activeIndex: number): void {
  133. const { activeIndex: stateActiveIndex } = this.getStates();
  134. if (stateActiveIndex !== activeIndex) {
  135. this._adapter.setNewActiveIndex(activeIndex);
  136. }
  137. }
  138. getDefaultActiveIndex(): number {
  139. let activeIndex;
  140. const props = this.getProps();
  141. if ('activeIndex' in props) {
  142. activeIndex = props.activeIndex;
  143. } else if ('defaultActiveIndex' in props) {
  144. activeIndex = props.defaultActiveIndex;
  145. }
  146. return activeIndex;
  147. }
  148. }
  149. export default CarouselFoundation;