traxxx/assets/js/main.js

146 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2021-02-05 02:50:03 +00:00
import config from 'config';
import { createApp } from 'vue';
import dayjs from 'dayjs';
2020-12-27 03:21:10 +00:00
import mitt from 'mitt';
2021-04-27 01:56:38 +00:00
import tippy from 'tippy.js';
2020-12-27 03:21:10 +00:00
import router from './router';
import initStore from './store';
import initUiObservers from './ui/observers';
2021-03-13 03:26:24 +00:00
import initAuthObservers from './auth/observers';
import setPageTitle from './set-page-title';
import { formatDate, formatDuration } from './format';
import '../css/style.scss';
2021-04-27 01:56:38 +00:00
import 'tippy.js/dist/tippy.css';
import Container from '../components/container/container.vue';
import Icon from '../components/icon/icon.vue';
2020-08-15 17:04:33 +00:00
import Footer from '../components/footer/footer.vue';
import Tooltip from '../components/tooltip/tooltip.vue';
2021-01-03 21:53:51 +00:00
import Dialog from '../components/dialog/dialog.vue';
async function init() {
2021-01-03 21:53:51 +00:00
let uid = 0;
const store = initStore(router);
const app = createApp(Container);
2020-12-27 03:21:10 +00:00
const events = mitt();
function getBasePath(media, type, options) {
if (store.state.ui.sfw) {
return config.media.assetPath;
}
if (media.isS3) {
return config.media.s3Path;
}
if (options?.local) {
return config.media.assetPath;
}
return config.media.mediaPath;
}
function getFilename(media, type, options) {
if (store.state.ui.sfw && type && !options?.original) {
return media.sfw[type];
}
if (store.state.ui.sfw) {
return media.sfw.path;
}
if (type && !options?.original) {
return media[type];
}
return media.path;
}
function getPath(media, type, options) {
if (!media) {
return null;
}
const path = getBasePath(media, type, options);
const filename = getFilename(media, type, options);
return `${path}/${filename}`;
}
2021-03-15 02:30:47 +00:00
await initAuthObservers(store, router);
await initUiObservers(store, router);
if (window.env.sfw) {
store.dispatch('setSfw', true);
}
app.use(store);
app.use(router);
await router.isReady();
app.mixin({
components: {
Icon,
2020-08-15 17:04:33 +00:00
Footer,
Tooltip,
'v-popover': Tooltip,
2021-01-03 21:53:51 +00:00
Dialog,
},
2020-12-27 03:21:10 +00:00
data() {
return {
events,
2021-02-05 02:50:03 +00:00
config,
2020-12-27 03:21:10 +00:00
};
},
computed: {
theme() { return this.$store.state.ui.theme; },
},
watch: {
pageTitle(title) {
setPageTitle(title); // for Options API components
},
},
2021-11-20 22:59:15 +00:00
beforeCreate() {
this.uid = uid;
uid += 1;
},
methods: {
formatDate,
formatDuration,
isAfter: (dateA, dateB) => dayjs(dateA).isAfter(dateB),
isBefore: (dateA, dateB) => dayjs(dateA).isBefore(dateB),
getPath,
getBgPath: (media, type) => `url(${getPath(media, type)})`,
},
});
app.directive('tooltip', {
beforeMount(el, binding) {
if (!binding.value) {
return;
}
2021-04-27 01:56:38 +00:00
// don't include HTML in native title attribute
const textEl = document.createElement('div');
textEl.innerHTML = binding.value;
el.title = textEl.textContent; // eslint-disable-line no-param-reassign
tippy(el, {
content: binding.value,
allowHTML: true,
duration: [0, 0],
});
},
});
app.mount('#container');
}
init();