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.

131 lines
4.1 KiB

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