git ssb

16+

Dominic / patchbay



Commit ee739c123cb53210216f3fe26a35eedcf2c6eecc

/calendar : factor out Day, DayName, styling

mixmix committed on 7/20/2018, 5:31:30 AM
Parent: 130d9b2d4b5cccafdcbb5d697ab3007a90c9741a

Files changed

app/page/calendar.jschanged
app/page/calendar.mcsschanged
app/page/calendar.jsView
@@ -20,152 +20,82 @@
2020 const DAYS = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
2121
2222 function Cal () {
2323 const year = Value(new Date().getFullYear())
24- const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1, 0)
24 + const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())
25 + // const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1, 0)
2526
26- const root = computed(year, year => {
27- return h('Calendar', [
28- Header(year),
29- MONTHS.map((month, i) => {
30- return h('div.month', [
31- h('div.label', month.substr(0, 3)),
32- h('div.graph', [
33- // Labels(),
34- Month(year, i)
35- ])
36- ])
37- })
38- ])
39- })
27 + const root = h('Calendar', [
28 + Header(year),
29 + h('div.months', computed(year, year => {
30 + return MONTHS.map((month, monthIndex) => Month({ month, monthIndex, year, today }))
31 + }))
32 + ])
4033
4134 return root
35 +}
4236
43- function Header (year) {
44- return h('div.header', [
45- h('p.year', [
46- year,
47- h('a', { 'ev-click': () => year.set(year() - 1) }, '-'),
48- h('a', { 'ev-click': () => year.set(year() + 1) }, '+')
49- ])
50- // h('p.percentage', yearProgress(year))
37 +function Header (year) {
38 + return h('div.header', [
39 + h('div.year', [
40 + year,
41 + h('a', { 'ev-click': () => year.set(year() - 1) }, '-'),
42 + h('a', { 'ev-click': () => year.set(year() + 1) }, '+')
5143 ])
52- }
44 + // h('p.percentage', yearProgress(year))
45 + ])
46 +}
5347
54- // function yearProgress (year) {
55- // const diff = new Date() - new Date(year, 0, 1, 0)
56- // const progress = ((diff / 31536000000) * 100).toFixed(2)
57- // const yd = Math.abs((progress / 100).toFixed(2))
48 +function Month ({ month, monthIndex, year, today }) {
49 + const monthLength = new Date(year, monthIndex + 1, 0).getDate()
50 + // NOTE Date takes month as a monthIndex i.e. april = 3
51 + // and day = 0 goes back a day
52 + const days = Array(monthLength).fill().map((_, i) => i + 1)
5853
59- // return progress < 0 ? yd + ` YEARS AWAY` : progress > 100 ? yd + ` YEARS AGO` : progress + '%'
60- // }
54 + var date
55 + var weekday
56 + var week
57 + var offset = getDay(new Date(year, monthIndex, 1)) - 1
6158
62- // function doLabels () {
63- // let html = ''
64- // let y = 0
59 + return h('CalendarMonth', [
60 + h('div.month-name', month.substr(0, 2)),
61 + h('div.days', { style: {display: 'grid'} }, [
62 + DAYS.map((day, i) => DayName(day, i)),
63 + days.map(Day)
64 + ])
65 + ])
6566
66- // for (var i = 0; i < 7; i++) {
67- // y = (i * 14)
68- // html += `<text class="dayLabel" x="5" y='${y}' dy="10">${DAYS[i].substr(0, 1)}</text>`
69- // }
70- // return html
71- // }
67 + function Day (day) {
68 + date = new Date(year, monthIndex, day)
69 + weekday = getDay(date)
70 + week = Math.ceil((day + offset) / 7)
7271
73- function Month (year, monthIndex) {
74- const monthLength = new Date(year, monthIndex + 1, 0).getDate()
75- // NOTE Date takes month as a monthIndex i.e. april = 3
76- // and day = 0 goes back a day
77- const days = Array(monthLength).fill().map((_, i) => i + 1)
78-
79- const style = {
80- display: 'grid',
81- 'grid-gap': '.2rem'
82- }
83-
84- var date
85- var weekday
86- var week
87- var offset = getDay(new Date(year, monthIndex, 1)) - 1
88-
89- return h('div', { style }, days.map(day => {
90- date = new Date(year, monthIndex, day)
91- weekday = getDay(date)
92- if (weekday === 0) weekday = 7 // urghh I want monday to be the start of the week
93- week = Math.ceil((day + offset) / 7)
94-
95- return h('div.day', {
96- attributes: { 'data-day': day },
97- style: {
98- 'grid-row': `${weekday} / ${weekday + 1}`,
99- 'grid-column': `${week} / ${week + 1}`
100- }
101- })
102- }))
72 + return h('CalendarDay', {
73 + attributes: { 'data-date': `${year}-${monthIndex + 1}-${day}` },
74 + style: {
75 + 'grid-row': `${weekday} / ${weekday + 1}`,
76 + 'grid-column': `${week + 1} / ${week + 2}`
77 + // column moved by 1 to make space for labels
78 + },
79 + classList: [
80 + date < today ? '-past' : '-future'
81 + ]
82 + })
10383 }
84 +}
10485
105- function getDay (date) {
106- const d = date.getDay()
107- return d === 0 ? 7 : d
108-
109- // Weeks run 0...6 (Sun - Sat)
110- // this shifts those days around by 1
111- }
112-
113- function doMonth (moxsd ........
114- .th) {
115- var html = ''
116- const monthLength = new Date(year, month + 1, 0).getDate()
117- let date = 0
118- let x = 0
119- let y = 0
120-
121- while (date < monthLength) {
122- x += 14
123- let week = 0
124-
125- while (week < 7 && date !== monthLength) {
126- y = week * 14
127- let day = new Date(year, month, date, 0)
128- let dotab = `tabIndex="0"`
129-
130- if (day.getDay() !== week) {
131- style = 'null'
132- dotab = ''
133- date--
134- } else if (String(day) === String(today)) {
135- style = 'today'
136- } else if (day < today) {
137- style = 'gone'
138- } else if (day.getDay() === 5 || day.getDay() === 6) {
139- style = 'weekend'
140- } else {
141- style = 'day'
142- }
143-
144- html += `<rect id="square" class='${style}' x='${x}' y='${y}' title='${(date + 1) === 0 ? 'null' : DAYS[week] + ' ' + (date + 1)}' width="12px" height="12px" rx="2" ry="2" onclick="
145- UpdateFooter(${year}, ${month}, ${(date + 1)}, ${week}, this)" onblur="" ${dotab}></rect>`
146-
147- week++
148- date++
149- }
86 +function DayName (day, index) {
87 + return h('CalendarDayName', {
88 + style: {
89 + 'grid-row': `${index + 1} / ${index + 2}`,
90 + 'grid-column': '1 / 2'
15091 }
151- return html
152- }
92 + }, day.substr(0, 1))
15393 }
15494
155-function UpdateFooter (year, month, date, week, obj) {
156- obj.addEventListener('blur', () => { footer.innerHTML = '' })
157- let diff = ((new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate(), 0) - new Date(year, month, date)) / 86400000)
158- let num = Math.abs(diff).toFixed()
159- let calc
95 +function getDay (date) {
96 + const dayIndex = date.getDay()
97 + return dayIndex === 0 ? 7 : dayIndex
16098
161- diff < 0 ? calc = `In ${num} Day${num > 1 ? 's' : ''}.` : diff == 0 ? calc = `Today.` : calc = `${num} Day${num > 1 ? 's' : ''} ago.`
162- footer.innerHTML = `${MONTHS[month]} ${date}, ${DAYS[week]}. ${calc}`
99 + // Weeks run 0...6 (Sun - Sat)
100 + // this shifts those days around by 1
163101 }
164-
165-function getYear (diff) {
166- location.hash = parseInt(location.hash.replace('#', '')) + parseInt(diff)
167-}
168-
169-// window.onhashchange = function () {
170-// Year(parseInt(location.hash.replace('#', '')))
171-// }
app/page/calendar.mcssView
@@ -7,20 +7,14 @@
77
88
99 Calendar {
1010 font-family: 'input_mono_regular', monospace
11- text-transform: uppercase
11 + text-transform: capitalize
1212
13- display: block
14- height: 100%
15- min-height: 400px
16- width: 100%
17-
1813 padding: 0
1914 margin: 0
2015
2116 section {
22- position: relative
2317 top: 50%
2418 margin: 0 auto
2519 user-select: none
2620 -webkit-user-select: none
@@ -31,13 +25,13 @@
3125 text-align: center
3226 }
3327
3428 div.header {
35- margin: 0 10px 40px 10px
29 + margin: 40px 0
3630 text-align: left
3731 align-content: flex-start
3832
39- p.year {
33 + div.year {
4034 font-size: 40px
4135 font-weight: bold
4236
4337 a {
@@ -50,8 +44,9 @@
5044 :hover {
5145 color: #ffffff
5246 background: #000
5347 cursor: pointer
48 + text-decoration: none
5449 }
5550 }
5651 }
5752
@@ -60,56 +55,75 @@
6055 color: #8b8b8b
6156 }
6257 }
6358
64- div.month {
65- margin: 10px
66- display: inline-block
67- /* width:100px */
68- /* height: 130px */
59 + div.months {
60 + display: flex
6961
70- div.label {
71- font-size: 20px
72- font-weight: bold
73- text-align: left
74- margin-bottom: 5px
62 + div.CalendarMonth {
63 + margin-right: 20px
7564 }
65 + }
7666
77- div.graph {
67 + /* @media only screen and (min-width: 1440px) */
68 + /* { section { width: 1440px; }} */
7869
79- div {
80- div.day {
81- background: #000
82- height: .8rem
83- width: .8rem
70 + /* @media only screen and (max-width: 1440px) */
71 + /* { section { width: 720px; }} */
8472
85- border-radius: .1rem
86- }
87- }
88- }
73 + /* @media only screen and (max-width: 720px) */
74 + /* { section { width: 360px; }} */
75 +}
76 +
77 +
78 +CalendarMonth {
79 + --day-width: 12px
80 + --day-border-radius: 2px
81 + --day-gap: 2px
82 +
83 + width: calc(7 * var(--day-width) + 6 * var(--day-gap))
84 + /* max of 6 weeks + day labels, with gaps between each */
85 +
86 + div.month-name {
87 + font-size: 20px
88 + font-weight: bold
89 + text-align: left
90 + margin-bottom: 5px
8991 }
9092
93 + div.days {
94 + grid-gap: var(--day-gap)
95 + justify-content: start
96 + align-content: start
9197
92- /* p { margin: 0px; } */
98 + div.CalendarDay {}
99 + }
100 +}
93101
94- /* svg.graph { color:white; width: 100%; height: 100px; } */
95- /* svg.graph text {font-family: 'input_mono_regular'; stroke:none; fill:#8b8b8b; font-size:14px; text-anchor: middle; } */
96- /* svg.graph text.dayLabel { font-size: 10px; } */
97- /* svg.graph rect { stroke:none; outline: none; } */
98- /* svg.graph rect:hover, rect:focus { fill: #ff1e00 !important; cursor:pointer; } */
102 +CalendarDay {
103 + width: var(--day-width)
104 + height: var(--day-width)
99105
100- /* svg.graph rect.null { fill: none; stroke: none;} */
101- /* svg.graph rect.today { fill:#ff1e00; } */
102- /* svg.graph rect.day { fill:#8b8b8b; } */
103- /* svg.graph rect.weekend { fill:#747474 } */
104- /* svg.graph rect.gone { fill:#000000 } */
105- /* svg.graph path { stroke-linecap: butt; stroke-dasharray: 1,1; fill:none; stroke:#333; stroke-width:13px } */
106 + border-radius: var(--day-border-radius)
106107
107- /* @media only screen and (min-width: 1440px) */
108- /* { section { width: 1440px; }} */
108 + -past {
109 + background: hsl(0, 0%, 20%)
110 + }
109111
110- /* @media only screen and (max-width: 1440px) */
111- /* { section { width: 720px; }} */
112 + -future {
113 + background: hsl(0, 0%, 80%)
114 + }
115 +}
112116
113- /* @media only screen and (max-width: 720px) */
114- /* { section { width: 360px; }} */
117 +CalendarDayName {
118 + color: hsl(0, 0%, 40%)
119 + font-family: arial
120 + font-size: calc(var(--day-width) - 3px)
121 + /* line-height: calc(var(--day-width) - 2px) */
122 +
123 + width: var(--day-width)
124 + height: var(--day-width)
125 +
126 + display: flex
127 + align-items: center
128 + justify-content: center
115129 }

Built with git-ssb-web