page-scroll.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.pageScrollMixin = void 0;
  6. function Behavior(behavior) {
  7. behavior.$id = Number(new Date()) + String(Math.random()).substring(2, 7);
  8. return behavior;
  9. }
  10. function getCurrentPage() {
  11. var pages = getCurrentPages();
  12. return pages[pages.length - 1] || {};
  13. }
  14. function onPageScroll(event) {
  15. var _getCurrentPage = getCurrentPage(),
  16. _getCurrentPage$vanPa = _getCurrentPage.vanPageScroller,
  17. vanPageScroller = _getCurrentPage$vanPa === void 0 ? [] : _getCurrentPage$vanPa;
  18. vanPageScroller.forEach(function (scroller) {
  19. if (typeof scroller === "function") {
  20. // @ts-ignore
  21. scroller(event);
  22. }
  23. });
  24. }
  25. var pageScrollMixin = function pageScrollMixin(scroller) {
  26. return Behavior({
  27. attached: function attached() {
  28. var page = getCurrentPage();
  29. if (Array.isArray(page.vanPageScroller)) {
  30. page.vanPageScroller.push(scroller.bind(this));
  31. } else {
  32. page.vanPageScroller = typeof page.onPageScroll === "function" ? [page.onPageScroll.bind(page), scroller.bind(this)] : [scroller.bind(this)];
  33. }
  34. page.onPageScroll = onPageScroll;
  35. },
  36. detached: function detached() {
  37. var page = getCurrentPage();
  38. page.vanPageScroller = (page.vanPageScroller || []).filter(function (item) {
  39. return item !== scroller;
  40. });
  41. }
  42. });
  43. };
  44. exports.pageScrollMixin = pageScrollMixin;