GridEventTarget.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  4. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  5. /**
  6. *+------------------------------------------------------------------------+
  7. *| Licensed Materials - Property of IBM
  8. *| IBM Cognos Products: Dashboard
  9. *| (C) Copyright IBM Corp. 2017, 2019
  10. *|
  11. *| US Government Users Restricted Rights - Use, duplication or disclosure
  12. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  13. *+------------------------------------------------------------------------+
  14. */
  15. define(['jquery', '../common/NonVIPREventTarget', 'underscore', '../VisEventHandler', '../../../lib/@waca/dashboard-common/dist/utils/EventChainLocal', '../../../util/EventUtils'], function ($, NonVIPREventTarget, _, VisEventHandler, EventChainLocal, EventUtils) {
  16. var GridEventTarget = function (_NonVIPREventTarget) {
  17. _inherits(GridEventTarget, _NonVIPREventTarget);
  18. function GridEventTarget() {
  19. var _ret;
  20. _classCallCheck(this, GridEventTarget);
  21. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  22. args[_key] = arguments[_key];
  23. }
  24. var _this = _possibleConstructorReturn(this, _NonVIPREventTarget.call.apply(_NonVIPREventTarget, [this].concat(args)));
  25. var options = args[0] || {};
  26. _this.visControl = options.visControl;
  27. // map from visDecorations to viprDecoration
  28. _this.viprDecorations = {};
  29. _this.viprDecorations[VisEventHandler.DECORATIONS.HIGHLIGHT] = 'highlighted';
  30. _this.viprDecorations[VisEventHandler.DECORATIONS.SELECT] = 'selected';
  31. return _ret = _this.getAPI(), _possibleConstructorReturn(_this, _ret);
  32. }
  33. /**
  34. * Get the array of event targets based on the event.
  35. * Event target represents a data point object that corresponds to the event.
  36. *
  37. * @param event Event object
  38. */
  39. GridEventTarget.prototype.getEventTargets = function getEventTargets(event) {
  40. var node = this._getTargetNode(event);
  41. if (!node) {
  42. return [];
  43. }
  44. var $node = $(node);
  45. var row = $node.attr('row');
  46. if (_.isUndefined(row) || _.isUndefined(this.visControl._dataRows[row]) || this._isSummaryRow($node) || !this._canProcessKeyDown(event) && !this.mouseWithinBoundCheck(event)) {
  47. return [];
  48. }
  49. if (this._isHeaderRow($node)) {
  50. return this._getHeaderTargets(event, $node);
  51. } else {
  52. return this._getRowTargets(event, $node);
  53. }
  54. };
  55. GridEventTarget.prototype.remove = function remove() {
  56. this.visControl = null;
  57. _NonVIPREventTarget.prototype.remove.call(this);
  58. };
  59. GridEventTarget.prototype._isSummaryRow = function _isSummaryRow(eventTarget) {
  60. return $(eventTarget).hasClass('summary');
  61. };
  62. GridEventTarget.prototype._isHeaderRow = function _isHeaderRow(eventTarget) {
  63. return $(eventTarget).hasClass('header-cell');
  64. };
  65. GridEventTarget.prototype._getHeaderSelectionContext = function _getHeaderSelectionContext(selectedHeaders) {
  66. var _this2 = this;
  67. var selectionContext = {
  68. mapIndex: [],
  69. slotId: 'grid_cols',
  70. actions: { actionsToKeep: [], actionOptions: {} }
  71. };
  72. _.each(selectedHeaders, function (header) {
  73. var col = $(header).attr('col');
  74. // Find position of data item in the slot
  75. var _visControl$findSlotD = _this2.visControl.findSlotDetails(col),
  76. mapIndex = _visControl$findSlotD.mapIndex;
  77. selectionContext.mapIndex.push(mapIndex);
  78. if (_this2.visControl.isRank(col)) {
  79. selectionContext.actions.actionsToKeep.push('sortAction');
  80. selectionContext.actions.actionOptions.sortAction = { rankSort: true };
  81. }
  82. });
  83. return selectionContext;
  84. };
  85. GridEventTarget.prototype._getHeaderTargets = function _getHeaderTargets(event, $node) {
  86. var targets = [];
  87. var row = parseInt($node.attr('row'));
  88. if (['click', 'contextmenu', 'hold', 'keydown'].indexOf(event.type) !== -1) {
  89. var target = this._getTargetNode(event);
  90. this.visControl.handleSelectColumnHeader(target, !(event.ctrlKey || event.metaKey), event.type === 'contextmenu');
  91. }
  92. var selectedHeaders = this.visControl.getSelectedColumnHeaderNodes();
  93. if (selectedHeaders && selectedHeaders.length > 0) {
  94. targets.push({
  95. key: row + ':',
  96. type: 'itemclass',
  97. source: $node,
  98. isEdgeSelect: true,
  99. values: this._getColumnTargetValues(selectedHeaders, row),
  100. selectionContext: this._getHeaderSelectionContext(selectedHeaders),
  101. area: 'visualization'
  102. });
  103. } else {
  104. // User deselects only selected header, resulting in nothing selected.
  105. // No items are selected (expected), but also don't want to enable
  106. // widget ODT from appearing later on.
  107. var eventChainLocal = new EventChainLocal(event);
  108. eventChainLocal.setProperty('preventDefaultContextBar', true);
  109. }
  110. return targets;
  111. };
  112. GridEventTarget.prototype._getColumnTargetValues = function _getColumnTargetValues(selectedHeaders, row) {
  113. var _this3 = this;
  114. var slots = [];
  115. _.each(selectedHeaders, function (header) {
  116. var col = $(header).attr('col');
  117. var _visControl$findSlotD2 = _this3.visControl.findSlotDetails(col),
  118. slotIndex = _visControl$findSlotD2.slotIndex;
  119. var value = _this3.visControl._dataRows[row][col];
  120. value = _.isUndefined(value.value) ? value : value.value;
  121. value = _.isArray(value) ? value[0] : value; //The grid doesn't stack values, so only the first value in the array is used
  122. slots[slotIndex] = { value: value };
  123. });
  124. return slots;
  125. };
  126. GridEventTarget.prototype._getRowTargets = function _getRowTargets(event, $node) {
  127. var targets = [];
  128. var row = parseInt($node.attr('row'));
  129. var col = parseInt($node.attr('col'));
  130. var isGrouped = this.visControl.isGroupedColumn(col);
  131. var numberOfColumns = isGrouped ? col + 1 : this.visControl._dataRows[row].length;
  132. //We treat the grid values row as item when its a left click to not filter with ordinal values.
  133. var treatItAsItem = event.type === 'click' && event.which === 1 || event.type === 'tap';
  134. targets.push({
  135. key: row + ':' + (numberOfColumns - 1),
  136. type: treatItAsItem ? 'item' : 'datapoint',
  137. source: $node,
  138. isEdgeSelect: true,
  139. values: this._getRowTargetValues(row, numberOfColumns),
  140. area: 'visualization'
  141. });
  142. return targets;
  143. };
  144. GridEventTarget.prototype._getRowTargetValues = function _getRowTargetValues(row, numberOfColumns) {
  145. var slots = [];
  146. var values = [];
  147. var value = void 0;
  148. for (var column = 0; column < numberOfColumns; column++) {
  149. //the reason that we treat it as a datapoint is because, when we select on the cell, its behavior is datapoint, hover and right-click should follow the same type to avoid confusion
  150. if (!this.visControl.isRank(column)) {
  151. value = this.visControl._dataRows[row][column];
  152. value = _.isUndefined(value.value) ? value : value.value;
  153. value = _.isArray(value) ? value[0] : value; //The grid doesn't stack values, so only the first value in the array is used
  154. values.push(EventUtils.toDeprecatedPayload(value));
  155. }
  156. }
  157. slots.push(values); //In the grid, value is always in the first slot
  158. return slots;
  159. };
  160. return GridEventTarget;
  161. }(NonVIPREventTarget);
  162. return GridEventTarget;
  163. });
  164. //# sourceMappingURL=GridEventTarget.js.map