RefreshTimerIndicator.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM IBM Cognos Products: BI Cloud (C)
  4. *
  5. * Copyright IBM Corp. 2016, 2019
  6. *
  7. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['../../../lib/@waca/core-client/js/core-client/ui/core/View', 'jquery', 'underscore', 'text!./templates/RefreshTimerIndicator.html', '../../../widgets/livewidget/nls/StringResources', '../../../lib/@waca/dashboard-common/dist/utils/Flyout', '../../../lib/@waca/core-client/js/core-client/utils/Utils'], function (View, $, _, template, resources, Flyout, Utils) {
  10. /**
  11. * Class to set refresh time interval.
  12. * Possible values are in seconds, minutes and hours.
  13. */
  14. var RefreshTimerIndicator = View.extend({
  15. templateString: template,
  16. /**
  17. * @classdesc The timer icon associated with a widget.
  18. * @constructs
  19. * @public
  20. * @param {Boolean} antoRefresh - the flag of auto refresh on or off.
  21. * @param {String} unit - the unit of the timer.
  22. * @param {Number} value - the value of the timer.
  23. *
  24. */
  25. init: function init(ownerWidget) {
  26. this.ownerWidget = ownerWidget;
  27. var queryRefreshInfo = ownerWidget.get('queryRefresh');
  28. var spec = queryRefreshInfo ? queryRefreshInfo : { autoRefresh: false };
  29. _.extend(this, spec);
  30. RefreshTimerIndicator.inherited('init', this, spec);
  31. if (this.$icon) {
  32. this.$icon.remove();
  33. }
  34. this.ariaLabel = resources.get('widgetTimer');
  35. this.title = resources.get('widgetTimer');
  36. this.clockIcon = this.ownerWidget.dashboardApi.getFeature('Icons').getIcon('common-clock-time').id;
  37. this.$icon = $(this.dotTemplate(this));
  38. },
  39. initEvents: function initEvents() {
  40. var _onClick = this._onClick.bind(this);
  41. this.$icon.on('mousedown tap keydown mouseup', _onClick);
  42. },
  43. remove: function remove() {
  44. this.$icon && this.$icon.off();
  45. this.$icon = null;
  46. RefreshTimerIndicator.inherited('remove', this, arguments);
  47. },
  48. _onClick: function _onClick(evt) {
  49. //some ancestor nodes were handling the 'mouseup' event causing unexpected behaviour (see 108341)
  50. if (evt.type === 'mouseup') {
  51. evt.stopPropagation();
  52. return;
  53. }
  54. if (evt.keyCode && evt.keyCode !== 13) {
  55. // Keypress and it is not enter.. ignore
  56. return;
  57. }
  58. if (evt.gesture) {
  59. // prevent the virtual click event from firing. Otherwise this method will be called twice.
  60. evt.gesture.preventDefault();
  61. }
  62. if (this.ownerWidget) {
  63. var lastRefreshed = this.ownerWidget.get('queryRefresh').lastRefreshed;
  64. var refreshedDuration = new Date().getTime() - lastRefreshed;
  65. var formatedDuration = resources.get('lastRefresh', { timeInterval: Utils.formatDuration(refreshedDuration) });
  66. if (!this.flyout) {
  67. this.flyout = new Flyout({
  68. container: document.body,
  69. selector: this.$icon,
  70. viewInstance: $('<div>' + formatedDuration + '</div>')
  71. });
  72. } else {
  73. this.flyout.view.empty();
  74. this.flyout.view.html(formatedDuration);
  75. }
  76. this.flyout.open(this.$icon);
  77. }
  78. evt.stopPropagation();
  79. }
  80. });
  81. return RefreshTimerIndicator;
  82. });
  83. //# sourceMappingURL=RefreshTimerIndicator.js.map