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
|
package client;
import haxe.io.Mime;
import js.Browser.document;
import js.Browser.navigator;
import js.Browser.window;
import js.html.Blob;
import js.html.Element;
import js.html.FileReader;
import js.html.URL;
import js.html.audio.AudioContext;
import js.lib.ArrayBuffer;
class Utils {
public static function nativeTrace(msg:Dynamic, ?infos:haxe.PosInfos):Void {
final fileData = '${infos.fileName}:${infos.lineNumber}';
var args:Array<Dynamic> = [fileData, msg];
if (infos.customParams != null) args = args.concat(infos.customParams);
js.Browser.window.console.log(
...haxe.Rest.of(args)
);
}
public static function isTouch():Bool {
return js.Syntax.code("'ontouchstart' in window");
}
public static function isIOS():Bool {
return ~/^(iPhone|iPad|iPod)/.match(navigator.platform)
|| (~/^Mac/.match(navigator.platform) && navigator.maxTouchPoints > 4);
}
public static var isMacSafari = _isMacSafari();
static function _isMacSafari():Bool {
final isMac = navigator.userAgent.contains("Macintosh");
final isSafari = navigator.userAgent.contains("Safari")
&& !navigator.userAgent.contains("Chrom")
&& !navigator.userAgent.contains("Edg");
return isMac && isSafari;
}
public static function isAndroid():Bool {
final ua = navigator.userAgent.toLowerCase();
return ua.indexOf("android") > -1;
}
public static function nodeFromString(div:String):Element {
final wrapper = document.createDivElement();
wrapper.innerHTML = div;
return wrapper.firstElementChild;
}
public static function outerHeight(el:Element):Float {
final style = window.getComputedStyle(el);
return (el.getBoundingClientRect().height
+ Std.parseFloat(style.marginTop)
+ Std.parseFloat(style.marginBottom));
}
public static function insertAtIndex(parent:Element, child:Element, i:Int) {
if (i >= parent.children.length) parent.appendChild(child);
else parent.insertBefore(child, parent.children[i]);
}
public static function getIndex(parent:Element, child:Element):Int {
var i = 0;
for (el in parent.children) {
if (el == child) break;
i++;
}
return i;
}
public static function hasFullscreen():Bool {
final doc:Dynamic = document;
return (document.fullscreenElement != null || doc.mozFullScreenElement != null
|| doc.webkitFullscreenElement != null);
}
public static function requestFullscreen(el:Element):Bool {
final el2:Dynamic = el;
if (el.requestFullscreen != null) {
el.requestFullscreen();
} else if (el2.webkitRequestFullscreen != null) {
el2.webkitRequestFullscreen(untyped Element.ALLOW_KEYBOARD_INPUT);
} else return false;
return true;
}
public static function cancelFullscreen(el:Element):Void {
final doc:Dynamic = document;
if (doc.cancelFullScreen != null) doc.cancelFullScreen();
else if (doc.webkitCancelFullScreen != null) doc.webkitCancelFullScreen();
}
public static function toggleFullscreen(el:Element):Bool {
if (hasFullscreen()) {
cancelFullscreen(el);
return false;
}
return requestFullscreen(el);
}
public static function copyToClipboard(text:String):Void {
final clipboardData = (window : Dynamic).clipboardData;
if (clipboardData != null && clipboardData.setData != null) {
// IE-specific code path to prevent textarea being shown while dialog is visible.
clipboardData.setData("Text", text);
return;
} else if ((document : Dynamic).queryCommandSupported != null) {
final textarea = document.createTextAreaElement();
textarea.textContent = text;
// Prevent scrolling to bottom of page in Microsoft Edge.
textarea.style.position = "fixed";
document.body.appendChild(textarea);
textarea.select();
try {
// Security exception may be thrown by some browsers.
document.execCommand("copy");
}
document.body.removeChild(textarea);
}
}
public static function matchedNum(ereg:EReg):Int {
#if js
return (ereg : Dynamic).r.m.length;
#else
#error "not implemented"
#end
}
public static function browseFile(
onFileLoad:(buffer:ArrayBuffer, name:String) -> Void
):Void {
browseFileImpl(onFileLoad, true, false);
}
public static function browseFileUrl(
onFileLoad:(url:String, name:String) -> Void,
revoke = false
):Void {
browseFileImpl(onFileLoad, false, revoke);
}
static function browseFileImpl(
onFileLoad:(data:Dynamic, name:String) -> Void,
isBinary:Bool,
revokeAfterLoad:Bool
):Void {
final input = document.createInputElement();
input.style.visibility = "hidden";
input.type = "file";
input.id = "browse";
input.onclick = e -> {
e.cancelBubble = true;
e.stopPropagation();
}
input.onchange = e -> {
final file = input.files[0] ?? return;
if (!isBinary) {
final url = URL.createObjectURL(file);
onFileLoad(url, file.name);
document.body.removeChild(input);
if (revokeAfterLoad) URL.revokeObjectURL(url);
return;
}
final reader = new FileReader();
reader.onload = e -> {
final result:ArrayBuffer = reader.result;
onFileLoad(result, file.name);
document.body.removeChild(input);
}
reader.onerror = e -> {
document.body.removeChild(input);
}
reader.readAsArrayBuffer(file);
}
document.body.appendChild(input);
input.click();
}
public static function saveFile(name:String, mime:Mime, data:String):Void {
final blob = new Blob([data], {
type: mime
});
final url = URL.createObjectURL(blob);
final a = document.createAnchorElement();
a.download = name;
a.href = url;
a.onclick = e -> {
e.cancelBubble = true;
e.stopPropagation();
}
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
public static function createResizeObserver(callback:(entries:Array<Dynamic>) -> Void):Null<{
observe:(el:Element) -> Void
}> {
final window = js.Browser.window;
final observer = (window : Dynamic).ResizeObserver ?? return null;
return js.Syntax.code("new ResizeObserver({0})", callback);
}
public static function createAudioContext():Null<AudioContext> {
final w:Dynamic = js.Browser.window;
final ctx = w.AudioContext ?? w.webkitAudioContext ?? return null;
return js.Syntax.code("new {0}()", ctx);
}
}
|