-
Notifications
You must be signed in to change notification settings - Fork 18
/
gui.js
121 lines (110 loc) · 3.41 KB
/
gui.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
import { action, extendObservable, reaction } from 'mobx'
import { join } from 'path'
import { getItem, setItem } from '../utilities/localStorage.js'
import { debounce } from '../utilities/common.js'
import i18next from '../utilities/i18next.js'
import moment from 'moment'
class GUI {
/**
* @prop {array} languages - Available languages.
* @prop {string} soundsDir - Directory with notification sounds.
* @prop {object} sounds - Loaded notification audio elements.
* @prop {string} language - Selected language.
* @prop {string} localCurrency - Selected local currency.
* @prop {object} soundAlerts - Sound alert settings.
* @prop {object} window - Window height and width.
*/
constructor() {
this.languages = [
{ language: 'en-US', name: 'English' },
{ language: 'fr-FR', name: 'French' },
{ language: 'pt-BR', name: 'Portuguese' },
{ language: 'ru-RU', name: 'Russian' },
{ language: 'sl-SI', name: 'Slovenian' },
{ language: 'es-ES', name: 'Spanish' }
]
/** Load notification sounds. */
this.soundsDir = join(__dirname, '..', 'assets', 'sounds')
this.sounds = {
incoming: new Audio(join(this.soundsDir, 'incoming.mp3')),
spendable: new Audio(join(this.soundsDir, 'spendable.mp3'))
}
/** Extend the store with observable properties. */
extendObservable(this, {
language: getItem('language') || 'en-US',
localCurrency: getItem('localCurrency') || 'EUR',
soundAlerts: getItem('soundAlerts') || {
incoming: false,
spendable: false
},
window: { height: window.innerHeight, width: window.innerWidth }
})
/** Update i18next and moment on language change. */
reaction(
() => this.language,
language => {
i18next.changeLanguage(language)
moment.locale(language)
},
{
fireImmediately: true,
name: 'GUI: language changed, updating i18next and moment.'
}
)
/** Update window size on resize. */
window.addEventListener(
'resize',
debounce(
() => this.setWindowSize(window.innerHeight, window.innerWidth),
0.1 * 1000
),
true
)
}
/**
* Set display language and save it to local storage.
* @function setLanguage
* @param {string} language - Display language.
*/
@action
setLanguage(language) {
this.language = language
setItem('language', this.language)
}
/**
* Set local currency and save it to local storage.
* @function setLocalCurrency
* @param {string} localCurrency - Local currency.
*/
@action
setLocalCurrency(localCurrency) {
this.localCurrency = localCurrency
setItem('localCurrency', this.localCurrency)
}
/**
* Set sound alert and save it to local storage.
* @function setSoundAlert
* @param {string} alert - Alert to toggle.
*/
@action
setSoundAlert(alert) {
this.soundAlerts[alert] = !this.soundAlerts[alert]
setItem('soundAlerts', this.soundAlerts)
}
/**
* Set window height and width.
* @function setWindowSize
* @param {number} height - Window height.
* @param {number} width - Window width.
*/
@action
setWindowSize(height, width) {
this.window.height = height
this.window.width = width
}
}
/** Initialize a new globally used store. */
const gui = new GUI()
/** Export initialized store as default export & store class as named export. */
export default gui
export { GUI }