You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.1 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_web_utils.dropzone', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var _t = core._t;
  23. var QWeb = core.qweb;
  24. var DropzoneMixin = {
  25. dropzoneData: {},
  26. dropzoneClasses: ['mk_dropzone'],
  27. _checkDropzoneEvent: function(event) {
  28. return true;
  29. },
  30. _startDropzone: function($dropzone) {
  31. if(this.$dropzone) {
  32. this._destroyDropzone();
  33. }
  34. this.$dropzone = $dropzone;
  35. this.$dropzone.dndHover().on({
  36. 'dndHoverStart.dropzone': this._hoverDropzoneEnter.bind(this),
  37. 'dndHoverEnd.dropzone': this._hoverDropzoneLeave.bind(this),
  38. });
  39. this.$dropzone.on('dragenter.dropzone', this._dragenterDropzone.bind(this));
  40. this.$dropzone.on('dragover.dropzone', this._dragoverDropzone.bind(this));
  41. this.$dropzone.on('dragleave.dropzone', this._dragleaveDropzone.bind(this));
  42. this.$dropzone.on('drop.dropzone', this._dropDropzone.bind(this));
  43. _.each(this.dropzoneData, function(value, key) {
  44. this.$dropzone.attr(key, value)
  45. }, this);
  46. },
  47. _destroyDropzone: function() {
  48. if(this.$dropzone) {
  49. this.$dropzone.off('.dropzone');
  50. this.$dropzone.dndHover('destroy');
  51. _.each(this.dropzoneData, function(value, key) {
  52. this.$dropzone.removeAttr(key)
  53. }, this);
  54. this.$dropzone = false;
  55. }
  56. },
  57. _toggleDropzone: function(state) {
  58. this.$dropzone.toggleClass(this.dropzoneClasses.join(" "), state);
  59. },
  60. _hoverDropzoneEnter: function(event, originalEvent) {
  61. if(this._checkDropzoneEvent(originalEvent)) {
  62. this._toggleDropzone(true);
  63. event.preventDefault();
  64. return false;
  65. }
  66. },
  67. _hoverDropzoneLeave: function(event, originalEvent) {
  68. this._toggleDropzone(false);
  69. event.stopPropagation();
  70. event.preventDefault();
  71. return false;
  72. },
  73. _handleDrag: function(event) {
  74. },
  75. _handleDrop: function(event) {
  76. },
  77. _dragenterDropzone: function(event) {
  78. if(this._checkDropzoneEvent(event)) {
  79. event.preventDefault();
  80. }
  81. },
  82. _dragoverDropzone: function(event) {
  83. if(this._checkDropzoneEvent(event)) {
  84. event.preventDefault();
  85. this._handleDrag(event);
  86. }
  87. },
  88. _dragleaveDropzone: function(event) {
  89. if(this._checkDropzoneEvent(event)) {
  90. event.preventDefault();
  91. }
  92. },
  93. _dropDropzone: function(event) {
  94. if(this._checkDropzoneEvent(event)) {
  95. event.preventDefault();
  96. event.stopPropagation();
  97. this._handleDrop(event);
  98. }
  99. }
  100. };
  101. var FileDropzoneMixin = _.extend({}, DropzoneMixin, {
  102. dropzoneData: {
  103. 'data-dropzone-text': _t("Drop files here to upload!"),
  104. },
  105. dropzoneClasses: DropzoneMixin.dropzoneClasses.concat(['mk_dropzone_file']),
  106. dropzoneCheck: window.File && window.FileReader && window.FileList && window.Blob,
  107. _checkDropzoneEvent: function(event) {
  108. var dataTransfer = event.originalEvent && event.originalEvent.dataTransfer;
  109. var fileCheck = dataTransfer && _.some(dataTransfer.types, function(type) {
  110. return type == "Files";
  111. });
  112. return this.dropzoneCheck && fileCheck;
  113. },
  114. _handleDrag: function(event) {
  115. event.originalEvent.dataTransfer.dropEffect = 'copy';
  116. },
  117. });
  118. return {
  119. DropzoneMixin: DropzoneMixin,
  120. FileDropzoneMixin: FileDropzoneMixin,
  121. };
  122. });