-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathCDN.js
96 lines (87 loc) · 2.37 KB
/
CDN.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
// All external libs URLs should be injected through this class.
// CDN libs URLs are accessible throuh CDN object properties
// like Formio.cdn.ace === 'http://cdn.form.io/ace/1.4.12'.
// For latest version use empty string
class CDN {
static defaultCDN = 'https://cdn.form.io';
constructor(baseUrl, overrides = {}) {
this.baseUrl = baseUrl || CDN.defaultCDN;
this.overrides = overrides;
this.libs = {
'js': '',
'ace': '1.4.12',
'bootstrap': '5.3.3',
'bootstrap4': '4.6.2',
'bootstrap5': '5.3.3',
'bootswatch': '5.3.3',
'bootstrap-icons': '1.11.1',
'ckeditor': '19.0.0',
'dragula': '3.7.3',
'flatpickr': '4.6.13',
'font-awesome': '4.7.0',
'grid': 'latest',
'moment-timezone': 'latest',
'quill': '2.0.0-dev.3',
'shortcut-buttons-flatpickr': '0.4.0',
'uswds': '2.4.8',
'core': ''
};
this.updateUrls();
}
getVersion(lib) {
return this.libs[lib];
}
// Sets a specific library version
setVersion(lib, version) {
this.libs[lib] = version;
this.updateUrls();
}
// Sets base CDN url for all libraries
setBaseUrl(url) {
this.baseUrl = url;
this.updateUrls();
}
// Allows to override CDN url for a specific library
setOverrideUrl(lib, url) {
this.overrides[lib] = url;
this.updateUrls();
}
// Removes override for a specific library
removeOverride(lib) {
delete this.overrides[lib];
this.updateUrls();
}
// Removes all overrides
removeOverrides() {
this.overrides = {};
this.updateUrls();
}
buildUrl(cdnUrl, lib, version) {
let url = cdnUrl;
if (lib) {
url += `/${lib}`;
}
// Only attach the version if this is the hosted cdn.
if (cdnUrl.match(/cdn\.(test-)?form.io/) && version && version !== 'latest') {
url += `/${version}`;
}
return url;
}
updateUrls() {
for (const lib in this.libs) {
if (lib in this.overrides) {
if (typeof this.overrides[lib] === 'string') {
this[lib] = this.buildUrl(this.overrides[lib], lib, this.libs[lib]);
}
else {
const override = this.overrides[lib];
this[lib] = this.buildUrl(override.cdn, override.lib || '', override.version || '');
}
}
else {
this[lib] = this.buildUrl(this.baseUrl, lib, this.libs[lib]);
}
}
}
}
export default CDN;