-
Notifications
You must be signed in to change notification settings - Fork 64
/
Browser.js
460 lines (366 loc) · 10.3 KB
/
Browser.js
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
import Basics exposing (never)
import Browser exposing (Internal, External)
import Browser.Dom as Dom exposing (NotFound)
import Elm.Kernel.Debug exposing (crash)
import Elm.Kernel.Debugger exposing (element, document)
import Elm.Kernel.Json exposing (runHelp)
import Elm.Kernel.List exposing (Nil)
import Elm.Kernel.Platform exposing (initialize)
import Elm.Kernel.Scheduler exposing (binding, fail, rawSpawn, succeed, spawn)
import Elm.Kernel.Utils exposing (Tuple0, Tuple2)
import Elm.Kernel.VirtualDom exposing (appendChild, applyPatches, diff, doc, node, passiveSupported, render, divertHrefToApp)
import Json.Decode as Json exposing (map)
import Maybe exposing (Just, Nothing)
import Result exposing (isOk)
import Task exposing (perform)
import Url exposing (fromString)
*/
// ELEMENT
var __Debugger_element;
var _Browser_element = __Debugger_element || F4(function(impl, flagDecoder, debugMetadata, args)
{
return __Platform_initialize(
flagDecoder,
args,
impl.__$init,
impl.__$update,
impl.__$subscriptions,
function(sendToApp, initialModel) {
var view = impl.__$view;
/**__PROD/
var domNode = args['node'];
//*/
/**__DEBUG/
var domNode = args && args['node'] ? args['node'] : __Debug_crash(0);
//*/
var currNode = _VirtualDom_virtualize(domNode);
return _Browser_makeAnimator(initialModel, function(model)
{
var nextNode = view(model);
var patches = __VirtualDom_diff(currNode, nextNode);
domNode = __VirtualDom_applyPatches(domNode, currNode, patches, sendToApp);
currNode = nextNode;
});
}
);
});
// DOCUMENT
var __Debugger_document;
var _Browser_document = __Debugger_document || F4(function(impl, flagDecoder, debugMetadata, args)
{
return __Platform_initialize(
flagDecoder,
args,
impl.__$init,
impl.__$update,
impl.__$subscriptions,
function(sendToApp, initialModel) {
var divertHrefToApp = impl.__$setup && impl.__$setup(sendToApp)
var view = impl.__$view;
var title = __VirtualDom_doc.title;
var bodyNode = __VirtualDom_doc.body;
var currNode = _VirtualDom_virtualize(bodyNode);
return _Browser_makeAnimator(initialModel, function(model)
{
__VirtualDom_divertHrefToApp = divertHrefToApp;
var doc = view(model);
var nextNode = __VirtualDom_node('body')(__List_Nil)(doc.__$body);
var patches = __VirtualDom_diff(currNode, nextNode);
bodyNode = __VirtualDom_applyPatches(bodyNode, currNode, patches, sendToApp);
currNode = nextNode;
__VirtualDom_divertHrefToApp = 0;
(title !== doc.__$title) && (__VirtualDom_doc.title = title = doc.__$title);
});
}
);
});
// ANIMATION
var _Browser_cancelAnimationFrame =
typeof cancelAnimationFrame !== 'undefined'
? cancelAnimationFrame
: function(id) { clearTimeout(id); };
var _Browser_requestAnimationFrame =
typeof requestAnimationFrame !== 'undefined'
? requestAnimationFrame
: function(callback) { return setTimeout(callback, 1000 / 60); };
function _Browser_makeAnimator(model, draw)
{
draw(model);
var state = __4_NO_REQUEST;
function updateIfNeeded()
{
state = state === __4_EXTRA_REQUEST
? __4_NO_REQUEST
: ( _Browser_requestAnimationFrame(updateIfNeeded), draw(model), __4_EXTRA_REQUEST );
}
return function(nextModel, isSync)
{
model = nextModel;
isSync
? ( draw(model),
state === __4_PENDING_REQUEST && (state = __4_EXTRA_REQUEST)
)
: ( state === __4_NO_REQUEST && _Browser_requestAnimationFrame(updateIfNeeded),
state = __4_PENDING_REQUEST
);
};
}
// APPLICATION
function _Browser_application(impl)
{
var onUrlChange = impl.__$onUrlChange;
var onUrlRequest = impl.__$onUrlRequest;
var key = function() { key.__sendToApp(onUrlChange(_Browser_getUrl())); };
return _Browser_document({
__$setup: function(sendToApp)
{
key.__sendToApp = sendToApp;
_Browser_window.addEventListener('popstate', key);
_Browser_window.navigator.userAgent.indexOf('Trident') < 0 || _Browser_window.addEventListener('hashchange', key);
return F2(function(domNode, event)
{
if (!event.ctrlKey && !event.metaKey && !event.shiftKey && event.button < 1 && !domNode.target && !domNode.hasAttribute('download'))
{
event.preventDefault();
var href = domNode.href;
var curr = _Browser_getUrl();
var next = __Url_fromString(href).a;
sendToApp(onUrlRequest(
(next
&& curr.__$protocol === next.__$protocol
&& curr.__$host === next.__$host
&& curr.__$port_.a === next.__$port_.a
)
? __Browser_Internal(next)
: __Browser_External(href)
));
}
});
},
__$init: function(flags)
{
return A3(impl.__$init, flags, _Browser_getUrl(), key);
},
__$view: impl.__$view,
__$update: impl.__$update,
__$subscriptions: impl.__$subscriptions
});
}
function _Browser_getUrl()
{
return __Url_fromString(__VirtualDom_doc.location.href).a || __Debug_crash(1);
}
var _Browser_go = F2(function(key, n)
{
return A2(__Task_perform, __Basics_never, __Scheduler_binding(function() {
n && history.go(n);
key();
}));
});
var _Browser_pushUrl = F2(function(key, url)
{
return A2(__Task_perform, __Basics_never, __Scheduler_binding(function() {
history.pushState({}, '', url);
key();
}));
});
var _Browser_replaceUrl = F2(function(key, url)
{
return A2(__Task_perform, __Basics_never, __Scheduler_binding(function() {
history.replaceState({}, '', url);
key();
}));
});
// GLOBAL EVENTS
var _Browser_fakeNode = { addEventListener: function() {}, removeEventListener: function() {} };
var _Browser_doc = typeof document !== 'undefined' ? document : _Browser_fakeNode;
var _Browser_window = typeof window !== 'undefined' ? window : _Browser_fakeNode;
var _Browser_on = F3(function(node, eventName, sendToSelf)
{
return __Scheduler_spawn(__Scheduler_binding(function(callback)
{
function handler(event) { __Scheduler_rawSpawn(sendToSelf(event)); }
node.addEventListener(eventName, handler, __VirtualDom_passiveSupported && { passive: true });
return function() { node.removeEventListener(eventName, handler); };
}));
});
var _Browser_decodeEvent = F2(function(decoder, event)
{
var result = __Json_runHelp(decoder, event);
return __Result_isOk(result) ? __Maybe_Just(result.a) : __Maybe_Nothing;
});
// PAGE VISIBILITY
function _Browser_visibilityInfo()
{
return (typeof __VirtualDom_doc.hidden !== 'undefined')
? { __$hidden: 'hidden', __$change: 'visibilitychange' }
:
(typeof __VirtualDom_doc.mozHidden !== 'undefined')
? { __$hidden: 'mozHidden', __$change: 'mozvisibilitychange' }
:
(typeof __VirtualDom_doc.msHidden !== 'undefined')
? { __$hidden: 'msHidden', __$change: 'msvisibilitychange' }
:
(typeof __VirtualDom_doc.webkitHidden !== 'undefined')
? { __$hidden: 'webkitHidden', __$change: 'webkitvisibilitychange' }
: { __$hidden: 'hidden', __$change: 'visibilitychange' };
}
// ANIMATION FRAMES
function _Browser_rAF()
{
return __Scheduler_binding(function(callback)
{
var id = _Browser_requestAnimationFrame(function() {
callback(__Scheduler_succeed(Date.now()));
});
return function() {
_Browser_cancelAnimationFrame(id);
};
});
}
function _Browser_now()
{
return __Scheduler_binding(function(callback)
{
callback(__Scheduler_succeed(Date.now()));
});
}
// DOM STUFF
function _Browser_withNode(id, doStuff)
{
return __Scheduler_binding(function(callback)
{
_Browser_requestAnimationFrame(function() {
var node = document.getElementById(id);
callback(node
? __Scheduler_succeed(doStuff(node))
: __Scheduler_fail(__Dom_NotFound(id))
);
});
});
}
function _Browser_withWindow(doStuff)
{
return __Scheduler_binding(function(callback)
{
_Browser_requestAnimationFrame(function() {
callback(__Scheduler_succeed(doStuff()));
});
});
}
// FOCUS and BLUR
var _Browser_call = F2(function(functionName, id)
{
return _Browser_withNode(id, function(node) {
node[functionName]();
return __Utils_Tuple0;
});
});
// WINDOW VIEWPORT
function _Browser_getViewport()
{
return {
__$scene: _Browser_getScene(),
__$viewport: {
__$x: _Browser_window.pageXOffset,
__$y: _Browser_window.pageYOffset,
__$width: _Browser_doc.documentElement.clientWidth,
__$height: _Browser_doc.documentElement.clientHeight
}
};
}
function _Browser_getScene()
{
var body = _Browser_doc.body;
var elem = _Browser_doc.documentElement;
return {
__$width: Math.max(body.scrollWidth, body.offsetWidth, elem.scrollWidth, elem.offsetWidth, elem.clientWidth),
__$height: Math.max(body.scrollHeight, body.offsetHeight, elem.scrollHeight, elem.offsetHeight, elem.clientHeight)
};
}
var _Browser_setViewport = F2(function(x, y)
{
return _Browser_withWindow(function()
{
_Browser_window.scroll(x, y);
return __Utils_Tuple0;
});
});
// ELEMENT VIEWPORT
function _Browser_getViewportOf(id)
{
return _Browser_withNode(id, function(node)
{
return {
__$scene: {
__$width: node.scrollWidth,
__$height: node.scrollHeight
},
__$viewport: {
__$x: node.scrollLeft,
__$y: node.scrollTop,
__$width: node.clientWidth,
__$height: node.clientHeight
}
};
});
}
var _Browser_setViewportOf = F3(function(id, x, y)
{
return _Browser_withNode(id, function(node)
{
node.scrollLeft = x;
node.scrollTop = y;
return __Utils_Tuple0;
});
});
// ELEMENT
function _Browser_getElement(id)
{
return _Browser_withNode(id, function(node)
{
var rect = node.getBoundingClientRect();
var x = _Browser_window.pageXOffset;
var y = _Browser_window.pageYOffset;
return {
__$scene: _Browser_getScene(),
__$viewport: {
__$x: x,
__$y: y,
__$width: _Browser_doc.documentElement.clientWidth,
__$height: _Browser_doc.documentElement.clientHeight
},
__$element: {
__$x: x + rect.left,
__$y: y + rect.top,
__$width: rect.width,
__$height: rect.height
}
};
});
}
// LOAD and RELOAD
function _Browser_reload(skipCache)
{
return A2(__Task_perform, __Basics_never, __Scheduler_binding(function(callback)
{
__VirtualDom_doc.location.reload(skipCache);
}));
}
function _Browser_load(url)
{
return A2(__Task_perform, __Basics_never, __Scheduler_binding(function(callback)
{
try
{
_Browser_window.location = url;
}
catch(err)
{
// Only Firefox can throw a NS_ERROR_MALFORMED_URI exception here.
// Other browsers reload the page, so let's be consistent about that.
__VirtualDom_doc.location.reload(false);
}
}));
}