Skip to content

Commit dc0465b

Browse files
committed
Refactor global instances of $api and $utils to be mixins.
... instead of Vue.prototype variables which is the idiomatic approach. Also, the refactor enables utils to be instantiated as a class that takes the i18n object for util functions to have accesss to i18n translation.
1 parent afef994 commit dc0465b

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

frontend/src/main.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,43 @@ import App from './App.vue';
77
import router from './router';
88
import store from './store';
99
import * as api from './api';
10-
import utils from './utils';
1110
import { models } from './constants';
11+
import Utils from './utils';
1212

1313
// Internationalisation.
1414
Vue.use(VueI18n);
15-
16-
// Create VueI18n instance with options
1715
const i18n = new VueI18n();
1816

1917
Vue.use(Buefy, {});
2018
Vue.config.productionTip = false;
2119

22-
// Custom global elements.
23-
Vue.prototype.$api = api;
24-
Vue.prototype.$utils = utils;
25-
26-
Vue.prototype.$reloadServerConfig = () => {
27-
// Get the config.js <script> tag, remove it, and re-add it.
28-
let s = document.querySelector('#server-config');
29-
const url = s.getAttribute('src');
30-
s.remove();
31-
32-
s = document.createElement('script');
33-
s.setAttribute('src', url);
34-
s.setAttribute('id', 'server-config');
35-
s.onload = () => {
36-
store.commit('setModelResponse',
37-
{ model: models.serverConfig, data: humps.camelizeKeys(window.CONFIG) });
38-
};
39-
document.body.appendChild(s);
40-
};
20+
// Globals.
21+
const ut = new Utils(i18n);
22+
Vue.mixin({
23+
computed: {
24+
$utils: () => ut,
25+
$api: () => api,
26+
},
27+
28+
methods: {
29+
$reloadServerConfig: () => {
30+
// Get the config.js <script> tag, remove it, and re-add it.
31+
let s = document.querySelector('#server-config');
32+
const url = s.getAttribute('src');
33+
s.remove();
34+
35+
s = document.createElement('script');
36+
s.setAttribute('src', url);
37+
s.setAttribute('id', 'server-config');
38+
s.onload = () => {
39+
store.commit('setModelResponse',
40+
{ model: models.serverConfig, data: humps.camelizeKeys(window.CONFIG) });
41+
};
42+
document.body.appendChild(s);
43+
},
44+
},
45+
});
46+
4147

4248
// window.CONFIG is loaded from /api/config.js directly in a <script> tag.
4349
if (window.CONFIG) {

frontend/src/utils.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,35 @@ dayjs.extend(relativeTime);
99

1010
const reEmail = /(.+?)@(.+?)/ig;
1111

12-
export default class utils {
13-
static months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
14-
'Sep', 'Oct', 'Nov', 'Dec'];
15-
16-
static days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
12+
export default class Utils {
13+
constructor(i18n) {
14+
this.i18n = i18n;
15+
}
1716

1817
// Parses an ISO timestamp to a simpler form.
19-
static niceDate = (stamp, showTime) => {
18+
niceDate = (stamp, showTime) => {
2019
if (!stamp) {
2120
return '';
2221
}
2322

2423
const d = new Date(stamp);
25-
let out = `${utils.days[d.getDay()]}, ${d.getDate()}`;
26-
out += ` ${utils.months[d.getMonth()]} ${d.getFullYear()}`;
24+
const day = this.i18n.t(`globals.days.${(d.getDay() + 1)}`);
25+
const month = this.i18n.t(`globals.months.${(d.getMonth() + 1)}`);
26+
let out = `${day}, ${d.getDate()}`;
27+
out += ` ${month} ${d.getFullYear()}`;
2728
if (showTime) {
2829
out += ` ${d.getHours()}:${d.getMinutes()}`;
2930
}
3031

3132
return out;
3233
};
3334

34-
static duration(start, end) {
35-
return dayjs(end).from(dayjs(start), true);
36-
}
35+
duration = (start, end) => dayjs(end).from(dayjs(start), true);
3736

3837
// Simple, naive, e-mail address check.
39-
static validateEmail = (e) => e.match(reEmail);
38+
validateEmail = (e) => e.match(reEmail);
4039

41-
static niceNumber = (n) => {
40+
niceNumber = (n) => {
4241
if (n === null || n === undefined) {
4342
return 0;
4443
}
@@ -69,20 +68,23 @@ export default class utils {
6968
}
7069

7170
// UI shortcuts.
72-
static confirm = (msg, onConfirm, onCancel) => {
71+
confirm = (msg, onConfirm, onCancel) => {
7372
Dialog.confirm({
7473
scroll: 'clip',
75-
message: !msg ? 'Are you sure?' : msg,
74+
message: !msg ? this.i18n.t('globals.messages.confirm') : msg,
75+
confirmText: this.i18n.t('globals.buttons.ok'),
76+
cancelText: this.i18n.t('globals.buttons.cancel'),
7677
onConfirm,
7778
onCancel,
7879
});
7980
};
8081

81-
static prompt = (msg, inputAttrs, onConfirm, onCancel) => {
82+
prompt = (msg, inputAttrs, onConfirm, onCancel) => {
8283
Dialog.prompt({
8384
scroll: 'clip',
8485
message: msg,
85-
confirmText: 'OK',
86+
confirmText: this.i18n.t('globals.buttons.ok'),
87+
cancelText: this.i18n.t('globals.buttons.cancel'),
8688
inputAttrs: {
8789
type: 'string',
8890
maxlength: 200,
@@ -94,7 +96,7 @@ export default class utils {
9496
});
9597
};
9698

97-
static toast = (msg, typ, duration) => {
99+
toast = (msg, typ, duration) => {
98100
Toast.open({
99101
message: msg,
100102
type: !typ ? 'is-success' : typ,

0 commit comments

Comments
 (0)