Flavor events.
[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 not (f) {
62 return function () { return !f.apply(this, arguments); };
63 }
64
65 function contains (element) {
66 return this.indexOf(element) >= 0;
67 }
68
69 function issubset (sub, sup) {
70 return sub.every(contains, sup);
71 }
72
73 function intersects (a, b) {
74 return Array.prototype.some.call(a, contains, b);
75 }
76
77 function ventureForth () {
78 var difficulty = document.querySelector("[data-events]:checked");
79 var denizens = document.querySelectorAll("[data-flags]:checked");
80 var hash = "#" + [difficulty.getAttribute("data-events"),
81 difficulty.getAttribute("data-nop")]
82 .concat(Array.prototype.map.call(denizens, function (element) {
83 return element.getAttribute("data-flags");
84 })).join(",");
85 location.href = "scenario.html" + hash;
86 }
87
88 if (applicationCache && applicationCache.status) {
89 applicationCache.update();
90 applicationCache.addEventListener('updateready', function () {
91 if (applicationCache.status === applicationCache.UPDATEREADY) {
92 applicationCache.swapCache();
93 }
94 });
95 }