sortable.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. "use strict";
  2. function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
  3. function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  6. window.addEventListener('touchmove', function () {});
  7. var Sortable =
  8. /*#__PURE__*/
  9. function () {
  10. function Sortable(list, options) {
  11. _classCallCheck(this, Sortable);
  12. this.list = typeof list === 'string' ? document.querySelector(list) : list;
  13. this.items = Array.from(this.list.children);
  14. this.animation = false;
  15. this.options = Object.assign({
  16. animationSpeed: 200,
  17. animationEasing: 'ease-out'
  18. }, options || {});
  19. this.dragStart = this.dragStart.bind(this);
  20. this.dragMove = this.dragMove.bind(this);
  21. this.dragEnd = this.dragEnd.bind(this);
  22. this.list.addEventListener('touchstart', this.dragStart, false);
  23. this.list.addEventListener('mousedown', this.dragStart, false);
  24. }
  25. _createClass(Sortable, [{
  26. key: "dragStart",
  27. value: function dragStart(e) {
  28. var _this = this;
  29. if (this.animation) return;
  30. if (e.type === 'mousedown' && e.which !== 1) return;
  31. if (e.type === 'touchstart' && e.touches.length > 1) return;
  32. this.handle = null;
  33. var el = e.target;
  34. while (el) {
  35. if (el.hasAttribute('data-sortable-handle')) this.handle = el;
  36. if (el.hasAttribute('data-sortable-item')) this.item = el;
  37. if (el.hasAttribute('data-sortable-list')) break;
  38. el = el.parentElement;
  39. }
  40. if (!this.handle) return;
  41. this.list.style.position = 'relative';
  42. this.list.style.height = this.list.offsetHeight + 'px';
  43. this.item.classList.add('is-dragging');
  44. this.itemHeight = this.items[1].offsetTop;
  45. this.listHeight = this.list.offsetHeight;
  46. this.startTouchY = this.getDragY(e);
  47. this.startTop = this.item.offsetTop;
  48. var offsetsTop = this.items.map(function (item) {
  49. return item.offsetTop;
  50. });
  51. this.items.forEach(function (item, index) {
  52. item.style.position = 'absolute';
  53. item.style.top = 0;
  54. item.style.left = 0;
  55. item.style.width = '100%';
  56. item.style.transform = "translateY(".concat(offsetsTop[index], "px)");
  57. item.style.zIndex = item == _this.item ? 2 : 1;
  58. });
  59. setTimeout(function () {
  60. _this.items.forEach(function (item) {
  61. if (_this.item == item) return;
  62. item.style.transition = "transform ".concat(_this.options.animationSpeed, "ms ").concat(_this.options.animationEasing);
  63. });
  64. });
  65. this.positions = this.items.map(function (item, index) {
  66. return index;
  67. });
  68. this.position = Math.round(this.startTop / this.listHeight * this.items.length);
  69. this.touch = e.type == 'touchstart';
  70. window.addEventListener(this.touch ? 'touchmove' : 'mousemove', this.dragMove, {
  71. passive: false
  72. });
  73. window.addEventListener(this.touch ? 'touchend' : 'mouseup', this.dragEnd, false);
  74. }
  75. }, {
  76. key: "dragMove",
  77. value: function dragMove(e) {
  78. var _this2 = this;
  79. if (this.animation) return;
  80. var top = this.startTop + this.getDragY(e) - this.startTouchY;
  81. var newPosition = Math.round(top / this.listHeight * this.items.length);
  82. this.item.style.transform = "translateY(".concat(top, "px)");
  83. this.positions.forEach(function (index) {
  84. if (index == _this2.position || index != newPosition) return;
  85. _this2.swapElements(_this2.positions, _this2.position, index);
  86. _this2.position = index;
  87. });
  88. this.items.forEach(function (item, index) {
  89. if (item == _this2.item) return;
  90. item.style.transform = "translateY(".concat(_this2.positions.indexOf(index) * _this2.itemHeight, "px)");
  91. });
  92. e.preventDefault();
  93. }
  94. }, {
  95. key: "dragEnd",
  96. value: function dragEnd(e) {
  97. var _this3 = this;
  98. this.animation = true;
  99. this.item.style.transition = "all ".concat(this.options.animationSpeed, "ms ").concat(this.options.animationEasing);
  100. this.item.style.transform = "translateY(".concat(this.position * this.itemHeight, "px)");
  101. this.item.classList.remove('is-dragging');
  102. setTimeout(function () {
  103. _this3.list.style.position = '';
  104. _this3.list.style.height = '';
  105. _this3.items.forEach(function (item) {
  106. item.style.top = '';
  107. item.style.left = '';
  108. item.style.right = '';
  109. item.style.position = '';
  110. item.style.transform = '';
  111. item.style.transition = '';
  112. item.style.width = '';
  113. item.style.zIndex = '';
  114. });
  115. _this3.positions.map(function (i) {
  116. return _this3.list.appendChild(_this3.items[i]);
  117. });
  118. _this3.items = Array.from(_this3.list.children);
  119. _this3.animation = false;
  120. }, this.options.animationSpeed);
  121. window.removeEventListener(this.touch ? 'touchmove' : 'mousemove', this.dragMove, {
  122. passive: false
  123. });
  124. window.removeEventListener(this.touch ? 'touchend' : 'mouseup', this.dragEnd, false);
  125. }
  126. }, {
  127. key: "swapElements",
  128. value: function swapElements(array, a, b) {
  129. var temp = array[a];
  130. array[a] = array[b];
  131. array[b] = temp;
  132. }
  133. }, {
  134. key: "getDragY",
  135. value: function getDragY(e) {
  136. return e.touches ? (e.touches[0] || e.changedTouches[0]).pageY : e.pageY;
  137. }
  138. }, {
  139. key: "removeItem",
  140. value: function removeItem(item) {
  141. this.items.splice(this.items.indexOf(item),1);
  142. item.remove();
  143. }
  144. }, {
  145. key: "addItemFrom",
  146. value: function addItemFrom(input) {
  147. var value = querySourceRaw(input);
  148. var inputElement = document.getElementById(input);
  149. if (inputElement) inputElement.value = '';
  150. var template = document.createElement('template');
  151. template.innerHTML =
  152. '<div class="list__item" data-sortable-item="data-sortable-item" style="">'+
  153. '<div class="list__item-close" onclick="removeSortableItem(\'#' + this.list.id + '\', this.parentNode);"></div>'+
  154. '<div class="list__item-content">'+
  155. '<div class="list__item-title">' + value + '</div>'+
  156. '</div>'+
  157. '<div class="list__item-handle" data-sortable-handle="data-sortable-handle"></div>'+
  158. '</div>'
  159. var new_item = template.content.firstChild;
  160. this.list.appendChild(new_item);
  161. this.items.push(new_item);
  162. }
  163. }, {
  164. key: "getValues",
  165. value: function getValues() {
  166. return Array.from(this.items.map(function(item) { return item.children[1].firstChild.innerHTML; }));
  167. }
  168. }]);
  169. return Sortable;
  170. }();
  171. var SortableMap = new Map([]);
  172. function createSortable(list) {
  173. SortableMap.set(list, new Sortable(list));
  174. }
  175. function removeSortableItem(list, item) {
  176. SortableMap.get(list).removeItem(item);
  177. }
  178. function addSortableItemFrom(list, input) {
  179. SortableMap.get(list).addItemFrom(input);
  180. }
  181. function getSortableValues(list) {
  182. console.log('getSortableValues',list)
  183. let sortable = SortableMap.get(list)
  184. if(sortable) {
  185. return sortable.getValues();
  186. } else {
  187. return Array.from([]);
  188. }
  189. }