-
Notifications
You must be signed in to change notification settings - Fork 80
/
main-window.js
151 lines (136 loc) · 4.09 KB
/
main-window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var h = require('./lib/h')
var Value = require('@mmckegg/mutant/value')
var computed = require('@mmckegg/mutant/computed')
var when = require('@mmckegg/mutant/when')
var send = require('@mmckegg/mutant/send')
var electron = require('electron')
var Player = require('./widgets/player')
var onceTrue = require('./lib/once-true')
var views = {
discoveryFeed: require('./views/discovery-feed'),
followingFeed: require('./views/following-feed'),
profile: require('./views/profile')
}
module.exports = function (client, config) {
var api = require('./api')(client, config)
var background = require('./models/background-remote')(config)
var currentView = Value(['discoveryFeed'])
var backHistory = []
var forwardHistory = []
var canGoForward = Value(false)
var canGoBack = Value(false)
var actions = {
openEditProfileWindow,
openJoinPubWindow,
viewProfile (id) {
actions.setView('profile', id)
},
setView: function (view, ...args) {
var newView = [view, ...args]
if (!isSame(newView, currentView())) {
canGoForward.set(false)
canGoBack.set(true)
forwardHistory.length = 0
backHistory.push(currentView())
currentView.set(newView)
}
}
}
var context = { config, api, background, actions }
var player = context.player = Player(context)
var profile = api.getOwnProfile()
onceTrue(api.profilesLoaded, (value) => {
if (!profile.displayName()) {
// prompt use to set up profile the first time they open app
openEditProfileWindow()
}
})
var rootElement = computed(currentView, (data) => {
if (Array.isArray(data) && views[data[0]]) {
return views[data[0]](context, ...data.slice(1))
}
})
return h('MainWindow', {
classList: [ '-' + process.platform ]
}, [
h('div.top', [
h('span.history', [
h('a', {
'ev-click': goBack,
classList: [ when(canGoBack, '-active') ]
}, '<'),
h('a', {
'ev-click': goForward,
classList: [ when(canGoForward, '-active') ]
}, '>')
]),
h('span.nav', [
h('a', {
'ev-click': send(actions.setView, 'discoveryFeed'),
classList: [ computed(currentView, (x) => x[0] === 'discoveryFeed' ? '-selected' : null) ]
}, 'Discovery'),
h('a', {
'ev-click': send(actions.setView, 'followingFeed'),
classList: [ computed(currentView, (x) => x[0] === 'followingFeed' ? '-selected' : null) ]
}, 'Following')
]),
h('span.appTitle', ['Ferment']),
h('span', [
h('a', {
'ev-click': send(actions.viewProfile, api.id),
title: 'Your Profile',
classList: [ computed(currentView, (x) => x[0] === 'profile' && x[1] === api.id ? '-selected' : null) ]
}, '😀'),
h('a -profile', {href: '#', 'ev-click': openEditProfileWindow}, ['Edit Profile']),
h('a -add', {href: '#', 'ev-click': openAddWindow}, ['+ Add Audio'])
])
]),
h('div.main', [
rootElement
]),
h('div.bottom', [
player.audioElement
])
])
// scoped
function goBack () {
if (backHistory.length) {
canGoForward.set(true)
forwardHistory.push(currentView())
currentView.set(backHistory.pop())
canGoBack.set(backHistory.length > 0)
}
}
function goForward () {
if (forwardHistory.length) {
backHistory.push(currentView())
currentView.set(forwardHistory.pop())
canGoForward.set(forwardHistory.length > 0)
canGoBack.set(true)
}
}
function openEditProfileWindow () {
electron.ipcRenderer.send('open-edit-profile-window', {
profile: profile.byMe(),
id: api.id
})
}
function openJoinPubWindow () {
electron.ipcRenderer.send('open-join-pub-window')
}
}
function openAddWindow () {
electron.ipcRenderer.send('open-add-window')
}
function isSame (a, b) {
if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) {
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false
}
}
return true
} else if (a === b) {
return true
}
}