index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Setting items
  2. const items = {
  3. "menu-position": { localStorage: "tablerMenuPosition", default: "top" },
  4. "menu-behavior": { localStorage: "tablerMenuBehavior", default: "sticky" },
  5. "container-layout": {
  6. localStorage: "tablerContainerLayout",
  7. default: "boxed",
  8. },
  9. }
  10. // Theme config
  11. const config = {}
  12. for (const [key, params] of Object.entries(items)) {
  13. const lsParams = localStorage.getItem(params.localStorage)
  14. config[key] = lsParams ? lsParams : params.default
  15. }
  16. // Parse url params
  17. const parseUrl = () => {
  18. const search = window.location.search.substring(1)
  19. const params = search.split("&")
  20. for (let i = 0; i < params.length; i++) {
  21. const arr = params[i].split("=")
  22. const key = arr[0]
  23. const value = arr[1]
  24. if (!!items[key]) {
  25. // Save to localStorage
  26. localStorage.setItem(items[key].localStorage, value)
  27. // Update local variables
  28. config[key] = value
  29. }
  30. }
  31. }
  32. // Toggle form controls
  33. const toggleFormControls = (form) => {
  34. for (const [key, params] of Object.entries(items)) {
  35. const elem = form.querySelector(
  36. `[name="settings-${key}"][value="${config[key]}"]`,
  37. )
  38. if (elem) {
  39. elem.checked = true
  40. }
  41. }
  42. }
  43. // Submit form
  44. const submitForm = (form) => {
  45. // Save data to localStorage
  46. for (const [key, params] of Object.entries(items)) {
  47. // Save to localStorage
  48. const value = form.querySelector(`[name="settings-${key}"]:checked`).value
  49. localStorage.setItem(params.localStorage, value)
  50. // Update local variables
  51. config[key] = value
  52. }
  53. window.dispatchEvent(new Event("resize"))
  54. new bootstrap.Offcanvas(form).hide()
  55. }
  56. // Parse url
  57. parseUrl()
  58. // Elements
  59. const form = document.querySelector("#offcanvasSettings")
  60. // Toggle form controls
  61. if (form) {
  62. form.addEventListener("submit", function (e) {
  63. e.preventDefault()
  64. submitForm(form)
  65. })
  66. toggleFormControls(form)
  67. }