resizer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  2. var Resizer = function () {
  3. function Resizer(containerSelector, resizerOptions) {
  4. if (resizerOptions === void 0) {
  5. resizerOptions = {};
  6. }
  7. this.containerSelector = containerSelector;
  8. this.resizerOptions = resizerOptions;
  9. this.offsetX = 0;
  10. this.dragging = false;
  11. this.options = _extends(Resizer.defaultOptions, this.resizerOptions, {});
  12. this.container = Resizer.getElement(containerSelector);
  13. this.target = this.container.firstElementChild;
  14. if (this.container.Resizer) {
  15. this.remove();
  16. }
  17. this.setup();
  18. }
  19. Resizer.removeBySelector = function (input) {
  20. var container = Resizer.getElement(input);
  21. if (container.hasOwnProperty('Resizer')) {
  22. container.Resizer.remove();
  23. } else {
  24. throw new Error('Resizer doesn\'t exist on element');
  25. }
  26. };
  27. Resizer.getElement = function (input) {
  28. var el;
  29. if (!input) {
  30. throw new Error('Missing param, should be an element or selector');
  31. }
  32. if (typeof input === 'string') {
  33. el = document.querySelector(input);
  34. if (!el) {
  35. throw new Error("Can not find element from selector " + input);
  36. }
  37. } else {
  38. el = input;
  39. }
  40. return el;
  41. };
  42. Resizer.createHandle = function (handleClass) {
  43. var el = document.createElement('div');
  44. el.dataset.rzHandle = handleClass || '';
  45. el.style.cursor = 'ew-resize';
  46. return el;
  47. };
  48. Resizer.createGhost = function () {
  49. var el = document.createElement('div');
  50. el.style.position = 'absolute';
  51. el.style.top = '0';
  52. el.style.bottom = '0';
  53. el.style.display = 'none';
  54. el.style.zIndex = '99999';
  55. return el;
  56. };
  57. Resizer.prototype.remove = function () {
  58. delete this.container.Resizer;
  59. this.container.style.position = null;
  60. this.container.querySelector('[data-rz-handle]').remove();
  61. this.target.style.flex = null;
  62. };
  63. Resizer.prototype.setup = function () {
  64. var _this = this;
  65. this.setupDom();
  66. this.handle.addEventListener('mousedown', function (e) {
  67. return _this.onDown(e);
  68. });
  69. this.container.addEventListener('mouseup', function (e) {
  70. return _this.onUp(e);
  71. });
  72. this.container.addEventListener('mousemove', function (e) {
  73. return _this.onMove(e);
  74. });
  75. this.container.Resizer = this;
  76. };
  77. Resizer.prototype.setupDom = function () {
  78. this.container.style.position = 'relative';
  79. this.handle = Resizer.createHandle();
  80. this.ghost = Resizer.createGhost();
  81. this.handle.appendChild(this.ghost);
  82. this.container.insertBefore(this.handle, this.target.nextElementSibling);
  83. };
  84. Resizer.prototype.setDragging = function (value) {
  85. if (value === void 0) {
  86. value = true;
  87. }
  88. if (this.dragging) {
  89. this.ghost.style.display = 'none';
  90. this.target.style.flex = "0 0 " + this.handleX + "px";
  91. } else {
  92. this.ghost.style.display = 'block';
  93. }
  94. return this.dragging = value;
  95. };
  96. Resizer.prototype.setHandleX = function (value) {
  97. if (value < 0) {
  98. value = 0;
  99. }
  100. if (value > this.container.clientWidth) {
  101. value = this.container.clientWidth;
  102. }
  103. this.ghost.style.left = value + "px";
  104. return this.handleX = value;
  105. };
  106. Resizer.prototype.onDown = function (e) {
  107. e.preventDefault();
  108. if (!this.dragging) {
  109. this.offsetX = e.offsetX;
  110. this.setHandleX(e.pageX - this.container.getBoundingClientRect().left - this.offsetX);
  111. this.setDragging(true);
  112. }
  113. };
  114. Resizer.prototype.onUp = function (e) {
  115. e.preventDefault();
  116. if (this.dragging) {
  117. this.setHandleX(e.pageX - this.container.getBoundingClientRect().left - this.offsetX);
  118. this.setDragging(false);
  119. }
  120. };
  121. Resizer.prototype.onMove = function (e) {
  122. e.preventDefault();
  123. if (this.dragging) {
  124. var x = e.pageX - this.container.getBoundingClientRect().left - this.offsetX;
  125. if (e.shiftKey) {
  126. x = Math.ceil(x / 20) * 20;
  127. }
  128. this.setHandleX(x);
  129. }
  130. };
  131. return Resizer;
  132. }();
  133. Resizer.defaultOptions = {
  134. width: 8
  135. };
  136. //# sourceMappingURL=resizer.js.map