uni-refresh.wxs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var pullDown = {
  2. threshold: 95,
  3. maxHeight: 200,
  4. callRefresh: 'onrefresh',
  5. callPullingDown: 'onpullingdown',
  6. refreshSelector: '.uni-refresh'
  7. };
  8. function ready(newValue, oldValue, ownerInstance, instance) {
  9. var state = instance.getState()
  10. state.canPullDown = newValue;
  11. console.log(newValue);
  12. }
  13. function touchStart(e, instance) {
  14. var state = instance.getState();
  15. state.refreshInstance = instance.selectComponent(pullDown.refreshSelector);
  16. state.canPullDown = (state.refreshInstance != null && state.refreshInstance != undefined);
  17. if (!state.canPullDown) {
  18. return
  19. }
  20. console.log("touchStart");
  21. state.height = 0;
  22. state.touchStartY = e.touches[0].pageY || e.changedTouches[0].pageY;
  23. state.refreshInstance.setStyle({
  24. 'height': 0
  25. });
  26. state.refreshInstance.callMethod("onchange", true);
  27. }
  28. function touchMove(e, ownerInstance) {
  29. var instance = e.instance;
  30. var state = instance.getState();
  31. if (!state.canPullDown) {
  32. return
  33. }
  34. var oldHeight = state.height;
  35. var endY = e.touches[0].pageY || e.changedTouches[0].pageY;
  36. var height = endY - state.touchStartY;
  37. if (height > pullDown.maxHeight) {
  38. return;
  39. }
  40. var refreshInstance = state.refreshInstance;
  41. refreshInstance.setStyle({
  42. 'height': height + 'px'
  43. });
  44. height = height < pullDown.maxHeight ? height : pullDown.maxHeight;
  45. state.height = height;
  46. refreshInstance.callMethod(pullDown.callPullingDown, {
  47. height: height
  48. });
  49. }
  50. function touchEnd(e, ownerInstance) {
  51. var state = e.instance.getState();
  52. if (!state.canPullDown) {
  53. return
  54. }
  55. state.refreshInstance.callMethod("onchange", false);
  56. var refreshInstance = state.refreshInstance;
  57. if (state.height > pullDown.threshold) {
  58. refreshInstance.callMethod(pullDown.callRefresh);
  59. return;
  60. }
  61. refreshInstance.setStyle({
  62. 'height': 0
  63. });
  64. }
  65. function propObserver(newValue, oldValue, instance) {
  66. pullDown = newValue;
  67. }
  68. module.exports = {
  69. touchmove: touchMove,
  70. touchstart: touchStart,
  71. touchend: touchEnd,
  72. propObserver: propObserver
  73. }