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";
35 window
.requestAnimationFrame
= (
36 window
.requestAnimationFrame
37 || window
.mozRequestAnimationFrame
38 || window
.webkitRequestAnimationFrame
);
39 window
.cancelAnimationFrame
= (
40 window
.cancelAnimationFrame
41 || window
.mozCancelAnimationFrame
42 || window
.webkitCancelAnimationFrame
);
44 function debounce (callback
, wait
) {
51 callback
.apply(this_
, args
);
61 id
= setTimeout(_
, wait
);
65 function iclamp (x
, lo
, hi
) {
66 return Math
.max(Math
.min(x
| 0, hi
), lo
);
69 function broadcast (data
, source
) {
70 source
= source
|| window
;
71 if (window
.parent
&& window
.parent
!== source
&& window
.parent
!== window
) {
72 window
.parent
.postMessage(data
, "*");
74 for (var i
= 0; i
< window
.frames
.length
; ++i
) {
75 var w
= window
.frames
[i
].window
;
77 w
.postMessage(data
, "*");
84 window
.addEventListener('message', function (event
) {
85 broadcast(event
.data
, event
.source
);
86 if (messages
[event
.data
.type
])
87 messages
[event
.data
.type
](event
.data
);
90 function stop (event
) {
91 event
.preventDefault();
94 function nextTheme (element
) {
95 var nextId
= (THEMES
.indexOf(element
.className
) + 1) % THEMES
.length
;
96 var theme
= THEMES
[nextId
];
97 element
.className
= theme
;
98 var id
= element
.id
? element
.id
99 : element
=== document
.body
.parentNode
? "html"
102 var key
= [APP
, "theme", window
.name
|| "window", id
].join(".");
103 localStorage
[key
] = theme
;
107 var repaint
= debounce(function () {
108 document
.body
.removeChild(
109 document
.body
.appendChild(
110 document
.createElement('style')));
113 window
.addEventListener('touchmove', stop
);
115 // Work around incorrect viewport units in (mostly Mobile) Safari. At
116 // page-load time they are often calculated as 0 (because the viewport
117 // isn't ready yet?) and they are not refreshed when the viewport does
119 window
.addEventListener('load', repaint
);
120 window
.addEventListener('resize', repaint
);
121 window
.addEventListener('orientationchange', repaint
);
123 function randomTheme () {
124 return THEMES
[(Math
.random() * THEMES
.length
) | 0];
127 function storedTheme () {
129 [APP
, "theme", window
.name
|| "window", "html"].join(".")];
132 function loadTheme () {
133 if (THEMES
.indexOf(location
.hash
.slice(1)) >= 0) {
134 document
.body
.parentNode
.className
= location
.hash
.slice(1);
135 } else if (document
.body
.parentNode
.className
=== "stored-theme") {
136 var random
= THEMES
[(Math
.random() * THEMES
.length
) | 0];
137 document
.body
.parentNode
.className
= storedTheme() || randomTheme();
138 } else if (document
.body
.parentNode
.className
=== "random-theme") {
139 document
.body
.parentNode
.className
= randomTheme();
143 window
.addEventListener('hashchange', function () {
144 var theme
= location
.hash
.slice(1);
145 if (THEMES
.indexOf(theme
) >= 0) {
146 document
.body
.parentNode
.className
= theme
;
150 window
.addEventListener('pageshow', repaint
);
152 function removeStatusBar () {
153 document
.body
.className
= "";
156 function addStatusBar () {
157 document
.body
.className
= "statusbar";
160 function open (event
) {
161 (this.target
=== "_parent" ? window
.parent
: window
).location
.href
163 event
.preventDefault();
166 window
.addEventListener('DOMContentLoaded', function () {
167 if (navigator
.standalone
) {
168 var links
= document
.querySelectorAll("a[href]:not([target=_blank])");
169 for (var i
= 0; i
< links
.length
; ++i
) {
170 links
[i
].addEventListener('click', open
);
174 if (typeof FastClick
!== "undefined")
175 FastClick
.attach(document
.body
, { tapDelay
: 50 });
179 function afterAnimationFrame (f
) {
180 /* DOM class modifications intended to trigger transitions
181 must be delayed for at least one frame after the element is
182 created, i.e. after it has gone through at least one full
185 window
.requestAnimationFrame(function () {
186 window
.requestAnimationFrame(f
);
191 var orig
= document
.getElementById("spinner");
192 var spinner
= orig
.cloneNode();
193 spinner
.className
= 'spinner';
194 spinner
.style
.opacity
= 1;
195 spinner
.style
.transform
= spinner
.style
["-webkit-transform"] = "rotateZ(0deg)";
196 document
.body
.replaceChild(spinner
, orig
);
197 afterAnimationFrame(function () {
198 var z
= (10 + (Math
.random() * 2) | 0) * 180;
199 var transform
= "rotateZ(" + z
+ "deg)";
200 spinner
.style
.transform
= spinner
.style
["-webkit-transform"] = transform
;
201 spinner
.style
.opacity
= 0;
206 var die
= document
.createElement("div");
207 die
.className
= 'die rolling d' + n
;
208 document
.getElementById("triggered").appendChild(die
);
209 die
.setAttribute('data-roll', 1 + (Math
.random() * n
) | 0);
210 var id
= setInterval(function () {
211 die
.setAttribute('data-roll', 1 + (Math
.random() * n
) | 0);
214 die
.addEventListener('animationend', function _ () {
215 die
.removeEventListener('animationend', _
);
216 die
.parentNode
.removeChild(die
);
218 die
.style
.transform
= die
.style
["-webkit-transform"] = "rotateY(-270deg)";
219 afterAnimationFrame(function () {
220 die
.style
.transform
= die
.style
["-webkit-transform"] = "rotateY(360deg)";
223 die
.removeEventListener('transitionend', _
);
224 die
.removeEventListener('webkitTransitionEnd', _
);
226 die
.addEventListener('transitionend', _
);
227 die
.addEventListener('webkitTransitionEnd', _
);
231 if (applicationCache
&& applicationCache
.status
) {
232 applicationCache
.update();
233 applicationCache
.addEventListener('updateready', function () {
234 if (applicationCache
.status
=== applicationCache
.UPDATEREADY
) {
235 applicationCache
.swapCache();
240 function dismiss () {
241 var open
= document
.querySelectorAll(".dialog.open");
242 for (var i
= 0; i
< open
.length
; ++i
)
243 open
[i
].className
= "dialog";
248 var target
= document
.getElementById(id
);
249 target
.className
+= " open";