settings.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var Settings = () => {
  2. var defaults = {
  3. icon: false,
  4. theme: 'light',
  5. _icons: ['default', 'light', 'dark'],
  6. _themes: ['light', 'dark', 'auto'],
  7. }
  8. var state = Object.assign({}, defaults)
  9. chrome.runtime.sendMessage({message: 'options.settings'}, (res) => {
  10. Object.assign(state, res)
  11. document.querySelector('body').classList.add(state.theme)
  12. m.redraw()
  13. })
  14. var events = {
  15. icon: (e) => {
  16. state.icon = state._icons[e.target.selectedIndex]
  17. chrome.runtime.sendMessage({
  18. message: 'options.icon',
  19. settings: {
  20. icon: state.icon,
  21. theme: state.theme,
  22. },
  23. })
  24. },
  25. theme: (e) => {
  26. state.theme = state._themes[e.target.selectedIndex]
  27. chrome.runtime.sendMessage({
  28. message: 'options.theme',
  29. settings: {
  30. icon: state.icon,
  31. theme: state.theme,
  32. },
  33. })
  34. document.querySelector('body').classList.remove(...state._themes)
  35. document.querySelector('body').classList.add(state.theme)
  36. }
  37. }
  38. var oncreate = {}
  39. var onupdate = {}
  40. var render = () =>
  41. m('.m-settings hidden',
  42. m('.row',
  43. m('h3', 'Settings')
  44. ),
  45. // icon
  46. m('.bs-callout',
  47. m('.row',
  48. m('.col-sm-12',
  49. m('.overflow',
  50. m('label.mdc-switch',
  51. m('select.mdc-elevation--z2 m-select', {
  52. onchange: events.icon
  53. },
  54. state._icons.map((icon) =>
  55. m('option', {selected: state.icon === icon}, icon)
  56. )
  57. ),
  58. m('span.mdc-switch-label',
  59. 'Extension Icon and Content Favicon Color'
  60. )
  61. )
  62. )
  63. )
  64. )
  65. ),
  66. // theme
  67. m('.bs-callout',
  68. m('.row',
  69. m('.col-sm-12',
  70. m('.overflow',
  71. m('label.mdc-switch',
  72. m('select.mdc-elevation--z2 m-select', {
  73. onchange: events.theme
  74. },
  75. state._themes.map((theme) =>
  76. m('option', {selected: state.theme === theme}, theme)
  77. )
  78. ),
  79. m('span.mdc-switch-label',
  80. 'Popup and Options Page Color Mode'
  81. )
  82. )
  83. )
  84. )
  85. )
  86. )
  87. )
  88. return {state, render}
  89. }