app.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. var example1 = document.getElementById('example1'),
  2. example2Left = document.getElementById('example2-left'),
  3. example2Right = document.getElementById('example2-right'),
  4. example3Left = document.getElementById('example3-left'),
  5. example3Right = document.getElementById('example3-right'),
  6. example4Left = document.getElementById('example4-left'),
  7. example4Right = document.getElementById('example4-right'),
  8. example5 = document.getElementById('example5'),
  9. example6 = document.getElementById('example6'),
  10. example7 = document.getElementById('example7'),
  11. gridDemo = document.getElementById('gridDemo'),
  12. multiDragDemo = document.getElementById('multiDragDemo'),
  13. swapDemo = document.getElementById('swapDemo');
  14. // Example 1 - Simple list
  15. new Sortable(example1, {
  16. animation: 150,
  17. ghostClass: 'blue-background-class'
  18. });
  19. // Example 2 - Shared lists
  20. new Sortable(example2Left, {
  21. group: 'shared', // set both lists to same group
  22. animation: 150
  23. });
  24. new Sortable(example2Right, {
  25. group: 'shared',
  26. animation: 150
  27. });
  28. // Example 3 - Cloning
  29. new Sortable(example3Left, {
  30. group: {
  31. name: 'shared',
  32. pull: 'clone' // To clone: set pull to 'clone'
  33. },
  34. animation: 150
  35. });
  36. new Sortable(example3Right, {
  37. group: {
  38. name: 'shared',
  39. pull: 'clone'
  40. },
  41. animation: 150
  42. });
  43. // Example 4 - No Sorting
  44. new Sortable(example4Left, {
  45. group: {
  46. name: 'shared',
  47. pull: 'clone',
  48. put: false // Do not allow items to be put into this list
  49. },
  50. animation: 150,
  51. sort: false // To disable sorting: set sort to false
  52. });
  53. new Sortable(example4Right, {
  54. group: 'shared',
  55. animation: 150
  56. });
  57. // Example 5 - Handle
  58. new Sortable(example5, {
  59. handle: '.handle', // handle class
  60. animation: 150
  61. });
  62. // Example 6 - Filter
  63. new Sortable(example6, {
  64. filter: '.filtered',
  65. animation: 150
  66. });
  67. // Example 7 - Thresholds
  68. var example7Sortable = new Sortable(example7, {
  69. animation: 150
  70. });
  71. var example7SwapThreshold = 1;
  72. var example7SwapThresholdInput = document.getElementById('example7SwapThresholdInput');
  73. var example7SwapThresholdCode = document.getElementById('example7SwapThresholdCode');
  74. var example7SwapThresholdIndicators = [].slice.call(document.querySelectorAll('.swap-threshold-indicator'));
  75. var example7InvertSwapInput = document.getElementById('example7InvertSwapInput');
  76. var example7InvertSwapCode = document.getElementById('example7InvertSwapCode');
  77. var example7InvertedSwapThresholdIndicators = [].slice.call(document.querySelectorAll('.inverted-swap-threshold-indicator'));
  78. var example7Squares = [].slice.call(document.querySelectorAll('.square'));
  79. var activeIndicators = example7SwapThresholdIndicators;
  80. var example7DirectionInput = document.getElementById('example7DirectionInput');
  81. var example7SizeProperty = 'width';
  82. function renderThresholdWidth(evt) {
  83. example7SwapThreshold = Number(evt.target.value);
  84. example7SwapThresholdCode.innerHTML = evt.target.value.indexOf('.') > -1 ? evt.target.value.padEnd(4, '0') : evt.target.value;
  85. for (var i = 0; i < activeIndicators.length; i++) {
  86. activeIndicators[i].style[example7SizeProperty] = (evt.target.value * 100) /
  87. (activeIndicators == example7SwapThresholdIndicators ? 1 : 2) + '%';
  88. }
  89. example7Sortable.option('swapThreshold', example7SwapThreshold);
  90. }
  91. example7SwapThresholdInput.addEventListener('input', renderThresholdWidth);
  92. example7InvertSwapInput.addEventListener('input', function(evt) {
  93. example7Sortable.option('invertSwap', evt.target.checked);
  94. for (var i = 0; i < activeIndicators.length; i++) {
  95. activeIndicators[i].style.display = 'none';
  96. }
  97. if (evt.target.checked) {
  98. example7InvertSwapCode.style.display = '';
  99. activeIndicators = example7InvertedSwapThresholdIndicators;
  100. } else {
  101. example7InvertSwapCode.style.display = 'none';
  102. activeIndicators = example7SwapThresholdIndicators;
  103. }
  104. renderThresholdWidth({
  105. target: example7SwapThresholdInput
  106. });
  107. for (i = 0; i < activeIndicators.length; i++) {
  108. activeIndicators[i].style.display = '';
  109. }
  110. });
  111. function renderDirection(evt) {
  112. for (var i = 0; i < example7Squares.length; i++) {
  113. example7Squares[i].style.display = evt.target.value === 'h' ? 'inline-block' : 'block';
  114. }
  115. for (i = 0; i < example7InvertedSwapThresholdIndicators.length; i++) {
  116. /* jshint expr:true */
  117. evt.target.value === 'h' && (example7InvertedSwapThresholdIndicators[i].style.height = '100%');
  118. evt.target.value === 'v' && (example7InvertedSwapThresholdIndicators[i].style.width = '100%');
  119. }
  120. for (i = 0; i < example7SwapThresholdIndicators.length; i++) {
  121. if (evt.target.value === 'h') {
  122. example7SwapThresholdIndicators[i].style.height = '100%';
  123. example7SwapThresholdIndicators[i].style.marginLeft = '50%';
  124. example7SwapThresholdIndicators[i].style.transform = 'translateX(-50%)';
  125. example7SwapThresholdIndicators[i].style.marginTop = '0';
  126. } else {
  127. example7SwapThresholdIndicators[i].style.width = '100%';
  128. example7SwapThresholdIndicators[i].style.marginTop = '50%';
  129. example7SwapThresholdIndicators[i].style.transform = 'translateY(-50%)';
  130. example7SwapThresholdIndicators[i].style.marginLeft = '0';
  131. }
  132. }
  133. if (evt.target.value === 'h') {
  134. example7SizeProperty = 'width';
  135. example7Sortable.option('direction', 'horizontal');
  136. } else {
  137. example7SizeProperty = 'height';
  138. example7Sortable.option('direction', 'vertical');
  139. }
  140. renderThresholdWidth({
  141. target: example7SwapThresholdInput
  142. });
  143. }
  144. example7DirectionInput.addEventListener('input', renderDirection);
  145. renderDirection({
  146. target: example7DirectionInput
  147. });
  148. // Grid demo
  149. new Sortable(gridDemo, {
  150. animation: 150,
  151. ghostClass: 'blue-background-class'
  152. });
  153. // Nested demo
  154. var nestedSortables = [].slice.call(document.querySelectorAll('.nested-sortable'));
  155. // Loop through each nested sortable element
  156. for (var i = 0; i < nestedSortables.length; i++) {
  157. new Sortable(nestedSortables[i], {
  158. group: 'nested',
  159. animation: 150,
  160. fallbackOnBody: true,
  161. swapThreshold: 0.65
  162. });
  163. }
  164. // MultiDrag demo
  165. new Sortable(multiDragDemo, {
  166. multiDrag: true,
  167. selectedClass: 'selected',
  168. animation: 150
  169. });
  170. // Swap demo
  171. new Sortable(swapDemo, {
  172. swap: true,
  173. swapClass: 'highlight',
  174. animation: 150
  175. });