You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Overlay.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* global $, APP */
  2. /**
  3. * Base class for overlay components - the components which are displayed on
  4. * top of the application with semi-transparent background covering the whole
  5. * screen.
  6. */
  7. export default class Overlay{
  8. /**
  9. * Creates new <tt>Overlay</tt> instance.
  10. */
  11. constructor() {
  12. /**
  13. *
  14. * @type {jQuery}
  15. */
  16. this.$overlay = null;
  17. }
  18. /**
  19. * Template method which should be used by subclasses to provide the overlay
  20. * content. The contents provided by this method are later subject to
  21. * the translation using {@link APP.translation.translateElement}.
  22. * @return {string} HTML representation of the overlay dialog contents.
  23. * @private
  24. */
  25. _buildOverlayContent() {
  26. return '';
  27. }
  28. /**
  29. * Constructs the HTML body of the overlay dialog.
  30. */
  31. buildOverlayHtml() {
  32. let overlayContent = this._buildOverlayContent();
  33. this.$overlay = $(`
  34. <div class='overlay__container'>
  35. <div class='overlay__content'>
  36. ${overlayContent}
  37. </div>
  38. </div>`);
  39. APP.translation.translateElement(this.$overlay);
  40. }
  41. /**
  42. * Checks whether the page reload overlay has been displayed.
  43. * @return {boolean} <tt>true</tt> if the page reload overlay is currently
  44. * visible or <tt>false</tt> otherwise.
  45. */
  46. isVisible() {
  47. return this.$overlay && this.$overlay.parents('body').length > 0;
  48. }
  49. /**
  50. * Template method called just after the overlay is displayed for the first
  51. * time.
  52. * @private
  53. */
  54. _onShow() {
  55. // To be overridden by subclasses.
  56. }
  57. /**
  58. * Shows the overlay dialog adn attaches the underlying HTML representation
  59. * to the DOM.
  60. */
  61. show() {
  62. !this.$overlay && this.buildOverlayHtml();
  63. if (!this.isVisible()) {
  64. this.$overlay.appendTo('body');
  65. this._onShow();
  66. }
  67. }
  68. /**
  69. * Hides the overlay dialog and detaches it's HTML representation from
  70. * the DOM.
  71. */
  72. hide() {
  73. this.$overlay && this.$overlay.detach();
  74. }
  75. }