navbar-darkmode-widget.blade.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. {{-- Navbar darkmode widget --}}
  2. <li class="nav-item adminlte-darkmode-widget">
  3. <a class="nav-link" href="#" role="button">
  4. <i class="{{ $makeIconClass() }}"></i>
  5. </a>
  6. </li>
  7. {{-- Add Javascript listener for the click event --}}
  8. @once
  9. @push('js')
  10. <script>
  11. $(() => {
  12. const body = document.querySelector('body');
  13. const widget = document.querySelector('li.adminlte-darkmode-widget');
  14. const widgetIcon = widget.querySelector('i');
  15. // Get the set of classes to be toggled on the widget icon.
  16. const iconClasses = [
  17. ...@json($makeIconEnabledClass()),
  18. ...@json($makeIconDisabledClass())
  19. ];
  20. // Add 'click' event listener for the darkmode widget.
  21. widget.addEventListener('click', () => {
  22. // Toggle dark-mode class on the main body tag.
  23. body.classList.toggle('dark-mode');
  24. // Support to IFrame mode: toggle dark-mode class on the body of
  25. // any present iframe.
  26. let iframes = document.querySelectorAll('div.iframe-mode iframe');
  27. iframes.forEach((f) => {
  28. b = f.contentDocument.querySelector('body');
  29. b.classList.toggle('dark-mode');
  30. });
  31. // Toggle the classes on the widget icon.
  32. iconClasses.forEach((c) => widgetIcon.classList.toggle(c));
  33. // Notify the server. The server will be in charge to persist
  34. // the dark mode configuration over multiple request.
  35. const fetchCfg = {
  36. headers: {'X-CSRF-TOKEN': '{{ csrf_token() }}'},
  37. method: 'POST',
  38. };
  39. fetch(
  40. "{{ route('adminlte.darkmode.toggle') }}",
  41. fetchCfg
  42. )
  43. .catch((error) => {
  44. console.log(
  45. 'Failed to notify server that dark mode was toggled',
  46. error
  47. );
  48. });
  49. });
  50. })
  51. </script>
  52. @endpush
  53. @endonce