lib/layers/nextlevel.js

  1. import { default as tmplNextLevel } from '../../templates/NextLevel.html';
  2. import { SocialButtons } from './social';
  3. import { AMP } from './amp';
  4. import { levelSummary } from '../tools/summary';
  5. /**
  6. * NextLevel Layer, creates content of transition layer between levels
  7. */
  8. export class NextLevel {
  9. /**
  10. *
  11. * @param {{}} owner An instance of {@link MiniMemory}
  12. */
  13. constructor(owner) {
  14. this.owner = owner;
  15. this.html = tmplNextLevel;
  16. this.ampMarkup = '';
  17. }
  18. /**
  19. * Initialize class after html insert.
  20. */
  21. init() {
  22. this.owner.shadowRoot
  23. .querySelector('#nextlevel #continue')
  24. .addEventListener('click', this.hide.bind(this));
  25. }
  26. /**
  27. * Shows nextlevel layer
  28. */
  29. show() {
  30. const layer = this.owner.shadowRoot.querySelector('#nextlevel');
  31. const summary = this.owner.shadowRoot.querySelector('#summary');
  32. const footer = this.owner.shadowRoot.querySelector('#nextlevel .footer');
  33. const btnContinue = this.owner.shadowRoot.querySelector('#continue');
  34. if (
  35. Object.keys(this.owner.settings.scores.currentPlayer.scores).length === 0
  36. ) {
  37. btnContinue.setAttribute('data-i18n', 'PLAY');
  38. } else {
  39. btnContinue.setAttribute('data-i18n', 'CONTINUE');
  40. }
  41. summary.innerHTML = levelSummary(
  42. this.owner.settings.scores.currentPlayer.scores
  43. );
  44. layer.classList.add('active');
  45. layer.style.opacity = '1';
  46. this.owner.i18n.update(this.owner.shadowRoot);
  47. footer.innerHTML = this.getAmpMarkup();
  48. new SocialButtons().html().then((html) => {
  49. footer.innerHTML += html;
  50. });
  51. }
  52. /**
  53. * Hides nextlevel layer
  54. */
  55. hide() {
  56. const layer = this.owner.shadowRoot.querySelector('#nextlevel');
  57. layer.style.opacity = '0';
  58. setTimeout(() => {
  59. layer.classList.remove('active');
  60. }, 300);
  61. }
  62. /**
  63. * get AMP markup from AMP library.
  64. * @return {String} returns the required markup if available,
  65. * empty otherwise
  66. *
  67. */
  68. getAmpMarkup() {
  69. if (!this.owner.manifest || !this.owner.manifest.amp) {
  70. return '';
  71. }
  72. const amp = new AMP(this.owner.manifest.amp);
  73. return amp.markup();
  74. }
  75. }