Source: yearsignaturedisplay.js

/**
 * @file Milesian Year Signature Display routines.
 * This package displays annual figures, in relation with yearsignaturedisplay.html.
 * imported routines are accessed through object 'modules'.
 * Only a few implementation comments are given here, since this code is mainly for demonstration purposes.
 * @see YearSignatureDisplay.html
 * @version M2021-08-O6
 * @author Louis A. de Fouquières https://github.com/Louis-Aime
 * @license MIT 2016-2022 
*/
//	Character set is UTF-8
/* Version	M2022-02-10	Adapt to seasons and lunar modules
	M2022-1-07 JSDoc
	M2021-08-06	Display in different calendars / languages by using global level formatters
	M2021-08-07	Add dominical letter
	M2021-02-15	Use calendrical-javascript modules and application-specific modules
	M2021-01-24 Missing year field in year panel
	M2021-01-14 Tailor to language and time zone
	M2020-12-29	New signature routines
	M2020-02-01 : comput epact on 1 1m, use year range constraint for milesian epact
	M2020-01-12 : strict mode
	M2019-08-23: add seasons' computation
	M2019-07-27: update dependencies
	M2019-05-12: 
		Display day of week as a Date-formatted string, instead of a select field
		Display Roman date with an explicit month indication
	M2019-01-13: Suppress "Milesian" line, since Milesian figures are like Gregorian,
		and rework Easter date in milesian
	M2018-10-26: Enhance comments
	M2017-01-09: Display Gold number 1 to 19, not 0 to 18.
*/
/* 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, sublicense, and/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 noninfringement.
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";
// These routines complement general display routines. Modules are loaded in "main" program under the "modules" wrapper.
function romanDateFrom21March (offset) {
	return (offset <= 10) 
		? romanDateFormat.format(new Date (1800, 2, (offset + 21))) 
		: romanDateFormat.format(new Date (1800, 3, (offset - 10)));
}
function milesianDateFrom30_3m (offset) {
	return	(offset <= -30 ? "<1 3m" :
		(offset <= 0 ? (30+offset) + " 3m" :
		(offset <= 31 ? (offset) + " 4m" : 
		(offset <= 61 ? (offset - 31) + " 5m" :
		(offset <= 92 ? (offset - 61) + " 6m" :
		(offset <= 122 ? (offset - 92) + " 7m" :
		(offset <= 153 ? (offset - 122) + " 8m" :
		(offset <= 183 ? (offset -153) + " 9m" :
		(offset <= 214 ? (offset - 183) + " 10m" :
		(offset <= 244 ? (offset - 214) + " 11m" :
		(offset <= 274 ? (offset - 244) + " 12m" : ">30 12m" ))))))))))) ;
}
/* A "day-month" display function. Display DayMonth located offset day after 21 March, in any language and calendar. 
	If calendar is not provided, milesian is used.
*/
function displayDateFrom21March (offset) {
	if (offset <= -30) return "<----";
	if (offset > 274) return "---->";
	let displayDate = new modules.ExtDate ("iso8601", 2000, 3, 21, 12);
	displayDate.setDate(displayDate.day() + offset); 
	return lunarDateFormat.format (displayDate);
}
function displayDOW (Day) { // Yield the string of the day of the week of rank Day. This one is limited to built-in calendar (traditional week)
	return DOWFormat.format (new modules.ExtDate ("iso8601", 1970, 1, 4+Day))
}
/* function displayYeartype (type) {
	var yearTypes = ["cave (et commune)", "longue (et commune)", "bissextile (et cave)"]
	return yearTypes [type];
}*/
function computeSignature(year) {	// Formaters are external
	// Begin with common and Julian calendar figures
	var signature = modules.julianSignature (year), m_signature = modules.milesianSignature (year);
	// The specified year
	document.yearglobal.year.value = year;
	// Set gold number
	document.yearglobal.gold.value = signature.goldNumber;
	// Julian figures
	document.details.j_type.value = signature.isLeap ? "bissextile" : "commune";
	document.details.j_day.value = displayDOW (signature.doomsday);
	document.details.j_dlet.value = signature.dominicalLetter;
	document.details.j_epact.value = signature.epact;
	document.details.j_residue.value = signature.easterResidue;
	document.details.j_daynumber.value = signature.easterOffset;
	document.details.j_romandate.value = romanDateFrom21March(signature.easterOffset);
	document.details.j_referdate.value = displayDateFrom21March (signature.easterOffset -2 +Math.floor(year/100) -Math.floor(year/400)); 
			//milesianDateFrom30_3m(signature.easterOffset -2 +Math.floor(year/100) -Math.floor(year/400));
	// Gregorian and Milesian figures
	signature = modules.gregorianSignature (year);
	document.details.g_type.value = signature.isLeap ? "bissextile" : "commune";
	document.details.m_type.value = m_signature.isLeap ? "abondante" : "cave" ;
	document.details.g_day.value = displayDOW (signature.doomsday);
	document.details.g_dlet.value = signature.dominicalLetter;
	document.details.g_epact.value = signature.epact;
	document.details.m_epact.value = m_signature.epact
	document.details.g_residue.value = signature.easterResidue;
	document.details.g_daynumber.value = signature.easterOffset;
	document.details.g_romandate.value = romanDateFrom21March(signature.easterOffset);
	document.details.g_referdate.value = displayDateFrom21March (signature.easterOffset)
			// milesianDateFrom30_3m(signature.easterOffset);
	// Seasons
	document.getElementById ("seasonsyear").innerHTML = year;
	try {
		document.seasons.winter1.value = seasonDateFormat.format (modules.tropicEvent(year,0)); 
		document.seasons.spring.value = seasonDateFormat.format (modules.tropicEvent(year,1)); 
		document.seasons.summer.value = seasonDateFormat.format (modules.tropicEvent(year,2)); 
		document.seasons.autumn.value = seasonDateFormat.format (modules.tropicEvent(year,3)); 
		document.seasons.winter2.value = seasonDateFormat.format (modules.tropicEvent(year,4)); 
	}
	catch (e) {		// seasons could not be computed, main reason is: year out of computational range
		document.seasons.winter1.value = 
		document.seasons.spring.value = 
		document.seasons.summer.value = 
		document.seasons.autumn.value = 
		document.seasons.winter2.value = "-- -- -- -- --";
	}
}
function setYear(year) {
	year = Math.round(year);	// Force to integer value
	if (isNaN (year)) alert ("non Integer: " + document.yearglobal.year.value)
	else {
		document.yearglobal.year.value = +year;
		computeSignature (+year);
		}
}	
function setYearOffset(shift) {
	shift = Math.round(shift);	// Force to integer value
	let year = Math.round(document.yearglobal.year.value) + Math.round(shift);
	if (year == NaN) alert ("non Integer: " + '"' + document.yearglobal.shift.value + '" "' + document.yearglobal.year.value + '"')
	else if (year < -271820 || year > 275760) alert ("Year out of range")
	else { 
		document.yearglobal.year.value = +year;
		// targetDate.setFromFields ({ year: year},"UTC");
		computeSignature (+year);
		}
}
function setYearToNow(myCalendar=milesian){ // Self explanatory
    targetDate = new modules.ExtDate(myCalendar); 
	// set to the middle of the year
	targetDate.setUTCHours (0, 0, 0, 0);
	targetDate.setFromFields ( { day: 1, month: 7 } ,"UTC"); 
	setYear(targetDate.fullYear());
}