This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.js
121 lines (101 loc) · 4.48 KB
/
main.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
(function() {
// Functions
// =========================================================================
/**
* Adds event listeners to change active stylesheet and restore previously
* activated stylesheet on reload.
*
* @example
*
* This link:
* <a href="#" data-link-title="Foo">Foo</a>
* Will active this existing link:
* <link rel="stylesheet alternate" title="Foo" href="..." >
*
* @example
*
* This link:
* <a href="#" data-link-href="path/to/file.css">Bar</a>
* Will activate this existing link:
* <link rel="stylesheet alternate" title="[someID]" href="path/to/file.css" >
* Or generate this active link:
* <link rel="stylesheet" title="Bar" href="path/to/file.css" >
*/
function initStyleSwitcher() {
var isInitialzed = false;
var sessionStorageKey = 'activeStylesheetHref';
function handleSwitch(activeHref, activeTitle) {
var activeElm = document.querySelector('link[href*="' + activeHref +'"],link[title="' + activeTitle +'"]');
if (!activeElm && activeHref) {
activeElm = document.createElement('link');
activeElm.setAttribute('href', activeHref);
activeElm.setAttribute('rel', 'stylesheet');
activeElm.setAttribute('title', activeTitle);
document.head.appendChild(activeElm);
activeElm.addEventListener('load', function linkOnLoad() {
activeElm.removeEventListener('load', linkOnLoad);
setActiveLink(activeElm);
});
}
else if (activeElm) {
setActiveLink(activeElm);
}
}
function setActiveLink(activeElm) {
var activeHref = activeElm.getAttribute('href');
var activeTitle = activeElm.getAttribute('title');
var inactiveElms = document.querySelectorAll('link[title]:not([href*="' + activeHref +'"]):not([title="' + activeTitle +'"])');
// Remove "alternate" keyword
activeElm.setAttribute('rel', (activeElm.rel || '').replace(/\s*alternate/g, '').trim());
// Force enable stylesheet (required for some browsers)
activeElm.disabled = true;
activeElm.disabled = false;
// Store active style sheet
sessionStorage.setItem(sessionStorageKey, activeHref);
// Disable other elms
for (var i = 0; i < inactiveElms.length; i++) {
var elm = inactiveElms[i];
elm.disabled = true;
// Fix for browsersync and alternate stylesheet updates. Will
// cause FOUC when switching stylesheets during development, but
// required to properly apply style updates when alternate
// stylesheets are enabled.
if (window.browsersyncObserver) {
var linkRel = elm.getAttribute('rel') || '';
var linkRelAlt = linkRel.indexOf('alternate') > -1 ? linkRel : (linkRel + ' alternate').trim();
elm.setAttribute('rel', linkRelAlt);
}
}
// CSS custom property ponyfil
if ((window.$docsify || {}).themeable) {
window.$docsify.themeable.util.cssVars();
}
}
// Event listeners
if (!isInitialzed) {
isInitialzed = true;
// Restore active stylesheet
document.addEventListener('DOMContentLoaded', function() {
var activeHref = sessionStorage.getItem(sessionStorageKey);
if (activeHref) {
handleSwitch(activeHref);
}
});
// Update active stylesheet
document.addEventListener('click', function(evt) {
var dataHref = evt.target.getAttribute('data-link-href');
var dataTitle = evt.target.getAttribute('data-link-title')
if (dataHref || dataTitle) {
dataTitle = dataTitle
|| evt.target.textContent
|| '_' + Math.random().toString(36).substr(2, 9); // UID
handleSwitch(dataHref, dataTitle);
evt.preventDefault();
}
});
}
}
// Main
// =========================================================================
initStyleSwitcher();
})();