AutoHidingControl.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Storytelling
  5. * (C) Copyright IBM Corp. 2014, 2018
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. */
  8. define(['baglass/core-client/js/core-client/ui/core/View'], function (View) {
  9. var AutoHidingControl = View.extend({
  10. autohide: null,
  11. init: function init(options) {
  12. AutoHidingControl.inherited('init', this, arguments);
  13. options = options || {};
  14. this.autohide = options.autohide || 6000;
  15. this.startHidden = options.startHidden || false;
  16. this.hidden = true;
  17. },
  18. render: function render() {
  19. var _this = this;
  20. return this._renderContent().then(function () {
  21. _this.$el.on('mouseenter', _this._cancelAutoHide.bind(_this));
  22. _this.$el.on('mouseleave', _this._setAutoHide.bind(_this));
  23. _this.hidden = false;
  24. if (_this.startHidden) {
  25. _this.hide({ duration: 0 });
  26. } else {
  27. _this.show({ duration: 0 });
  28. }
  29. });
  30. },
  31. destroy: function destroy() {
  32. var _this2 = this;
  33. this._cancelAutoHide();
  34. return this.hide().then(function () {
  35. _this2.remove();
  36. });
  37. },
  38. toggle: function toggle() {
  39. if (this.hidden) {
  40. this.show();
  41. } else {
  42. this.hide();
  43. }
  44. },
  45. _cancelAutoHide: function _cancelAutoHide() {
  46. if (this.timeout) {
  47. clearTimeout(this.timeout);
  48. }
  49. this.timeout = null;
  50. },
  51. _setAutoHide: function _setAutoHide() {
  52. this.timeout = setTimeout(this.hide.bind(this), this.autohide);
  53. },
  54. show: function show() {
  55. var _this3 = this;
  56. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  57. return Promise.delay(options.delay || 0).then(function () {
  58. return _this3._show(options).then(function () {
  59. _this3.$el.attr('aria-hidden', 'false');
  60. _this3.hidden = false;
  61. _this3._cancelAutoHide();
  62. _this3._setAutoHide();
  63. });
  64. });
  65. },
  66. hide: function hide() {
  67. var _this4 = this;
  68. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  69. if (this.hidden) {
  70. return Promise.resolve();
  71. }
  72. return this._hide(options).then(function () {
  73. _this4.$el.attr('aria-hidden', 'true');
  74. _this4.hidden = true;
  75. });
  76. },
  77. // Override these!
  78. _renderContent: function _renderContent() {
  79. return Promise.resolve();
  80. },
  81. // _show is expected to return a promise that resolves when it is done showing.
  82. _show: function _show() {
  83. return Promise.resolve();
  84. },
  85. // _hide is expected to return a promise that resolves when it is done hiding.
  86. _hide: function _hide() {
  87. return Promise.resolve();
  88. }
  89. });
  90. return AutoHidingControl;
  91. });
  92. //# sourceMappingURL=AutoHidingControl.js.map