summaryrefslogtreecommitdiff
path: root/www/js/menu.js
blob: eb878a50a19b0f6516916cdcf4916e7dc8604d9c (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
(function () {
  "use strict";
  html5ks.menu = {
    mainMenu: function () {
      html5ks._next = function () {};
      this.context(false);
      html5ks.api.stop("all");
      html5ks.api.window("hide");
      html5ks.api.play("music", "music_menus");
      html5ks.api.show("url", "ui/main/bg-main.png");
      this.elements.mainMenu.style.display = "block";
      html5ks.state.status = "menu";
    },

    activeDialog: null,

    dialog: function (name) {
      this.activeDialog = this.elements.dialog[name];
      this.activeDialog.style.display = "block";
      this.elements.dialogs.style.display = "block";
      this.elements.context.style.display = "none";
      html5ks.elements.gray.style.display = "block";
    },

    initElements: function () {
      this.elements = {
        dialogs: document.getElementById("dialogs"),
        dialog: {
          options: document.getElementById("options"),
          retn: document.getElementById("return")
        },
        mainMenu: document.getElementById("main-menu"),
        main: {
          start: document.getElementById("start")
        },
        context: document.getElementById("context"),
        contextMenu: document.getElementById("context-menu"),
        contextInfo: document.getElementById("context-info")
      };
    },

    initEvents: function () {
      var options = document.getElementsByClassName("option");
      var change = function (e) {
        var target = e.target;
        html5ks.persistent[target.id] = target.type === "checkbox" ? target.checked : target.value;
        switch (target.id) {
          case "fullscreen":
            if (target.checked) {
              html5ks.fullscreen();
            } else {
              html5ks.fullscreen(false);
            }
            break;
          case "scaleAll":
            var scaleVideo = document.getElementById("scaleVideo");
            if (!target.checked) {
              scaleVideo.checked = false;
              scaleVideo.parentNode.className += " disabled";
            } else {
              scaleVideo.checked = true;
              scaleVideo.parentNode.className = scaleVideo.parentNode.className.replace("disabled", "");
            }
            // fall-through
          case "scaleVideo":
            html5ks.scale();
            break;
          case "musicVolume":
          case "soundVolume":
            html5ks.api.set_volume(target.value, 0, target.id.replace("Volume", ""));
            break;
        }
      };

      options[0].parentNode.parentNode.addEventListener("change", change, false);
      options[0].parentNode.parentNode.addEventListener("input", change, false);

      var optionsButton = document.getElementsByClassName("options-button");

      var showOptions = function (e) {
        html5ks.menu.dialog("options");
        e.stopPropagation();
      };

      for (var i = optionsButton.length - 1; i >= 0; i--) {
        optionsButton[i].addEventListener("click", showOptions, false);
      }

      this.elements.dialog.retn.addEventListener("click", function (e) {
        html5ks.menu.activeDialog.style.display = "none";
        html5ks.menu.activeDialog = null;
        html5ks.menu.elements.dialogs.style.display = "none";
        if (html5ks.state.status === "context") {
          html5ks.menu.context(true);
        } else {
          html5ks.elements.gray.style.display = "none";
        }
        e.stopPropagation();
      }, false);

      // quit
      var close = function () {
        window.close();
        top.open('','_self','');
        top.close();
      };
      ["AppleWebKit", "MSIE", "Trident"].forEach(function (ua) {
        if (navigator.userAgent.indexOf(ua) > -1) {
          var quit = document.getElementsByClassName("quit");
          for (var i = quit.length - 1; i >= 0; i--) {
            quit[i].className = quit[i].className.replace("disabled", "");
            quit[i].addEventListener("click", close, false);
          }
          return false;
        }
      }, this);

      // context menu
      html5ks.elements.container.addEventListener("contextmenu", function (e) {
        switch (html5ks.state.status) {
          case "scene":
          case "context":
            this.context();
        }
        e.preventDefault();
      }.bind(this), false);

      document.getElementById("context-return").addEventListener("click", function () {
        html5ks.menu.context(false);
      }, false);

      document.getElementById("show-image").addEventListener("click", function () {
        html5ks.menu.context(false);
        html5ks.elements.window.style.display = "none";
        html5ks.elements.container.addEventListener("click", function click() {
          this.removeEventListener("click", click, true);
          html5ks.menu.context(true);
        }, true);
      }, false);

      document.getElementById("skip-mode").addEventListener("click", function () {
        html5ks.menu.context(false);
        html5ks.api.speed("skip", true);
        html5ks.next();
      }, false);

      document.getElementById("auto-mode").addEventListener("click", function () {
        html5ks.menu.context(false);
        html5ks.api.speed("auto", true);
        html5ks.next();
      }, false);

      document.getElementById("goto-main-menu").addEventListener("click", function () {
        html5ks.menu.mainMenu();
      }, false);
    },

    initOptions: function () {
      var options = document.getElementsByClassName("option"),
          values = html5ks.persistent;

      if (!html5ks.persistent.scaleAll) {
        document.getElementById("scaleVideo").parentNode.className += " disabled";
      }

      for (var i = options.length - 1; i >= 0; i--) {
        var option = options[i];
        switch (option.type) {
          case "checkbox":
            option.checked = values[option.id];
            break;
          case "range":
            option.value = values[option.id];
            break;
          default:
            console.error("unknown option type %s", option.type);
        }
      }
    },

    init: function () {
      this.initElements();
      this.initEvents();
      this.initOptions();

      when.all([html5ks.fetch("json", "imachine"),
          html5ks.fetch("json", "script")]).then(function () {
        var start = this.elements.main.start;
        start.addEventListener("click", function () {
          this.elements.mainMenu.style.display = "none";
          html5ks.imachine.start().then(this.mainMenu.bind(this));
        }.bind(this), false);
        start.className = start.className.replace("disabled", "");
      }.bind(this));
    },

    _hadWindow: null,

    context: function (show) {
      switch (show) {
        case true:
          this._hadWindow = this._hadWindow !== null ? this._hadWindow : html5ks.api.window();
          html5ks.state.status = "context";
          html5ks.elements.gray.style.display = "block";
          html5ks.elements.window.style.display = "none";
          this.elements.context.style.display = "block";
          break;
        case false:
          html5ks.state.status = "scene";
          html5ks.elements.gray.style.display = "none";
          if (html5ks.state.status === "scene" && this._hadWindow) {
            this._hadWindow = null;
            html5ks.elements.window.style.display = "block";
          }
          this.elements.context.style.display = "none";
          break;
        default:
          this.context(this.elements.context.style.display !== "block");
      }
    }
  };
}());