playlist.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. let initPlaylist = function() {
  2. let el = document.getElementById('playlist')
  3. window.playlist = new Sortable(el, {
  4. group: "videos", // or { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] }
  5. sort: true, // sorting inside list
  6. delay: 0, // time in milliseconds to define when the sorting should start
  7. delayOnTouchOnly: false, // only delay if user is using touch
  8. touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
  9. store: null, // @see Store
  10. animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
  11. easing: "cubic-bezier(1, 0, 0, 1)", // Easing for animation. Defaults to null. See https://easings.net/ for examples.
  12. handle: ".hand", // Drag handle selector within list items
  13. // filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
  14. preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
  15. // draggable: ".item", // Specifies which items inside the element should be draggable
  16. dataIdAttr: 'data-id', // HTML attribute that is used by the `toArray()` method
  17. ghostClass: "sortable-ghost", // Class name for the drop placeholder
  18. chosenClass: "sortable-chosen", // Class name for the chosen item
  19. dragClass: "sortable-drag", // Class name for the dragging item
  20. swapThreshold: 1, // Threshold of the swap zone
  21. invertSwap: false, // Will always use inverted swap zone if set to true
  22. invertedSwapThreshold: 1, // Threshold of the inverted swap zone (will be set to swapThreshold value by default)
  23. direction: 'horizontal', // Direction of Sortable (will be detected automatically if not given)
  24. forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
  25. fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback
  26. fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body
  27. fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.
  28. removeCloneOnHide: true, // Remove the clone element when it is not showing, rather than just hiding it
  29. emptyInsertThreshold: 5, // px, distance mouse must be from empty sortable to insert drag element into it
  30. setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {
  31. dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent
  32. },
  33. // Element is chosen
  34. onChoose: function (/**Event*/evt) {
  35. evt.oldIndex; // element index within parent
  36. },
  37. // Element is unchosen
  38. onUnchoose: function(/**Event*/evt) {
  39. // same properties as onEnd
  40. },
  41. // Element dragging started
  42. onStart: function (/**Event*/evt) {
  43. evt.oldIndex; // element index within parent
  44. },
  45. // Element dragging ended
  46. onEnd: function (/**Event*/evt) {
  47. var itemEl = evt.item; // dragged HTMLElement
  48. evt.to; // target list
  49. evt.from; // previous list
  50. evt.oldIndex; // element's old index within old parent
  51. evt.newIndex; // element's new index within new parent
  52. evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
  53. evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
  54. evt.clone // the clone element
  55. evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
  56. },
  57. // Element is dropped into the list from another list
  58. onAdd: function (/**Event*/evt) {
  59. // same properties as onEnd
  60. console.log("vidéo Ajouté", evt)
  61. },
  62. // Changed sorting within list
  63. onUpdate: function (/**Event*/evt) {
  64. // same properties as onEnd
  65. console.log("Update VidéoList", evt);
  66. },
  67. // Called by any change to the list (add / update / remove)
  68. onSort: function (/**Event*/evt) {
  69. // same properties as onEnd
  70. },
  71. // Element is removed from the list into another list
  72. onRemove: function (/**Event*/evt) {
  73. // same properties as onEnd
  74. },
  75. // Attempt to drag a filtered element
  76. onFilter: function (/**Event*/evt) {
  77. var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.
  78. },
  79. // Event when you move an item in the list or between lists
  80. onMove: function (/**Event*/evt, /**Event*/originalEvent) {
  81. // Example: https://jsbin.com/nawahef/edit?js,output
  82. evt.dragged; // dragged HTMLElement
  83. evt.draggedRect; // DOMRect {left, top, right, bottom}
  84. evt.related; // HTMLElement on which have guided
  85. evt.relatedRect; // DOMRect
  86. evt.willInsertAfter; // Boolean that is true if Sortable will insert drag element after target by default
  87. originalEvent.clientY; // mouse position
  88. // return false; — for cancel
  89. // return -1; — insert before target
  90. // return 1; — insert after target
  91. // return true; — keep default insertion point based on the direction
  92. // return void; — keep default insertion point based on the direction
  93. },
  94. // Called when creating a clone of element
  95. onClone: function (/**Event*/evt) {
  96. var origEl = evt.item;
  97. var cloneEl = evt.clone;
  98. },
  99. // Called when dragging element changes position
  100. onChange: function(evt) {
  101. evt.newIndex // most likely why this event is used is to get the dragging element's current index
  102. // same properties as onEnd
  103. // console.log("change", evt.oldIndex, evt.newIndex);
  104. if (evt.oldIndex == window.curVideo) {
  105. window.curVideo = evt.newIndex
  106. }
  107. let currentPlaylist = window.playlist.el.getElementsByTagName("li")
  108. for (let index = 0; index < currentPlaylist.length; index++) {
  109. const element = currentPlaylist[index];
  110. element.setAttribute('index', index)
  111. }
  112. }
  113. });
  114. }
  115. export { initPlaylist };