git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: f8a3919796796fb604a4d98decee02f962ee7cee

Files: f8a3919796796fb604a4d98decee02f962ee7cee / main-window.js

4246 bytesRaw
1var Modules = require('./modules')
2var h = require('./lib/h')
3var Value = require('@mmckegg/mutant/value')
4var when = require('@mmckegg/mutant/when')
5var computed = require('@mmckegg/mutant/computed')
6var toCollection = require('@mmckegg/mutant/dict-to-collection')
7var MutantDict = require('@mmckegg/mutant/dict')
8var MutantMap = require('@mmckegg/mutant/map')
9var watch = require('@mmckegg/mutant/watch')
10
11var plugs = require('patchbay/plugs')
12
13module.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 h('a', {
100 href: `#/notifications`,
101 classList: [
102 when(selected(`/notifications`), '-selected')
103 ]
104 }, 'Mentions')
105 ])
106 ]),
107 mainElement
108 ])
109
110 // scoped
111
112 function goBack () {
113 if (backHistory.length) {
114 canGoForward.set(true)
115 forwardHistory.push(currentView())
116 currentView.set(backHistory.pop())
117 canGoBack.set(backHistory.length > 0)
118 }
119 }
120
121 function goForward () {
122 if (forwardHistory.length) {
123 backHistory.push(currentView())
124 currentView.set(forwardHistory.pop())
125 canGoForward.set(forwardHistory.length > 0)
126 canGoBack.set(true)
127 }
128 }
129
130 function setView (view) {
131 if (!views.has(view)) {
132 views.put(view, screenView(view))
133 }
134 if (view !== currentView()) {
135 canGoForward.set(false)
136 canGoBack.set(true)
137 forwardHistory.length = 0
138 backHistory.push(currentView())
139 currentView.set(view)
140 }
141 }
142
143 function doSearch () {
144 var value = searchBox.value.trim()
145 if (value.startsWith('/') || value.startsWith('?') || value.startsWith('@') || value.startsWith('#') || value.startsWith('%')) {
146 setView(value)
147 } else if (value.trim()) {
148 setView(`?${value.trim()}`)
149 } else {
150 setView('/public')
151 }
152 }
153
154 function selected (view) {
155 return computed([currentView, view], (currentView, view) => {
156 return currentView === view
157 })
158 }
159}
160
161function isSame (a, b) {
162 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
163 for (var i = 0; i < a.length; i++) {
164 if (a[i] !== b[i]) {
165 return false
166 }
167 }
168 return true
169 } else if (a === b) {
170 return true
171 }
172}
173

Built with git-ssb-web