forked from captbaritone/webamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reducers.js
401 lines (384 loc) · 9.63 KB
/
reducers.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { combineReducers } from "redux";
import { BANDS, WINDOWS } from "./constants";
import {
CLOSE_WINAMP,
SET_BALANCE,
SET_BAND_VALUE,
SET_FOCUS,
SET_BAND_FOCUS,
SET_FOCUSED_WINDOW,
SET_MEDIA,
SET_SCRUB_POSITION,
SET_SKIN_DATA,
SET_VOLUME,
START_LOADING,
START_WORKING,
STEP_MARQUEE,
STOP_WORKING,
TOGGLE_DOUBLESIZE_MODE,
TOGGLE_EQUALIZER_WINDOW,
CLOSE_EQUALIZER_WINDOW,
TOGGLE_PLAYLIST_WINDOW,
SET_EQ_AUTO,
SET_EQ_ON,
SET_EQ_OFF,
TOGGLE_LLAMA_MODE,
TOGGLE_REPEAT,
TOGGLE_SHADE_MODE,
TOGGLE_EQUALIZER_SHADE_MODE,
TOGGLE_PLAYLIST_SHADE_MODE,
TOGGLE_SHUFFLE,
TOGGLE_TIME_MODE,
TOGGLE_VISUALIZER_STYLE,
UNSET_FOCUS,
UPDATE_TIME_ELAPSED,
SET_USER_MESSAGE,
UNSET_USER_MESSAGE,
SET_PLAYLIST_SCROLL_POSITION,
CLICKED_TRACK,
CTRL_CLICKED_TRACK,
SELECT_ALL,
SELECT_ZERO,
INVERT_SELECTION,
PLAYLIST_SIZE_CHANGED,
REMOVE_ALL_TRACKS,
REMOVE_TRACKS,
SET_AVALIABLE_SKINS,
LOAD_AUDIO_FILE,
LOAD_AUDIO_URL
} from "./actionTypes";
import { playlistEnabled } from "./config";
const mapObject = (obj, iteratee) =>
// TODO: Could return the original reference if no values change
Object.keys(obj).reduce((newObj, key) => {
newObj[key] = iteratee(obj[key], key);
return newObj;
}, {});
const filterObject = (obj, predicate) =>
// TODO: Could return the original reference if no values change
Object.keys(obj).reduce((newObj, key) => {
if (predicate(obj[key], key)) {
newObj[key] = obj[key];
}
return newObj;
}, {});
const defaultUserInput = {
focus: null,
bandFocused: null,
scrubPosition: 0,
userMessage: null
};
export const userInput = (state = defaultUserInput, action) => {
switch (action.type) {
case SET_FOCUS:
return { ...state, focus: action.input, bandFocused: null };
case SET_BAND_FOCUS:
return { ...state, focus: action.input, bandFocused: action.bandFocused };
case UNSET_FOCUS:
return { ...state, focus: null, bandFocused: null };
case SET_SCRUB_POSITION:
return { ...state, scrubPosition: action.position };
case SET_USER_MESSAGE:
return { ...state, userMessage: action.message };
case UNSET_USER_MESSAGE:
return { ...state, userMessage: null };
default:
return state;
}
};
const defaultWindowsState = {
focused: WINDOWS.MAIN,
equalizer: true,
playlist: playlistEnabled
};
const windows = (state = defaultWindowsState, action) => {
switch (action.type) {
case SET_FOCUSED_WINDOW:
return { ...state, focused: action.window };
case TOGGLE_EQUALIZER_WINDOW:
return { ...state, equalizer: !state.equalizer };
case CLOSE_EQUALIZER_WINDOW:
return { ...state, equalizer: false };
case TOGGLE_PLAYLIST_WINDOW:
if (!playlistEnabled) {
return state;
}
return { ...state, playlist: !state.playlist };
default:
return state;
}
};
const defaultDisplayState = {
doubled: false,
marqueeStep: 0,
loading: true,
llama: false,
closed: false,
shade: false,
equalizerShade: false,
playlistShade: false,
working: false,
skinImages: {},
skinColors: null,
skinCursors: null,
skinPlaylistStyle: {},
skinRegion: {},
visualizerStyle: 2,
playlistScrollPosition: 0,
playlistSize: [0, 0]
};
const display = (state = defaultDisplayState, action) => {
switch (action.type) {
case TOGGLE_DOUBLESIZE_MODE:
return { ...state, doubled: !state.doubled };
case TOGGLE_SHADE_MODE:
return { ...state, shade: !state.shade };
case TOGGLE_EQUALIZER_SHADE_MODE:
return { ...state, equalizerShade: !state.equalizerShade };
case TOGGLE_PLAYLIST_SHADE_MODE:
return { ...state, playlistShade: !state.playlistShade };
case TOGGLE_LLAMA_MODE:
return { ...state, llama: !state.llama };
case STEP_MARQUEE:
// TODO: Prevent this from becoming huge
return { ...state, marqueeStep: state.marqueeStep + 1 };
case STOP_WORKING:
return { ...state, working: false };
case START_WORKING:
return { ...state, working: true };
case START_LOADING:
return { ...state, loading: true };
case CLOSE_WINAMP:
return { ...state, closed: true };
case SET_SKIN_DATA:
return {
...state,
loading: false,
skinImages: action.skinImages,
skinColors: action.skinColors,
skinPlaylistStyle: action.skinPlaylistStyle,
skinCursors: action.skinCursors,
skinRegion: action.skinRegion
};
case TOGGLE_VISUALIZER_STYLE:
return { ...state, visualizerStyle: (state.visualizerStyle + 1) % 3 };
case SET_PLAYLIST_SCROLL_POSITION:
return { ...state, playlistScrollPosition: action.position };
case PLAYLIST_SIZE_CHANGED:
return { ...state, playlistSize: action.size };
default:
return state;
}
};
const defaultSettingsState = {
avaliableSkins: []
};
const settings = (state = defaultSettingsState, action) => {
switch (action.type) {
case SET_AVALIABLE_SKINS:
return { ...state, avaliableSkins: action.skins };
default:
return state;
}
};
const equalizer = (state, action) => {
if (!state) {
state = {
on: true,
auto: false,
sliders: {
preamp: 50
}
};
BANDS.forEach(band => {
state.sliders[band] = 50;
});
return state;
}
switch (action.type) {
case SET_BAND_VALUE:
const newSliders = { ...state.sliders, [action.band]: action.value };
return { ...state, sliders: newSliders };
case SET_EQ_ON:
return { ...state, on: true };
case SET_EQ_OFF:
return { ...state, on: false };
case SET_EQ_AUTO:
return { ...state, auto: action.value };
default:
return state;
}
};
// Dummy data for now
const defaultTracksState = {
"0": {
selected: false,
title: "Llama Whipping Intro",
artist: "DJ Mike Llama",
duration: "221"
},
"1": {
selected: false,
title: "Rock Is Dead",
artist: "Marilyn Manson",
duration: "221"
},
"2": {
selected: true,
title: "Spybreak! (Short One)",
artist: "Propellerheads",
duration: "171"
},
"3": {
selected: false,
title: "Clubbed to Death",
artist: "Rob D",
duration: "215"
},
"4": {
selected: false,
title: "Leave You Far Behind",
artist: "Lunatic Calm",
duration: "174"
},
"5": {
selected: false,
title: "Dragula",
artist: "Rob Zombie",
duration: "484"
},
"6": {
selected: false,
title: "Ultrasonic Sound",
artist: "Hive",
duration: "152"
},
"7": {
selected: false,
title: "Du hast",
artist: "Rammstein",
duration: "214"
}
};
const tracks = (state = defaultTracksState, action) => {
switch (action.type) {
case CLICKED_TRACK:
return mapObject(state, (track, id) => ({
...track,
selected: id === String(action.id)
}));
case CTRL_CLICKED_TRACK:
const t = state[action.id];
return {
...state,
[action.id]: { ...t, selected: !t.selected }
};
case SELECT_ALL:
return mapObject(state, track => ({ ...track, selected: true }));
case SELECT_ZERO:
return mapObject(state, track => ({ ...track, selected: false }));
case INVERT_SELECTION:
return mapObject(state, track => ({
...track,
selected: !track.selected
}));
case REMOVE_ALL_TRACKS:
return {};
case REMOVE_TRACKS:
return filterObject(state, (track, id) => !action.ids.includes(id));
default:
return state;
}
};
const defaultPlaylistState = {
trackOrder: [0, 1, 2, 3, 4, 5, 6],
currentTrack: 0
};
const playlist = (state = defaultPlaylistState, action) => {
switch (action.type) {
case REMOVE_ALL_TRACKS:
return { ...state, trackOrder: [], currentTrack: null };
case REMOVE_TRACKS:
return {
...state,
trackOrder: state.trackOrder.filter(id => !action.ids.includes(id))
};
default:
return state;
}
};
const media = (state, action) => {
if (!state) {
return {
timeMode: "ELAPSED",
timeElapsed: 0,
length: null, // Consider renaming to "duration"
kbps: null,
khz: null,
volume: 50,
balance: 0,
name: "",
channels: null,
shuffle: false,
repeat: false,
// TODO: Enforce possible values
status: "STOPPED"
};
}
switch (action.type) {
case "PLAY":
case "IS_PLAYING":
return { ...state, status: "PLAYING" };
case "PAUSE":
return { ...state, status: "PAUSED" };
case "STOP":
case "IS_STOPPED":
return { ...state, status: "STOPPED" };
case TOGGLE_TIME_MODE:
const newMode = state.timeMode === "REMAINING" ? "ELAPSED" : "REMAINING";
return { ...state, timeMode: newMode };
case UPDATE_TIME_ELAPSED:
return { ...state, timeElapsed: action.elapsed };
case LOAD_AUDIO_FILE:
case LOAD_AUDIO_URL:
return {
...state,
timeElapsed: 0,
length: null,
kbps: null,
khz: null,
channels: null,
name: null
};
case SET_MEDIA:
return {
...state,
length: action.length,
kbps: action.kbps,
khz: action.khz,
channels: action.channels,
name: action.name
};
case SET_VOLUME:
return { ...state, volume: action.volume };
case SET_BALANCE:
return { ...state, balance: action.balance };
case TOGGLE_REPEAT:
return { ...state, repeat: !state.repeat };
case TOGGLE_SHUFFLE:
return { ...state, shuffle: !state.shuffle };
default:
return state;
}
};
const reducer = combineReducers({
userInput,
windows,
display,
settings,
equalizer,
tracks,
playlist,
media
});
export default reducer;