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.
6 You can copy, modify, distribute and perform the work, even for
7 commercial purposes, all without asking permission.
9 See https://creativecommons.org/publicdomain/zero/1.0/ for details.
14 var APP
= "com.yukkurigames.mlpccg";
34 window
.requestAnimationFrame
= (
35 window
.requestAnimationFrame
36 || window
.mozRequestAnimationFrame
37 || window
.webkitRequestAnimationFrame
);
38 window
.cancelAnimationFrame
= (
39 window
.cancelAnimationFrame
40 || window
.mozCancelAnimationFrame
41 || window
.webkitCancelAnimationFrame
);
43 function debounce (callback
, wait
) {
50 callback
.apply(this_
, args
);
60 id
= setTimeout(_
, wait
);
64 function iclamp (x
, lo
, hi
) {
65 return Math
.max(Math
.min(x
| 0, hi
), lo
);
68 function broadcast (data
, source
) {
69 source
= source
|| window
;
70 if (window
.parent
&& window
.parent
!== source
&& window
.parent
!== window
) {
71 window
.parent
.postMessage(data
, "*");
73 for (var i
= 0; i
< window
.frames
.length
; ++i
) {
74 var w
= window
.frames
[i
].window
;
76 w
.postMessage(data
, "*");
83 window
.addEventListener('message', function (event
) {
84 broadcast(event
.data
, event
.source
);
85 if (messages
[event
.data
.type
])
86 messages
[event
.data
.type
](event
.data
);
89 function stop (event
) {
90 event
.preventDefault();
93 function nextTheme (element
) {
94 var nextId
= (THEMES
.indexOf(element
.className
) + 1) % THEMES
.length
;
95 var theme
= THEMES
[nextId
];
96 element
.className
= theme
;
97 var id
= element
.id
? element
.id
98 : element
=== document
.body
.parentNode
? "html"
101 var key
= [APP
, "theme", window
.name
|| "window", id
].join(".");
102 localStorage
[key
] = theme
;
106 var repaint
= debounce(function () {
107 document
.body
.removeChild(
108 document
.body
.appendChild(
109 document
.createElement('style')));
112 window
.addEventListener('touchmove', stop
);
114 // Work around incorrect viewport units in (mostly Mobile) Safari. At
115 // page-load time they are often calculated as 0 (because the viewport
116 // isn't ready yet?) and they are not refreshed when the viewport does
118 window
.addEventListener('load', repaint
);
119 window
.addEventListener('resize', repaint
);
120 window
.addEventListener('orientationchange', repaint
);
122 function randomTheme () {
123 return THEMES
[(Math
.random() * THEMES
.length
) | 0];
126 function storedTheme () {
128 [APP
, "theme", window
.name
|| "window", "html"].join(".")];
131 function loadTheme () {
132 if (THEMES
.indexOf(location
.hash
.slice(1)) >= 0) {
133 document
.body
.parentNode
.className
= location
.hash
.slice(1);
134 } else if (document
.body
.parentNode
.className
=== "stored-theme") {
135 var random
= THEMES
[(Math
.random() * THEMES
.length
) | 0];
136 document
.body
.parentNode
.className
= storedTheme() || randomTheme();
137 } else if (document
.body
.parentNode
.className
=== "random-theme") {
138 document
.body
.parentNode
.className
= randomTheme();
142 window
.addEventListener('hashchange', function () {
143 var theme
= location
.hash
.slice(1);
144 if (THEMES
.indexOf(theme
) >= 0) {
145 document
.body
.parentNode
.className
= theme
;
149 window
.addEventListener('pageshow', repaint
);
151 function removeStatusBar () {
152 document
.body
.className
= "";
155 function addStatusBar () {
156 document
.body
.className
= "statusbar";
159 function open (event
) {
160 (this.target
=== "_parent" ? window
.parent
: window
).location
.href
162 event
.preventDefault();
165 window
.addEventListener('DOMContentLoaded', function () {
166 if (navigator
.standalone
) {
167 var links
= document
.querySelectorAll("a[href]:not([target=_blank])");
168 for (var i
= 0; i
< links
.length
; ++i
) {
169 links
[i
].addEventListener('click', open
);
173 if (typeof FastClick
!== "undefined")
174 FastClick
.attach(document
.body
, { tapDelay
: 50 });
178 function afterAnimationFrame (f
) {
179 /* DOM class modifications intended to trigger transitions
180 must be delayed for at least one frame after the element is
181 created, i.e. after it has gone through at least one full
184 window
.requestAnimationFrame(function () {
185 window
.requestAnimationFrame(f
);
190 var orig
= document
.getElementById("spinner");
191 var spinner
= orig
.cloneNode();
192 spinner
.className
= 'spinner';
193 spinner
.style
.opacity
= 1;
194 spinner
.style
.transform
= spinner
.style
["-webkit-transform"] = "rotateZ(0deg)";
195 document
.body
.replaceChild(spinner
, orig
);
196 afterAnimationFrame(function () {
197 var z
= (10 + (Math
.random() * 2) | 0) * 180;
198 var transform
= "rotateZ(" + z
+ "deg)";
199 spinner
.style
.transform
= spinner
.style
["-webkit-transform"] = transform
;
200 spinner
.style
.opacity
= 0;
205 var die
= document
.createElement("div");
206 die
.className
= 'die rolling d' + n
;
207 document
.getElementById("triggered").appendChild(die
);
208 die
.setAttribute('data-roll', 1 + (Math
.random() * n
) | 0);
209 var id
= setInterval(function () {
210 die
.setAttribute('data-roll', 1 + (Math
.random() * n
) | 0);
213 die
.addEventListener('animationend', function _ () {
214 die
.removeEventListener('animationend', _
);
215 die
.parentNode
.removeChild(die
);
217 die
.style
.transform
= die
.style
["-webkit-transform"] = "rotateY(-270deg)";
218 afterAnimationFrame(function () {
219 die
.style
.transform
= die
.style
["-webkit-transform"] = "rotateY(360deg)";
222 die
.removeEventListener('transitionend', _
);
223 die
.removeEventListener('webkitTransitionEnd', _
);
225 die
.addEventListener('transitionend', _
);
226 die
.addEventListener('webkitTransitionEnd', _
);
230 if (applicationCache
&& applicationCache
.status
) {
231 applicationCache
.update();
232 applicationCache
.addEventListener('updateready', function () {
233 if (applicationCache
.status
=== applicationCache
.UPDATEREADY
) {
234 applicationCache
.swapCache();
239 function dismiss () {
240 var open
= document
.querySelectorAll(".dialog.open");
241 for (var i
= 0; i
< open
.length
; ++i
)
242 open
[i
].className
= "dialog";
247 var target
= document
.getElementById(id
);
248 target
.className
+= " open";