()
| 78 | } |
| 79 | } |
| 80 | function initStyleSwitcher() { |
| 81 | var isInitialzed = false; |
| 82 | var sessionStorageKey = 'activeStylesheetHref'; |
| 83 | |
| 84 | function handleSwitch(activeHref, activeTitle) { |
| 85 | var activeElm = document.querySelector('link[href*="' + activeHref + '"],link[title="' + activeTitle + '"]'); |
| 86 | |
| 87 | if (!activeElm && activeHref) { |
| 88 | activeElm = document.createElement('link'); |
| 89 | activeElm.setAttribute('href', activeHref); |
| 90 | activeElm.setAttribute('rel', 'stylesheet'); |
| 91 | activeElm.setAttribute('title', activeTitle); |
| 92 | |
| 93 | document.head.appendChild(activeElm); |
| 94 | |
| 95 | activeElm.addEventListener('load', function linkOnLoad() { |
| 96 | activeElm.removeEventListener('load', linkOnLoad); |
| 97 | setActiveLink(activeElm); |
| 98 | }); |
| 99 | } |
| 100 | else if (activeElm) { |
| 101 | setActiveLink(activeElm); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function setActiveLink(activeElm) { |
| 106 | var activeHref = activeElm.getAttribute('href'); |
| 107 | var activeTitle = activeElm.getAttribute('title'); |
| 108 | var inactiveElms = document.querySelectorAll('link[title]:not([href*="' + activeHref + '"]):not([title="' + activeTitle + '"])'); |
| 109 | |
| 110 | // Remove "alternate" keyword |
| 111 | activeElm.setAttribute('rel', (activeElm.rel || '').replace(/\s*alternate/g, '').trim()); |
| 112 | |
| 113 | // Force enable stylesheet (required for some browsers) |
| 114 | activeElm.disabled = true; |
| 115 | activeElm.disabled = false; |
| 116 | |
| 117 | // Store active style sheet |
| 118 | sessionStorage.setItem(sessionStorageKey, activeHref); |
| 119 | |
| 120 | // Disable other elms |
| 121 | for (var i = 0; i < inactiveElms.length; i++) { |
| 122 | var elm = inactiveElms[i]; |
| 123 | |
| 124 | elm.disabled = true; |
| 125 | |
| 126 | // Fix for browsersync and alternate stylesheet updates. Will |
| 127 | // cause FOUC when switching stylesheets during development, but |
| 128 | // required to properly apply style updates when alternate |
| 129 | // stylesheets are enabled. |
| 130 | if (window.browsersyncObserver) { |
| 131 | var linkRel = elm.getAttribute('rel') || ''; |
| 132 | var linkRelAlt = linkRel.indexOf('alternate') > -1 ? linkRel : (linkRel + ' alternate').trim(); |
| 133 | |
| 134 | elm.setAttribute('rel', linkRelAlt); |
| 135 | } |
| 136 | } |
| 137 |
no test coverage detected