Files: a2b4868abcd60a593d913223d3af542f4c8e95b2 / modules / theme.js
3121 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 | |
90 | pull( |
91 | themes(), |
92 | pull.unique('id'), |
93 | pull.map(renderTheme), |
94 | pull.drain(function (el) { |
95 | themesList.appendChild(el) |
96 | }, function (err) { |
97 | if (err) console.error(err) |
98 | }) |
99 | ) |
100 | |
101 | return h('div.column.scroll-y', h('div', |
102 | updateForm(h('form.themes__form', {onsubmit: onsubmit, onreset: onreset}, |
103 | themeInput = h('input.themes__id', {placeholder: 'theme id', |
104 | value: link.href}), ' ', |
105 | h('input.themes__reset', {type: 'reset'}), ' ', |
106 | h('input.themes__submit', {type: 'submit', value: 'Save'}))), |
107 | themesList |
108 | )) |
109 | |
110 | function onsubmit(e) { |
111 | e.preventDefault() |
112 | useTheme(localStorage.themeId = themeInput.value) |
113 | } |
114 | |
115 | function onreset(e) { |
116 | e.preventDefault() |
117 | useSavedTheme() |
118 | } |
119 | } |
120 | |
121 | exports.screen_view = function (path) { |
122 | if(path === '/theme') return theme_view() |
123 | } |
124 |
Built with git-ssb-web