DragDrop.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import Utils from '../core/Utils'
  2. import Plugin from './Plugin'
  3. import templateDragDrop from '../templates/dragdrop.hbs'
  4. /**
  5. * Drag & Drop plugin
  6. *
  7. */
  8. export default class DragDrop extends Plugin {
  9. constructor (core, opts) {
  10. super(core, opts)
  11. this.type = 'selecter'
  12. // set default options
  13. const defaultOptions = {
  14. bla: 'blabla',
  15. autoSubmit: true,
  16. modal: true
  17. }
  18. // merge default options with the ones set by user
  19. this.opts = defaultOptions
  20. Object.assign(this.opts, opts)
  21. // get the element where the Drag & Drop event will occur
  22. this.dropzone = document.querySelectorAll(this.opts.selector)[0]
  23. this.dropzoneInput = document.querySelectorAll('.UppyDragDrop-input')[0]
  24. this.status = document.querySelectorAll('.UppyDragDrop-status')[0]
  25. this.isDragDropSupported = this.checkDragDropSupport()
  26. // crazy stuff so that ‘this’ will behave in class
  27. this.listenForEvents = this.listenForEvents.bind(this)
  28. this.handleDrop = this.handleDrop.bind(this)
  29. this.checkDragDropSupport = this.checkDragDropSupport.bind(this)
  30. this.handleInputChange = this.handleInputChange.bind(this)
  31. console.log(templateDragDrop({
  32. chooseFile: 'smth',
  33. orDragDrop: 'going on here'
  34. }))
  35. }
  36. /**
  37. * Checks if the browser supports Drag & Drop
  38. * @return {Boolean} true if supported, false otherwise
  39. */
  40. checkDragDropSupport () {
  41. const div = document.createElement('div')
  42. if (!('draggable' in div) || !('ondragstart' in div && 'ondrop' in div)) {
  43. return false
  44. }
  45. if (!('FormData' in window)) {
  46. return false
  47. }
  48. if (!('FileReader' in window)) {
  49. return false
  50. }
  51. return true
  52. }
  53. listenForEvents () {
  54. console.log(`waiting for some files to be dropped on ${this.opts.selector}`)
  55. if (this.isDragDropSupported) {
  56. Utils.addClass(this.dropzone, 'is-dragdrop-supported')
  57. }
  58. // prevent default actions for all drag & drop events
  59. const strEvents = 'drag dragstart dragend dragover dragenter dragleave drop'
  60. Utils.addListenerMulti(this.dropzone, strEvents, (e) => {
  61. e.preventDefault()
  62. e.stopPropagation()
  63. })
  64. // Toggle is-dragover state when files are dragged over or dropped
  65. Utils.addListenerMulti(this.dropzone, 'dragover dragenter', (e) => {
  66. Utils.addClass(this.dropzone, 'is-dragover')
  67. })
  68. Utils.addListenerMulti(this.dropzone, 'dragleave dragend drop', (e) => {
  69. Utils.removeClass(this.dropzone, 'is-dragover')
  70. })
  71. const onDrop = new Promise((resolve, reject) => {
  72. this.dropzone.addEventListener('drop', (e) => {
  73. resolve(this.handleDrop.bind(null, e))
  74. })
  75. })
  76. const onInput = new Promise((resolve, reject) => {
  77. this.dropzoneInput.addEventListener('change', (e) => {
  78. resolve(this.handleInputChange.bind(null, e))
  79. })
  80. })
  81. return Promise.race([onDrop, onInput]).then(handler => handler())
  82. // this.dropzone.addEventListener('drop', this.handleDrop);
  83. // this.dropzoneInput.addEventListener('change', this.handleInputChange);
  84. }
  85. displayStatus (status) {
  86. this.status.innerHTML = status
  87. }
  88. handleDrop (e) {
  89. console.log('all right, someone dropped something here...')
  90. const files = e.dataTransfer.files
  91. // files = Array.from(files);
  92. // const formData = new FormData(this.dropzone);
  93. // console.log('pizza', formData);
  94. // for (var i = 0; i < files.length; i++) {
  95. // formData.append('file', files[i]);
  96. // console.log('pizza', files[i]);
  97. // }
  98. return Promise.resolve({from: 'DragDrop', files: files})
  99. }
  100. handleInputChange () {
  101. // const fileInput = document.querySelectorAll('.UppyDragDrop-input')[0];
  102. const formData = new FormData(this.dropzone)
  103. console.log('@todo: No support for formData yet', formData)
  104. const files = []
  105. return Promise.resolve({from: 'DragDrop', files: files})
  106. }
  107. run (results) {
  108. console.log({
  109. class: 'DragDrop',
  110. method: 'run',
  111. results: results
  112. })
  113. return this.listenForEvents()
  114. }
  115. }