You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/app/settings.coffee

164 lines
3.6 KiB

11 years ago
class app.Settings
PREFERENCE_KEYS = [
'hideDisabled'
'hideIntro'
'manualUpdate'
'fastScroll'
'arrowScroll'
'analyticsConsent'
'docs'
'dark' # legacy
'theme'
'layout'
'size'
'tips'
'autoInstall'
'spaceScroll'
'spaceTimeout'
]
INTERNAL_KEYS = [
'count'
'schema'
'version'
'news'
]
10 years ago
LAYOUTS: ['_max-width', '_sidebar-hidden', '_native-scrollbars']
@defaults:
10 years ago
count: 0
hideDisabled: false
hideIntro: false
news: 0
manualUpdate: false
schema: 1
analyticsConsent: false
theme: 'auto'
4 years ago
spaceScroll: 1
spaceTimeout: 0.5
10 years ago
constructor: ->
@store = new CookiesStore
@cache = {}
@autoSupported = window.matchMedia('(prefers-color-scheme)').media != 'not all'
if @autoSupported
@darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)')
@darkModeQuery.addListener => @setTheme(@get('theme'))
get: (key) ->
return @cache[key] if @cache.hasOwnProperty(key)
@cache[key] = @store.get(key) ? @constructor.defaults[key]
if key == 'theme' and @cache[key] == 'auto' and !@darkModeQuery
@cache[key] = 'default'
else
@cache[key]
10 years ago
set: (key, value) ->
@store.set(key, value)
delete @cache[key]
@setTheme(value) if key == 'theme'
return
10 years ago
del: (key) ->
@store.del(key)
delete @cache[key]
return
10 years ago
11 years ago
hasDocs: ->
try !!@store.get('docs')
11 years ago
getDocs: ->
@store.get('docs')?.split('/') or app.config.default_docs
11 years ago
setDocs: (docs) ->
@set 'docs', docs.join('/')
return
getTips: ->
@store.get('tips')?.split('/') or []
setTips: (tips) ->
@set 'tips', tips.join('/')
return
setLayout: (name, enable) ->
@toggleLayout(name, enable)
layout = (@store.get('layout') || '').split(' ')
$.arrayDelete(layout, '')
if enable
layout.push(name) if layout.indexOf(name) is -1
else
$.arrayDelete(layout, name)
if layout.length > 0
@set 'layout', layout.join(' ')
else
@del 'layout'
return
hasLayout: (name) ->
layout = (@store.get('layout') || '').split(' ')
layout.indexOf(name) isnt -1
setSize: (value) ->
@set 'size', value
return
dump: ->
@store.dump()
export: ->
data = @dump()
delete data[key] for key in INTERNAL_KEYS
data
import: (data) ->
for key, value of @export()
@del key unless data.hasOwnProperty(key)
for key, value of data
@set key, value if PREFERENCE_KEYS.indexOf(key) isnt -1
return
11 years ago
reset: ->
@store.reset()
@cache = {}
11 years ago
return
initLayout: ->
if @get('dark') is 1
@set('theme', 'dark')
@del 'dark'
@setTheme(@get('theme'))
@toggleLayout(layout, @hasLayout(layout)) for layout in @LAYOUTS
@initSidebarWidth()
return
setTheme: (theme) ->
if theme is 'auto'
theme = if @darkModeQuery.matches then 'dark' else 'default'
classList = document.documentElement.classList
classList.remove('_theme-default', '_theme-dark')
classList.add('_theme-' + theme)
@updateColorMeta()
return
updateColorMeta: ->
6 years ago
color = getComputedStyle(document.documentElement).getPropertyValue('--headerBackground').trim()
$('meta[name=theme-color]').setAttribute('content', color)
return
toggleLayout: (layout, enable) ->
classList = document.body.classList
classList.toggle(layout, enable) unless app.router?.isSettings
classList.toggle('_overlay-scrollbars', $.overlayScrollbarsEnabled())
return
initSidebarWidth: ->
size = @get('size')
document.documentElement.style.setProperty('--sidebarWidth', size + 'px') if size
return