VisDnDProviderSet.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Business Analytics (C) Copyright IBM Corp. 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class VisDnD
  10. * @hideconstructor
  11. *
  12. * @classdesc A VisDnDProviderSet registers a list of source providers for a given target type (eg: 'slot.item' or 'type.livewidget)
  13. */
  14. define([], function () {
  15. return function () {
  16. //A DnDProviderSet registers a set of source providers for a given target type (eg: 'slot.item' or 'type.livewidget)
  17. function VisDnDProviderSet() {
  18. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  19. _classCallCheck(this, VisDnDProviderSet);
  20. this.dashboardAPI = options.dashboardAPI;
  21. this.dashboardDnD = options.dashboardDnD;
  22. this.content = options.content;
  23. //The type of the target and the set of source providers that handle that type must be defined.
  24. this.targetType = options.targetType;
  25. this.dndProviderList = options.dndProviderList || [];
  26. this.containedTargetsToIgnore = options.containedTargetsToIgnore || [];
  27. this._setupDnD();
  28. }
  29. VisDnDProviderSet.prototype.addProviders = function addProviders(providers) {
  30. var _dndProviderList;
  31. (_dndProviderList = this.dndProviderList).push.apply(_dndProviderList, providers);
  32. };
  33. VisDnDProviderSet.prototype.destroy = function destroy() {
  34. this.dashboardAPI = null;
  35. this.content = null;
  36. this.dashboardDnD.deregisterDropHandler(this.dropHandler.id);
  37. this.dashboardDnD = null;
  38. this.dndProviderList.forEach(function (provider) {
  39. return provider.destroy();
  40. });
  41. this.dndProviderList = null;
  42. this.containedTargetsToIgnore = null;
  43. };
  44. VisDnDProviderSet.prototype._setupDnD = function _setupDnD() {
  45. var _this = this;
  46. this.dropHandler = this.dashboardDnD.registerDropHandler({
  47. accepts: function accepts(source, target) {
  48. return _this.accepts(source, target);
  49. },
  50. onDrop: function onDrop(source, target) {
  51. _this.onDrop(source, target);
  52. }
  53. });
  54. };
  55. VisDnDProviderSet.prototype._containedTargetsToIgnore = function _containedTargetsToIgnore(target) {
  56. // When drop handler corresponding drop target contains smaller target (eg, livewidget drop zone contains overlay drop zone ('slot.item') ),
  57. // We want to ignore the smaller target, because otherwise it will mess up the bigger target drag enter/leave state, and in fact,
  58. // we don't have to worry about smaller target, because it has its own handler to handler it already.
  59. if (target && target.type) {
  60. return this.containedTargetsToIgnore.indexOf(target.type) !== -1;
  61. }
  62. return false;
  63. };
  64. //NOTE: While we're using callbacks, we should standardize the names
  65. //onDragEnter, onDragLeave, onDrop
  66. VisDnDProviderSet.prototype.accepts = function accepts(source, target) {
  67. if (this._containedTargetsToIgnore(target)) {
  68. return false;
  69. }
  70. var accepts = false;
  71. if (target.type === this.targetType && target.info.contentId === this.content.getId()) {
  72. // console.log('accepts?', this.targetType, target.info && JSON.stringify(target.info.dragInfo));
  73. var provider = this.dndProviderList.find(function (provider) {
  74. return provider.supports(source, target);
  75. });
  76. if (provider) {
  77. accepts = provider.accepts(source, target);
  78. if (accepts && target.info.onDragEnter) {
  79. target.info.onDragEnter(target);
  80. this.currentTarget = target;
  81. }
  82. }
  83. }
  84. if (!accepts || target !== this.currentTarget) {
  85. this._clearHighlighting(target);
  86. }
  87. this.currentTarget = target;
  88. return accepts;
  89. };
  90. VisDnDProviderSet.prototype._clearHighlighting = function _clearHighlighting(target) {
  91. this.currentTarget && this.currentTarget.info && this.currentTarget.info.onDragLeave && this.currentTarget.info.onDragLeave(target);
  92. };
  93. VisDnDProviderSet.prototype.onDrop = function onDrop(source, target) {
  94. this._clearHighlighting(target);
  95. this.currentTarget = null;
  96. if (target.type === this.targetType && target.info.contentId === this.content.getId()) {
  97. var provider = this.dndProviderList.find(function (provider) {
  98. return provider.supports(source, target);
  99. });
  100. if (provider) {
  101. return provider.onDrop(source, target);
  102. }
  103. }
  104. return false;
  105. };
  106. return VisDnDProviderSet;
  107. }();
  108. });
  109. //# sourceMappingURL=VisDnDProviderSet.js.map