BinAction.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 2018, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['../../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../../widgets/livewidget/nls/StringResources', '../api/SlotActionsProviderAPI', '../../../../apiHelpers/SlotAPIHelper'], function (APIFactory, stringResources, SlotActionsProviderAPI, SlotAPIHelper) {
  9. var BinAction = function () {
  10. function BinAction(_ref) {
  11. var content = _ref.content,
  12. dashboardAPI = _ref.dashboardAPI,
  13. features = _ref.features;
  14. _classCallCheck(this, BinAction);
  15. this.content = content;
  16. this.dashboard = dashboardAPI;
  17. this.icons = features && features['Dashboard.Icons'];
  18. this.content.getFeature('SlotActions').registerProvider('autoBinAction', this.getAPI());
  19. }
  20. BinAction.prototype.getAPI = function getAPI() {
  21. if (!this.api) {
  22. this.api = APIFactory.createAPI(this, [SlotActionsProviderAPI]);
  23. }
  24. return this.api;
  25. };
  26. BinAction.prototype._isOlap = function _isOlap(dataSource, dataItem) {
  27. return !!(dataSource.isOlapPackage() || dataItem && dataItem.getMetadataColumn().isOlapColumn());
  28. };
  29. BinAction.prototype._supportAction = function _supportAction(slot, itemIndex) {
  30. // Olap sources are unsupported, for now.
  31. var visualization = this.content.getFeature('Visualization');
  32. var dataSource = visualization.getDataSource();
  33. var dataItem = slot.getDataItemList()[itemIndex];
  34. var isOlap = this._isOlap(dataSource, dataItem);
  35. var unsupportedSource = !dataSource || isOlap;
  36. var definition = visualization.getDefinition();
  37. return itemIndex !== undefined && definition && definition.getProperty('canApplyBinning') && !unsupportedSource && !(SlotAPIHelper.isMultiMeasuresSeriesSlot(slot) || SlotAPIHelper.isMultiMeasuresValueSlot(slot)) && SlotAPIHelper.doesDataItemSupportBinning(slot, dataItem);
  38. };
  39. BinAction.prototype._getViewState = function _getViewState(slot, itemIndex) {
  40. var definition = this.content.getFeature('Visualization').getDefinition();
  41. if (slot) {
  42. // @todo get slot from visualizationAPI and use new SLOTAPI
  43. var dataItem = slot.getDataItemList()[itemIndex];
  44. if (dataItem) {
  45. var viewState = {
  46. height: 280,
  47. width: 400, // those are hints only; for the placement of the toolbar.,
  48. defaultNumberOfBins: definition.getProperty('binningConfig').defaultNumberOfBins,
  49. binningStateCb: dataItem.getBinning.bind(dataItem)
  50. };
  51. return viewState;
  52. }
  53. }
  54. return null;
  55. };
  56. /**
  57. * Find the correct data item index
  58. * @param {Object} slot - slot api for slot we're checking against
  59. * @return {Integer || undefined} Returns the index of the first data item in this slot that has the appropriate type for autobinning or undefined if none is found
  60. */
  61. BinAction.prototype._findDataItemIndex = function _findDataItemIndex(slot) {
  62. var index = void 0;
  63. // when this path is triggered from rclick on axis title, we get the wrong mapindex for the data item
  64. // index is null but we have data items, maybe 1st dataitem isnt the right one which is the behaviour we saw by letting the incorrect index propagate forward
  65. // works fine if its from data slots view, mapindex comes in defined and correct
  66. if (slot.getDataItemList().length > 0) {
  67. var dataItems = slot.getDataItemList();
  68. dataItems.find(function (dataItem, idx) {
  69. var metaDataColumn = dataItem.getMetadataColumn();
  70. if (metaDataColumn && metaDataColumn.getType() === 'fact') {
  71. index = idx;
  72. return true;
  73. }
  74. });
  75. }
  76. return index;
  77. };
  78. BinAction.prototype.getSlotActionList = function getSlotActionList(slotId, itemIndex) {
  79. var visualization = this.content.getFeature('Visualization');
  80. var slot = visualization.getSlots().getSlot(slotId);
  81. if (itemIndex === null || itemIndex === undefined) {
  82. itemIndex = this._findDataItemIndex(slot);
  83. }
  84. if (slot && this._supportAction(slot, itemIndex)) {
  85. var viewOptions = this._getViewState(slot, itemIndex);
  86. var autoBinIcon = this.icons.getIcon('dashboard-autobin');
  87. return [{
  88. name: 'autoBinAction',
  89. label: stringResources.get('autoBinActionText'),
  90. icon: autoBinIcon.id,
  91. type: 'NextView',
  92. view: {
  93. module: 'dashboard-analytics/dialogs/AutobinDialog',
  94. state: viewOptions
  95. },
  96. actions: {
  97. apply: this._applyBinning.bind(this, slot, itemIndex)
  98. }
  99. }];
  100. }
  101. return [];
  102. };
  103. BinAction.prototype._applyBinning = function _applyBinning(slot, itemIndex, binningState) {
  104. var dataItem = slot.getDataItemList()[itemIndex];
  105. if (binningState) {
  106. dataItem.setBinning({
  107. bins: binningState.numberOfBins
  108. });
  109. } else {
  110. dataItem.setBinning(null);
  111. }
  112. };
  113. return BinAction;
  114. }();
  115. return BinAction;
  116. });
  117. //# sourceMappingURL=BinAction.js.map