Commit 811284f39dbc61e5447ee29fd4fa26b02f455624
Add theme view
Charles Lehner committed on 8/12/2016, 7:48:32 AMParent: 2730a445b6f16877bff840d9f38ee9e6892edd9f
Files changed
modules/theme.js | added |
style.css | changed |
modules/theme.js | ||
---|---|---|
@@ -1,0 +1,115 @@ | ||
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 link = document.head.appendChild(h('link', {rel: 'stylesheet'})) | |
11 | +var activeTheme | |
12 | + | |
13 | +function 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 | + | |
25 | +setImmediate(function () { | |
26 | + useTheme(localStorage.themeId || '') | |
27 | +}) | |
28 | + | |
29 | +function 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 | + | |
55 | +function onRadioClick(e) { | |
56 | + if (this.checked) useTheme(this.value) | |
57 | +} | |
58 | + | |
59 | +function 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 | + | |
67 | +function 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 | + | |
78 | +function hPull() { | |
79 | + var args = [].slice.call(arguments) | |
80 | + var stream = args.pop() | |
81 | + var parent = h.apply(this, args) | |
82 | + pull(stream, pull.drain(function (el) { | |
83 | + parent.appendChild(el) | |
84 | + }, function (err) { | |
85 | + if (err) console.error(err) | |
86 | + })) | |
87 | + return parent | |
88 | +} | |
89 | + | |
90 | +function theme_view() { | |
91 | + var themeInput | |
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 | + hPull('form.themes__list', pull(themes(), pull.map(renderTheme))) | |
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 | + | |
113 | +exports.screen_view = function (path) { | |
114 | + if(path === '/theme') return theme_view() | |
115 | +} |
Built with git-ssb-web