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
|
function initThemeChooser(settings) {
var isInitialized = false;
var currentThemeSystem; // don't set this directly. use setThemeSystem
var currentStylesheetEl;
var loadingEl = document.getElementById('loading');
var systemSelectEl = document.querySelector('#theme-system-selector select');
var themeSelectWrapEls = Array.prototype.slice.call( // convert to real array
document.querySelectorAll('.selector[data-theme-system]')
);
systemSelectEl.addEventListener('change', function() {
setThemeSystem(this.value);
});
setThemeSystem(systemSelectEl.value);
themeSelectWrapEls.forEach(function(themeSelectWrapEl) {
var themeSelectEl = themeSelectWrapEl.querySelector('select');
themeSelectWrapEl.addEventListener('change', function() {
setTheme(
currentThemeSystem,
themeSelectEl.options[themeSelectEl.selectedIndex].value
);
});
});
function setThemeSystem(themeSystem) {
var selectedTheme;
currentThemeSystem = themeSystem;
themeSelectWrapEls.forEach(function(themeSelectWrapEl) {
var themeSelectEl = themeSelectWrapEl.querySelector('select');
if (themeSelectWrapEl.getAttribute('data-theme-system') === themeSystem) {
selectedTheme = themeSelectEl.options[themeSelectEl.selectedIndex].value;
themeSelectWrapEl.style.display = 'inline-block';
} else {
themeSelectWrapEl.style.display = 'none';
}
});
setTheme(themeSystem, selectedTheme);
}
function setTheme(themeSystem, themeName) {
var stylesheetUrl = generateStylesheetUrl(themeSystem, themeName);
var stylesheetEl;
function done() {
if (!isInitialized) {
isInitialized = true;
settings.init(themeSystem);
}
else {
settings.change(themeSystem);
}
showCredits(themeSystem, themeName);
}
if (stylesheetUrl) {
stylesheetEl = document.createElement('link');
stylesheetEl.setAttribute('rel', 'stylesheet');
stylesheetEl.setAttribute('href', stylesheetUrl);
document.querySelector('head').appendChild(stylesheetEl);
loadingEl.style.display = 'inline';
whenStylesheetLoaded(stylesheetEl, function() {
if (currentStylesheetEl) {
currentStylesheetEl.parentNode.removeChild(currentStylesheetEl);
}
currentStylesheetEl = stylesheetEl;
loadingEl.style.display = 'none';
done();
});
} else {
if (currentStylesheetEl) {
currentStylesheetEl.parentNode.removeChild(currentStylesheetEl);
currentStylesheetEl = null
}
done();
}
}
function generateStylesheetUrl(themeSystem, themeName) {
if (themeSystem === 'bootstrap') {
if (themeName) {
return 'https://bootswatch.com/4/' + themeName + '/bootstrap.min.css';
}
else { // the default bootstrap theme
return 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css';
}
}
}
function showCredits(themeSystem, themeName) {
var creditId;
if (themeSystem.match('bootstrap')) {
if (themeName) {
creditId = 'bootstrap-custom';
}
else {
creditId = 'bootstrap-standard';
}
}
Array.prototype.slice.call( // convert to real array
document.querySelectorAll('.credits')
).forEach(function(creditEl) {
if (creditEl.getAttribute('data-credit-id') === creditId) {
creditEl.style.display = 'block';
} else {
creditEl.style.display = 'none';
}
})
}
function whenStylesheetLoaded(linkNode, callback) {
var isReady = false;
function ready() {
if (!isReady) { // avoid double-call
isReady = true;
callback();
}
}
linkNode.onload = ready; // does not work cross-browser
setTimeout(ready, 2000); // max wait. also handles browsers that don't support onload
}
}
|