dedup events2attrs

This commit is contained in:
les 2021-03-10 16:09:35 +01:00
parent 27e4f2e18f
commit 11e6c93b74
No known key found for this signature in database
GPG key ID: 352918250B012177
3 changed files with 58 additions and 42 deletions

38
assets/helper.js Normal file
View file

@ -0,0 +1,38 @@
import { take, get } from 'lodash'
export function attributesFromEvents (_events, _tags) {
const colors = ['blue', 'orange', 'yellow', 'teal', 'indigo', 'green', 'red', 'purple', 'pink', 'gray']
const tags = take(_tags, 10).map(t => t.tag)
let attributes = []
attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'green', fillMode: 'outline' } })
function getColor (event) {
const color = { class: 'vc-rounded-full', color: 'blue', fillMode: 'normal' }
const tag = get(event, 'tags[0]')
if (!tag) { return color }
const idx = tags.indexOf(tag)
if (idx < 0) { return color }
color.color = colors[idx]
return color
}
attributes = attributes.concat(_events
.filter(e => !e.multidate)
.map(e => {
return {
key: e.id,
dot: getColor(e),
dates: new Date(e.start_datetime * 1000)
}
}))
attributes = attributes.concat(_events
.filter(e => e.multidate)
.map(e => ({
key: e.id,
highlight: getColor(e),
dates: { start: new Date(e.start_datetime * 1000), end: new Date(e.end_datetime * 1000) }
})))
return attributes
}

View file

@ -16,7 +16,7 @@
<script> <script>
import { mapState, mapActions } from 'vuex' import { mapState, mapActions } from 'vuex'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { take, get } from 'lodash' import { attributesFromEvents } from '../assets/helper'
export default { export default {
name: 'Calendar', name: 'Calendar',
@ -33,40 +33,7 @@ export default {
computed: { computed: {
...mapState(['tags', 'filters', 'in_past', 'settings']), ...mapState(['tags', 'filters', 'in_past', 'settings']),
attributes () { attributes () {
const colors = ['blue', 'orange', 'yellow', 'teal', 'indigo', 'green', 'red', 'purple', 'pink', 'gray'] return attributesFromEvents(this.events, this.tags)
const tags = take(this.tags, 10).map(t => t.tag)
let attributes = []
attributes.push({ key: 'today', dates: new Date(), highlight: { color: 'green', fillMode: 'outline' } })
function getColor (event) {
const color = { class: 'vc-rounded-full', color: 'blue', fillMode: 'normal' }
const tag = get(event, 'tags[0]')
if (!tag) { return color }
const idx = tags.indexOf(tag)
if (idx < 0) { return color }
color.color = colors[idx]
return color
}
attributes = attributes.concat(this.events
.filter(e => !e.multidate)
.map(e => {
return {
key: e.id,
dot: getColor(e),
dates: new Date(e.start_datetime * 1000)
}
}))
attributes = attributes.concat(this.events
.filter(e => e.multidate)
.map(e => ({
key: e.id,
highlight: getColor(e),
dates: { start: new Date(e.start_datetime * 1000), end: new Date(e.end_datetime * 1000) }
})))
return attributes
} }
}, },
methods: { methods: {

View file

@ -48,16 +48,17 @@
//- v-btn-toggle(v-else dense group v-model='value.recurrent.type' color='primary') //- v-btn-toggle(v-else dense group v-model='value.recurrent.type' color='primary')
//- v-btn(text link v-for='whenPattern in whenPatterns' :value='whenPattern.key' :key='whenPatterns.key') {{whenPattern.label}} //- v-btn(text link v-for='whenPattern in whenPatterns' :value='whenPattern.key' :key='whenPatterns.key') {{whenPattern.label}}
//- List(v-if='event.type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")') List(v-if='type==="normal" && todayEvents.length' :events='todayEvents' :title='$t("event.same_day")')
</template> </template>
<script> <script>
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { mapState } from 'vuex' import { mapState } from 'vuex'
import List from '@/components/List' import List from '@/components/List'
import { attributesFromEvents } from '../../assets/helper'
export default { export default {
name: 'WhenInput', name: 'DateInput',
components: { List }, components: { List },
props: { props: {
value: { type: Object, default: () => ({ from: null, due: null, recurrent: null }) } value: { type: Object, default: () => ({ from: null, due: null, recurrent: null }) }
@ -71,6 +72,7 @@ export default {
date: null, date: null,
page: null, page: null,
frequency: '', frequency: '',
events: [],
frequencies: [ frequencies: [
{ value: '1w', text: this.$t('event.each_week') }, { value: '1w', text: this.$t('event.each_week') },
{ value: '2w', text: this.$t('event.each_2w') }, { value: '2w', text: this.$t('event.each_2w') },
@ -79,7 +81,16 @@ export default {
} }
}, },
computed: { computed: {
...mapState(['settings', 'events']), ...mapState(['settings', 'tags']),
todayEvents () {
const start = dayjs(this.value.from).startOf('day').unix()
const end = dayjs(this.value.from).endOf('day').unix()
const events = this.events.filter(e => e.start_datetime >= start && e.start_datetime <= end)
return events
},
attributes () {
return attributesFromEvents(this.events, this.tags)
},
fromDate () { fromDate () {
if (this.value.multidate) { if (this.value.multidate) {
return ({ start: dayjs(this.value.from).toDate(), end: dayjs(this.value.due).toDate() }) return ({ start: dayjs(this.value.from).toDate(), end: dayjs(this.value.due).toDate() })
@ -105,9 +116,6 @@ export default {
isRecurrent () { isRecurrent () {
return !!this.value.recurrent return !!this.value.recurrent
}, },
attributes () {
return []
},
whenPatterns () { whenPatterns () {
if (!this.value.from) { return } if (!this.value.from) { return }
const date = dayjs(this.value.from) const date = dayjs(this.value.from)
@ -153,7 +161,7 @@ export default {
return '' return ''
} }
}, },
mounted () { async mounted () {
if (this.value.multidate) { if (this.value.multidate) {
this.type = 'multidate' this.type = 'multidate'
} else if (this.value.recurrent) { } else if (this.value.recurrent) {
@ -161,6 +169,9 @@ export default {
} else { } else {
this.type = 'normal' this.type = 'normal'
} }
this.events = await this.$api.getEvents({
start: dayjs().unix()
})
}, },
methods: { methods: {
updateRecurrent (value) { updateRecurrent (value) {