draggable.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { SuperComponent, wxComponent } from '../../common/src/index';
  17. import config from '../../common/config';
  18. import props from './props';
  19. import { getRect, systemInfo } from '../../common/utils';
  20. const { prefix } = config;
  21. const name = `${prefix}-draggable`;
  22. let Draggable = class Draggable extends SuperComponent {
  23. constructor() {
  24. super(...arguments);
  25. this.properties = props;
  26. this.externalClasses = [`${prefix}-class`];
  27. this.data = {
  28. prefix,
  29. classPrefix: name,
  30. };
  31. this.lifetimes = {
  32. ready() {
  33. this.computedRect();
  34. },
  35. };
  36. this.methods = {
  37. onTouchStart(e) {
  38. if (this.properties.direction === 'none')
  39. return;
  40. this.startX = e.touches[0].clientX + systemInfo.windowWidth - this.rect.right;
  41. this.startY = e.touches[0].clientY + systemInfo.windowHeight - this.rect.bottom;
  42. this.triggerEvent('start', { startX: this.startX, startY: this.startY, rect: this.rect, e });
  43. },
  44. onTouchMove(e) {
  45. if (this.properties.direction === 'none')
  46. return;
  47. let x = this.startX - e.touches[0].clientX;
  48. let y = this.startY - e.touches[0].clientY;
  49. if (this.properties.direction === 'vertical') {
  50. x = systemInfo.windowWidth - this.rect.right;
  51. }
  52. if (this.properties.direction === 'horizontal') {
  53. y = systemInfo.windowHeight - this.rect.bottom;
  54. }
  55. this.triggerEvent('move', { x, y, rect: this.rect, e });
  56. },
  57. onTouchEnd(e) {
  58. return __awaiter(this, void 0, void 0, function* () {
  59. if (this.properties.direction === 'none')
  60. return;
  61. yield this.computedRect();
  62. this.triggerEvent('end', { rect: this.rect, e });
  63. });
  64. },
  65. computedRect() {
  66. return __awaiter(this, void 0, void 0, function* () {
  67. this.rect = { right: 0, bottom: 0, width: 0, height: 0 };
  68. try {
  69. this.rect = yield getRect(this, `.${this.data.classPrefix}`);
  70. }
  71. catch (e) {
  72. }
  73. });
  74. },
  75. };
  76. }
  77. };
  78. Draggable = __decorate([
  79. wxComponent()
  80. ], Draggable);
  81. export default Draggable;