DashboardServiceability.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 2019, 2020
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. /**
  9. * @class DashboardServiceability
  10. * @hideconstructor
  11. * @classdesc API class that is used to manage DashboardServiceability
  12. * (including the corrsponding view) across API calls
  13. */
  14. define(['jquery', '../../../lib/@waca/dashboard-common/dist/core/APIFactory', '../../../lib/@waca/core-client/js/core-client/ui/KeyCodes', './api/DashboardServiceabilityAPI'], function ($, APIFactory, KeyCodes, DashboardServiceabilityAPI) {
  15. var DashboardServiceability = function () {
  16. function DashboardServiceability(options) {
  17. var _this = this;
  18. _classCallCheck(this, DashboardServiceability);
  19. this._dashboardApi = options.features['API'];
  20. this._selectedContent = null;
  21. this._selectedContentIds = [];
  22. this._keysPressed = [];
  23. this._slideout = null;
  24. this._openPromise = null;
  25. this._closePromise = null;
  26. this._dashboardState = null;
  27. this._callback = function (event) {
  28. this._keyUpDownCallback(event);
  29. }.bind(this);
  30. this._dashboardState = options.features['DashboardState'];
  31. this._isInfoVisible = false;
  32. this._dashboardApi.getCanvasWhenReady().then(function (canvas) {
  33. _this.canvas = canvas;
  34. canvas.on('change:selections:select', _this.onContentSelection, _this);
  35. canvas.on('change:selections:deselect', _this.onContentDeselection, _this);
  36. });
  37. }
  38. DashboardServiceability.prototype.getAPI = function getAPI() {
  39. return this._api;
  40. };
  41. DashboardServiceability.prototype.initialize = function initialize() {
  42. this._api = APIFactory.createAPI(this, [DashboardServiceabilityAPI]);
  43. this._dashboardState.onChangeActive(this.dashboardStateCallback.bind(this));
  44. return new Promise.resolve();
  45. };
  46. DashboardServiceability.prototype.showS12yView = function showS12yView(widgetInfo) {
  47. return this._dashboardApi.showSlideOut({
  48. 'width': 400,
  49. 'position': 'right',
  50. 'content': {
  51. 'module': 'dashboard-core/js/features/dashboard/dashboardServiceability/view/ServiceabilityView',
  52. id: 's12ySlideoutContainer',
  53. widgetInfo: widgetInfo,
  54. dashboardApi: this._dashboardApi
  55. },
  56. resizable: {
  57. min: 400
  58. }
  59. });
  60. };
  61. DashboardServiceability.prototype.openPanel = function openPanel() {
  62. this._closePromise = null;
  63. this._openPromise = Promise.resolve(false);
  64. if (this._selectedContent) {
  65. var widgetIsReady = this._selectedContent.getFeature('Serviceability') === undefined ? false : true;
  66. if (widgetIsReady) {
  67. this._slideout = this.showS12yView(this._selectedContent.getFeature('Serviceability').getContentInfo());
  68. if (this._slideout) {
  69. this._openPromise = this._monitorOpeningSlideout();
  70. }
  71. }
  72. }
  73. return this._openPromise;
  74. };
  75. DashboardServiceability.prototype._monitorOpeningSlideout = function _monitorOpeningSlideout() {
  76. var _this2 = this;
  77. return new Promise(function (resolve) {
  78. if (_this2._slideout) {
  79. _this2._slideout.on('done:show', function () {
  80. resolve(true);
  81. });
  82. _this2._slideout.on('hide', function () {
  83. _this2._slideout = null;
  84. });
  85. }
  86. });
  87. };
  88. DashboardServiceability.prototype.closePanel = function closePanel() {
  89. this._openPromise = null;
  90. this._closePromise = Promise.resolve();
  91. if (this._slideout) {
  92. this._slideout.hide();
  93. this._closePromise = this._monitorClosingSlideout();
  94. }
  95. return this._closePromise;
  96. };
  97. DashboardServiceability.prototype.showInfo = function showInfo() {
  98. this._isInfoVisible = true;
  99. this._dashboardApi.getCanvas().findContent().forEach(function (content) {
  100. var serviceabilityFeature = content.getFeature('Serviceability');
  101. if (serviceabilityFeature) {
  102. serviceabilityFeature.showInfo();
  103. }
  104. });
  105. };
  106. DashboardServiceability.prototype.hideInfo = function hideInfo() {
  107. this._isInfoVisible = false;
  108. this._dashboardApi.getCanvas().findContent().forEach(function (content) {
  109. var serviceabilityFeature = content.getFeature('Serviceability');
  110. if (serviceabilityFeature) {
  111. serviceabilityFeature.hideInfo();
  112. }
  113. });
  114. };
  115. /**
  116. * @implements DashboardServiceabilityAPI.isRenderTimeOn
  117. */
  118. DashboardServiceability.prototype.isInfoVisible = function isInfoVisible() {
  119. return this._isInfoVisible;
  120. };
  121. DashboardServiceability.prototype._monitorClosingSlideout = function _monitorClosingSlideout() {
  122. var _this3 = this;
  123. return new Promise(function (resolve) {
  124. if (_this3._slideout) {
  125. _this3._slideout.on('hide', function () {
  126. _this3._slideout = null;
  127. resolve();
  128. });
  129. } else {
  130. resolve();
  131. }
  132. });
  133. };
  134. DashboardServiceability.prototype.getPanelState = function getPanelState() {
  135. var _this4 = this;
  136. var promise = this._openPromise || this._closePromise || Promise.resolve();
  137. return promise.then(function () {
  138. return {
  139. isOpen: _this4._slideout !== null
  140. };
  141. });
  142. };
  143. DashboardServiceability.prototype.onContentSelection = function onContentSelection(event) {
  144. var _this5 = this;
  145. event.info.value.forEach(function (id) {
  146. var content = _this5._dashboardApi.getCanvas().getContent(id);
  147. if (content && content.getFeature('Serviceability')) {
  148. _this5._selectedContentIds.push(id);
  149. }
  150. });
  151. this._updateSelectedWidget();
  152. };
  153. /**
  154. * @description One of the tasks this method needs to do is hiding info on
  155. * contents when selecting another tab. Listening to `change:selections:deselect`
  156. * event is necessary because re-selecting the same tab will also trigger an
  157. * `change:selections:select` event hence we cannot tell if we're selecting a different
  158. * tab or not. Another taks of this method is to remove some contents from the
  159. * internal _selectedContent variable.
  160. * @param {Object} event
  161. */
  162. DashboardServiceability.prototype.onContentDeselection = function onContentDeselection(event) {
  163. var _this6 = this;
  164. this._selectedContentIds = this._selectedContentIds.filter(function (id) {
  165. return event.info.value.indexOf(id) === -1;
  166. });
  167. this._updateSelectedWidget();
  168. event.info.value.forEach(function (id) {
  169. var content = _this6._dashboardApi.getCanvas().getContent(id);
  170. if (content && content.getType() === 'page') {
  171. _this6.hideInfo();
  172. }
  173. });
  174. };
  175. DashboardServiceability.prototype.setSelectedWidget = function setSelectedWidget(widget) {
  176. this._selectedContent = widget;
  177. };
  178. DashboardServiceability.prototype._updateSelectedWidget = function _updateSelectedWidget() {
  179. this._selectedContent = null;
  180. if (this._selectedContentIds.length == 1) {
  181. var id = this._selectedContentIds[0];
  182. this.setSelectedWidget(this._dashboardApi.getCanvas().getContent(id));
  183. }
  184. };
  185. DashboardServiceability.prototype.destroy = function destroy() {
  186. if (this.canvas) {
  187. this.canvas.off('change:selections:select', this.onContentSelection, this);
  188. this.canvas.off('change:selections:deselect', this.onContentDeselection, this);
  189. this.canvas = null;
  190. }
  191. $('body').off('keydown.serviceability', this._callback);
  192. $('body').off('keyup.serviceability', this._callback);
  193. this._api = null;
  194. this._slideout = null;
  195. this._selectedContentIds = null;
  196. this._dashboardApi = null;
  197. this._keysPressed = null;
  198. this._dashboardState = null;
  199. };
  200. DashboardServiceability.prototype.dashboardStateCallback = function dashboardStateCallback(payload) {
  201. if (payload.info.value === true) {
  202. $('body').on('keydown.serviceability', this._callback);
  203. $('body').on('keyup.serviceability', this._callback);
  204. } else {
  205. $('body').off('keydown.serviceability', this._callback);
  206. $('body').off('keyup.serviceability', this._callback);
  207. }
  208. };
  209. DashboardServiceability.prototype._keyUpDownCallback = function _keyUpDownCallback(e) {
  210. if (e.type === 'keydown' && !(this._keysPressed.indexOf(e.keyCode) >= 0)) {
  211. if (this._keysPressed.length === 2) {
  212. this._keysPressed.shift();
  213. }
  214. this._keysPressed.push(e.keyCode);
  215. }
  216. if (e.type === 'keyup' && this._keysPressed.indexOf(e.keyCode) >= 0) {
  217. var index = this._keysPressed.indexOf(e.keyCode);
  218. this._keysPressed.splice(index, 1);
  219. }
  220. if (this._keysReady()) {
  221. this.openPanel();
  222. this._keysPressed = [];
  223. }
  224. };
  225. DashboardServiceability.prototype._keysReady = function _keysReady() {
  226. return this._keysPressed.length === 2 && this._keysPressed[0] === KeyCodes.CTRL && this._keysPressed[1] === KeyCodes.PERIOD;
  227. };
  228. return DashboardServiceability;
  229. }();
  230. return DashboardServiceability;
  231. });
  232. //# sourceMappingURL=DashboardServiceability.js.map