git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: 3dfc64e44061b79b9d06b8b067d711ed62c28896

Files: 3dfc64e44061b79b9d06b8b067d711ed62c28896 / main-window.js

3671 bytesRaw
1var combine = require('depject')
2var Modules = require('patchbay/modules')
3var SbotApi = require('./api')
4var extend = require('xtend')
5var h = require('./lib/h')
6var plugs = require('patchbay/plugs')
7var Value = require('@mmckegg/mutant/value')
8var when = require('@mmckegg/mutant/when')
9var computed = require('@mmckegg/mutant/computed')
10
11module.exports = function (ssbClient, config) {
12 var api = SbotApi(ssbClient, config)
13 var modules = combine(extend(Modules, {
14 'sbot-api.js': api,
15 'public.js': require('./views/public-feed.js'),
16 'blob-url.js': {
17 blob_url: function (link) {
18 var prefix = config.blobsPrefix != null ? config.blobsPrefix : `http://localhost:${config.blobsPort}`
19 if (typeof link.link === 'string') {
20 link = link.link
21 }
22 return `${prefix}/${encodeURIComponent(link)}`
23 }
24 }
25 }))
26
27 var screenView = plugs.first(modules.plugs.screen_view)
28 var forwardHistory = []
29 var backHistory = []
30 var views = {
31 '/public': screenView('/public')
32 }
33 var canGoForward = Value(false)
34 var canGoBack = Value(false)
35 var currentView = Value(['/public'])
36 var rootElement = computed(currentView, (data) => {
37 if (Array.isArray(data)) {
38 return views[data[0]]
39 }
40 })
41
42 window.onhashchange = function (ev) {
43 var path = window.location.hash.substring(1)
44 if (path) {
45 setView(path)
46 }
47 }
48
49 var mainElement = h('div.main', [
50 rootElement
51 ])
52
53 return h('MainWindow', {
54 classList: [ '-' + process.platform ]
55 }, [
56 h('div.top', [
57 h('span.history', [
58 h('a', {
59 'ev-click': goBack,
60 classList: [ when(canGoBack, '-active') ]
61 }, '<'),
62 h('a', {
63 'ev-click': goForward,
64 classList: [ when(canGoForward, '-active') ]
65 }, '>')
66 ]),
67 h('span.nav', [
68 h('a', {
69 href: '#/public',
70 classList: [
71 when(selected('/public'), '-selected')
72 ]
73 }, 'Feed'),
74 h('a', {
75 href: '#/private',
76 classList: [
77 when(selected('/private'), '-selected')
78 ]
79 }, 'Private')
80 ]),
81 h('span.appTitle', ['Patchwork']),
82 h('span.nav', [
83 h('a', {
84 href: `#${ssbClient.id}`,
85 classList: [
86 when(selected(`${ssbClient.id}`), '-selected')
87 ]
88 }, 'Profile')
89 ])
90 ]),
91 mainElement
92 ])
93
94 // scoped
95
96 function goBack () {
97 if (backHistory.length) {
98 canGoForward.set(true)
99 forwardHistory.push(currentView())
100 currentView.set(backHistory.pop())
101 canGoBack.set(backHistory.length > 0)
102 }
103 }
104
105 function goForward () {
106 if (forwardHistory.length) {
107 backHistory.push(currentView())
108 currentView.set(forwardHistory.pop())
109 canGoForward.set(forwardHistory.length > 0)
110 canGoBack.set(true)
111 }
112 }
113
114 function setView (view, ...args) {
115 var newView = [view, ...args]
116 views[view] = screenView(view, ...args)
117 if (!isSame(newView, currentView())) {
118 canGoForward.set(false)
119 canGoBack.set(true)
120 forwardHistory.length = 0
121 backHistory.push(currentView())
122 }
123 currentView.set(newView)
124 currentView().scrollTop = 0
125 }
126
127 function selected (view) {
128 return computed([currentView, view], (currentView, view) => {
129 return currentView && currentView[0] === view
130 })
131 }
132}
133
134function isSame (a, b) {
135 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
136 for (var i = 0; i < a.length; i++) {
137 if (a[i] !== b[i]) {
138 return false
139 }
140 }
141 return true
142 } else if (a === b) {
143 return true
144 }
145}
146

Built with git-ssb-web