-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar-mod.html
185 lines (168 loc) · 6.42 KB
/
calendar-mod.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<!--
Copyright 2024 Paul H. Tyson
phtyson at sbcglobal dot net
This Source Code Form (calendar-mod) is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
The imported calendrica library modules are derived from
https://github.com/Sarabveer/calendrica-js and are subject to the terms
of the CALENDRICA 4.0 license. See the lib/calendrica directory
for this license.
-->
<html lang="en">
<head>
<title>Calendar Mod</title>
<script src="lib/d3.v7.min.js"></script>
<script type="module">
import {fixedFromGregorian} from './lib/calendrica/gregorian.js';
import {solarLongitudeAfter,lunarPhaseAtOrBefore} from './lib/calendrica/astronomy.js';
/** locale, e.g. "en-US", "en" */
const userLocale =
navigator.languages && navigator.languages.length
? navigator.languages[0]
: navigator.language;
/** URL query parameters */
const urlQueryParams = (new URL(document.URL)).searchParams;
/** The year to make calendar for. */
const y = parseInt(urlQueryParams.get("year")) || (new Date()).getUTCFullYear();
/** R.D. of solstices and equinoxes */
const equinox03 = Math.floor(solarLongitudeAfter(0,fixedFromGregorian(y,3,20)));
const solstice06 = Math.floor(solarLongitudeAfter(90,fixedFromGregorian(y,6,20)));
const equinox09 = Math.floor(solarLongitudeAfter(180,fixedFromGregorian(y,9,20)));
const solstice12 = Math.floor(solarLongitudeAfter(270,fixedFromGregorian(y,12,20)));
/**
* 8-element array of date arrays, one for each page.
* Each page has 2 arrays--one full month and one half.
*/
const yearDays = [[d3.timeDays(new Date(y,0,1),new Date(y,1,1)),
d3.timeDays(new Date(y,1,1),new Date(y,1,16))],
[d3.timeDays(new Date(y,1,16),new Date(y,2,1)),
d3.timeDays(new Date(y,2,1),new Date(y,3,1))],
[d3.timeDays(new Date(y,3,1),new Date(y,4,1)),
d3.timeDays(new Date(y,4,1),new Date(y,4,16))],
[d3.timeDays(new Date(y,4,16),new Date(y,5,1)),
d3.timeDays(new Date(y,5,1),new Date(y,6,1))],
[d3.timeDays(new Date(y,6,1),new Date(y,7,1)),
d3.timeDays(new Date(y,7,1),new Date(y,7,16))],
[d3.timeDays(new Date(y,7,16),new Date(y,8,1)),
d3.timeDays(new Date(y,8,1),new Date(y,9,1))],
[d3.timeDays(new Date(y,9,1),new Date(y,10,1)),
d3.timeDays(new Date(y,10,1),new Date(y,10,16))],
[d3.timeDays(new Date(y,10,16),new Date(y,11,1)),
d3.timeDays(new Date(y,11,1),new Date(y+1,0,1))]];
const weekDays = ["Su","M","Tu","W","Th","F","Sa"];
/**
* Return month name for date.
* @param d the Date
* @param sh boolean to make short name
*/
const monthName = function(d,sh) {
return d instanceof Date ?
d.toLocaleDateString(userLocale,{month: sh?"short":"long"})
: "";
}
/**
* Enrich list of dates with month/year header
* and blank at position 15 of full month.
*/
const monthData = function(d,i,nl) {
const col1 = i%2===0;
const full = d.length>16;
let mHdr = monthName(d[0],!full);
if (full) {
if (col1) {
mHdr += `<span class="year">${y}</span>`;
} else {
mHdr = `<span class="year">${y}</span>${mHdr}`;
}
}
let arr = [];
if (full) {
arr = [mHdr,...d.slice(0,15),"",...d.slice(15)];
} else {
arr = [mHdr,...d];
}
return arr;
}
/**
* Turn date into d Wd format plus lunar phase and season start,
* if applicable.
* Symbols from https://en.wikipedia.org/wiki/Astronomical_symbols
* (retrieved 2024-02-28).
*/
const dayData = function(d,i,nl) {
let str = "";
if (d instanceof Date) {
const gDate = [d.getUTCFullYear()
,d.getUTCMonth()+1
,d.getUTCDate()];
const rd = fixedFromGregorian(...gDate);
str = `${d.getDate()} ${weekDays[d.getDay()]}`;
/** lunar phases
* new: unicode 🌑 , astrosym.40.svg
* 1st: unicode 🌓 , astrosym.45.svg
* full: unicode 🌕 , astrosym.39.svg
* last: unicode 🌗 , astrosym.44.svg
*/
if (Math.floor(lunarPhaseAtOrBefore(0,rd+1)) === rd) {
str += ` <span class="lunar-phase">🌑</span>`;
} else if (Math.floor(lunarPhaseAtOrBefore(90,rd+1)) === rd) {
str += ` <span class="lunar-phase">🌓</span>`;
} else if (Math.floor(lunarPhaseAtOrBefore(180,rd+1)) === rd) {
str += ` <span class="lunar-phase">🌕</span>`;
} else if (Math.floor(lunarPhaseAtOrBefore(270,rd+1)) === rd) {
str += ` <span class="lunar-phase">🌗</span>`;
}
/** solstices and equinoxes
* Aries March Equinox astrosym.11.svg, ♈
* Cancer June solstice astrosym.14.svg, ♋
* Libra September Equinox astrosym.17.svg, ♎
* Capricornus December solstice astrosysm.20.svg, ♑
*/
if (rd === equinox03) {
str += ` <span class="equinox">♈</span>`;
} else if (rd === solstice06) {
str += ` <span class="solstice">♋</span>`;
} else if (rd === equinox09) {
str += ` <span class="equinox">♎</span>`;
} else if (rd === solstice12) {
str += ` <span class="solstice">♑</span>` }
} else {
str = d;
}
return str;
}
/**
* Generate html for grid layout on 8 pages.
*/
const makeCalendar = function() {
d3.select("#pocketmod")
.selectAll("div")
.data(yearDays)
.join("div")
.attr("class",(d,i)=>`page${i+1}`)
.classed("odd-page",(d,i)=>i%2===0)
.classed("even-page",(d,i)=>i%2===1)
.selectAll("div")
.data(d=>d)
.join("div")
.attr("class",d=>`month${d.length>16?2:1}`)
.selectAll("div")
.data(monthData)
.join("div")
.classed("month-header1",(d,i,nl)=>i===0 && nl.length<=16)
.classed("month-header2",(d,i,nl)=>i===0 && nl.length>16)
.classed("day",(d,i)=>i>0 && d)
.classed("weekend",d => d instanceof Date && [0,6].indexOf(d.getUTCDay())>-1)
.html(dayData);
}
document.addEventListener("load",makeCalendar());
</script>
<link rel="stylesheet" href="style/calendar-mod.css"/>
</head>
<body>
<div id="pocketmod" class="pocketmod">
</div>
</body>
</html>