git ssb

16+

Dominic / patchbay



Commit 368b6fabc6c25007c87d6e7a601888a0dd21c561

/calendar : have selection window display friggen gatherings !

mixmix committed on 7/22/2018, 10:30:31 AM
Parent: 7a1861362de764504e02614b8c8bc5837fdc089d

Files changed

app/page/calendar.jschanged
app/page/calendar.mcsschanged
app/page/calendar.jsView
@@ -1,12 +1,14 @@
11 const nest = require('depnest')
2-const { h, Array: MutantArray, Struct, computed, watch, throttle } = require('mutant')
2 +const { h, Array: MutantArray, map, Struct, computed, watch, throttle, resolve } = require('mutant')
33 const pull = require('pull-stream')
44 const { isMsg } = require('ssb-ref')
55
66 exports.gives = nest('app.page.calendar')
77
88 exports.needs = nest({
9 + 'message.html.render': 'first',
10 + 'sbot.async.get': 'first',
911 'sbot.pull.stream': 'first'
1012 })
1113
1214 exports.create = (api) => {
@@ -16,18 +18,56 @@
1618 const d = new Date()
1719 const state = Struct({
1820 today: new Date(d.getFullYear(), d.getMonth(), d.getDate()),
1921 year: d.getFullYear(),
20- events: MutantArray([])
22 + events: MutantArray([]),
23 + range: Struct({
24 + gte: new Date(d.getFullYear(), d.getMonth(), d.getDate()),
25 + lt: new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1)
26 + })
2127 })
2228
2329 watch(state.year, year => getEvents(year, state.events))
2430
25- return h('CalendarPage', { title: '/calendar' }, [
26- Calendar(state)
31 + const page = h('CalendarPage', { title: '/calendar' }, [
32 + Calendar(state),
33 + Events(state)
2734 ])
35 +
36 + page.scroll = i => {
37 + const gte = resolve(state.range.gte)
38 + state.range.gte.set(new Date(gte.getFullYear(), gte.getMonth(), gte.getDate() + i))
39 + const lt = resolve(state.range.lt)
40 + state.range.lt.set(new Date(lt.getFullYear(), lt.getMonth(), lt.getDate() + i))
41 + }
42 +
43 + return page
2844 }
2945
46 + function Events (state) {
47 + return h('CalendarEvents', computed([state.events, state.range], (events, range) => {
48 + const keys = events
49 + .filter(ev => ev.date >= range.gte && ev.date < range.lt)
50 + .sort((a, b) => a.date - b.date)
51 + .map(ev => ev.data.key)
52 +
53 + const gatherings = MutantArray([])
54 +
55 + pull(
56 + pull.values(keys),
57 + pull.asyncMap((key, cb) => {
58 + api.sbot.async.get(key, (err, value) => {
59 + if (err) return cb(err)
60 + cb(null, {key, value})
61 + })
62 + }),
63 + pull.drain(msg => gatherings.push(msg))
64 + )
65 +
66 + return map(gatherings, g => api.message.html.render(g))
67 + }))
68 + }
69 +
3070 function getEvents (year, events) {
3171 const query = [{
3272 $filter: {
3373 value: {
@@ -81,13 +121,15 @@
81121 function Calendar (state) {
82122 // TODO assert events is an Array of object
83123 // of form { date, data }
84124
125 + const setRange = state.range.set
126 +
85127 return h('Calendar', [
86128 Header(state.year),
87- h('div.months', computed(throttle(state, 100), ({ today, year, events }) => {
129 + h('div.months', computed(throttle(state, 100), ({ today, year, events, range }) => {
88130 return MONTHS.map((month, monthIndex) => {
89- return Month({ month, monthIndex, today, year, events })
131 + return Month({ month, monthIndex, today, year, events, range, setRange })
90132 })
91133 }))
92134 ])
93135 }
@@ -101,31 +143,34 @@
101143 ])
102144 ])
103145 }
104146
105-function Month ({ month, monthIndex, today, year, events }) {
147 +function Month ({ month, monthIndex, today, year, events, range, setRange }) {
106148 const monthLength = new Date(year, monthIndex + 1, 0).getDate()
107149 // NOTE Date takes month as a monthIndex i.e. april = 3
108150 // and day = 0 goes back a day
109151 const days = Array(monthLength).fill().map((_, i) => i + 1)
110152
111- var date
112- var dateEnd
113153 var weekday
114154 var week
115155 var offset = getDay(new Date(year, monthIndex, 1)) - 1
116156
157 + const setMonthRange = () => setRange({
158 + gte: new Date(year, monthIndex, 1),
159 + lt: new Date(year, monthIndex + 1, 1)
160 + })
161 +
117162 return h('CalendarMonth', [
118- h('div.month-name', month.substr(0, 2)),
163 + h('div.month-name', { 'ev-click': setMonthRange }, month.substr(0, 2)),
119164 h('div.days', { style: {display: 'grid'} }, [
120165 DAYS.map((day, i) => DayName(day, i)),
121166 days.map(Day)
122167 ])
123168 ])
124169
125170 function Day (day) {
126- date = new Date(year, monthIndex, day)
127- dateEnd = new Date(year, monthIndex, day + 1)
171 + const date = new Date(year, monthIndex, day)
172 + const dateEnd = new Date(year, monthIndex, day + 1)
128173 weekday = getDay(date)
129174 week = Math.ceil((day + offset) / 7)
130175
131176 const eventsOnDay = events.filter(e => {
@@ -143,17 +188,25 @@
143188 // column moved by 1 to make space for labels
144189 },
145190 classList: [
146191 date < today ? '-past' : '-future',
147- eventsOnDay.length ? '-events' : ''
192 + eventsOnDay.length ? '-events' : '',
193 + date >= range.gte && date < range.lt ? '-selected' : ''
148194 ]
149195 }
150196
151197 if (!eventsOnDay.length) return h('CalendarDay', opts)
152198
199 + opts['ev-click'] = () => setRange({
200 + gte: date,
201 + lt: dateEnd
202 + })
203 + opts['ev-hover'] = () => console.log(date)
204 +
153205 return h('CalendarDay', opts, [
206 + // TODO add awareness of whether I'm going to events
154207 // TODO try a FontAwesome circle
155- h('div', [
208 + h('div.dot', [
156209 // Math.random() > 0.3 ? h('div') : ''
157210 ])
158211 ])
159212 }
@@ -174,5 +227,4 @@
174227
175228 // Weeks run 0...6 (Sun - Sat)
176229 // this shifts those days around by 1
177230 }
178-
app/page/calendar.mcssView
@@ -2,8 +2,12 @@
22 width: 100%
33
44 display: grid
55 justify-content: center
6 + align-content: start
7 + justify-items: center
8 +
9 + overflow-y: scroll
610 }
711
812
913 Calendar {
@@ -11,8 +15,9 @@
1115 text-transform: capitalize
1216
1317 padding: 0
1418 margin: 0
19 + margin-bottom: 4rem
1520
1621 section {
1722 top: 50%
1823 margin: 0 auto
@@ -86,8 +91,9 @@
8691 div.month-name {
8792 font-size: 20px
8893 font-weight: bold
8994 text-align: left
95 + cursor: pointer
9096 margin-bottom: 5px
9197 }
9298
9399 div.days {
@@ -101,8 +107,9 @@
101107
102108 CalendarDay {
103109 width: calc(2 * var(--day-radius))
104110 height: calc(2 * var(--day-radius))
111 + cursor: pointer
105112
106113 border-radius: var(--day-border-radius)
107114
108115 display: flex
@@ -116,8 +123,16 @@
116123 -future {
117124 background: hsl(0, 0%, 80%)
118125 }
119126
127 + -selected {
128 + background: deeppink
129 +
130 + -future {
131 + background: deepskyblue
132 + }
133 + }
134 +
120135 -events {
121136 /* border-radius: var(--day-radius) */
122137 /* background: hsla(277, 57%, 45%, 1) */
123138
@@ -156,4 +171,17 @@
156171 display: flex
157172 align-items: center
158173 justify-content: center
159174 }
175 +
176 +CalendarEvents {
177 + width: 50rem
178 +
179 + div {
180 + width: 50rem
181 +
182 + div.Message {
183 + width: 50rem
184 + section.bottom { display: none }
185 + }
186 + }
187 +}

Built with git-ssb-web