git ssb

0+

mixmix / marama



Tree: c99cbbf914e6a178a87b75810cb4784f19010329

Files: c99cbbf914e6a178a87b75810cb4784f19010329 / index.js

2548 bytesRaw
1const h = require('mutant/h')
2const DayTile = require('./day-tile')
3const DayLabel = require('./day-label')
4const getDay = require('./lib/get-day')
5
6const DAYS = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
7const DEFAULT_FORMAT = 'rows'
8
9module.exports = function Marama (opts = {}) {
10 const day = opts.today || startOfDay()
11 const {
12 events = [],
13 month = day.getMonth() + 1, // month number (common defn)
14 year = day.getFullYear(),
15 range = null,
16 onSelect = () => {},
17 styles = {},
18 today = day
19 } = opts
20 if (opts.style) console.error('Marama: you have passed in **style** instead of styles!')
21
22 const monthIndex = month - 1 // month number (Date API defn)
23 const monthLength = new Date(year, monthIndex + 1, 0).getDate()
24 // NOTE Date takes month as a monthIndex i.e. april = 3
25 // and day = 0 goes back a day
26 const days = Array(monthLength).fill().map((_, i) => i + 1)
27 const dayOpts = {
28 events,
29 year,
30 monthIndex,
31 // day TBD
32 today,
33 offset: getDay(new Date(year, monthIndex, 1)) - 1, // how far into the week the month starts
34 weekFormat: getWeekFormat(styles),
35 showNumbers: Boolean(styles.showNumbers),
36 range,
37 onSelect
38 }
39
40 return h('div.Marama', [
41 h('div.days', { style: getStyles(styles) }, [
42 DAYS.map((day, i) => DayLabel(day, i, dayOpts.weekFormat)),
43 days.map(day => {
44 dayOpts.day = day // note we're mutating this object (might save memory?)
45 return DayTile(dayOpts)
46 })
47 ])
48 ])
49}
50
51function getStyles (styles = {}) {
52 const {
53 tileRadius = 6,
54 tileGap = 1,
55 dotRadius,
56 dotBorder = 1
57 } = styles
58 const format = getWeekFormat(styles)
59
60 const _styles = {
61 '--tile-radius': `${tileRadius}px`,
62 '--tile-gap': `${tileGap}px`,
63 '--dot-radius': `${dotRadius || Math.floor(tileRadius / 2)}px`,
64 '--dot-border': `${dotBorder}px`,
65 display: 'grid',
66 [`grid-template-${format}`]: '2 * calc(var(--tile-radius) + 2 * var(--tile-gap)) repeat(6, calc(2 * var(--tile-radius)))'
67 }
68
69 return _styles
70}
71
72function getWeekFormat (styles) {
73 if (styles.weekFormat && !['rows', 'columns'].includes(styles.weekFormat)) {
74 throw new Error('marama styles.weekFormat must be either "rows" or "columns"')
75 }
76 return styles.weekFormat || DEFAULT_FORMAT
77}
78
79function startOfDay (d = new Date()) {
80 return new Date(d.getFullYear(), d.getMonth(), d.getDate())
81}
82
83// function endOfDay (d = new Date()) {
84// return new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1)
85// }
86

Built with git-ssb-web