drag-drop.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.DragDrop = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. // shim for using process in browser
  3. var process = module.exports = {};
  4. // cached from whatever global is present so that test runners that stub it
  5. // don't break things. But we need to wrap it in a try catch in case it is
  6. // wrapped in strict mode code which doesn't define any globals. It's inside a
  7. // function because try/catches deoptimize in certain engines.
  8. var cachedSetTimeout;
  9. var cachedClearTimeout;
  10. function defaultSetTimout() {
  11. throw new Error('setTimeout has not been defined');
  12. }
  13. function defaultClearTimeout () {
  14. throw new Error('clearTimeout has not been defined');
  15. }
  16. (function () {
  17. try {
  18. if (typeof setTimeout === 'function') {
  19. cachedSetTimeout = setTimeout;
  20. } else {
  21. cachedSetTimeout = defaultSetTimout;
  22. }
  23. } catch (e) {
  24. cachedSetTimeout = defaultSetTimout;
  25. }
  26. try {
  27. if (typeof clearTimeout === 'function') {
  28. cachedClearTimeout = clearTimeout;
  29. } else {
  30. cachedClearTimeout = defaultClearTimeout;
  31. }
  32. } catch (e) {
  33. cachedClearTimeout = defaultClearTimeout;
  34. }
  35. } ())
  36. function runTimeout(fun) {
  37. if (cachedSetTimeout === setTimeout) {
  38. //normal enviroments in sane situations
  39. return setTimeout(fun, 0);
  40. }
  41. // if setTimeout wasn't available but was latter defined
  42. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  43. cachedSetTimeout = setTimeout;
  44. return setTimeout(fun, 0);
  45. }
  46. try {
  47. // when when somebody has screwed with setTimeout but no I.E. maddness
  48. return cachedSetTimeout(fun, 0);
  49. } catch(e){
  50. try {
  51. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  52. return cachedSetTimeout.call(null, fun, 0);
  53. } catch(e){
  54. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  55. return cachedSetTimeout.call(this, fun, 0);
  56. }
  57. }
  58. }
  59. function runClearTimeout(marker) {
  60. if (cachedClearTimeout === clearTimeout) {
  61. //normal enviroments in sane situations
  62. return clearTimeout(marker);
  63. }
  64. // if clearTimeout wasn't available but was latter defined
  65. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  66. cachedClearTimeout = clearTimeout;
  67. return clearTimeout(marker);
  68. }
  69. try {
  70. // when when somebody has screwed with setTimeout but no I.E. maddness
  71. return cachedClearTimeout(marker);
  72. } catch (e){
  73. try {
  74. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  75. return cachedClearTimeout.call(null, marker);
  76. } catch (e){
  77. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  78. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  79. return cachedClearTimeout.call(this, marker);
  80. }
  81. }
  82. }
  83. var queue = [];
  84. var draining = false;
  85. var currentQueue;
  86. var queueIndex = -1;
  87. function cleanUpNextTick() {
  88. if (!draining || !currentQueue) {
  89. return;
  90. }
  91. draining = false;
  92. if (currentQueue.length) {
  93. queue = currentQueue.concat(queue);
  94. } else {
  95. queueIndex = -1;
  96. }
  97. if (queue.length) {
  98. drainQueue();
  99. }
  100. }
  101. function drainQueue() {
  102. if (draining) {
  103. return;
  104. }
  105. var timeout = runTimeout(cleanUpNextTick);
  106. draining = true;
  107. var len = queue.length;
  108. while(len) {
  109. currentQueue = queue;
  110. queue = [];
  111. while (++queueIndex < len) {
  112. if (currentQueue) {
  113. currentQueue[queueIndex].run();
  114. }
  115. }
  116. queueIndex = -1;
  117. len = queue.length;
  118. }
  119. currentQueue = null;
  120. draining = false;
  121. runClearTimeout(timeout);
  122. }
  123. process.nextTick = function (fun) {
  124. var args = new Array(arguments.length - 1);
  125. if (arguments.length > 1) {
  126. for (var i = 1; i < arguments.length; i++) {
  127. args[i - 1] = arguments[i];
  128. }
  129. }
  130. queue.push(new Item(fun, args));
  131. if (queue.length === 1 && !draining) {
  132. runTimeout(drainQueue);
  133. }
  134. };
  135. // v8 likes predictible objects
  136. function Item(fun, array) {
  137. this.fun = fun;
  138. this.array = array;
  139. }
  140. Item.prototype.run = function () {
  141. this.fun.apply(null, this.array);
  142. };
  143. process.title = 'browser';
  144. process.browser = true;
  145. process.env = {};
  146. process.argv = [];
  147. process.version = ''; // empty string to avoid regexp issues
  148. process.versions = {};
  149. function noop() {}
  150. process.on = noop;
  151. process.addListener = noop;
  152. process.once = noop;
  153. process.off = noop;
  154. process.removeListener = noop;
  155. process.removeAllListeners = noop;
  156. process.emit = noop;
  157. process.prependListener = noop;
  158. process.prependOnceListener = noop;
  159. process.listeners = function (name) { return [] }
  160. process.binding = function (name) {
  161. throw new Error('process.binding is not supported');
  162. };
  163. process.cwd = function () { return '/' };
  164. process.chdir = function (dir) {
  165. throw new Error('process.chdir is not supported');
  166. };
  167. process.umask = function() { return 0; };
  168. },{}],2:[function(require,module,exports){
  169. (function (process){
  170. module.exports = runParallel
  171. function runParallel (tasks, cb) {
  172. var results, pending, keys
  173. var isSync = true
  174. if (Array.isArray(tasks)) {
  175. results = []
  176. pending = tasks.length
  177. } else {
  178. keys = Object.keys(tasks)
  179. results = {}
  180. pending = keys.length
  181. }
  182. function done (err) {
  183. function end () {
  184. if (cb) cb(err, results)
  185. cb = null
  186. }
  187. if (isSync) process.nextTick(end)
  188. else end()
  189. }
  190. function each (i, err, result) {
  191. results[i] = result
  192. if (--pending === 0 || err) {
  193. done(err)
  194. }
  195. }
  196. if (!pending) {
  197. // empty
  198. done(null)
  199. } else if (keys) {
  200. // object
  201. keys.forEach(function (key) {
  202. tasks[key](function (err, result) { each(key, err, result) })
  203. })
  204. } else {
  205. // array
  206. tasks.forEach(function (task, i) {
  207. task(function (err, result) { each(i, err, result) })
  208. })
  209. }
  210. isSync = false
  211. }
  212. }).call(this,require('_process'))
  213. },{"_process":1}],"/":[function(require,module,exports){
  214. module.exports = dragDrop
  215. const parallel = require('run-parallel')
  216. function dragDrop (elem, listeners) {
  217. if (typeof elem === 'string') {
  218. const selector = elem
  219. elem = window.document.querySelector(elem)
  220. if (!elem) {
  221. throw new Error(`"${selector}" does not match any HTML elements`)
  222. }
  223. }
  224. if (!elem) {
  225. throw new Error(`"${elem}" is not a valid HTML element`)
  226. }
  227. if (typeof listeners === 'function') {
  228. listeners = { onDrop: listeners }
  229. }
  230. let timeout
  231. elem.addEventListener('dragenter', onDragEnter, false)
  232. elem.addEventListener('dragover', onDragOver, false)
  233. elem.addEventListener('dragleave', onDragLeave, false)
  234. elem.addEventListener('drop', onDrop, false)
  235. // Function to remove drag-drop listeners
  236. return function remove () {
  237. removeDragClass()
  238. elem.removeEventListener('dragenter', onDragEnter, false)
  239. elem.removeEventListener('dragover', onDragOver, false)
  240. elem.removeEventListener('dragleave', onDragLeave, false)
  241. elem.removeEventListener('drop', onDrop, false)
  242. }
  243. function onDragEnter (e) {
  244. if (listeners.onDragEnter) {
  245. listeners.onDragEnter(e)
  246. }
  247. // Prevent event
  248. e.stopPropagation()
  249. e.preventDefault()
  250. return false
  251. }
  252. function onDragOver (e) {
  253. e.stopPropagation()
  254. e.preventDefault()
  255. if (listeners.onDragOver) {
  256. listeners.onDragOver(e)
  257. }
  258. if (e.dataTransfer.items) {
  259. // Only add "drag" class when `items` contains items that are able to be
  260. // handled by the registered listeners (files vs. text)
  261. const items = Array.from(e.dataTransfer.items)
  262. const fileItems = items.filter(item => { return item.kind === 'file' })
  263. const textItems = items.filter(item => { return item.kind === 'string' })
  264. if (fileItems.length === 0 && !listeners.onDropText) return
  265. if (textItems.length === 0 && !listeners.onDrop) return
  266. if (fileItems.length === 0 && textItems.length === 0) return
  267. }
  268. elem.classList.add('drag')
  269. clearTimeout(timeout)
  270. e.dataTransfer.dropEffect = 'copy'
  271. return false
  272. }
  273. function onDragLeave (e) {
  274. e.stopPropagation()
  275. e.preventDefault()
  276. if (listeners.onDragLeave) {
  277. listeners.onDragLeave(e)
  278. }
  279. clearTimeout(timeout)
  280. timeout = setTimeout(removeDragClass, 50)
  281. return false
  282. }
  283. function onDrop (e) {
  284. e.stopPropagation()
  285. e.preventDefault()
  286. if (listeners.onDragLeave) {
  287. listeners.onDragLeave(e)
  288. }
  289. clearTimeout(timeout)
  290. removeDragClass()
  291. const pos = {
  292. x: e.clientX,
  293. y: e.clientY
  294. }
  295. // text drop support
  296. const text = e.dataTransfer.getData('text')
  297. if (text && listeners.onDropText) {
  298. listeners.onDropText(text, pos)
  299. }
  300. // File drop support. The `dataTransfer.items` API supports directories, so we
  301. // use it instead of `dataTransfer.files`, even though it's much more
  302. // complicated to use.
  303. // See: https://github.com/feross/drag-drop/issues/39
  304. if (listeners.onDrop && e.dataTransfer.items) {
  305. const fileList = e.dataTransfer.files
  306. // Handle directories in Chrome using the proprietary FileSystem API
  307. const items = Array.from(e.dataTransfer.items).filter(item => {
  308. return item.kind === 'file'
  309. })
  310. if (items.length === 0) return
  311. parallel(items.map(item => {
  312. return cb => {
  313. processEntry(item.webkitGetAsEntry(), cb)
  314. }
  315. }), (err, results) => {
  316. // This catches permission errors with file:// in Chrome. This should never
  317. // throw in production code, so the user does not need to use try-catch.
  318. if (err) throw err
  319. const entries = results.flat(Infinity)
  320. const files = entries.filter(item => {
  321. return item.isFile
  322. })
  323. const directories = entries.filter(item => {
  324. return item.isDirectory
  325. })
  326. listeners.onDrop(files, pos, fileList, directories)
  327. })
  328. }
  329. return false
  330. }
  331. function removeDragClass () {
  332. elem.classList.remove('drag')
  333. }
  334. }
  335. function processEntry (entry, cb) {
  336. let entries = []
  337. if (entry.isFile) {
  338. entry.file(file => {
  339. file.fullPath = entry.fullPath // preserve pathing for consumer
  340. file.isFile = true
  341. file.isDirectory = false
  342. cb(null, file)
  343. }, err => {
  344. cb(err)
  345. })
  346. } else if (entry.isDirectory) {
  347. const reader = entry.createReader()
  348. readEntries(reader)
  349. }
  350. function readEntries (reader) {
  351. reader.readEntries(entries_ => {
  352. if (entries_.length > 0) {
  353. entries = entries.concat(Array.from(entries_))
  354. readEntries(reader) // continue reading entries until `readEntries` returns no more
  355. } else {
  356. doneEntries()
  357. }
  358. })
  359. }
  360. function doneEntries () {
  361. parallel(entries.map(entry => {
  362. return cb => {
  363. processEntry(entry, cb)
  364. }
  365. }), (err, results) => {
  366. if (err) {
  367. cb(err)
  368. } else {
  369. results.push({
  370. fullPath: entry.fullPath,
  371. name: entry.name,
  372. isFile: false,
  373. isDirectory: true
  374. })
  375. cb(null, results)
  376. }
  377. })
  378. }
  379. }
  380. },{"run-parallel":2}]},{},[])("/")
  381. });