Jump to content

MediaWiki:Gadget-grTimeline.js

From Garmin Wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Garmin software-update timeline — self-contained renderer (no libraries).
 * Renders <div class="gr-timeline" data-platform="Fenix 7"></div> from the
 * per-platform wiki data page MediaWiki:GarminTimeline-<slug>. Maintained by the
 * bot; no WordPress involved.
 *  - Always horizontal. Year labels live in a fixed left gutter; the lanes sit
 *    in a horizontal-scroll area, so scrolling clips cleanly (no overlap).
 *  - On a narrow viewport the year does not fit, so the track opens scrolled
 *    to the latest release (earlier months are a swipe away).
 *  - Focal zoom: pinch (touch), Ctrl/Cmd + wheel (desktop) zoom about the
 *    pointer; the +/-/Fit buttons zoom about the viewport centre.
 *  - Round HTML dots; colours use MediaWiki Codex theme tokens (dark/light). */
( function () {
	'use strict';
	var MONTHS = [ 'Jan', '', 'Mar', '', 'May', '', 'Jul', '', 'Sep', '', 'Nov', '' ];
	var MON3 = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
	var cache = {};
	var tipEl = null;

	function slugify( s ) { return s.replace( /[^A-Za-z0-9]+/g, '-' ); }

	function loadPlatform( platform, node ) {
		if ( cache[ platform ] ) { return cache[ platform ]; }
		var jsonUrl = node && node.getAttribute( 'data-json' );
		var source = ( node && node.getAttribute( 'data-source' ) ) || '';
		var url;
		if ( jsonUrl || source === 'wp' ) {
			url = jsonUrl || ( ( window.grTimelineWp && window.grTimelineWp.rest ) || '' ) + encodeURIComponent( slugify( platform ) );
		} else {
			url = mw.config.get( 'wgScript' ) + '?title=' +
				encodeURIComponent( 'MediaWiki:GarminTimeline-' + slugify( platform ) ) +
				'&action=raw&ctype=application/json&maxage=0&smaxage=0';
		}
		cache[ platform ] = fetch( url, { credentials: 'same-origin', cache: 'no-cache' } ).then( function ( r ) {
			if ( !r.ok ) { throw new Error( 'timeline ' + r.status ); }
			return r.json();
		} );
		return cache[ platform ];
	}

	function parseDate( s ) {
		var d = new Date( s + 'T00:00:00' );
		return isNaN( d.getTime() ) ? null : d;
	}

	function yearFrac( d ) {
		var y = d.getFullYear();
		var start = new Date( y, 0, 1 ).getTime();
		var end = new Date( y + 1, 0, 1 ).getTime();
		return ( d.getTime() - start ) / ( end - start );
	}

	function niceDate( s ) {
		var d = parseDate( s );
		return d ? ( MON3[ d.getMonth() ] + ' ' + d.getDate() + ', ' + d.getFullYear() ) : s;
	}

	function el( tag, cls, txt ) {
		var e = document.createElement( tag );
		if ( cls ) { e.className = cls; }
		if ( txt != null ) { e.textContent = txt; }
		return e;
	}

	function kind( u ) { return u.t === 'stable' ? 'stable' : 'beta'; }
	function kindLabel( u ) {
		if ( u.t === 'stable' ) { return 'Stable'; }
		return u.t === 'alpha' ? 'Alpha' : 'Beta';
	}

	function wikiUrl( platform, anchor ) {
		var title = ( platform + ' Software Updates' ).replace( / /g, '_' );
		var url;
		if ( window.mw && mw.config && mw.config.get ) {
			var base = mw.config.get( 'wgArticlePath' ) || ( mw.config.get( 'wgScript' ) + '?title=$1' );
			url = base.replace( '$1', encodeURIComponent( title ).replace( /%2F/g, '/' ) );
		} else {
			var wikiBase = ( window.grTimelineWp && window.grTimelineWp.wikiBase ) || 'https://wiki.garminrumors.com/';
			url = wikiBase.replace( /\/?$/, '/' ) + title;
		}
		return anchor ? url + '#' + anchor : url;
	}

	/* ---- hover/focus tooltip ---- */
	function tipNode() {
		if ( !tipEl ) { tipEl = el( 'div', 'grtl-tip' ); document.body.appendChild( tipEl ); }
		return tipEl;
	}
	function showTip( anchor, u ) {
		var t = tipNode();
		t.className = 'grtl-tip grtl-' + kind( u );
		t.textContent = '';
		var row = el( 'div', 'grtl-tip-row' );
		row.appendChild( el( 'span', 'grtl-pill grtl-' + kind( u ), kindLabel( u ) ) );
		row.appendChild( el( 'span', 'grtl-tip-ver', 'v' + u.v ) );
		t.appendChild( row );
		t.appendChild( el( 'div', 'grtl-tip-date', niceDate( u.d ) ) );
		var r = anchor.getBoundingClientRect();
		t.style.left = ( r.left + r.width / 2 ) + 'px';
		t.style.top = ( r.top - 10 ) + 'px';
		t.classList.add( 'grtl-show' );
	}
	function hideTip() { if ( tipEl ) { tipEl.classList.remove( 'grtl-show' ); } }

	/* ---- details modal ---- */
	function openModal( u, platform ) {
		hideTip();
		var overlay = el( 'div', 'grtl-overlay' );
		var modal = el( 'div', 'grtl-modal grtl-' + kind( u ) );

		var head = el( 'div', 'grtl-mhead' );
		var close = el( 'button', 'grtl-close', '\u00d7' );
		close.type = 'button';
		close.setAttribute( 'aria-label', 'Close' );
		head.appendChild( close );
		var top = el( 'div', 'grtl-mtop' );
		top.appendChild( el( 'span', 'grtl-mver', 'v' + u.v ) );
		top.appendChild( el( 'span', 'grtl-pill grtl-' + kind( u ), kindLabel( u ) ) );
		head.appendChild( top );
		head.appendChild( el( 'div', 'grtl-msub', platform + ' \u00b7 ' + niceDate( u.d ) ) );
		modal.appendChild( head );

		var body = el( 'div', 'grtl-mbody' );
		if ( u.c && u.c.length ) {
			var ul = null;
			u.c.forEach( function ( line ) {
				line = ( line || '' ).trim();
				if ( !line ) { return; }
				if ( line.charAt( line.length - 1 ) === ':' ) {
					body.appendChild( el( 'div', 'grtl-sub', line ) );
					ul = null;
				} else {
					if ( !ul ) { ul = el( 'ul' ); body.appendChild( ul ); }
					ul.appendChild( el( 'li', null, line ) );
				}
			} );
		} else {
			body.appendChild( el( 'p', 'grtl-empty', 'No changelog recorded for this build.' ) );
		}
		modal.appendChild( body );

		var foot = el( 'div', 'grtl-mfoot' );
		var a = el( 'a', 'grtl-btn', 'View full release notes on the wiki' );
		a.href = wikiUrl( platform, u.w );
		foot.appendChild( a );
		modal.appendChild( foot );

		overlay.appendChild( modal );
		document.body.appendChild( overlay );

		function done() { overlay.remove(); document.removeEventListener( 'keydown', onKey ); }
		function onKey( e ) { if ( e.key === 'Escape' ) { done(); } }
		close.addEventListener( 'click', done );
		a.addEventListener( 'click', done );
		overlay.addEventListener( 'click', function ( e ) { if ( e.target === overlay ) { done(); } } );
		document.addEventListener( 'keydown', onKey );
	}

	/* ---- focal zoom ---- */
	function setupZoom( box, scroll, track, wrap, onChange ) {
		var z = 1, MIN = 1, MAX = 6;
		var minus = el( 'button', 'grtl-zbtn', '\u2212' );
		var fit = el( 'button', 'grtl-zbtn grtl-zfit', 'Fit' );
		var plus = el( 'button', 'grtl-zbtn', '+' );
		minus.type = fit.type = plus.type = 'button';
		minus.setAttribute( 'aria-label', 'Zoom out' );
		plus.setAttribute( 'aria-label', 'Zoom in' );
		fit.setAttribute( 'aria-label', 'Reset zoom' );
		wrap.appendChild( minus ); wrap.appendChild( fit ); wrap.appendChild( plus );

		function refresh() {
			fit.textContent = z <= 1.001 ? 'Fit' : ( Math.round( z * 10 ) / 10 ) + '\u00d7';
			minus.disabled = z <= MIN;
			plus.disabled = z >= MAX;
		}
		function applyAt( nz, focalX ) {
			nz = Math.min( MAX, Math.max( MIN, nz ) );
			var rect = scroll.getBoundingClientRect();
			var fx = ( focalX == null ) ? rect.left + rect.width / 2 : focalX;
			var before = track.getBoundingClientRect().width;
			var ratio = before > 0 ? ( scroll.scrollLeft + ( fx - rect.left ) ) / before : 0;
			z = Math.round( nz * 100 ) / 100;
			box.style.setProperty( '--grtl-zoom', z );
			var after = track.getBoundingClientRect().width;
			scroll.scrollLeft = ratio * after - ( fx - rect.left );
			refresh();
			if ( onChange ) { onChange(); }
		}
		minus.addEventListener( 'click', function () { applyAt( z - 0.5, null ); } );
		plus.addEventListener( 'click', function () { applyAt( z + 0.5, null ); } );
		fit.addEventListener( 'click', function () {
			applyAt( 1, null );
			if ( onChange ) { onChange( 'fit' ); }
		} );

		scroll.addEventListener( 'wheel', function ( e ) {
			if ( e.ctrlKey || e.metaKey ) {
				e.preventDefault();
				applyAt( z * ( e.deltaY < 0 ? 1.12 : 0.89 ), e.clientX );
			}
		}, { passive: false } );

		var pinch = null;
		function tdist( t ) { return Math.hypot( t[ 0 ].clientX - t[ 1 ].clientX, t[ 0 ].clientY - t[ 1 ].clientY ); }
		function tmid( t ) { return ( t[ 0 ].clientX + t[ 1 ].clientX ) / 2; }
		scroll.addEventListener( 'touchstart', function ( e ) {
			if ( e.touches.length === 2 ) { pinch = { d: tdist( e.touches ), z: z }; }
		}, { passive: true } );
		scroll.addEventListener( 'touchmove', function ( e ) {
			if ( pinch && e.touches.length === 2 ) {
				e.preventDefault();
				applyAt( pinch.z * tdist( e.touches ) / pinch.d, tmid( e.touches ) );
			}
		}, { passive: false } );
		scroll.addEventListener( 'touchend', function ( e ) { if ( e.touches.length < 2 ) { pinch = null; } } );

		refresh();
	}

	function buildLane( items, platform ) {
		var lane = el( 'div', 'grtl-lane' );
		items.forEach( function ( u ) {
			var dot = el( 'button', 'grtl-dot grtl-' + kind( u ) );
			dot.type = 'button';
			dot.style.left = ( u._frac * 100 ) + '%';
			dot.setAttribute( 'aria-label', 'v' + u.v + ' ' + kindLabel( u ) + ' ' + u.d );
			( function ( upd, d ) {
				d.addEventListener( 'mouseenter', function () { showTip( d, upd ); } );
				d.addEventListener( 'mouseleave', hideTip );
				d.addEventListener( 'focus', function () { showTip( d, upd ); } );
				d.addEventListener( 'blur', hideTip );
				d.addEventListener( 'click', function () { openModal( upd, platform ); } );
			}( u, dot ) );
			lane.appendChild( dot );
		} );
		var months = el( 'div', 'grtl-months' );
		MONTHS.forEach( function ( m, i ) {
			if ( !m ) { return; }
			var s = el( 'span', 'grtl-mlabel', m );
			s.style.left = ( ( i / 12 ) * 100 ) + '%';
			months.appendChild( s );
		} );
		lane.appendChild( months );
		return lane;
	}

	function render( node, platform, updates ) {
		node.textContent = '';
		var box = el( 'div', 'grtl-box' );

		var byYear = {};
		updates.forEach( function ( u ) {
			var d = parseDate( u.d );
			if ( !d ) { return; }
			u._frac = yearFrac( d );
			u._t = d.getTime();
			( byYear[ d.getFullYear() ] = byYear[ d.getFullYear() ] || [] ).push( u );
		} );
		var years = Object.keys( byYear ).map( Number ).sort( function ( a, b ) { return b - a; } );
		years.forEach( function ( y ) { byYear[ y ].sort( function ( a, b ) { return a._t - b._t; } ); } );

		var head = el( 'div', 'grtl-head' );
		var leg = el( 'div', 'grtl-legend' );
		var l1 = el( 'span', 'grtl-leg' );
		l1.appendChild( el( 'span', 'grtl-swatch grtl-stable' ) );
		l1.appendChild( el( 'span', null, 'Stable' ) );
		var l2 = el( 'span', 'grtl-leg' );
		l2.appendChild( el( 'span', 'grtl-swatch grtl-beta' ) );
		l2.appendChild( el( 'span', null, 'Beta' ) );
		leg.appendChild( l1 ); leg.appendChild( l2 );
		head.appendChild( leg );
		var zoomWrap = el( 'div', 'grtl-zoom' );
		head.appendChild( zoomWrap );
		box.appendChild( head );

		if ( !years.length ) {
			box.appendChild( el( 'p', 'grtl-empty', 'No dated updates for ' + platform + '.' ) );
			node.appendChild( box );
			return;
		}

		var bodyEl = el( 'div', 'grtl-body' );
		var gutter = el( 'div', 'grtl-gutter' );
		var scroll = el( 'div', 'grtl-scroll' );
		var track = el( 'div', 'grtl-track' );
		years.forEach( function ( y ) {
			gutter.appendChild( el( 'div', 'grtl-gcell', String( y ) ) );
			track.appendChild( buildLane( byYear[ y ], platform ) );
		} );
		scroll.appendChild( track );
		scroll.addEventListener( 'scroll', hideTip );
		bodyEl.appendChild( gutter );
		bodyEl.appendChild( scroll );
		box.appendChild( bodyEl );

		var foot = el( 'div', 'grtl-foot' );
		foot.appendChild( el( 'span', null, updates.length + ' updates \u00b7 ' + platform ) );
		var hint = el( 'span', 'grtl-foot-hint', '\u00b7 scroll sideways for earlier months' );
		foot.appendChild( hint );
		box.appendChild( foot );
		node.appendChild( box );

		function latestFrac() {
			var bestT = 0, frac = 1;
			years.forEach( function ( y ) {
				byYear[ y ].forEach( function ( u ) {
					if ( u._t >= bestT ) { bestT = u._t; frac = u._frac; }
				} );
			} );
			return frac;
		}
		function scrollToLatest() {
			var W = track.getBoundingClientRect().width;
			var V = scroll.clientWidth;
			if ( !( W > V + 4 ) ) { return; }
			// Put the newest release ~80% across the viewport so recent months
			// are in view; earlier months stay a swipe away.
			var target = latestFrac() * W - V * 0.8;
			scroll.scrollLeft = Math.max( 0, Math.min( W - V, target ) );
		}
		function updateHint( why ) {
			// Compare the (zoomable) track width to the viewport rather than
			// scrollWidth, which is inflated a constant ~50px by edge dots.
			var over = track.getBoundingClientRect().width > scroll.clientWidth + 4;
			hint.style.display = over ? '' : 'none';
			if ( why === 'fit' ) { scrollToLatest(); }
		}
		setupZoom( box, scroll, track, zoomWrap, updateHint );
		function afterLayout() {
			updateHint();
			scrollToLatest();
		}
		if ( window.requestAnimationFrame ) {
			requestAnimationFrame( function () { requestAnimationFrame( afterLayout ); } );
		} else { afterLayout(); }
		window.addEventListener( 'resize', updateHint );
	}

	function init( root ) {
		var nodes = ( root || document ).querySelectorAll( '.gr-timeline:not(.grtl-ready)' );
		nodes.forEach( function ( n ) {
			n.classList.add( 'grtl-ready' );
			n.textContent = 'Loading update timeline\u2026';
			var plat = n.getAttribute( 'data-platform' );
			loadPlatform( plat, n ).then( function ( arr ) {
				render( n, plat, arr || [] );
			} ).catch( function () { n.textContent = 'Timeline failed to load.'; } );
		} );
	}

	function boot() { init( document ); }
	if ( window.mw && mw.hook ) { mw.hook( 'wikipage.content' ).add( function ( $c ) { init( $c && $c[ 0 ] ); } ); }
	if ( window.jQuery ) { jQuery( boot ); } else if ( document.readyState !== 'loading' ) { boot(); } else { document.addEventListener( 'DOMContentLoaded', boot ); }
	setTimeout( boot, 400 );
}() );
Garmin Wiki
A project by GarminRumors.com