New event.
[heroik.git] / heroik.js
1 /* The person who associated a work with this deed has dedicated the
2 work to the public domain by waiving all of his or her rights to
3 the work worldwide under copyright law, including all related and
4 neighboring rights, to the extent allowed by law.
5
6 You can copy, modify, distribute and perform the work, even for
7 commercial purposes, all without asking permission.
8
9 See https://creativecommons.org/publicdomain/zero/1.0/ for details.
10 */
11
12 "use strict";
13
14 function offsetTop (el) {
15 var offset = 0;
16 for (offset = 0; el && el.offsetTop !== undefined; el = el.offsetParent) {
17 offset += el.offsetTop;
18 }
19 return offset;
20 }
21
22 function scrollToId (id) {
23 var el = id ? document.getElementById(id) : null;
24 if (el) {
25 window.scrollTo(0, offsetTop(el) - 20);
26 history.replaceState(null, "", "#" + id);
27 }
28 }
29
30 function open (event) {
31 var href = this.href;
32 var target = this.target === "_parent" ? window.parent : window;
33
34 if (this.getAttribute('href')[0] === "#")
35 scrollToId(this.getAttribute('href').slice(1));
36 else
37 target.location.href = this.href;
38 event.preventDefault();
39 }
40
41 window.addEventListener('load', function () {
42 if (typeof FastClick !== "undefined")
43 FastClick.attach(document.body, { tapDelay: 50 });
44 scrollToId(location.hash.slice(1));
45 });
46
47 window.addEventListener('DOMContentLoaded', function () {
48 if (navigator.standalone)
49 document.body.className += ' standalone';
50
51 var links = document.querySelectorAll(
52 "a[href]:not([target=_blank])");
53 for (var i = 0; i < links.length; ++i)
54 links[i].addEventListener('click', open);
55 });
56
57 function choice (seq) {
58 return seq[(Math.random() * seq.length) | 0];
59 }
60
61 function contains (element) {
62 return this.indexOf(element) >= 0;
63 }
64
65 function issubset (sub, sup) {
66 return sub.every(contains, sup);
67 }
68
69 function intersects (a, b) {
70 return Array.prototype.some.call(a, contains, b);
71 }
72
73 function ventureForth () {
74 var difficulty = document.querySelector("[data-events]:checked");
75 var denizens = document.querySelectorAll("[data-flags]:checked");
76 var hash = "#" + [difficulty.getAttribute("data-events"),
77 difficulty.getAttribute("data-nop")]
78 .concat(Array.prototype.map.call(denizens, function (element) {
79 return element.getAttribute("data-flags");
80 })).join(",");
81 location.href = "scenario.html" + hash;
82 }
83
84 if (applicationCache && applicationCache.status) {
85 applicationCache.update();
86 applicationCache.addEventListener('updateready', function () {
87 if (applicationCache.status === applicationCache.UPDATEREADY) {
88 applicationCache.swapCache();
89 }
90 });
91 }