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.

117 lines
3.7 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. dropzoneEvents: {
  28. 'dragenter .mk_dropzone': '_dragenterDropzone',
  29. 'dragover .mk_dropzone': '_dragoverDropzone',
  30. 'dragleave .mk_dropzone': '_dragleaveDropzone',
  31. 'drop .mk_dropzone': '_dropDropzone',
  32. },
  33. _checkDropzoneEvent: function(event) {
  34. return true;
  35. },
  36. _renderDropzone: function($dropzone) {
  37. this.$dropzone = $dropzone;
  38. this.$dropzone.dndHover().on({
  39. 'dndHoverStart': this._hoverDropzoneEnter.bind(this),
  40. 'dndHoverEnd': this._hoverDropzoneLeave.bind(this),
  41. });
  42. _.each(this.dropzoneData, function(value, key) {
  43. this.$dropzone.attr(key, value)
  44. }, this);
  45. },
  46. _toggleDropzone: function(state) {
  47. this.$dropzone.toggleClass(this.dropzoneClasses, state);
  48. },
  49. _hoverDropzoneEnter: function(event, originalEvent) {
  50. if(this._checkDropzoneEvent(originalEvent)) {
  51. this._toggleDropzone(true);
  52. event.preventDefault();
  53. return false;
  54. }
  55. },
  56. _hoverDropzoneLeave: function(event, originalEvent) {
  57. this._toggleDropzone(false);
  58. event.stopPropagation();
  59. event.preventDefault();
  60. return false;
  61. },
  62. _handleDrag: function(event) {
  63. },
  64. _handleDrop: function(event) {
  65. },
  66. _dragenterDropzone: function(event) {
  67. if(this._checkDropzoneEvent(event)) {
  68. event.preventDefault();
  69. }
  70. },
  71. _dragoverDropzone: function(event) {
  72. if(this._checkDropzoneEvent(event)) {
  73. event.preventDefault();
  74. this._handleDrag(event);
  75. }
  76. },
  77. _dragleaveDropzone: function(event) {
  78. if(this._checkDropzoneEvent(event)) {
  79. event.preventDefault();
  80. }
  81. },
  82. _dropDropzone: function(event) {
  83. if(this._checkDropzoneEvent(event)) {
  84. event.preventDefault();
  85. event.stopPropagation();
  86. this._handleDrop(event);
  87. }
  88. }
  89. };
  90. var FileDropzoneMixin = _.extend({}, DropzoneMixin, {
  91. dropzoneData: {
  92. 'data-dropzone-text': _t("Drop files here to upload!"),
  93. },
  94. dropzoneClasses: DropzoneMixin.dropzoneClasses + ' mk_dropzone_file',
  95. dropzoneCheck: window.File && window.FileReader && window.FileList && window.Blob,
  96. _checkDropzoneEvent: function(event) {
  97. var dataTransfer = event.originalEvent && event.originalEvent.dataTransfer;
  98. var fileCheck = dataTransfer && _.some(dataTransfer.types, function(type) {
  99. return type == "Files";
  100. });
  101. return this.dropzoneCheck && fileCheck;
  102. },
  103. _handleDrag: function(event) {
  104. event.originalEvent.dataTransfer.dropEffect = 'copy';
  105. },
  106. });
  107. return {
  108. DropzoneMixin: DropzoneMixin,
  109. FileDropzoneMixin: FileDropzoneMixin,
  110. };
  111. });