swiper.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __rest = (this && this.__rest) || function (s, e) {
  8. var t = {};
  9. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  10. t[p] = s[p];
  11. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  12. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  13. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
  14. t[p[i]] = s[p[i]];
  15. }
  16. return t;
  17. };
  18. import { SuperComponent, wxComponent, useControl } from '../common/src/index';
  19. import config from '../common/config';
  20. import { DIRECTION } from './common/constants';
  21. import props from './props';
  22. const { prefix } = config;
  23. const easings = {
  24. linear: 'linear',
  25. easeInCubic: 'cubic-bezier(0.32, 0, 0.67, 0)',
  26. easeOutCubic: 'cubic-bezier(0.33, 1, 0.68, 1)',
  27. easeInOutCubic: 'cubic-bezier(0.65, 0, 0.35, 1)',
  28. };
  29. const defaultNavigation = {
  30. type: "dots",
  31. minShowNum: 2,
  32. hasNavBtn: false,
  33. };
  34. let Swiper = class Swiper extends SuperComponent {
  35. constructor() {
  36. super(...arguments);
  37. this.externalClasses = [`${prefix}-class`];
  38. this.options = {
  39. multipleSlots: true,
  40. };
  41. this.properties = props;
  42. this.observers = {
  43. navigation(val) {
  44. this.setData({
  45. _navigation: Object.assign(Object.assign({}, defaultNavigation), val),
  46. });
  47. },
  48. current(val) {
  49. this.update(+val);
  50. },
  51. autoplay(val) {
  52. if (val) {
  53. this.play();
  54. }
  55. else {
  56. this.pause();
  57. }
  58. },
  59. interval(val) {
  60. if (this._old_interval && this._old_interval !== val) {
  61. this.replay();
  62. }
  63. this._old_interval = val;
  64. },
  65. direction(val) {
  66. if (this._old_direction && this._old_direction !== val) {
  67. this.initItem();
  68. }
  69. this._old_direction = val;
  70. },
  71. };
  72. this.children = null;
  73. this.$nav = null;
  74. this.timer = null;
  75. this.updateTimer = null;
  76. this.control = null;
  77. this.relations = {
  78. './swiper-item': {
  79. type: 'child',
  80. linked: function () {
  81. clearTimeout(this.updateTimer);
  82. this.updateTimer = setTimeout(() => {
  83. this.initItem();
  84. this.updateNav(this.control.get());
  85. });
  86. },
  87. unlinked: function () {
  88. this.initItem();
  89. this.update(0);
  90. },
  91. },
  92. './swiper-nav': {
  93. type: 'child',
  94. },
  95. };
  96. this.data = {
  97. _current: 0,
  98. _navigation: null,
  99. _width: 0,
  100. _height: 0,
  101. offsetX: 0,
  102. offsetY: 0,
  103. total: 0,
  104. easings,
  105. inited: false,
  106. currentInited: false,
  107. prefix,
  108. classPrefix: `${prefix}-swiper`,
  109. };
  110. }
  111. attached() {
  112. this.control = useControl.call(this, {
  113. valueKey: 'current',
  114. strict: false,
  115. });
  116. }
  117. detached() {
  118. this.pause();
  119. }
  120. ready() {
  121. this.createSelectorQuery()
  122. .select('#swiper')
  123. .boundingClientRect((rect) => {
  124. this.setData({
  125. _width: rect.width,
  126. _height: rect.height,
  127. });
  128. this.initItem();
  129. this.initNav();
  130. this.initCurrent();
  131. })
  132. .exec();
  133. }
  134. initItem() {
  135. const { direction } = this.properties;
  136. this.children = this.getRelationNodes('./swiper-item');
  137. this.children.forEach((item, index) => {
  138. item.setIndex(index, direction);
  139. });
  140. this.setData({
  141. total: this.children.length,
  142. });
  143. }
  144. initNav() {
  145. var _a;
  146. const { _navigation } = this.data;
  147. if (_navigation) {
  148. this.$nav = this.selectComponent('#swiperNav');
  149. }
  150. else {
  151. this.$nav = (_a = this.getRelationNodes('./swiper-nav')) === null || _a === void 0 ? void 0 : _a[0];
  152. }
  153. }
  154. inited() {
  155. this.updateNav(this.control.get());
  156. this.setData({
  157. inited: true,
  158. });
  159. }
  160. initCurrent() {
  161. let index = +this.control.initValue;
  162. index = Math.min(index, this.children.length - 1);
  163. index = Math.max(index, 0);
  164. this.control.set(index, Object.assign({ currentInited: true, inited: index === 0 }, this.calcOffset(index)));
  165. }
  166. play() {
  167. this.pause();
  168. const { interval } = this.properties;
  169. this.timer = setInterval(() => {
  170. const { inited } = this.data;
  171. if (!inited)
  172. return;
  173. this.next({ cycle: true, source: 'autoplay' });
  174. }, interval);
  175. }
  176. replay() {
  177. const { autoplay } = this.properties;
  178. autoplay && this.play();
  179. }
  180. pause() {
  181. this.timer && clearInterval(this.timer);
  182. this.timer = null;
  183. }
  184. goto(index, source) {
  185. if (this.control.get() === index) {
  186. this.update(index);
  187. return;
  188. }
  189. this.control.change(index, {
  190. current: index,
  191. source,
  192. }, () => {
  193. this.update(index);
  194. });
  195. }
  196. update(index, finish) {
  197. if (!this.children)
  198. return;
  199. const len = this.children.length;
  200. let fixIndex = +index;
  201. if (Number.isNaN(fixIndex))
  202. return;
  203. if (fixIndex <= 0) {
  204. fixIndex = 0;
  205. }
  206. else if (fixIndex > len - 1) {
  207. fixIndex = len - 1;
  208. }
  209. this.updateNav(fixIndex);
  210. this.control.set(fixIndex, this.calcOffset(fixIndex), finish);
  211. }
  212. updateNav(index) {
  213. var _a;
  214. if (!this.$nav)
  215. return;
  216. const { direction, paginationPosition } = this.properties;
  217. (_a = this.$nav) === null || _a === void 0 ? void 0 : _a.onChange({
  218. index,
  219. total: this.children.length,
  220. direction,
  221. paginationPosition,
  222. });
  223. }
  224. calcOffset(index) {
  225. const { direction } = this.properties;
  226. const { _width, _height } = this.data;
  227. if (direction === DIRECTION.HOR) {
  228. return {
  229. offsetX: -index * _width,
  230. };
  231. }
  232. return {
  233. offsetY: -index * _height,
  234. };
  235. }
  236. next(opt) {
  237. const innerVal = this.control.get();
  238. const len = this.children.length;
  239. let nextIndex = innerVal;
  240. if (opt.cycle && innerVal === len - 1) {
  241. nextIndex = 0;
  242. }
  243. else if (len - 1 > innerVal) {
  244. nextIndex += 1;
  245. }
  246. this.goto(nextIndex, opt.source);
  247. }
  248. prev(opt) {
  249. const innerVal = this.control.get();
  250. const len = this.children.length;
  251. let nextIndex = innerVal;
  252. if (opt.cycle && innerVal === 0) {
  253. nextIndex = len - 1;
  254. }
  255. else if (nextIndex > 0) {
  256. nextIndex -= 1;
  257. }
  258. this.goto(nextIndex, opt.source);
  259. }
  260. onSwiperNavBtnChange(e) {
  261. const _a = e.detail, { dir } = _a, rest = __rest(_a, ["dir"]);
  262. this.pause();
  263. this === null || this === void 0 ? void 0 : this[dir](rest);
  264. }
  265. };
  266. Swiper = __decorate([
  267. wxComponent()
  268. ], Swiper);
  269. export default Swiper;