-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathembeddableLoader.js
78 lines (68 loc) · 2.46 KB
/
embeddableLoader.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
'use strict'
const makeSznSelectBundleScript = (global => { // eslint-disable-line no-unused-vars
const BUNDLES = {
ELEMENT_ES2016: 'es2016',
ELEMENT_ES3: 'es3',
ELEMENTS_ES2016: 'bundle-elements.es2016',
ELEMENTS_ES3: 'bundle-elements.es3',
ALL_CE: 'bundle-full.ce',
ALL_ES2016: 'bundle-full.es2016',
ALL_ES3: 'bundle-full.es3',
}
const DEFAULT_PACKAGE_URL = 'https://cdn.jsdelivr.net/npm/@seznam/szn-select@<VERSION>/'
function makeBundleLoadScript(urlsConfiguration, useAsyncLoading) {
if (typeof self === 'undefined') {
throw new Error('The loader can only be used at the client-side')
}
if (global.SznElements && global.SznElements['szn-tethered'] && global.SznElements['szn-select']) {
return null // already loaded
}
const bundleUrl = getSznSelectBundleUrl(urlsConfiguration)
const script = document.createElement('script')
script.async = useAsyncLoading
script.defer = useAsyncLoading
script.src = bundleUrl
return script
}
function getSznSelectBundleUrl(urlsConfiguration) {
// Firefox 52-based browsers (e.g. Palemoon) have faulty support for for-scoped const variable declarations
const firefoxVersionMatch = navigator.userAgent.match(/ Firefox\/(\d+)/)
const firefoxVersion = firefoxVersionMatch && parseInt(firefoxVersionMatch[1], 10)
const supportsForConst = !firefoxVersion || firefoxVersion > 52
const es2016Supported = supportsForConst && global.Proxy && Array.prototype.includes
const runtimeLoaded = global.SznElements && global.SznElements.init && global.SznElements.injectStyles
const dependenciesLoaded = runtimeLoaded && global.SznElements['szn-tethered']
const bundleToLoad = dependenciesLoaded ?
(es2016Supported ?
BUNDLES.ELEMENT_ES2016
:
BUNDLES.ELEMENT_ES3
)
:
(runtimeLoaded ?
(es2016Supported ?
BUNDLES.ELEMENTS_ES2016
:
BUNDLES.ELEMENTS_ES3
)
:
(global.customElements ?
BUNDLES.ALL_CE
:
(es2016Supported ?
BUNDLES.ALL_ES2016
:
BUNDLES.ALL_ES3
)
)
)
return (
urlsConfiguration[bundleToLoad] ||
(() => {
const baseUrl = urlsConfiguration.package || DEFAULT_PACKAGE_URL
return /\/$/.test(baseUrl) ? baseUrl : `${baseUrl}/`
})() + `szn-select.${bundleToLoad}.min.js`
)
}
return makeBundleLoadScript
})(self)