gancio/plugins/filters.js

106 lines
4.2 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-16 14:46:45 +02:00
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'
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
const instance_timezone = store.state.settings.instance_timezone
const locale = store.state.locale
dayjs.tz.setDefault(instance_timezone)
dayjs.locale(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(locale).format('ddd, D MMMM HH:mm'))
Vue.filter('dateFormat', (value, format) => dayjs.tz(value).format(format))
Vue.filter('unixFormat', (timestamp, format) => dayjs.unix(timestamp).tz(instance_timezone).format(format))
2019-07-23 01:31:43 +02:00
// shown in mobile homepage
Vue.filter('day', value => dayjs.unix(value).tz(instance_timezone).locale(store.state.locale).format('dddd, D MMM'))
Vue.filter('mediaURL', (event, type, format = '.jpg') => {
if (event.media && event.media.length) {
if (type === 'alt') {
return event.media[0].name
} else {
return store.state.settings.baseurl + '/media/' + (type === 'thumb' ? 'thumb/' : '') + event.media[0].url.replace(/.jpg$/, format)
}
} else if (type !== 'alt') {
return store.state.settings.baseurl + '/media/' + (type === 'thumb' ? 'thumb/' : '') + 'logo.svg'
}
return ''
})
2019-07-23 01:31:43 +02:00
Vue.filter('from', timestamp => dayjs.unix(timestamp).tz(instance_timezone).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
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') {
recurrent = app.i18n.t(`event.recurrent_${frequency}_days`, { days: dayjs.unix(parent.start_datetime).tz(instance_timezone).format('dddd') })
2020-02-11 15:39:57 +01:00
} else if (frequency === '1m' || frequency === '2m') {
const d = type === 'ordinal' ? dayjs.unix(parent.start_datetime).date() : dayjs.unix(parent.start_datetime).tz(instance_timezone).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(instance_timezone)
const end = dayjs.unix(event.end_datetime).tz(instance_timezone)
2019-10-28 17:33:20 +01:00
2020-02-11 11:53:32 +01:00
// const normal = `${start.format('dddd, D MMMM (HH:mm-')}${end.format('HH:mm) ')}`
// // recurrent event
// if (event.parent && where !== 'home') {
// const { frequency, days, type } = event.parent.recurrent
// if (frequency === '1w' || frequency === '2w') {
2020-10-16 14:46:45 +02:00
// const recurrent = app.i18n.tc(`event.recurrent_${frequency}_days`, days.length, { days: days.map(d => dayjs().day(d - 1).format('dddd')) })
2020-02-11 11:53:32 +01:00
// return `${normal} - ${recurrent}`
// } else if (frequency === '1m' || frequency === '2m') {
2020-10-16 14:46:45 +02:00
// const d = type === 'ordinal' ? days : days.map(d => dayjs().day(d - 1).format('dddd'))
2020-02-11 11:53:32 +01:00
// const recurrent = app.i18n.tc(`event.recurrent_${frequency}_${type}`, days.length, { days: d })
// return `${normal} - ${recurrent}`
// }
// return 'recurrent '
// }
2019-07-23 01:31:43 +02:00
// multidate
2019-05-30 12:04:14 +02:00
if (event.multidate) {
2022-07-23 23:03:17 +02:00
return `${start.format('ddd, D MMM, HH:mm')} - ${end.format('ddd, D MMM, HH:mm')}`
2019-09-11 19:12:24 +02:00
}
2019-07-23 01:31:43 +02:00
// normal event
2022-07-23 23:03:17 +02:00
return `${start.format('dddd, D MMMM, HH:mm')}-${end.format('HH:mm')}`
2019-05-30 12:04:14 +02:00
})
2019-06-07 17:02:33 +02:00
}