InfoIndicator.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. 'use strict';
  2. /*
  3. *+------------------------------------------------------------------------+
  4. *| Licensed Materials - Property of IBM
  5. *| IBM Cognos Products: Dashboard
  6. *| (C) Copyright IBM Corp. 2017, 2019
  7. *|
  8. *| US Government Users Restricted Rights - Use, duplication or disclosure
  9. *| restricted by GSA ADP Schedule Contract with IBM Corp.
  10. *+------------------------------------------------------------------------+
  11. */
  12. define(['underscore', '../../../lib/@waca/core-client/js/core-client/ui/core/View', '../../../lib/@waca/dashboard-common/dist/utils/Flyout', '../../../widgets/livewidget/nls/StringResources', 'text!./templates/InfoIndicator.html', './InfoFlyoutView'], function (_, View, Flyout, stringResources, template, InfoFlyoutView) {
  13. 'use strict';
  14. var infoIndicator = View.extend({
  15. templateString: template,
  16. events: {
  17. 'clicktap': '_onClick',
  18. 'keydown': '_onClick'
  19. },
  20. init: function init(options) {
  21. infoIndicator.inherited('init', this, arguments);
  22. var warnIcon = options.ownerWidget.dashboardApi.getFeature('Icons').getIcon('warnIcon');
  23. this.messages = {};
  24. this.$el.empty().append(this.dotTemplate({
  25. title: stringResources.get('warning'),
  26. iconId: warnIcon.id
  27. }));
  28. this._hideIndicator();
  29. },
  30. remove: function remove() {
  31. if (this.flyout) {
  32. this.flyout.destroy();
  33. }
  34. infoIndicator.inherited('remove', this, arguments);
  35. },
  36. /**
  37. * @function addInfo
  38. * Adds an array of information to be rendered by the indicator.
  39. * This can be called multiple times before calling {@link InfoIndicator#reset} and
  40. * the information added with the same identifier will be merged as an unique information.
  41. * Each array item must be provided in the following format.
  42. * @param {Object[]} info - array of objects representing an information
  43. * @example
  44. * indicator.addInfo([{
  45. * id: 'information-identifier',
  46. * label: 'information label'
  47. * items: [{
  48. * id: 'info-data-identifier',
  49. * label: 'information data label'
  50. * }],
  51. * small: true //Change the max-height of the dialog content to 100px instead of 300x
  52. * }]);
  53. */
  54. addInfo: function addInfo(info) {
  55. var _this = this;
  56. // filter out empty messages
  57. var filtered = _.filter(info, function (i) {
  58. return i.items.length > 0;
  59. });
  60. // merge the messages to the map
  61. // each service should has its own indicator section, and the message should be updated when that section get updated
  62. _.each(filtered, function (i) {
  63. _this.messages[i.id] = i;
  64. });
  65. if (_.isEmpty(this.messages)) {
  66. this._hideIndicator();
  67. } else {
  68. this.$el.show();
  69. }
  70. },
  71. /**
  72. * @function reset
  73. * Reset the information messages
  74. */
  75. reset: function reset() {
  76. this.messages = {};
  77. this._hideIndicator();
  78. },
  79. /**
  80. * Hide the info indicator
  81. */
  82. _hideIndicator: function _hideIndicator() {
  83. this.$el.hide();
  84. },
  85. clearMessagesByIdAndSubtype: function clearMessagesByIdAndSubtype(idType) {
  86. var _this2 = this;
  87. var id = idType.id;
  88. var type = idType.type;
  89. Object.values(this.messages).forEach(function (message) {
  90. for (var i = 0; i < message.items.length; i++) {
  91. if (message.items[i].id === id && message.items[i].type === type) {
  92. message.items.splice(i, 1);
  93. if (message.items.length === 0) {
  94. _this2.clearMessagesWithIds([message.id]);
  95. }
  96. }
  97. }
  98. });
  99. },
  100. /**
  101. * Clear all messages with the specified id. If there are no messages remaining
  102. * after the clear then hide the indicator.
  103. * @param {Array} ids - array of ids to clear in the messages object.
  104. */
  105. clearMessagesWithIds: function clearMessagesWithIds(ids) {
  106. var _this3 = this;
  107. ids.forEach(function (id) {
  108. if (_this3.messages[id]) {
  109. delete _this3.messages[id];
  110. }
  111. });
  112. // Check if there are messages available, if not, hide
  113. if (_.isEmpty(this.messages)) {
  114. this._hideIndicator();
  115. }
  116. },
  117. _onClick: function _onClick(event) {
  118. if (event.type === 'keydown') {
  119. event.stopPropagation();
  120. var key = event.key || event.keyCode || event.which || '';
  121. if (key !== 'Enter' && key !== 13) {
  122. return;
  123. }
  124. }
  125. if (event.gesture) {
  126. event.gesture.preventDefault();
  127. }
  128. if (event.cancelable) {
  129. event.preventDefault();
  130. }
  131. if (!this.flyout) {
  132. this.flyout = new Flyout({
  133. container: document.body,
  134. selector: this.$el,
  135. popoverClass: 'filterPopover visTopPopover',
  136. viewport: 'body',
  137. viewClass: InfoFlyoutView,
  138. viewOptions: {
  139. messages: _.values(this.messages)
  140. }
  141. });
  142. } else {
  143. this.flyout.view.setMessages(_.values(this.messages));
  144. }
  145. this.flyout.open(this.$el);
  146. event.stopPropagation();
  147. }
  148. });
  149. return infoIndicator;
  150. });
  151. //# sourceMappingURL=InfoIndicator.js.map