Files: 84d01396e7c8c55181da6c71061acdb78090dc4c / modules / theme.js
3132 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 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( |
100 | themes(), |
101 | pull.unique('id'), // TODO: update existing items with new data |
102 | pull.map(renderTheme) |
103 | )) |
104 | )) |
105 | |
106 | function onsubmit(e) { |
107 | e.preventDefault() |
108 | useTheme(localStorage.themeId = themeInput.value) |
109 | } |
110 | |
111 | function onreset(e) { |
112 | e.preventDefault() |
113 | useTheme(localStorage.themeId || '') |
114 | } |
115 | } |
116 | |
117 | exports.screen_view = function (path) { |
118 | if(path === '/theme') return theme_view() |
119 | } |
120 |
Built with git-ssb-web