drag.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import $ from 'jquery';
  2. import grid from './grid';
  3. import model from './index';
  4. class Drag {
  5. public oSelf: JQuery<HTMLElement> | null = null
  6. private targetPosition: { left: number, top: number } | null = null
  7. private mouseClientX = 0
  8. private mouseCurrentX = 0
  9. private targetOffsetX = 0
  10. private targetOffsetY = 0
  11. public targetRowSpan = 0
  12. public targetColSpan = 0
  13. public model: JQuery<HTMLElement> | null = null
  14. public container: JQuery<HTMLElement> | null = null
  15. private _move: ((e: JQuery.MouseMoveEvent) => Boolean) | null = null
  16. public init(container: JQuery<HTMLElement>) {
  17. this.model = $('.model', this.container = container);
  18. }
  19. public move(e: JQuery.MouseMoveEvent) {
  20. this._move && this._move(e);
  21. }
  22. public targetDown({ clientX, clientY, currentTarget }: JQuery.MouseDownEvent) {
  23. this.oSelf = $(currentTarget);
  24. const { rowSpan, colSpan } = this.oSelf.trigger('clear').data('magnet');
  25. this.model!.css({
  26. width: colSpan * grid.size - grid.Margin,
  27. height: rowSpan * grid.size - grid.Margin,
  28. ...this.oSelf.position()
  29. }).show();
  30. this.targetRowSpan = rowSpan;
  31. this.targetColSpan = colSpan;
  32. this.targetPosition = this.oSelf.position();
  33. this.mouseClientX = this.mouseCurrentX = clientX;
  34. const { left, top } = this.oSelf.offset()!;
  35. this.targetOffsetX = clientX - left;
  36. this.targetOffsetY = clientY - top;
  37. this._move = this.targetMove;
  38. return false;
  39. }
  40. private targetMove({ clientY, clientX, screenX }: JQuery.MouseMoveEvent) {
  41. if (!this.oSelf || !this.container) return false;
  42. const offset = this.container.offset()!,
  43. maxLeft = this.container.width()! - this.oSelf.width()!,
  44. maxTop = this.container.height()! - this.oSelf.height()!,
  45. offsetX = clientX - this.mouseCurrentX,
  46. left = Math.min(Math.max(clientX - offset.left - this.targetOffsetX, 0), maxLeft),
  47. top = Math.min(Math.max(clientY - offset.top - this.targetOffsetY, 0), maxTop);
  48. this.oSelf.css({ left, top });
  49. if (screenX + 10 > window.screen.width) {
  50. model.timer = model.timer || window.setInterval(() => {
  51. if (left === maxLeft) { return model.resizeContainer() }
  52. return model.moveContainerLeft();
  53. }, 10);
  54. } else if (screenX < 10) {
  55. model.timer = model.timer || window.setInterval(model.moveContainerRight.bind(model), 15);
  56. } else {
  57. if (model.timer) {
  58. clearInterval(model.timer);
  59. model.timer = 0;
  60. }
  61. if (offsetX > 0 && left === maxLeft) {
  62. model.resizeContainer(offsetX);
  63. }
  64. }
  65. grid.check();
  66. this.mouseCurrentX = clientX;
  67. return false;
  68. }
  69. public containerDown({ clientX, currentTarget }: JQuery.MouseDownEvent) {
  70. this.oSelf = $(currentTarget).addClass('move');
  71. this.mouseClientX = this.mouseCurrentX = clientX;
  72. this._move = this.containerMove;
  73. }
  74. private containerMove({ clientX }: JQuery.MouseMoveEvent) {
  75. if (!this.oSelf) return false;
  76. let offsetx = clientX - this.mouseCurrentX,
  77. t = Math.min(Math.max(parseInt(this.oSelf.css('left')) + offsetx, model.dragWrapperWidth - this.oSelf.width()!), 0);
  78. this.oSelf!.css('left', t);
  79. model.scroll();
  80. this.mouseCurrentX = clientX;
  81. return false;
  82. }
  83. public clear({ clientX, target }: JQuery.MouseUpEvent) {
  84. if (!this.oSelf || !this.model) return false;
  85. if (model.timer) {
  86. clearInterval(model.timer);
  87. model.timer = 0;
  88. }
  89. const flag = clientX === this.mouseClientX;
  90. if (!this.oSelf.hasClass('move')) {
  91. this.oSelf.hasClass('err') && this.model.css(this.targetPosition!);
  92. this.oSelf.trigger('setPosition', this.model.position());
  93. this.oSelf.hasClass('addMagnet') ? this.oSelf.trigger('create') : this.oSelf.animate(this.model.position(), function () {
  94. flag || $(this).trigger('save');
  95. });
  96. this.model.hide();
  97. } else if (flag && target === this.oSelf.get(0)) {
  98. model.hideMask();
  99. }
  100. this.oSelf.removeClass('err move');
  101. this.mouseClientX = 0;
  102. this.oSelf = this._move = null;
  103. }
  104. }
  105. export default new Drag();