git ssb

16+

Dominic / patchbay



Tree: a02496d0475717090976150d52e656f9915bcf7f

Files: a02496d0475717090976150d52e656f9915bcf7f / modules / theme.js

2963 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var plugs = require('../plugs')
4var cat = require('pull-cat')
5
6var sbot_links2 = plugs.first(exports.sbot_links2 = [])
7var avatar_name = plugs.first(exports.avatar_name = [])
8var blob_url = require('../plugs').first(exports.blob_url = [])
9
10var link = document.head.appendChild(h('link', {rel: 'stylesheet'}))
11var activeTheme
12
13function useTheme(id) {
14 activeTheme = id
15 link.href = id ? blob_url(id) : ''
16 var forms = [].slice.call(document.querySelectorAll('.themes__form'))
17 forms.forEach(updateForm)
18
19 var radios = [].slice.call(document.querySelectorAll('input[type=radio]'))
20 radios.forEach(function (radio) {
21 radio.checked = (radio.value === activeTheme)
22 })
23}
24
25setImmediate(function () {
26 useTheme(localStorage.themeId || '')
27})
28
29function themes() {
30 return cat([
31 pull.values([
32 {
33 id: '',
34 name: 'none',
35 feed: ''
36 }
37 ]),
38 pull(
39 sbot_links2({
40 query: [
41 {$filter: {rel: ['mentions', {$prefix: 'patchbay-'}, {$gt: null}]}},
42 {$filter: {dest: {$prefix: '&'}}},
43 {$map: {id: 'dest', feed: 'source', name: ['rel', 1]}}
44 ],
45 live: true,
46 sync: false,
47 }),
48 pull.filter(function (link) {
49 return /\.css$/.test(link.name)
50 })
51 )
52 ])
53}
54
55function onRadioClick(e) {
56 if (this.checked) useTheme(this.value)
57}
58
59function updateForm(form) {
60 var same = localStorage.themeId === activeTheme
61 form.querySelector('.themes__id').value = activeTheme
62 form.querySelector('.themes__reset').disabled = same
63 form.querySelector('.themes__submit').disabled = same
64 return form
65}
66
67function renderTheme(link) {
68 return h('div.theme',
69 h('input', {type: 'radio', name: 'theme',
70 value: link.id, onclick: onRadioClick,
71 checked: link.id === activeTheme
72 }),
73 link.id ? h('a', {href: '#'+link.id}, link.name) : link.name, ' ',
74 link.feed ? h('a', {href: '#'+link.feed}, avatar_name(link.feed)) : ''
75 )
76}
77
78function theme_view() {
79 var themeInput
80 var themesList = h('form.themes__list')
81
82 pull(
83 themes(),
84 pull.unique('id'),
85 pull.map(renderTheme),
86 pull.drain(function (el) {
87 themesList.appendChild(el)
88 }, function (err) {
89 if (err) console.error(err)
90 })
91 )
92
93 return h('div.column.scroll-y', h('div',
94 updateForm(h('form.themes__form', {onsubmit: onsubmit, onreset: onreset},
95 themeInput = h('input.themes__id', {placeholder: 'theme id',
96 value: link.href}), ' ',
97 h('input.themes__reset', {type: 'reset'}), ' ',
98 h('input.themes__submit', {type: 'submit', value: 'Save'}))),
99 themesList
100 ))
101
102 function onsubmit(e) {
103 e.preventDefault()
104 useTheme(localStorage.themeId = themeInput.value)
105 }
106
107 function onreset(e) {
108 e.preventDefault()
109 useTheme(localStorage.themeId || '')
110 }
111}
112
113exports.screen_view = function (path) {
114 if(path === '/theme') return theme_view()
115}
116

Built with git-ssb-web