uni-refresh.wxs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. state.height = 0;
  21. state.touchStartY = e.touches[0].pageY || e.changedTouches[0].pageY;
  22. state.refreshInstance.setStyle({
  23. 'height': 0
  24. });
  25. state.refreshInstance.callMethod("onchange", true);
  26. }
  27. function touchMove(e, ownerInstance) {
  28. var instance = e.instance;
  29. var state = instance.getState();
  30. if (!state.canPullDown) {
  31. return
  32. }
  33. var oldHeight = state.height;
  34. var endY = e.touches[0].pageY || e.changedTouches[0].pageY;
  35. var height = endY - state.touchStartY;
  36. if (height > pullDown.maxHeight) {
  37. return;
  38. }
  39. var refreshInstance = state.refreshInstance;
  40. refreshInstance.setStyle({
  41. 'height': height + 'px'
  42. });
  43. height = height < pullDown.maxHeight ? height : pullDown.maxHeight;
  44. state.height = height;
  45. refreshInstance.callMethod(pullDown.callPullingDown, {
  46. height: height
  47. });
  48. }
  49. function touchEnd(e, ownerInstance) {
  50. var state = e.instance.getState();
  51. if (!state.canPullDown) {
  52. return
  53. }
  54. state.refreshInstance.callMethod("onchange", false);
  55. var refreshInstance = state.refreshInstance;
  56. if (state.height > pullDown.threshold) {
  57. refreshInstance.callMethod(pullDown.callRefresh);
  58. return;
  59. }
  60. refreshInstance.setStyle({
  61. 'height': 0
  62. });
  63. }
  64. function propObserver(newValue, oldValue, instance) {
  65. pullDown = newValue;
  66. }
  67. module.exports = {
  68. touchmove: touchMove,
  69. touchstart: touchStart,
  70. touchend: touchEnd,
  71. propObserver: propObserver
  72. }