This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/plugins/globals.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
import t from '../src/translator';
2020-09-28 09:29:13 -07:00
import config from '../data/config.suml';
import {buildDict} from "../src/helpers";
2021-06-09 23:45:13 -07:00
import {DateTime} from "luxon";
2021-07-24 10:18:39 -07:00
import {decodeTime} from 'ulid';
export default ({ app, store }) => {
2021-01-06 06:21:20 -08:00
Vue.prototype.$eventHub = new Vue();
2021-04-08 15:43:57 -07:00
2020-11-10 14:41:56 -08:00
Vue.prototype.$base = process.env.BASE_URL;
2021-04-08 15:43:57 -07:00
Vue.prototype.$t = t;
Vue.prototype.$translateForPronoun = (str, pronoun) =>
pronoun.format(
2021-03-05 11:05:56 -08:00
t(`flags.${str.replace(/ /g, '_').replace(/'/g, `*`)}`, {}, false) || str
);
2021-04-08 15:43:57 -07:00
2020-09-28 09:29:13 -07:00
Vue.prototype.config = config;
2021-04-08 15:43:57 -07:00
Vue.prototype.locales = buildDict(function* () {
2020-10-28 08:22:29 -07:00
if (config.locale !== '_') {
yield [ config.locale, process.env.LOCALES[config.locale] ];
2020-10-28 08:22:29 -07:00
}
for (let l in process.env.LOCALES) {
if (process.env.LOCALES.hasOwnProperty(l) && l !== config.locale) {
yield [l, process.env.LOCALES[l]];
}
}
});
2021-04-08 15:43:57 -07:00
store.commit('setSpelling', app.$cookies.get('spelling') || 'traditional');
2021-04-08 15:43:57 -07:00
Vue.prototype.buildImageUrl = (imageId, size) => `${process.env.BUCKET}/images/${imageId}-${size}.png`
2021-04-08 15:43:57 -07:00
Vue.prototype.$loadScript = (name, src) => {
if (!process.client || document.querySelectorAll(`script.${name}-script`).length > 0) {
2021-08-09 23:50:22 -07:00
return new Promise((resolve, reject) => { resolve(); });
2021-04-08 15:43:57 -07:00
}
2021-08-09 23:50:22 -07:00
let resolveFn; let rejectFn;
const promise = new Promise((resolve, reject) => {
resolveFn = resolve;
rejectFn = reject;
});
2021-04-08 15:43:57 -07:00
const s = document.createElement('script');
s.setAttribute('src', src);
s.classList.add(`${name}-script`);
2021-08-09 23:50:22 -07:00
s.onload = resolveFn;
s.onerror = rejectFn;
2021-04-08 15:43:57 -07:00
document.body.appendChild(s);
2021-08-09 23:50:22 -07:00
return promise;
2021-04-08 15:43:57 -07:00
};
2021-06-09 23:45:13 -07:00
Vue.prototype.$datetime = (timestamp) => {
const dt = DateTime.fromSeconds(timestamp);
return dt.toFormat('y-MM-dd HH:mm')
}
2021-07-24 10:18:39 -07:00
Vue.prototype.$ulidTime = (ulid) => {
return decodeTime(ulid) / 1000;
}
}