Files: a5417350593eb41e7c3b56c8edf96aa895411fb2 / main-window.js
4052 bytesRaw
1 | var Modules = require('./modules') |
2 | var h = require('./lib/h') |
3 | var Value = require('@mmckegg/mutant/value') |
4 | var when = require('@mmckegg/mutant/when') |
5 | var computed = require('@mmckegg/mutant/computed') |
6 | var toCollection = require('@mmckegg/mutant/dict-to-collection') |
7 | var MutantDict = require('@mmckegg/mutant/dict') |
8 | var MutantMap = require('@mmckegg/mutant/map') |
9 | var watch = require('@mmckegg/mutant/watch') |
10 | |
11 | var plugs = require('patchbay/plugs') |
12 | |
13 | module.exports = function (config, ssbClient) { |
14 | var modules = Modules(config, ssbClient) |
15 | |
16 | var screenView = plugs.first(modules.plugs.screen_view) |
17 | |
18 | var searchTimer = null |
19 | var searchBox = h('input.search', { |
20 | type: 'search', |
21 | placeholder: 'word, @key, #channel' |
22 | }) |
23 | |
24 | searchBox.oninput = function () { |
25 | clearTimeout(searchTimer) |
26 | searchTimer = setTimeout(doSearch, 500) |
27 | } |
28 | |
29 | searchBox.onfocus = function () { |
30 | if (searchBox.value) { |
31 | doSearch() |
32 | } |
33 | } |
34 | |
35 | var forwardHistory = [] |
36 | var backHistory = [] |
37 | var views = MutantDict({ |
38 | '/public': screenView('/public') |
39 | }) |
40 | |
41 | var canGoForward = Value(false) |
42 | var canGoBack = Value(false) |
43 | var currentView = Value('/public') |
44 | |
45 | watch(currentView, (view) => { |
46 | window.location.hash = `#${view}` |
47 | }) |
48 | |
49 | window.onhashchange = function (ev) { |
50 | var path = window.location.hash.substring(1) |
51 | if (path) { |
52 | setView(path) |
53 | } |
54 | } |
55 | |
56 | var mainElement = h('div.main', MutantMap(toCollection(views), (item) => { |
57 | return h('div.view', { |
58 | hidden: computed([item.key, currentView], (a, b) => a !== b) |
59 | }, [ item.value ]) |
60 | })) |
61 | |
62 | return h('MainWindow', { |
63 | classList: [ '-' + process.platform ] |
64 | }, [ |
65 | h('div.top', [ |
66 | h('span.history', [ |
67 | h('a', { |
68 | 'ev-click': goBack, |
69 | classList: [ when(canGoBack, '-active') ] |
70 | }, '<'), |
71 | h('a', { |
72 | 'ev-click': goForward, |
73 | classList: [ when(canGoForward, '-active') ] |
74 | }, '>') |
75 | ]), |
76 | h('span.nav', [ |
77 | h('a', { |
78 | href: '#/public', |
79 | classList: [ |
80 | when(selected('/public'), '-selected') |
81 | ] |
82 | }, 'Public'), |
83 | h('a', { |
84 | href: '#/private', |
85 | classList: [ |
86 | when(selected('/private'), '-selected') |
87 | ] |
88 | }, 'Private') |
89 | ]), |
90 | h('span.appTitle', ['Patchwork']), |
91 | h('span', [ searchBox ]), |
92 | h('span.nav', [ |
93 | h('a', { |
94 | href: `#${ssbClient.id}`, |
95 | classList: [ |
96 | when(selected(`${ssbClient.id}`), '-selected') |
97 | ] |
98 | }, 'Profile') |
99 | ]) |
100 | ]), |
101 | mainElement |
102 | ]) |
103 | |
104 | // scoped |
105 | |
106 | function goBack () { |
107 | if (backHistory.length) { |
108 | canGoForward.set(true) |
109 | forwardHistory.push(currentView()) |
110 | currentView.set(backHistory.pop()) |
111 | canGoBack.set(backHistory.length > 0) |
112 | } |
113 | } |
114 | |
115 | function goForward () { |
116 | if (forwardHistory.length) { |
117 | backHistory.push(currentView()) |
118 | currentView.set(forwardHistory.pop()) |
119 | canGoForward.set(forwardHistory.length > 0) |
120 | canGoBack.set(true) |
121 | } |
122 | } |
123 | |
124 | function setView (view) { |
125 | if (!views.has(view)) { |
126 | views.put(view, screenView(view)) |
127 | } |
128 | if (view !== currentView()) { |
129 | canGoForward.set(false) |
130 | canGoBack.set(true) |
131 | forwardHistory.length = 0 |
132 | backHistory.push(currentView()) |
133 | currentView.set(view) |
134 | } |
135 | } |
136 | |
137 | function doSearch () { |
138 | var value = searchBox.value.trim() |
139 | if (value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) { |
140 | setView(value) |
141 | } else if (value.trim()) { |
142 | setView(`?${value.trim()}`) |
143 | } else { |
144 | setView('/public') |
145 | } |
146 | } |
147 | |
148 | function selected (view) { |
149 | return computed([currentView, view], (currentView, view) => { |
150 | return currentView === view |
151 | }) |
152 | } |
153 | } |
154 | |
155 | function isSame (a, b) { |
156 | if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) { |
157 | for (var i = 0; i < a.length; i++) { |
158 | if (a[i] !== b[i]) { |
159 | return false |
160 | } |
161 | } |
162 | return true |
163 | } else if (a === b) { |
164 | return true |
165 | } |
166 | } |
167 |
Built with git-ssb-web