Files: 506b936a9c62f45d8e99106da18ee6c393ee5b59 / modules / theme.js
3781 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var plugs = require('../plugs') |
4 | var cat = require('pull-cat') |
5 | |
6 | var sbot_links2 = plugs.first(exports.sbot_links2 = []) |
7 | var avatar_name = plugs.first(exports.avatar_name = []) |
8 | var blob_url = require('../plugs').first(exports.blob_url = []) |
9 | |
10 | var defaultTheme = { |
11 | id: '&JFa42U6HtPm9k+s+AmpDIAoTJJI/PzoRC/J/WCfduDY=.sha256', |
12 | name: 'patchbay-minimal.css' |
13 | } |
14 | |
15 | var link = document.head.appendChild(h('link', {rel: 'stylesheet'})) |
16 | var activeTheme |
17 | |
18 | function useTheme(id) { |
19 | activeTheme = id |
20 | link.href = id ? blob_url(id) : '' |
21 | var forms = [].slice.call(document.querySelectorAll('.themes__form')) |
22 | forms.forEach(updateForm) |
23 | |
24 | var radios = [].slice.call(document.querySelectorAll('input[type=radio]')) |
25 | radios.forEach(function (radio) { |
26 | radio.checked = (radio.value === activeTheme) |
27 | }) |
28 | } |
29 | |
30 | function useSavedTheme() { |
31 | useTheme(localStorage.themeId || defaultTheme.id) |
32 | } |
33 | |
34 | setImmediate(useSavedTheme) |
35 | |
36 | function themes() { |
37 | return cat([ |
38 | pull.values([ |
39 | { |
40 | id: '', |
41 | name: 'none', |
42 | feed: '' |
43 | }, |
44 | defaultTheme, |
45 | ]), |
46 | pull( |
47 | sbot_links2({ |
48 | query: [ |
49 | {$filter: {rel: ['mentions', {$prefix: 'patchbay-'}, {$gt: null}]}}, |
50 | {$filter: {dest: {$prefix: '&'}}}, |
51 | {$map: {id: 'dest', feed: 'source', name: ['rel', 1]}} |
52 | ], |
53 | live: true, |
54 | sync: false, |
55 | }), |
56 | pull.filter(function (link) { |
57 | return /\.css$/.test(link.name) |
58 | }) |
59 | ) |
60 | ]) |
61 | } |
62 | |
63 | function onRadioClick(e) { |
64 | if (this.checked) useTheme(this.value) |
65 | } |
66 | |
67 | function updateForm(form) { |
68 | var same = localStorage.themeId === activeTheme |
69 | form.querySelector('.themes__id').value = activeTheme |
70 | form.querySelector('.themes__reset').disabled = same |
71 | form.querySelector('.themes__submit').disabled = same |
72 | return form |
73 | } |
74 | |
75 | function renderTheme(link) { |
76 | return h('div.theme', |
77 | h('input', {type: 'radio', name: 'theme', |
78 | value: link.id, onclick: onRadioClick, |
79 | checked: link.id === activeTheme |
80 | }), |
81 | link.id ? h('a', {href: '#'+link.id}, link.name) : link.name, ' ', |
82 | link.feed ? h('a', {href: '#'+link.feed}, avatar_name(link.feed)) : '' |
83 | ) |
84 | } |
85 | |
86 | function theme_view() { |
87 | var themeInput |
88 | var themesList = h('form.themes__list') |
89 | var themesByKey = {} |
90 | |
91 | pull( |
92 | themes(), |
93 | pull.unique('id'), |
94 | pull.drain(function (theme) { |
95 | // replace old versions of themes in the list |
96 | var key = theme.feed + theme.name |
97 | var oldTheme = themesByKey[key] |
98 | theme.el = renderTheme(theme) |
99 | themesByKey[key] = theme |
100 | if (!oldTheme) { |
101 | themesList.appendChild(theme.el) |
102 | } else if (oldTheme.id === localStorage.themeId |
103 | || oldTheme.id === activeTheme) { |
104 | // show old version because the user is still using it |
105 | oldTheme.el.appendChild(document.createTextNode(' (old)')) |
106 | themesList.appendChild(theme.el) |
107 | } else { |
108 | themesList.replaceChild(theme.el, oldTheme.el) |
109 | } |
110 | }, function (err) { |
111 | if (err) console.error(err) |
112 | }) |
113 | ) |
114 | |
115 | return h('div.column.scroll-y', h('div', |
116 | updateForm(h('form.themes__form', {onsubmit: onsubmit, onreset: onreset}, |
117 | themeInput = h('input.themes__id', {placeholder: 'theme id', |
118 | value: link.href}), ' ', |
119 | h('input.themes__reset', {type: 'reset'}), ' ', |
120 | h('input.themes__submit', {type: 'submit', value: 'Save'}))), |
121 | themesList |
122 | )) |
123 | |
124 | function onsubmit(e) { |
125 | e.preventDefault() |
126 | useTheme(localStorage.themeId = themeInput.value) |
127 | } |
128 | |
129 | function onreset(e) { |
130 | e.preventDefault() |
131 | useSavedTheme() |
132 | } |
133 | } |
134 | |
135 | exports.menu_items = function () { |
136 | return h('a', {href:'#/theme'}, '/theme') |
137 | } |
138 | |
139 | exports.screen_view = function (path) { |
140 | if(path === '/theme') return theme_view() |
141 | } |
142 |
Built with git-ssb-web