filedrophandler.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Provides a files drag and drop event detector. It works on
  16. * HTML5 browsers.
  17. *
  18. * @see ../demos/filedrophandler.html
  19. */
  20. goog.provide('goog.events.FileDropHandler');
  21. goog.provide('goog.events.FileDropHandler.EventType');
  22. goog.require('goog.array');
  23. goog.require('goog.dom');
  24. goog.require('goog.events.BrowserEvent');
  25. goog.require('goog.events.EventHandler');
  26. goog.require('goog.events.EventTarget');
  27. goog.require('goog.events.EventType');
  28. goog.require('goog.log');
  29. goog.require('goog.log.Level');
  30. /**
  31. * A files drag and drop event detector. Gets an {@code element} as parameter
  32. * and fires {@code goog.events.FileDropHandler.EventType.DROP} event when files
  33. * are dropped in the {@code element}.
  34. *
  35. * @param {Element|Document} element The element or document to listen on.
  36. * @param {boolean=} opt_preventDropOutside Whether to prevent a drop on the
  37. * area outside the {@code element}. Default false.
  38. * @constructor
  39. * @extends {goog.events.EventTarget}
  40. * @final
  41. */
  42. goog.events.FileDropHandler = function(element, opt_preventDropOutside) {
  43. goog.events.EventTarget.call(this);
  44. /**
  45. * Handler for drag/drop events.
  46. * @type {!goog.events.EventHandler<!goog.events.FileDropHandler>}
  47. * @private
  48. */
  49. this.eventHandler_ = new goog.events.EventHandler(this);
  50. var doc = element;
  51. if (opt_preventDropOutside) {
  52. doc = goog.dom.getOwnerDocument(element);
  53. }
  54. // Add dragenter listener to the owner document of the element.
  55. this.eventHandler_.listen(
  56. doc, goog.events.EventType.DRAGENTER, this.onDocDragEnter_);
  57. // Add dragover listener to the owner document of the element only if the
  58. // document is not the element itself.
  59. if (doc != element) {
  60. this.eventHandler_.listen(
  61. doc, goog.events.EventType.DRAGOVER, this.onDocDragOver_);
  62. }
  63. // Add dragover and drop listeners to the element.
  64. this.eventHandler_.listen(
  65. element, goog.events.EventType.DRAGOVER, this.onElemDragOver_);
  66. this.eventHandler_.listen(
  67. element, goog.events.EventType.DROP, this.onElemDrop_);
  68. };
  69. goog.inherits(goog.events.FileDropHandler, goog.events.EventTarget);
  70. /**
  71. * Whether the drag event contains files. It is initialized only in the
  72. * dragenter event. It is used in all the drag events to prevent default actions
  73. * only if the drag contains files. Preventing default actions is necessary to
  74. * go from dragenter to dragover and from dragover to drop. However we do not
  75. * always want to prevent default actions, e.g. when the user drags text or
  76. * links on a text area we should not prevent the browser default action that
  77. * inserts the text in the text area. It is also necessary to stop propagation
  78. * when handling drag events on the element to prevent them from propagating
  79. * to the document.
  80. * @private
  81. * @type {boolean}
  82. */
  83. goog.events.FileDropHandler.prototype.dndContainsFiles_ = false;
  84. /**
  85. * A logger, used to help us debug the algorithm.
  86. * @type {goog.log.Logger}
  87. * @private
  88. */
  89. goog.events.FileDropHandler.prototype.logger_ =
  90. goog.log.getLogger('goog.events.FileDropHandler');
  91. /**
  92. * The types of events fired by this class.
  93. * @enum {string}
  94. */
  95. goog.events.FileDropHandler.EventType = {
  96. DROP: goog.events.EventType.DROP
  97. };
  98. /** @override */
  99. goog.events.FileDropHandler.prototype.disposeInternal = function() {
  100. goog.events.FileDropHandler.superClass_.disposeInternal.call(this);
  101. this.eventHandler_.dispose();
  102. };
  103. /**
  104. * Dispatches the DROP event.
  105. * @param {goog.events.BrowserEvent} e The underlying browser event.
  106. * @private
  107. */
  108. goog.events.FileDropHandler.prototype.dispatch_ = function(e) {
  109. goog.log.fine(this.logger_, 'Firing DROP event...');
  110. var event = new goog.events.BrowserEvent(e.getBrowserEvent());
  111. event.type = goog.events.FileDropHandler.EventType.DROP;
  112. this.dispatchEvent(event);
  113. };
  114. /**
  115. * Handles dragenter on the document.
  116. * @param {goog.events.BrowserEvent} e The dragenter event.
  117. * @private
  118. */
  119. goog.events.FileDropHandler.prototype.onDocDragEnter_ = function(e) {
  120. goog.log.log(
  121. this.logger_, goog.log.Level.FINER,
  122. '"' + e.target.id + '" (' + e.target + ') dispatched: ' + e.type);
  123. var dt = e.getBrowserEvent().dataTransfer;
  124. // Check whether the drag event contains files.
  125. this.dndContainsFiles_ = !!(
  126. dt && ((dt.types && (goog.array.contains(dt.types, 'Files') ||
  127. goog.array.contains(dt.types, 'public.file-url'))) ||
  128. (dt.files && dt.files.length > 0)));
  129. // If it does
  130. if (this.dndContainsFiles_) {
  131. // Prevent default actions.
  132. e.preventDefault();
  133. }
  134. goog.log.log(
  135. this.logger_, goog.log.Level.FINER,
  136. 'dndContainsFiles_: ' + this.dndContainsFiles_);
  137. };
  138. /**
  139. * Handles dragging something over the document.
  140. * @param {goog.events.BrowserEvent} e The dragover event.
  141. * @private
  142. */
  143. goog.events.FileDropHandler.prototype.onDocDragOver_ = function(e) {
  144. goog.log.log(
  145. this.logger_, goog.log.Level.FINEST,
  146. '"' + e.target.id + '" (' + e.target + ') dispatched: ' + e.type);
  147. if (this.dndContainsFiles_) {
  148. // Prevent default actions.
  149. e.preventDefault();
  150. // Disable the drop on the document outside the drop zone.
  151. var dt = e.getBrowserEvent().dataTransfer;
  152. dt.dropEffect = 'none';
  153. }
  154. };
  155. /**
  156. * Handles dragging something over the element (drop zone).
  157. * @param {goog.events.BrowserEvent} e The dragover event.
  158. * @private
  159. */
  160. goog.events.FileDropHandler.prototype.onElemDragOver_ = function(e) {
  161. goog.log.log(
  162. this.logger_, goog.log.Level.FINEST,
  163. '"' + e.target.id + '" (' + e.target + ') dispatched: ' + e.type);
  164. if (this.dndContainsFiles_) {
  165. // Prevent default actions and stop the event from propagating further to
  166. // the document. Both lines are needed! (See comment above).
  167. e.preventDefault();
  168. e.stopPropagation();
  169. // Allow the drop on the drop zone.
  170. var dt = e.getBrowserEvent().dataTransfer;
  171. // IE bug #811625 (https://goo.gl/UWuxX0) will throw error SCRIPT65535
  172. // when attempting to set property effectAllowed on IE10+.
  173. // See more: https://github.com/google/closure-library/issues/485.
  174. try {
  175. dt.effectAllowed = 'all';
  176. } catch (err) {
  177. }
  178. dt.dropEffect = 'copy';
  179. }
  180. };
  181. /**
  182. * Handles dropping something onto the element (drop zone).
  183. * @param {goog.events.BrowserEvent} e The drop event.
  184. * @private
  185. */
  186. goog.events.FileDropHandler.prototype.onElemDrop_ = function(e) {
  187. goog.log.log(
  188. this.logger_, goog.log.Level.FINER,
  189. '"' + e.target.id + '" (' + e.target + ') dispatched: ' + e.type);
  190. // If the drag and drop event contains files.
  191. if (this.dndContainsFiles_) {
  192. // Prevent default actions and stop the event from propagating further to
  193. // the document. Both lines are needed! (See comment above).
  194. e.preventDefault();
  195. e.stopPropagation();
  196. // Dispatch DROP event.
  197. this.dispatch_(e);
  198. }
  199. };