-
Notifications
You must be signed in to change notification settings - Fork 1
/
milesianclockonload.js
295 lines (273 loc) · 14.5 KB
/
milesianclockonload.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/** Milesian Clock functions, onload part.
* These functions are associated with the Milesian clock html page:
* They use the basic Milesian calendar functions, and the conversion functions of other calendar,
* in order to display the Milesian on-line clock and to perform calendar conversion.
* This code is strongly tight with milesianclock.html.
* Here the general framework and the event listeners are set.
* Only a few implementation comments are given here, since this code is mainly for demonstration purposes.
* @file
* @version M2022-11-10
* @author Louis A. de Fouquières https://github.com/Louis-Aime
* @license MIT 2016-2022
* @see milesianclock.html
*/
// Character set is UTF-8.
/* Version M2022-11-10 Updated calendar names
*/
/* Copyright Louis A. de Fouquières https://github.com/Louis-Aime 2016-2022
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sub-license, or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
1. The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
2. Changes with respect to any former version shall be documented.
The software is provided "as is", without warranty of any kind,
express of implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose and non-infringement.
In no event shall the authors of copyright holders be liable for any
claim, damages or other liability, whether in an action of contract,
tort or otherwise, arising from, out of or in connection with the software
or the use or other dealings in the software.
*/
"use strict";
var
jdcounterselector = "julianDay"; // for use with ExtCountDate; not a const to avoid throw when changing page.
var
milesian, // the Milesian calendar (plays a special role)
calendars = [], // an array (pointers to) calendar objects
targetDate = undefined, // new Date(), // Reference UTC date
customCalIndex = 0, // initialised and later changed.
switchingDate = { year : 1582, month : 10, day : 15 },
TZ = "", // time zone for specifying a date; "" means "system", only alternative value is "UTC".
TZOffset = 0,
// TZSettings : {mode : "", msoffset : 0}, // deprecate
Options = {weekday : "long", day : "numeric", month: "long", year : "numeric",
hour : "numeric", minute : "numeric", second : "numeric"},
askedOptions, // for custom calendar
userOptions, //
unicodeFormat, // for Unicode selected calendar
unicodeOptions,
clockFormat,
astroCalend,
weekFormat, // formater for day of week for current custom calendars
DOWFormat, // formater for the traditional week day, used for the yearly figures
romanDateFormat, // monthDay for julian and gregorian calendar
lunarDateFormat, // monthDay to indicate a general astronomical event
dracoDateFormat, // year month day
seasonDateFormat, // year month day hours minutes
milesianClock;
function compLocalePresentationCalendar() { // Manage date string display parameters. This function has to be changed as per milesian clock application.
var
Locale = document.LocaleOptions.Language.value,
Calendar = document.LocaleOptions.Calendar.value,
timeZone = document.LocaleOptions.TimeZone.value,
testDTF;
// Negotiate effective language code and display
// Test if passed language is OK
try {
testDTF = new modules.ExtDateTimeFormat (undef(Locale))
}
catch (e) {
alert (e);
document.LocaleOptions.Language.value = "";
return
}
Locale = testDTF.resolvedOptions().locale; // Here Locale is no longer an empty string
if (Locale.includes("-u-")) // The Unicode extension ("-u-") is indicated in the specified locale, drop it
Locale = Locale.substring (0,Locale.indexOf("-u-"));
// Set presentation Options from one of the standard presentations listed.
switch (document.LocaleOptions.DatePresentation.value) {
case "long":
Options = {weekday : "long", day : "numeric", month : "long", year : "numeric", era : "long", eraDisplay : "always"}; break;
case "standard":
Options = {weekday : "long", day : "numeric", month: "long", year : "numeric", era : "long", eraDisplay : "auto"}; break;
case "short":
Options = {weekday : "short", day : "numeric", month: "short", year : "numeric", era : "short"}; break;
case "narrow":
Options = {weekday : "narrow", day : "numeric", month: "narrow", year : "numeric", era : "narrow"}; break;
case "numeric":
Options = {day : "numeric", month : "numeric", year : "numeric", era : "narrow"}; break;
case "2-digit":
Options = {day : "2-digit", month : "2-digit", year : "numeric"}; break;
case "year-2-digit":
Options = {day : "2-digit", month : "2-digit", year : "2-digit", eraDisplay : "never"}; break;
case "none": Options = {}
}
switch (document.LocaleOptions.TimePresentation.value) {
case "total": Options.hour = "numeric"; Options.minute = "numeric"; Options.second = "numeric";
Options.fractionalSecond = 3; Options.timeZoneName = "short"; break;
case "detail": Options.hour = "numeric"; Options.minute = "numeric"; Options.second = "numeric";
Options.timeZoneName = "long"; break;
case "second": Options.hour = "numeric"; Options.minute = "numeric"; Options.second = "numeric"; break;
case "minute": Options.hour = "numeric"; Options.minute = "numeric"; break;
case "hour": Options.hour = "numeric"; break;
case "hmstz": Options.hour = "2-digit"; Options.minute = "2-digit"; Options.second = "2-digit";
Options.timeZoneName = "short"; break;
case "hm": Options.hour = "2-digit"; Options.minute = "2-digit"; break;
case "none":
}
// Add Unicode calendar in options
if (Calendar != "") Options.calendar = Calendar; // No error expected, since calendar is picked up from a list
// Add time zone in Options
if (timeZone != "")
Options.timeZone = timeZone ;
// is timeZone valid ?
try {
testDTF = new Intl.DateTimeFormat (Locale, Options);
}
catch (e) {
alert (e);
delete Options.timeZone; // Delete property
document.LocaleOptions.TimeZone.value = "";
return
}
// Formater for Milesian clock.
clockFormat = new modules.ExtDateTimeFormat (Locale,{timeZone: undef(TZ),weekday:"long",day:"numeric",month:"long",year:"numeric"},milesian);
// Compute formater for custom calendar
askedOptions = new modules.ExtDateTimeFormat (undef(Locale),Options,calendars[customCalIndex]);
userOptions = askedOptions.resolvedOptions();
// Compute formater for Unicode. Used once, but computed options are useful
unicodeFormat = new modules.ExtDateTimeFormat (undef(Locale),Options); // unicode calendar in Options.calendar
unicodeOptions = unicodeFormat.resolvedOptions();
weekFormat = new modules.ExtDateTimeFormat (undef (Locale), {weekday : "long", timeZone : undef(TZ)}, calendars[customCalIndex]);
// Select calendar for astronomical data
switch (document.LocaleOptions.astrocalendar.value) {
case undefined : case "milesian" : astroCalend = milesian; break;
case "custom" : astroCalend = calendars[customCalIndex]; break;
case "unicode" : astroCalend = unicodeOptions.calendar; break;
default : throw new RangeError ("Unexpected value for calendar choice option: " + document.LocaleOptions.astrocalendar.value);
}
// fix astro date formaters
DOWFormat = new modules.ExtDateTimeFormat (undef(Locale),
{ weekday : "long", calendar : "iso8601"});
romanDateFormat = new modules.ExtDateTimeFormat (undef(Locale),
{month : "short", day : "numeric", calendar : "iso8601" });
dracoDateFormat = new modules.ExtDateTimeFormat (undef(Locale),
{year : "numeric", month : "short", day : "numeric", timeZone : undef (TZ) }, astroCalend);
seasonDateFormat = new modules.ExtDateTimeFormat (undef(Locale),
{year : "numeric", month : "short", day : "numeric", hour : "numeric", minute : "numeric", timeZone : Options.timeZone }, astroCalend);
lunarDateFormat = new modules.ExtDateTimeFormat (undef(Locale),
{month : "short", day : "numeric", timeZone : undef (TZ) }, astroCalend);
}
window.onload = function () { // Initiate fields and set event listeners
TZ = document.TZmode.TZcontrol.value;
[ switchingDate.day, switchingDate.month, switchingDate.year ]
= [ document.gregorianswitch.day.value, document.gregorianswitch.month.value, document.gregorianswitch.year.value ];
loadComplete.then (() => {
milesian = new modules.MilesianCalendar ("milesian",pldrDOM);
calendars.push (milesian);
calendars.push (new modules.ProlepticGregorianCalendar ("iso_8601"));
calendars.push (new modules.JulianCalendar ("julian"));
calendars.push (new modules.GregorianCalendar ("gregorian", modules.ExtDate.fullUTC(switchingDate.year, switchingDate.month, switchingDate.day), pldrDOM));
calendars.push (new modules.FrenchRevCalendar ("frenchrev",pldrDOM));
customCalIndex = calendars.findIndex (item => item.id == document.custom.calend.value); // set initial custom calendar - but calendars must exist !
// targetDate = new modules.ExtDate(milesian);
milesianClock = new modules.SolarClock( document.querySelector("#mclock") );
compLocalePresentationCalendar();
clockAnimate.clockRun(1); // start clock
});
document.gregorianswitch.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
let
day = Math.round (event.srcElement.elements.day.value),
month = event.srcElement.elements.month.value,
year = Math.round (event.srcElement.elements.year.value),
testDate = new modules.ExtDate (calendars.find(item => item.id == "iso_8601"),modules.ExtDate.fullUTC(year, month, day)),
index = calendars.findIndex (item => item.id == "gregorian");
if ( (testDate.valueOf() >= Date.UTC(1582,9,15,0,0,0,0)) && (testDate.day() == day) )
calendars[index] = new modules.GregorianCalendar("gregorian", testDate.valueOf(), pldrDOM)
else alert ("Invalid switching date to Gregorian calendar: " + day + '/' + month + '/' + year );
// confirm current switching date
[document.gregorianswitch.day.value, document.gregorianswitch.month.value, document.gregorianswitch.year.value ]
= [ switchingDate.day, switchingDate.month, switchingDate.year ] = [ day, month, year ];
compLocalePresentationCalendar(); // because we changed one calendar, disseminate change.
if (calendars[customCalIndex].id == "gregorian") targetDate = new modules.ExtDate (calendars[index], targetDate.valueOf());
// sweep former gregorian calendar out of current data
setDisplay();
})
document.custom.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
calcCustomDate()
})
document.custom.calend.addEventListener("blur", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
customCalIndex = calendars.findIndex (item => item.id == document.custom.calend.value); // change custom calendar
targetDate = new modules.ExtDate(calendars[customCalIndex], targetDate.valueOf()); // set custom calendar if changed, and set date.
compLocalePresentationCalendar();
setDisplay();
})
document.week.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
calcWeek()
})
document.daycounter.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
calcJulianDay()
})
document.control.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.changeDayOffset()
})
document.control.now.addEventListener("click", function (event) {
clockAnimate.clockRun(0);
setDateToNow()
})
document.control.minus.addEventListener("click", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
clockAnimate.changeDayOffset()
clockAnimate.setDayOffset(-1)
})
document.control.plus.addEventListener("click", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
clockAnimate.changeDayOffset()
clockAnimate.setDayOffset(+1)
})
document.time.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
calcTime()
})
document.timeShift.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
myTimeShift.changeAddTime()
})
document.timeShift.minus.addEventListener("click", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
myTimeShift.changeAddTime()
myTimeShift.addTime(-1)
})
document.timeShift.plus.addEventListener("click", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
clockAnimate.clockRun(0);
myTimeShift.changeAddTime()
myTimeShift.addTime(+1)
})
document.TZmode.addEventListener("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
TZ = document.TZmode.TZcontrol.value;
setDisplay();
})
document.getElementById("h0").addEventListener("click", function (event) {
clockAnimate.clockRun(0);
setUTCHoursFixed(0)
})
document.getElementById("h12").addEventListener("click", function (event) {
clockAnimate.clockRun(0);
setUTCHoursFixed(12)
})
document.LocaleOptions.addEventListener ("submit", function (event) {
event.preventDefault(); // necessary to avoid re-loading with multi-fields forms especially when fields are fetched from event.
compLocalePresentationCalendar();
setDisplay()
})
}