page-scroll.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pageScrollMixin = void 0;
  4. var validator_1 = require("../common/validator");
  5. var utils_1 = require("../common/utils");
  6. function onPageScroll(event) {
  7. var _a = (0, utils_1.getCurrentPage)().vanPageScroller, vanPageScroller = _a === void 0 ? [] : _a;
  8. vanPageScroller.forEach(function (scroller) {
  9. if (typeof scroller === 'function') {
  10. // @ts-ignore
  11. scroller(event);
  12. }
  13. });
  14. }
  15. function pageScrollMixin(scroller) {
  16. return Behavior({
  17. attached: function () {
  18. var page = (0, utils_1.getCurrentPage)();
  19. if (!(0, utils_1.isDef)(page)) {
  20. return;
  21. }
  22. var _scroller = scroller.bind(this);
  23. var _a = page.vanPageScroller, vanPageScroller = _a === void 0 ? [] : _a;
  24. if ((0, validator_1.isFunction)(page.onPageScroll) && page.onPageScroll !== onPageScroll) {
  25. vanPageScroller.push(page.onPageScroll.bind(page));
  26. }
  27. vanPageScroller.push(_scroller);
  28. page.vanPageScroller = vanPageScroller;
  29. page.onPageScroll = onPageScroll;
  30. this._scroller = _scroller;
  31. },
  32. detached: function () {
  33. var _this = this;
  34. var page = (0, utils_1.getCurrentPage)();
  35. if (!(0, utils_1.isDef)(page) || !(0, utils_1.isDef)(page.vanPageScroller)) {
  36. return;
  37. }
  38. var vanPageScroller = page.vanPageScroller;
  39. var index = vanPageScroller.findIndex(function (v) { return v === _this._scroller; });
  40. if (index > -1) {
  41. page.vanPageScroller.splice(index, 1);
  42. }
  43. this._scroller = undefined;
  44. },
  45. });
  46. }
  47. exports.pageScrollMixin = pageScrollMixin;