Files: c99cbbf914e6a178a87b75810cb4784f19010329 / day-tile.js
2301 bytesRaw
1 | const h = require('mutant/h') |
2 | const getDay = require('./lib/get-day') |
3 | |
4 | module.exports = function Day ({ today, year, monthIndex, day, offset, weekFormat, showNumbers, events, range, onSelect }) { |
5 | const date = new Date(year, monthIndex, day) |
6 | const dateEnd = new Date(year, monthIndex, day + 1) |
7 | const weekday = getDay(date) |
8 | const week = Math.ceil((day + offset) / 7) |
9 | |
10 | const eventsOnDay = eventsInRange({gte: date, lt: dateEnd}) |
11 | const attending = eventsOnDay.some(e => { |
12 | return e.data.attending |
13 | }) |
14 | |
15 | const style = weekFormat === 'rows' |
16 | ? { |
17 | 'grid-row': `${week + 1} / ${week + 2}`, // new weeks on each col (leaving space for labels col) |
18 | 'grid-column': `${weekday} / ${weekday + 1}` // new weekdays on each row |
19 | // moved by 1 to make space for labels |
20 | } |
21 | : { |
22 | 'grid-row': `${weekday} / ${weekday + 1}`, // new weekdays on each row |
23 | 'grid-column': `${week + 1} / ${week + 2}` // new weeks on each col (leaving space for labels col) |
24 | } |
25 | |
26 | const opts = { |
27 | style, |
28 | classList: [ |
29 | date < today ? '-past' : '-future', |
30 | eventsOnDay.length ? '-events' : '', |
31 | inRange(range, date) ? '-range' : '', |
32 | attending ? '-attending' : '' |
33 | ], |
34 | attributes: { |
35 | 'title': `${year}-${monthIndex + 1}-${day}`, |
36 | 'data-date': `${year}-${monthIndex + 1}-${day}` |
37 | } |
38 | } |
39 | if (onSelect) opts['ev-click'] = handleSelect |
40 | |
41 | return h('div.MaramaDayTile', opts, [ |
42 | showNumbers |
43 | ? day : eventsOnDay.length |
44 | ? h('div.dot') : '' |
45 | ]) |
46 | |
47 | /// helpers |
48 | |
49 | function eventsInRange (range) { |
50 | return events.filter(e => inRange(range, e.date)) |
51 | } |
52 | |
53 | function handleSelect (ev) { |
54 | if (ev.shiftKey && validRange(range)) { |
55 | const newRange = dateEnd >= range.lt |
56 | ? { gte: range.gte, lt: dateEnd } |
57 | : { gte: date, lt: range.lt } |
58 | newRange.events = eventsInRange(newRange) |
59 | |
60 | return onSelect(newRange) |
61 | } |
62 | |
63 | return onSelect({ |
64 | gte: date, |
65 | lt: dateEnd, |
66 | events: eventsOnDay |
67 | }) |
68 | } |
69 | } |
70 | |
71 | function inRange (range, date) { |
72 | if (!validRange(range)) return false |
73 | return (date >= range.gte) && (date < range.lt) |
74 | } |
75 | |
76 | function validRange (range) { |
77 | if (!range) return false |
78 | if (!(range.gte instanceof Date)) return false |
79 | if (!(range.lt instanceof Date)) return false |
80 | |
81 | return true |
82 | } |
83 |
Built with git-ssb-web