manipulation.mjs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. function appendSlide(slides) {
  2. const swiper = this;
  3. const {
  4. params,
  5. slidesEl
  6. } = swiper;
  7. if (params.loop) {
  8. swiper.loopDestroy();
  9. }
  10. const appendElement = slideEl => {
  11. if (typeof slideEl === 'string') {
  12. const tempDOM = document.createElement('div');
  13. tempDOM.innerHTML = slideEl;
  14. slidesEl.append(tempDOM.children[0]);
  15. tempDOM.innerHTML = '';
  16. } else {
  17. slidesEl.append(slideEl);
  18. }
  19. };
  20. if (typeof slides === 'object' && 'length' in slides) {
  21. for (let i = 0; i < slides.length; i += 1) {
  22. if (slides[i]) appendElement(slides[i]);
  23. }
  24. } else {
  25. appendElement(slides);
  26. }
  27. swiper.recalcSlides();
  28. if (params.loop) {
  29. swiper.loopCreate();
  30. }
  31. if (!params.observer || swiper.isElement) {
  32. swiper.update();
  33. }
  34. }
  35. function prependSlide(slides) {
  36. const swiper = this;
  37. const {
  38. params,
  39. activeIndex,
  40. slidesEl
  41. } = swiper;
  42. if (params.loop) {
  43. swiper.loopDestroy();
  44. }
  45. let newActiveIndex = activeIndex + 1;
  46. const prependElement = slideEl => {
  47. if (typeof slideEl === 'string') {
  48. const tempDOM = document.createElement('div');
  49. tempDOM.innerHTML = slideEl;
  50. slidesEl.prepend(tempDOM.children[0]);
  51. tempDOM.innerHTML = '';
  52. } else {
  53. slidesEl.prepend(slideEl);
  54. }
  55. };
  56. if (typeof slides === 'object' && 'length' in slides) {
  57. for (let i = 0; i < slides.length; i += 1) {
  58. if (slides[i]) prependElement(slides[i]);
  59. }
  60. newActiveIndex = activeIndex + slides.length;
  61. } else {
  62. prependElement(slides);
  63. }
  64. swiper.recalcSlides();
  65. if (params.loop) {
  66. swiper.loopCreate();
  67. }
  68. if (!params.observer || swiper.isElement) {
  69. swiper.update();
  70. }
  71. swiper.slideTo(newActiveIndex, 0, false);
  72. }
  73. function addSlide(index, slides) {
  74. const swiper = this;
  75. const {
  76. params,
  77. activeIndex,
  78. slidesEl
  79. } = swiper;
  80. let activeIndexBuffer = activeIndex;
  81. if (params.loop) {
  82. activeIndexBuffer -= swiper.loopedSlides;
  83. swiper.loopDestroy();
  84. swiper.recalcSlides();
  85. }
  86. const baseLength = swiper.slides.length;
  87. if (index <= 0) {
  88. swiper.prependSlide(slides);
  89. return;
  90. }
  91. if (index >= baseLength) {
  92. swiper.appendSlide(slides);
  93. return;
  94. }
  95. let newActiveIndex = activeIndexBuffer > index ? activeIndexBuffer + 1 : activeIndexBuffer;
  96. const slidesBuffer = [];
  97. for (let i = baseLength - 1; i >= index; i -= 1) {
  98. const currentSlide = swiper.slides[i];
  99. currentSlide.remove();
  100. slidesBuffer.unshift(currentSlide);
  101. }
  102. if (typeof slides === 'object' && 'length' in slides) {
  103. for (let i = 0; i < slides.length; i += 1) {
  104. if (slides[i]) slidesEl.append(slides[i]);
  105. }
  106. newActiveIndex = activeIndexBuffer > index ? activeIndexBuffer + slides.length : activeIndexBuffer;
  107. } else {
  108. slidesEl.append(slides);
  109. }
  110. for (let i = 0; i < slidesBuffer.length; i += 1) {
  111. slidesEl.append(slidesBuffer[i]);
  112. }
  113. swiper.recalcSlides();
  114. if (params.loop) {
  115. swiper.loopCreate();
  116. }
  117. if (!params.observer || swiper.isElement) {
  118. swiper.update();
  119. }
  120. if (params.loop) {
  121. swiper.slideTo(newActiveIndex + swiper.loopedSlides, 0, false);
  122. } else {
  123. swiper.slideTo(newActiveIndex, 0, false);
  124. }
  125. }
  126. function removeSlide(slidesIndexes) {
  127. const swiper = this;
  128. const {
  129. params,
  130. activeIndex
  131. } = swiper;
  132. let activeIndexBuffer = activeIndex;
  133. if (params.loop) {
  134. activeIndexBuffer -= swiper.loopedSlides;
  135. swiper.loopDestroy();
  136. }
  137. let newActiveIndex = activeIndexBuffer;
  138. let indexToRemove;
  139. if (typeof slidesIndexes === 'object' && 'length' in slidesIndexes) {
  140. for (let i = 0; i < slidesIndexes.length; i += 1) {
  141. indexToRemove = slidesIndexes[i];
  142. if (swiper.slides[indexToRemove]) swiper.slides[indexToRemove].remove();
  143. if (indexToRemove < newActiveIndex) newActiveIndex -= 1;
  144. }
  145. newActiveIndex = Math.max(newActiveIndex, 0);
  146. } else {
  147. indexToRemove = slidesIndexes;
  148. if (swiper.slides[indexToRemove]) swiper.slides[indexToRemove].remove();
  149. if (indexToRemove < newActiveIndex) newActiveIndex -= 1;
  150. newActiveIndex = Math.max(newActiveIndex, 0);
  151. }
  152. swiper.recalcSlides();
  153. if (params.loop) {
  154. swiper.loopCreate();
  155. }
  156. if (!params.observer || swiper.isElement) {
  157. swiper.update();
  158. }
  159. if (params.loop) {
  160. swiper.slideTo(newActiveIndex + swiper.loopedSlides, 0, false);
  161. } else {
  162. swiper.slideTo(newActiveIndex, 0, false);
  163. }
  164. }
  165. function removeAllSlides() {
  166. const swiper = this;
  167. const slidesIndexes = [];
  168. for (let i = 0; i < swiper.slides.length; i += 1) {
  169. slidesIndexes.push(i);
  170. }
  171. swiper.removeSlide(slidesIndexes);
  172. }
  173. function Manipulation(_ref) {
  174. let {
  175. swiper
  176. } = _ref;
  177. Object.assign(swiper, {
  178. appendSlide: appendSlide.bind(swiper),
  179. prependSlide: prependSlide.bind(swiper),
  180. addSlide: addSlide.bind(swiper),
  181. removeSlide: removeSlide.bind(swiper),
  182. removeAllSlides: removeAllSlides.bind(swiper)
  183. });
  184. }
  185. export { Manipulation as default };