VisDnDShapeToSlotProvider.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 VisDnDShapeToSlotProvider
  10. * @hideconstructor
  11. *
  12. * @classdesc Handles drag and drop of shape to visualization and slot.
  13. */
  14. define(['underscore', 'dashboard-analytics/util/ShapeUtils'], function (_, ShapeUtils) {
  15. return function () {
  16. function VisDnDShapeToSlotProvider() {
  17. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  18. _classCallCheck(this, VisDnDShapeToSlotProvider);
  19. this.dashboardAPI = options.dashboardAPI;
  20. this.content = options.content;
  21. this.transaction = options.transaction;
  22. }
  23. VisDnDShapeToSlotProvider.prototype.destroy = function destroy() {
  24. this.content = null;
  25. };
  26. /**
  27. * @param {Object} source - the DndSource defined by whichever source starts the drag
  28. * @param {Object} target - the DnDTarget defined by the drop target which includes target specific information.
  29. * @param {String} target.el - the drop node.
  30. * @param {String} target.type - the type of the target (eg. slot.item)
  31. * @param {String} target.info - a target-specific function which includes information about that target.
  32. * @return {boolean} true if this provider supports the dndSource and target.
  33. */
  34. VisDnDShapeToSlotProvider.prototype.supports = function supports(source) {
  35. return source.type === 'widget' && source.data.model && source.data.model.type === 'shape';
  36. };
  37. VisDnDShapeToSlotProvider.prototype.accepts = function accepts(source, target) {
  38. if (!this.supports(source, target)) {
  39. return false;
  40. }
  41. // Shape widget can only be accepted if addAfter is undefined
  42. return target.info.addAfter === true ? false : this._acceptsShapeOnSlot(target.info.slot);
  43. };
  44. //Accept shape if slot is shapable
  45. VisDnDShapeToSlotProvider.prototype._acceptsShapeOnSlot = function _acceptsShapeOnSlot(slot) {
  46. if (slot) {
  47. return slot.getDefinition().isShapable();
  48. } else {
  49. var visualization = this.content.getFeature('Visualization');
  50. var slots = visualization.getSlots().getSlotList();
  51. return slots.find(function (slot) {
  52. return slot.getDefinition().isShapable();
  53. });
  54. }
  55. };
  56. VisDnDShapeToSlotProvider.prototype.onDrop = function onDrop(source, target) {
  57. if (!this.supports(source, target)) {
  58. return false;
  59. }
  60. return this.applyShape(source);
  61. };
  62. /**
  63. * Apply the dragObject (shape) onto the widget
  64. * @param options.shapeModel object - the model of a shape
  65. */
  66. VisDnDShapeToSlotProvider.prototype.applyShape = function applyShape(source) {
  67. var transactionToken = this.transaction.startTransaction();
  68. var shapeModel = source.data.model;
  69. ShapeUtils.addShapeWidgetToContent(this.content, { shapeModel: shapeModel }, transactionToken);
  70. this.transaction.endTransaction(transactionToken);
  71. };
  72. return VisDnDShapeToSlotProvider;
  73. }();
  74. });
  75. //# sourceMappingURL=VisDnDShapeToSlotProvider.js.map