git ssb

10+

Matt McKegg / patchwork



Tree: aeb64ef804d9d89d3664419052ca631bf516c9ec

Files: aeb64ef804d9d89d3664419052ca631bf516c9ec / main-window.js

2920 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 'blob-url.js': {
16 blob_url: function (link) {
17 var prefix = config.blobsPrefix != null ? config.blobsPrefix : `http://localhost:${config.blobsPort}`
18 if (typeof link.link === 'string') {
19 link = link.link
20 }
21 return `${prefix}/${encodeURIComponent(link)}`
22 }
23 }
24 }))
25
26 var screenView = plugs.first(modules.plugs.screen_view)
27 var forwardHistory = []
28 var backHistory = []
29 var views = {
30 '/public': screenView('/public')
31 }
32 var canGoForward = Value(false)
33 var canGoBack = Value(false)
34 var currentView = Value(['/public'])
35 var rootElement = computed(currentView, (data) => {
36 if (Array.isArray(data)) {
37 return views[data[0]]
38 }
39 })
40
41 window.onhashchange = function (ev) {
42 setView(window.location.hash.substring(1))
43 }
44
45 var mainElement = h('div.main', [
46 rootElement
47 ])
48
49 return h('MainWindow', {
50 classList: [ '-' + process.platform ]
51 }, [
52 h('div.top', [
53 h('span.history', [
54 h('a', {
55 'ev-click': goBack,
56 classList: [ when(canGoBack, '-active') ]
57 }, '<'),
58 h('a', {
59 'ev-click': goForward,
60 classList: [ when(canGoForward, '-active') ]
61 }, '>')
62 ]),
63 h('span.appTitle', ['Patchwork'])
64 ]),
65 mainElement
66 ])
67
68 // scoped
69
70 function goBack () {
71 if (backHistory.length) {
72 canGoForward.set(true)
73 forwardHistory.push(currentView())
74 currentView.set(backHistory.pop())
75 canGoBack.set(backHistory.length > 0)
76 }
77 }
78
79 function goForward () {
80 if (forwardHistory.length) {
81 backHistory.push(currentView())
82 currentView.set(forwardHistory.pop())
83 canGoForward.set(forwardHistory.length > 0)
84 canGoBack.set(true)
85 }
86 }
87
88 function setView (view, ...args) {
89 var newView = [view, ...args]
90 if (!isSame(newView, currentView())) {
91 if (!views[view]) {
92 views[view] = screenView(view, ...args)
93 }
94
95 canGoForward.set(false)
96 canGoBack.set(true)
97 forwardHistory.length = 0
98 backHistory.push(currentView())
99 currentView.set(newView)
100 currentView().scrollTop = 0
101 }
102 }
103}
104
105function isSame (a, b) {
106 if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
107 for (var i = 0; i < a.length; i++) {
108 if (a[i] !== b[i]) {
109 return false
110 }
111 }
112 return true
113 } else if (a === b) {
114 return true
115 }
116}
117

Built with git-ssb-web