JumpToView.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Watson Analytics (C) Copyright IBM Corp. 2018, 2020
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. *
  7. */
  8. define(['../../lib/@waca/core-client/js/core-client/ui/core/View', '../../lib/@waca/core-client/js/core-client/utils/ContentFormatter', 'jquery', 'underscore', '../../widgets/livewidget/nls/StringResources', '../../util/JumpToActionHelper', 'text!../templates/JumpTo.html'], function (View, ContentFormatter, $, _, stringResources, JumpToActionHelper, JumpToTemplate) {
  9. /**
  10. *
  11. * View to manage/launch Jump to
  12. **/
  13. var JumpToView = View.extend({
  14. templateString: JumpToTemplate,
  15. events: {
  16. 'primaryaction .drillToTarget': '_drillToTarget',
  17. 'primaryaction .manageDrillThrough': '_manageDrillThrough',
  18. 'primaryaction .addNewTarget': '_addNewTarget'
  19. },
  20. init: function init(options) {
  21. JumpToView.inherited('init', this, arguments);
  22. options = options || {};
  23. this.content = options.content;
  24. this.dashboard = options.dashboardApi;
  25. this.sourceId = options.sourceId;
  26. if (options.jumpToTargets) {
  27. this.jumpToTargets = options.jumpToTargets(this.content, this.sourceId, options.selections);
  28. }
  29. this.isDataPointSelection = options.isDataPointSelection || false;
  30. this.scopedPageContext = options.scopedPageContext;
  31. },
  32. remove: function remove() {
  33. JumpToView.inherited('remove', this, arguments);
  34. },
  35. render: function render() {
  36. return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
  37. return drillThroughService.getDrillThroughModelApi().then(this._getJumpToTargets.bind(this)).then(this._renderHtml.bind(this));
  38. }.bind(this));
  39. },
  40. notifyRenderComplete: function notifyRenderComplete() {
  41. this.$el.find('.visJumpToView-item.drillToTarget .visJumpToView-item-content').each(function (index, elem) {
  42. ContentFormatter.middleShortenString(elem);
  43. });
  44. //Set focus to the first item
  45. this.$el.find('.visJumpToView-item:first').focus();
  46. },
  47. _renderHtml: function _renderHtml(items) {
  48. if (items.length === 0) {
  49. this._addNewTargetItem(items);
  50. } else {
  51. _.each(items, function (item) {
  52. item.isTarget = true;
  53. });
  54. //Add manage to this list of items
  55. //check if the 'Manage...' item has already been added to the items array.
  56. var manageIndex = items.findIndex(function (item) {
  57. return item.isManage === true;
  58. });
  59. if (manageIndex === -1) {
  60. this._addManageItem(items);
  61. }
  62. }
  63. if (items.length > 0) {
  64. items[items.length - 1].hideDivider = true;
  65. }
  66. var sHtml = this.dotTemplate({ jumpToItems: items });
  67. var $sHtml = $(sHtml);
  68. this.$el.empty().append($sHtml);
  69. },
  70. _addNewTargetItem: function _addNewTargetItem(items) {
  71. if (!this._isDataPointSelection() && this._getAuthoringMode()) {
  72. items.push({
  73. addNewTarget: true,
  74. getId: function getId() {
  75. return 'addNewTarget';
  76. },
  77. getName: function getName() {
  78. return stringResources.get('drillthrough_addNewTarget');
  79. }
  80. });
  81. }
  82. },
  83. _getAuthoringMode: function _getAuthoringMode() {
  84. return this.dashboard.getMode() === 'authoring';
  85. },
  86. _addManageItem: function _addManageItem(items) {
  87. if (!this._isDataPointSelection() && this._getAuthoringMode()) {
  88. items.push({
  89. isManage: true,
  90. getId: function getId() {
  91. return 'manage';
  92. },
  93. getName: function getName() {
  94. return stringResources.get('drillthrough_manage');
  95. }
  96. });
  97. }
  98. },
  99. _getJumpToTargets: function _getJumpToTargets(dtModelApi) {
  100. if (this.jumpToTargets) {
  101. return Promise.resolve(this.jumpToTargets.targets);
  102. } else {
  103. var entries = dtModelApi.getDrillDefinitionEntries(this.content);
  104. this.jumpToTargets = JumpToActionHelper.getJumpToTargets({
  105. dashboardApi: this.dashboard,
  106. content: this.content,
  107. drillDefinitions: entries,
  108. selections: undefined,
  109. sourceId: this.sourceId
  110. });
  111. return Promise.resolve(this.jumpToTargets.targets);
  112. }
  113. },
  114. _isDataPointSelection: function _isDataPointSelection() {
  115. return this.isDataPointSelection;
  116. },
  117. _drillToTarget: function _drillToTarget(event) {
  118. var id = event.currentTarget.getAttribute('data-id');
  119. var target = this.jumpToTargets.targetsMap[id];
  120. if (target) {
  121. target.jumpTo();
  122. }
  123. },
  124. _manageDrillThrough: function _manageDrillThrough() {
  125. if (this.toolbar) {
  126. this.remove();
  127. this.toolbar.hide();
  128. }
  129. return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
  130. return drillThroughService.getDrillThroughModelApi().then(function (dtModelApi) {
  131. var controller = drillThroughService.getDrillThroughController();
  132. var dlg = controller.manageDrillDefinition({
  133. content: this.content,
  134. dashboard: this.dashboard,
  135. drillThroughModelApi: dtModelApi
  136. });
  137. dlg.open();
  138. dlg.disableOk();
  139. }.bind(this));
  140. }.bind(this));
  141. },
  142. _addNewTarget: function _addNewTarget() {
  143. if (this.toolbar) {
  144. this.remove();
  145. this.toolbar.hide();
  146. }
  147. return this.dashboard.getDashboardSvc('DrillThroughService').then(function (drillThroughService) {
  148. var controller = drillThroughService.getDrillThroughController();
  149. controller.createDrillDefinition(this.content);
  150. }.bind(this));
  151. },
  152. preload: function preload() {
  153. return Promise.resolve();
  154. },
  155. renderCallBack: function renderCallBack(toolbar) {
  156. this.toolbar = toolbar;
  157. }
  158. });
  159. return JumpToView;
  160. });
  161. //# sourceMappingURL=JumpToView.js.map