gancio/plugins/filters.js

93 lines
3.8 KiB
JavaScript
Raw Normal View History

2019-04-03 00:25:12 +02:00
import Vue from 'vue'
2020-10-16 14:46:45 +02:00
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import utc from 'dayjs/plugin/utc'
import timezone from 'dayjs/plugin/timezone'
import localizedFormat from 'dayjs/plugin/localizedFormat'
2020-10-25 00:33:03 +02:00
import 'dayjs/locale/it'
import 'dayjs/locale/en'
2020-10-25 00:33:03 +02:00
import 'dayjs/locale/es'
import 'dayjs/locale/ca'
import 'dayjs/locale/pl'
import 'dayjs/locale/eu'
import 'dayjs/locale/nb'
import 'dayjs/locale/fr'
import 'dayjs/locale/de'
import 'dayjs/locale/gl'
2022-07-21 11:44:50 +02:00
import 'dayjs/locale/sk'
2023-01-09 17:02:29 +01:00
import 'dayjs/locale/ru'
2022-11-25 12:38:00 +01:00
import 'dayjs/locale/pt'
import 'dayjs/locale/zh'
2020-10-25 00:33:03 +02:00
2020-10-16 14:46:45 +02:00
dayjs.extend(relativeTime)
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(localizedFormat)
2019-04-03 00:25:12 +02:00
2019-06-25 01:05:38 +02:00
export default ({ app, store }) => {
2019-10-20 14:22:55 +02:00
// set timezone to instance_timezone!!
// to show local time relative to event's place
2020-02-09 18:15:44 +01:00
// not where in the world I'm looking at the page from
app.i18n.defaultLocale = store.state.settings.instance_locale
const instance_timezone = store.state.settings.instance_timezone
dayjs.tz.setDefault(instance_timezone)
dayjs.locale(app.i18n.locale || store.state.settings.instance_locale)
2019-10-20 14:22:55 +02:00
2019-07-23 01:31:43 +02:00
// replace links with anchors
2020-02-09 18:15:44 +01:00
// TODO: remove fb tracking id?
2019-10-20 14:22:55 +02:00
Vue.filter('linkify', value => value.replace(/(https?:\/\/([^\s]+))/g, '<a href="$1">$2</a>'))
2019-08-02 13:43:28 +02:00
Vue.filter('url2host', url => url.match(/^https?:\/\/(.[^/:]+)/i)[1])
Vue.filter('datetime', value => dayjs.tz(value).locale(app.i18n.locale || store.state.settings.instance_locale).format('ddd, D MMMM HH:mm'))
Vue.filter('dateFormat', (value, format) => dayjs.tz(value).format(format))
2022-08-05 18:08:15 +02:00
Vue.filter('unixFormat', (timestamp, format) => dayjs.unix(timestamp).tz().format(format))
2019-07-23 01:31:43 +02:00
// shown in mobile homepage
Vue.filter('day', value => dayjs.unix(value).tz().locale(app.i18n.locale || store.state.settings.instance_locale).format('dddd, D MMM'))
Vue.filter('mediaURL', (event, type, format = '.jpg') => {
2022-09-05 13:03:35 +02:00
const mediaPath = type === 'download' ? '/download/' : '/media/'
if (event.media && event.media.length) {
if (type === 'alt') {
return event.media[0].name
} else {
2022-09-05 13:03:35 +02:00
return store.state.settings.baseurl + mediaPath + (type === 'thumb' ? 'thumb/' : '') + event.media[0].url.replace(/.jpg$/, format)
}
} else if (type !== 'alt') {
2022-09-05 13:03:35 +02:00
return store.state.settings.baseurl + mediaPath + (type === 'thumb' ? 'thumb/' : '') + 'logo.svg'
}
return ''
})
2019-07-23 01:31:43 +02:00
2022-08-05 18:08:15 +02:00
Vue.filter('from', timestamp => dayjs.unix(timestamp).tz().fromNow())
2020-02-11 11:53:32 +01:00
2020-02-11 15:39:57 +01:00
Vue.filter('recurrentDetail', event => {
2020-11-17 00:34:56 +01:00
const parent = event.parent
2022-12-12 11:43:22 +01:00
if (!parent.recurrent || !parent.recurrent.frequency) return 'error!'
2021-01-11 00:17:56 +01:00
const { frequency, type } = parent.recurrent
2020-02-11 15:39:57 +01:00
let recurrent
if (frequency === '1w' || frequency === '2w') {
2022-08-05 18:08:15 +02:00
recurrent = app.i18n.t(`event.recurrent_${frequency}_days`, { days: dayjs.unix(parent.start_datetime).tz().format('dddd') })
2020-02-11 15:39:57 +01:00
} else if (frequency === '1m' || frequency === '2m') {
2022-08-05 18:08:15 +02:00
const d = type === 'ordinal' ? dayjs.unix(parent.start_datetime).date() : dayjs.unix(parent.start_datetime).tz().format('dddd')
2021-05-11 15:12:49 +02:00
if (type === 'ordinal') {
recurrent = app.i18n.t(`event.recurrent_${frequency}_days`, { days: d })
} else {
recurrent = app.i18n.t(`event.recurrent_${frequency}_ordinal`,
{ n: app.i18n.t('ordinal.' + type), days: d })
}
2020-02-11 15:39:57 +01:00
}
return recurrent
})
2020-02-11 11:53:32 +01:00
Vue.filter('when', (event) => {
const start = dayjs.unix(event.start_datetime).tz().locale(app.i18n.locale || store.state.settings.instance_locale)
2022-11-04 12:22:21 +01:00
const end = event.end_datetime && dayjs.unix(event.end_datetime).tz().locale(app.i18n.locale || store.state.settings.instance_locale)
2019-07-23 01:31:43 +02:00
2022-11-04 12:22:21 +01:00
let time = start.format('dddd D MMMM HH:mm')
if (end) {
time += event.multidate ? `${end.format('dddd D MMMM HH:mm')}` : `-${end.format('HH:mm')}`
2022-11-04 12:22:21 +01:00
}
return time
2019-05-30 12:04:14 +02:00
})
2019-06-07 17:02:33 +02:00
}