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