sortable.js 8.4 KB

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