Home / Admin / Sugar Calendar: Mark today in the month calendar
Duplicate Snippet

Embed Snippet on Your Site

Sugar Calendar: Mark today in the month calendar

<10
Code Preview
js
// Sugar Calendar: mark today in the month calendar block.
// WPCode: JavaScript Snippet, Auto Insert, Site Wide Footer (the header works too).
( function () {
	// Styles for the dot (added once, so this works as a single WPCode JavaScript snippet).
	if ( ! document.getElementById( "sctd-today-dot-css" ) ) {
		var style = document.createElement( "style" );
		style.id = "sctd-today-dot-css";
		style.textContent =
		".sugar-calendar-block__calendar-month__body__day.sctd-today .sugar-calendar-block__calendar-month__body__day__number {\n" +
		"	position: relative;\n" +
		"	font-weight: 600;\n" +
		"}\n" +
		".sugar-calendar-block__calendar-month__body__day.sctd-today .sugar-calendar-block__calendar-month__body__day__number::before {\n" +
		"	content: \"\";\n" +
		"	position: absolute;\n" +
		"	top: -6px;\n" +
		"	left: 50%;\n" +
		"	width: 5px;\n" +
		"	height: 5px;\n" +
		"	border-radius: 50%;\n" +
		"	background: var(--sctd-color, #2271b1);\n" +
		"	transform: translateX(-50%);\n" +
		"}\n";
		document.head.appendChild( style );
	}
	// Your site timezone, as in Settings » General (an IANA name like "Europe/Stockholm").
	// Leave it empty ("") to use each visitor's own date instead.
	var TZ = "Europe/Stockholm";
	// Today in that timezone, worked out live in the browser so cached pages stay right.
	function today() {
		var now = new Date();
		if ( TZ ) {
			try {
				var parts = new Intl.DateTimeFormat( "en-US", { timeZone: TZ, year: "numeric", month: "numeric", day: "numeric" } ).formatToParts( now );
				var get   = function ( t ) { return parseInt( parts.find( function ( p ) { return p.type === t; } ).value, 10 ); };
				return { y: get( "year" ), m: get( "month" ), d: get( "day" ) };
			} catch ( e ) {} // Unknown timezone name: fall back to the visitor's date.
		}
		return { y: now.getFullYear(), m: now.getMonth() + 1, d: now.getDate() };
	}
	function markToday( monthEl ) {
		// The block keeps the month being viewed in hidden form fields, updated on every navigation.
		var block = monthEl.closest( ".sugar-calendar-block" ) || document;
		var mIn   = block.querySelector( "input[name=sc_month]" );
		var yIn   = block.querySelector( "input[name=sc_year]" );
		if ( ! mIn || ! yIn ) {
			return;
		}
		var t = today();
		if ( block.dataset && block.dataset.accentcolor ) {
			monthEl.style.setProperty( "--sctd-color", block.dataset.accentcolor );
		}
		monthEl.querySelectorAll( ".sugar-calendar-block__calendar-month__body__day" ).forEach( function ( cell ) {
			// Days from the neighbouring months are "offset" cells; never mark those.
			var isViewedMonth = ! cell.classList.contains( "sugar-calendar-block__calendar-month__body__day-offset" );
			var numEl         = cell.querySelector( ".sugar-calendar-block__calendar-month__body__day__number" );
			var isToday       = isViewedMonth && numEl &&
				parseInt( yIn.value, 10 ) === t.y &&
				parseInt( mIn.value, 10 ) === t.m &&
				parseInt( numEl.textContent, 10 ) === t.d;
			cell.classList.toggle( "sctd-today", !! isToday );
		} );
	}
	function run() {
		document.querySelectorAll( ".sugar-calendar-block__calendar-month" ).forEach( markToday );
	}
	function start() {
		// Month navigation re-renders the grid over AJAX, so re-apply whenever the DOM changes.
		var queued = false;
		new MutationObserver( function () {
			if ( queued ) {
				return;
			}
			queued = true;
			setTimeout( function () {
				queued = false;
				run();
			}, 0 );
		} ).observe( document.body, { childList: true, subtree: true } );
		run();
	}
	// Works in the header or the footer: wait for the page if it isn't there yet.
	if ( document.body ) {
		start();
	} else {
		document.addEventListener( "DOMContentLoaded", start );
	}
} )();

Comments

Add a Comment