|
4 | 4 | @basset(base_path('vendor/studio-42/elfinder/img/icons-big.svg')) |
5 | 5 | @basset(base_path('vendor/studio-42/elfinder/img/logo.png')) |
6 | 6 | @basset(base_path('vendor/studio-42/elfinder/css/elfinder.min.css')) |
7 | | -@basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme.min.css') |
| 7 | +@basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme.min.css', false) |
| 8 | +<link |
| 9 | + rel="prefetch" |
| 10 | + href="{{ Basset::getUrl('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme.min.css') }}" |
| 11 | + data-stylesheet-name="elfinder-theme-dark" |
| 12 | +/> |
| 13 | +@basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css', false) |
| 14 | +<link |
| 15 | + rel="prefetch" |
| 16 | + href="{{ Basset::getUrl('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css') }}" |
| 17 | + data-stylesheet-name="elfinder-theme-light" |
| 18 | +/> |
8 | 19 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/images/loading.svg', false) |
9 | 20 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.eot', false) |
10 | 21 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.svg', false) |
|
14 | 25 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.ttf', false) |
15 | 26 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/font/material.woff2', false) |
16 | 27 | @basset('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css', false) |
17 | | -<span data-elfinder-light-theme-url="{{ Basset::getUrl('https://cdn.jsdelivr.net/gh/RobiNN1/elFinder-Material-Theme@3.0.0/Material/css/theme-gray.min.css') }}" style="display:none"></span> |
| 28 | + |
18 | 29 | @bassetBlock('elfinderThemeSwitcherScript.js') |
19 | 30 | <script type="module"> |
20 | | - function getElfinderStyleSheet(main = true) { |
21 | | - const regex = new RegExp(main ? `RobiNN1\/elFinder-Material-Theme@3.0.0\/Material\/css\/theme\.min\.css` : `RobiNN1\/elFinder-Material-Theme@3.0.0\/Material\/css\/theme-gray\.min\.css`); const linkElements = document.querySelectorAll('link[rel="stylesheet"]'); |
22 | | - // Find the main elfinder stylesheet |
23 | | - let selectedLinkElement; |
24 | | - for (const linkElement of linkElements) { |
25 | | - if (regex.test(linkElement.href)) { |
26 | | - selectedLinkElement = linkElement; |
27 | | - break; |
28 | | - } |
29 | | - } |
30 | | - return selectedLinkElement; |
31 | | - } |
32 | | -
|
33 | | - function addElfinderLightStylesheet() { |
34 | | - let lightThemeEl = document.querySelector('[data-elfinder-light-theme-url]'); |
35 | | - if (!lightThemeEl) return; |
36 | | - let lightThemeUrl = lightThemeEl.getAttribute('data-elfinder-light-theme-url'); |
37 | | - if (!lightThemeUrl) return; |
| 31 | + const getElfinderStyleLink = (name) => document.querySelector(`[data-stylesheet-name="elfinder-theme-${name}"]`); |
38 | 32 |
|
39 | | - let mainStyleSheet = getElfinderStyleSheet(); |
40 | | - let lightStyleSheet = getElfinderStyleSheet(false); |
41 | | - // if found append the light mode css to the main theme stylesheet |
42 | | - if (mainStyleSheet && ! lightStyleSheet) { |
43 | | - let themeLight = document.createElement('link'); |
44 | | - themeLight.href = lightThemeUrl; |
45 | | - themeLight.rel = 'stylesheet'; |
46 | | - themeLight.type = 'text/css'; |
47 | | - mainStyleSheet.insertAdjacentElement('afterend', themeLight); |
48 | | - } |
| 33 | + const setElfinderMode = (name) => { |
| 34 | + const darkModeStyleLink = getElfinderStyleLink('dark'); |
| 35 | + darkModeStyleLink.rel = name === 'dark' ? 'stylesheet' : 'prefetch'; |
| 36 | + const lightModeStyleLink = getElfinderStyleLink('light'); |
| 37 | + lightModeStyleLink.rel = name === 'light' ? 'stylesheet' : 'prefetch'; |
49 | 38 | } |
50 | 39 |
|
51 | | - let colorMode = window.parent.colorMode?.result ?? window.colorMode?.result ?? false; |
52 | | -
|
53 | | - if(colorMode !== 'dark') { |
54 | | - addElfinderLightStylesheet(); |
55 | | - } |
56 | | -
|
57 | | - // register a color mode change event so that we remove |
58 | | - // the light stylesheet when the color mode change |
59 | | - if(colorMode) { |
60 | | - let colorModeClass = window.parent.colorMode ?? window.colorMode; |
61 | | - colorModeClass.onChange(function(scheme) { |
62 | | - let getMainStylesheet = scheme === 'dark' ? false : true; |
63 | | - let selectedLinkElement = getElfinderStyleSheet(getMainStylesheet); |
64 | | -
|
65 | | - if (! selectedLinkElement) { |
66 | | - return true; |
67 | | - } |
68 | | - // in case we switched to dark mode, remove the ligth theme css |
69 | | - if(scheme === 'dark') { |
70 | | - selectedLinkElement.parentNode.removeChild(selectedLinkElement); |
71 | | - return true; |
72 | | - } |
73 | | - addElfinderLightStylesheet() |
74 | | - }); |
| 40 | + const colorModeClass = window.parent.colorMode ?? window.colorMode ?? null; |
| 41 | + if (colorModeClass) { |
| 42 | + // Set mode based on configuration and watch for changes |
| 43 | + setElfinderMode(colorModeClass.result); |
| 44 | + colorModeClass.onChange((scheme) => setElfinderMode(scheme)); |
| 45 | + } else { |
| 46 | + // Default to light mode |
| 47 | + setElfinderMode('light'); |
75 | 48 | } |
76 | 49 | </script> |
77 | 50 | @endBassetBlock |
|
0 commit comments