playlist.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. let currentPlaylist = []
  2. class Playlist {
  3. constructor(){
  4. let self = this
  5. this.addVideo = function(){
  6. const dialogConfig = {
  7. title: 'Select video files',
  8. buttonLabel: 'Validate Selection',
  9. filters: [
  10. { name: 'Movies', extensions: ['mkv', 'avi', 'mp4', "m4v", "webm", "ogv"] }
  11. ],
  12. properties: ['openFile', 'multiSelections']
  13. };
  14. electron.openDialog('showOpenDialog', dialogConfig)
  15. .then( (result) => {
  16. window.videoPlayer.src = result?.filePaths[0]
  17. window.videoPlayer.play()
  18. self.createPlaylist(result.filePaths)
  19. })
  20. .catch( (err) => {console.log("err", err)})
  21. }
  22. this.createPlaylist = function(list){
  23. let playlist = document.getElementById('playlist')
  24. let emptyFlag = document.getElementById('emptyFlag')
  25. emptyFlag.style.display = "none"
  26. list?.forEach( (source) => {
  27. currentPlaylist.push(source)
  28. localStorage.setItem('playlist', currentPlaylist.join("|"));
  29. let pathfull = source.split("/")
  30. let filename = pathfull[pathfull.length - 1]
  31. let videoNeo = document.createElement('li')
  32. let delButton = document.createElement('button')
  33. delButton.className = "delete is-small"
  34. delButton.addEventListener('click', function (event) {
  35. console.log("db click")
  36. self.removeVideo(event.srcElement.parentNode)
  37. });
  38. videoNeo.innerHTML = `<span class="hand"> </span>${filename}`
  39. videoNeo.setAttribute("path", source);
  40. videoNeo.setAttribute("index", playlist.getElementsByTagName('li').length);
  41. videoNeo.appendChild(delButton)
  42. videoNeo.addEventListener('dblclick', function (event) {
  43. videoPlayer.src = videoNeo.getAttribute('path')
  44. window.curVideo = videoNeo.getAttribute('index')
  45. window.videoPlayer.play()
  46. });
  47. playlist.appendChild(videoNeo)
  48. // console.log("window.playlist", window.playlist);
  49. })
  50. }
  51. this.removeVideo = function(val) {
  52. var emptyFlag = document.getElementById('emptyFlag')
  53. var listElements = document.getElementById("playlist");
  54. let sourcePath = val.getAttribute("path")
  55. let sourceIndex = currentPlaylist.indexOf(sourcePath)
  56. listElements.removeChild(val);
  57. if ( sourceIndex !== -1) {
  58. currentPlaylist.splice(sourceIndex,1);
  59. }
  60. // If playlist is empty
  61. if (listElements.childElementCount == 1) {
  62. emptyFlag.style.display = "flex"
  63. window.videoPlayer.pause()
  64. window.videoPlayer.src = "./assets/preview.mp4";
  65. window.videoPlayer.play()
  66. }
  67. //update playlist cache
  68. localStorage.setItem('playlist', currentPlaylist.join("|"));
  69. }
  70. this.initPlaylist = function() {
  71. let el = document.getElementById('playlist')
  72. window.playlist = new Sortable(el, {
  73. group: "videos", // or { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] }
  74. sort: true, // sorting inside list
  75. delay: 0, // time in milliseconds to define when the sorting should start
  76. delayOnTouchOnly: false, // only delay if user is using touch
  77. touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event
  78. store: null, // @see Store
  79. animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
  80. easing: "cubic-bezier(1, 0, 0, 1)", // Easing for animation. Defaults to null. See https://easings.net/ for examples.
  81. handle: ".hand", // Drag handle selector within list items
  82. // filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
  83. preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter`
  84. // draggable: ".item", // Specifies which items inside the element should be draggable
  85. dataIdAttr: 'data-id', // HTML attribute that is used by the `toArray()` method
  86. ghostClass: "sortable-ghost", // Class name for the drop placeholder
  87. chosenClass: "sortable-chosen", // Class name for the chosen item
  88. dragClass: "sortable-drag", // Class name for the dragging item
  89. swapThreshold: 1, // Threshold of the swap zone
  90. invertSwap: false, // Will always use inverted swap zone if set to true
  91. invertedSwapThreshold: 1, // Threshold of the inverted swap zone (will be set to swapThreshold value by default)
  92. direction: 'horizontal', // Direction of Sortable (will be detected automatically if not given)
  93. forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
  94. fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback
  95. fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body
  96. fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag.
  97. removeCloneOnHide: true, // Remove the clone element when it is not showing, rather than just hiding it
  98. emptyInsertThreshold: 5, // px, distance mouse must be from empty sortable to insert drag element into it
  99. setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) {
  100. dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent
  101. },
  102. // Element is chosen
  103. onChoose: function (/**Event*/evt) {
  104. evt.oldIndex; // element index within parent
  105. },
  106. // Element is unchosen
  107. onUnchoose: function(/**Event*/evt) {
  108. // same properties as onEnd
  109. },
  110. // Element dragging started
  111. onStart: function (/**Event*/evt) {
  112. evt.oldIndex; // element index within parent
  113. },
  114. // Element dragging ended
  115. onEnd: function (/**Event*/evt) {
  116. var itemEl = evt.item; // dragged HTMLElement
  117. evt.to; // target list
  118. evt.from; // previous list
  119. evt.oldIndex; // element's old index within old parent
  120. evt.newIndex; // element's new index within new parent
  121. evt.oldDraggableIndex; // element's old index within old parent, only counting draggable elements
  122. evt.newDraggableIndex; // element's new index within new parent, only counting draggable elements
  123. evt.clone // the clone element
  124. evt.pullMode; // when item is in another sortable: `"clone"` if cloning, `true` if moving
  125. },
  126. // Element is dropped into the list from another list
  127. onAdd: function (/**Event*/evt) {
  128. // same properties as onEnd
  129. console.log("vidéo Ajouté", evt)
  130. },
  131. // Changed sorting within list
  132. onUpdate: function (/**Event*/evt) {
  133. // same properties as onEnd
  134. console.log("Update VidéoList", evt);
  135. },
  136. // Called by any change to the list (add / update / remove)
  137. onSort: function (/**Event*/evt) {
  138. // same properties as onEnd
  139. },
  140. // Element is removed from the list into another list
  141. onRemove: function (/**Event*/evt) {
  142. // same properties as onEnd
  143. },
  144. // Attempt to drag a filtered element
  145. onFilter: function (/**Event*/evt) {
  146. var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event.
  147. },
  148. // Event when you move an item in the list or between lists
  149. onMove: function (/**Event*/evt, /**Event*/originalEvent) {
  150. // Example: https://jsbin.com/nawahef/edit?js,output
  151. evt.dragged; // dragged HTMLElement
  152. evt.draggedRect; // DOMRect {left, top, right, bottom}
  153. evt.related; // HTMLElement on which have guided
  154. evt.relatedRect; // DOMRect
  155. evt.willInsertAfter; // Boolean that is true if Sortable will insert drag element after target by default
  156. originalEvent.clientY; // mouse position
  157. // return false; — for cancel
  158. // return -1; — insert before target
  159. // return 1; — insert after target
  160. // return true; — keep default insertion point based on the direction
  161. // return void; — keep default insertion point based on the direction
  162. },
  163. // Called when creating a clone of element
  164. onClone: function (/**Event*/evt) {
  165. var origEl = evt.item;
  166. var cloneEl = evt.clone;
  167. },
  168. // Called when dragging element changes position
  169. onChange: function(evt) {
  170. evt.newIndex // most likely why this event is used is to get the dragging element's current index
  171. // same properties as onEnd
  172. // console.log("change", evt.oldIndex, evt.newIndex);
  173. if (evt.oldIndex == window.curVideo) {
  174. window.curVideo = evt.newIndex
  175. }
  176. let currentPlaylist = window.playlist.el.getElementsByTagName("li")
  177. for (let index = 0; index < currentPlaylist.length; index++) {
  178. const element = currentPlaylist[index];
  179. element.setAttribute('index', index)
  180. }
  181. }
  182. });
  183. }
  184. }
  185. }
  186. export { Playlist };