aboutsummaryrefslogtreecommitdiff
path: root/pointless/viewer/stage.js
blob: 99e240aef62cab53f0cbb8a579398dfe5234db0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * stage.js
 * Part of Pointless Viewer, a Beamer presentation viewer
 * Copyright 2018 Pacien TRAN-GIRARD
 * License: GNU GPL v3
 */

"use strict";

class Stage {
  constructor(onReady, onNext, onPrevious) {
    this.onNext = onNext;
    this.onPrevious = onPrevious;
    this.audienceScreen = null;
    this.presenterScreen = null;

    this.projector = window.open(window.location.href, "_blank", "toolbar=0,location=0,menubar=0");
    if (this.projector == null)
      alert("Please allow pop-ups, then refresh this page.");

    const self = this;
    this.projector.addEventListener("load", function() {
      self.audienceScreen = new Screen(self.projector, false, false);
      self.presenterScreen = new Screen(window, true, true);
      self._watchDetach();
      onReady();
    });

    this._registerEventHandler(window);
    this._registerEventHandler(this.projector);
  }

  setPage(page) {
    this.audienceScreen.setPage(page);
    this.presenterScreen.setPage(page);
  }

  _registerEventHandler(window) {
    const self = this;
    window.addEventListener("keydown", function(event) {
      self._onCommand(event);
    })
  }

  _onCommand(keyboardEvent) {
    switch (keyboardEvent.key) {
      case "Enter":
      case " ":
      case "ArrowRight":
      case "n":
        return this.onNext();

      case "ArrowLeft":
      case "p":
        return this.onPrevious();
    }
  }

  _watchDetach() {
    const self = this;
    window.addEventListener("beforeunload", function() {
      self._setMessage(self.projector, "Controller detached");
    });

    this.projector.addEventListener("beforeunload", function() {
      self._setMessage(window, "Viewer detached");
    });
  }

  _setMessage(window, message) {
    const messageBar = window.document.getElementById("message");
    messageBar.textContent = message;
  }
}