forked from mixmix/ferment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-window.js
230 lines (205 loc) · 6.46 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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 watch = require('@mmckegg/mutant/watch')
var MutantMap = require('@mmckegg/mutant/map')
var LatestUpdate = require('./lib/latest-update')
var sanitizeFileName = require('sanitize-filename')
var views = {
discoveryFeed: require('./views/discovery-feed'),
followingFeed: require('./views/following-feed'),
profile: require('./views/profile'),
post: require('./views/audio-post')
}
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 latestUpdate = LatestUpdate()
window.latestUpdate = latestUpdate
var actions = {
openEditProfileWindow,
openJoinPubWindow,
editPost,
saveFile (item, cb) {
electron.remote.dialog.showSaveDialog(electron.remote.getCurrentWindow(), {
title: 'Export Audio File',
defaultPath: sanitizeFileName(`${item.author.displayName()} - ${item.title()}.mp3`),
filters: [
{name: 'MP3', extensions: ['mp3']}
]
}, function (path) {
if (path) {
background.exportFile(item.audioSrc(), path, cb)
} else {
cb && cb(false)
}
})
},
repost (item) {
},
viewProfile (id) {
actions.setView('profile', id)
},
viewPost (id) {
actions.setView('post', 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(), mainElement.scrollTop])
currentView.set(newView)
mainElement.scrollTop = 0
}
}
}
background.stats(x => console.log(x))
var profile = api.getOwnProfile()
var discoveryFeed = api.getDiscoveryFeed()
var suggestedProfiles = api.getSuggestedProfiles(15)
var following = MutantMap(api.rankProfileIds(profile.following), id => api.getProfile(id))
var followingFeed = api.getFollowingFeed()
// hold these open
watch(suggestedProfiles)
watch(discoveryFeed)
watch(followingFeed)
var context = { config, api, background, actions, discoveryFeed, followingFeed, suggestedProfiles, following, profile }
var player = context.player = Player(context)
var rootElement = computed(currentView, (data) => {
if (Array.isArray(data) && views[data[0]]) {
return views[data[0]](context, ...data.slice(1))
}
})
var mainElement = h('div.main', [
rootElement
])
onceTrue(api.profilesLoaded, (value) => {
// make sure we're connected to the pub that invited us
if (!profile.displayName()) {
// prompt use to set up profile the first time they open app
openEditProfileWindow()
}
})
electron.ipcRenderer.on('playPause', function () {
player.togglePlay()
})
electron.ipcRenderer.on('nextTrack', function () {
player.playNext()
})
electron.ipcRenderer.on('previousTrack', function () {
player.playPrevious()
})
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', ['LolaShare']),
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'])
])
]),
when(latestUpdate,
h('div.info', [
h('a.message -update', { href: 'https://github.com/LolaShare/LolaShare/releases' }, [
h('strong', ['🎉 LolaShare ', latestUpdate, ' has been released.']), ' Click here for more info!'
])
])
),
mainElement,
h('div.bottom', [
player.audioElement
])
])
// scoped
function goBack () {
if (backHistory.length) {
canGoForward.set(true)
forwardHistory.push([currentView(), mainElement.scrollTop])
var item = backHistory.pop()
currentView.set(item[0])
setTimeout(() => {
mainElement.scrollTop = item[1]
}, 50)
canGoBack.set(backHistory.length > 0)
}
}
function goForward () {
if (forwardHistory.length) {
backHistory.push([currentView(), mainElement.scrollTop])
var item = forwardHistory.pop()
currentView.set(item[0])
setTimeout(() => {
mainElement.scrollTop = item[1]
}, 50)
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 editPost (opts) {
if (opts.id && opts.item.type === 'ferment/audio') {
electron.ipcRenderer.send('open-add-window', opts)
}
}
function openAddWindow (opts) {
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
}
}