summaryrefslogtreecommitdiff
path: root/www/js/menu.js
blob: 00d40b7f4a3c79eb646e34aad04fb6d6a9a21e28 (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
(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 "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].classList.remove("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.stopPropagation();
        }
        e.preventDefault();
      }.bind(this), true);

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

      document.getElementById("show-image").addEventListener("click", function () {
        var state = html5ks.menu._state;
        html5ks.menu._state = {};
        html5ks.menu.context(false);
        var done = function () {
          this.removeEventListener("click", done, true);
          html5ks.menu._state = state;
          html5ks.menu.context(true);
        };
        html5ks.elements.container.addEventListener("click", done, 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;

      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 (d) {
                  for (var k in d) {
                    if (k.slice(0, 3) === (html5ks.persistent.language === "en" ? "fr_" : "en_")) {
                      delete d[k];
                    }
                  }
                }, console.error)]).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), console.error);
                  }.bind(this), false);
                  start.classList.remove("disabled");
                }.bind(this), console.error);
    },

    _state: null,

    context: function (show) {
      switch (show) {
        case true:
          // return early if context already on
          if (this.context("status")) return;
          this._state = this._state || {
            window: html5ks.api.window(),
            nvl: html5ks.api.nvl("status"),
            status: html5ks.state.status
          };
          html5ks.api.window("hide");
          html5ks.api.nvl("hide");
          html5ks.state.status = "context";
          html5ks.elements.gray.style.display = "block";
          this.elements.context.style.display = "block";
          break;
        case false:
          this.elements.context.style.display = "none";
          html5ks.elements.gray.style.display = "none";
          if (this._state) {
            html5ks.api.window(this._state.window);
            html5ks.api.nvl(this._state.nvl);
            html5ks.state.status = this._state.status;
            this._state = null;
          }
          break;
        case "status":
          return this.elements.context.style.display === "block";
        default:
          this.context(!this.context("status"));
      }
    }
  };
}());