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.

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