MediaWiki:Common.js

From Leaving The Cradle Wiki
Revision as of 07:41, 17 December 2023 by Darth Biomech (talk | contribs)
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */


	var calculatorBody = document.getElementById('mw-time-calculator');
	var timelineparent = document.getElementById('timeline');


	if (calculatorBody)	
	{
		setTimeout( CreateTimeCalculator, 1000 );
	}	
	
	if (timelineparent)	
	{
		setTimeout( CreateTimeline, 900 );
	}	


function CreateTimeline() {
    timelineparent = document.getElementById('timeline');
	if (timelineparent)	
	{
		var smallestTime = 0;
		var biggesttime=0;
		var scale = Number(timelineparent.getAttribute("data-scale"));
		if (scale <=0 || scale == NaN){scale = 1}
		var log = timelineparent.getAttribute("data-log");
		var timelinePoints = timelineparent.getElementsByTagName("UL")[0].getElementsByTagName("li");
		var el = document.createElement('div');
		el.setAttribute('style', 'height: 80px;width: 100%; overflow-x: scroll;margin-bottom: 10pt;');
		var timelineBody = document.createElement('div');
		timelineBody.setAttribute('class', 'timeline-body');
		var dates = [];
		var expungedDates = [];
		var numzero = 0;
		for (i = 0; i < timelinePoints.length; i++) 
		{
			var curPoint = timelinePoints[i];
			curPoint.setAttribute('id','timeline-event-'+i);
			var number = curPoint.getElementsByTagName("B")[0].innerHTML.split("-")[0];
			if (number.includes('~'))
			{
			expungedDates.push(i);
			number = Number(number.replace(/\D/g,''));
			dates.push(number);	
			continue;
			}
			number = Number(number.replace(/\D/g,''));
			if (number >= biggesttime){biggesttime = number;}
			if (number == 0 && biggesttime > 0)
			{
				smallestTime = biggesttime;
				biggesttime = 0;
				numzero = i;
			}
			dates.push(number);			
		}
		var dateSwitch = 1;
		var prevwidth = 100;
		
		for (i = 0; i < dates.length; i++) 
		{
			var curdate=dates[i];
			if (expungedDates.includes(i))
			{
				continue;
			}
			if (i < numzero) 
				curdate = -curdate
			var dateText = timelinePoints[i].innerHTML
			dateText = dateText.replace(/(<b>).+(<\/b>)/i,'');
			dateText = dateText.replace(/(<a).+?(>)/ig,'');
			dateText = dateText.replace(/(<).+?(>)/ig,'');
			var link = document.createElement('a');
			link.setAttribute('href', '#timeline-event-'+i);
			var shortened = '';
			if (dateText.length >199)
				shortened = '...';
			link.setAttribute('title',curdate+': '+dateText.substring(0, 200)+shortened);
			link.setAttribute('class', 'timeline-event-marker');
			if (i == numzero) 
			{
				link.setAttribute('class', 'timeline-event-marker timeline-event-marker-groundzero');
			}
			link.innerHTML = '';
			var curdatewidth = (100/(smallestTime+biggesttime))*(curdate+smallestTime);
			
			if ((curdatewidth - prevwidth)/scale < 3)
				dateSwitch = -dateSwitch + 1;
			else 
				dateSwitch = 0;
			var historyevent = document.createElement('div');
			historyevent.setAttribute('style', 'height:0px;width:0px;position:absolute;top:-6pt;left:'+(curdatewidth)+'%;');
			historyevent.setAttribute('class', 'timeline-event');
			
			var eventtime = document.createElement('div');
			
			eventtime.setAttribute('style', 'height:0px;width:0px;position:absolute;top:20pt;flex-shrink: 0;display: flex;justify-content: center;align-items: center;');
			if (dateSwitch>0){
				eventtime.setAttribute('style', 'height:0px;width:0px;position:absolute;top:-4pt;flex-shrink: 0;display: flex;justify-content: center;align-items: center;');	
			}
			eventtime.innerHTML = curdate;
			link.appendChild(eventtime);
			historyevent.appendChild(link);
			timelineBody.appendChild(historyevent);
			prevwidth = Math.floor(curdatewidth);
		}
		
		timelineBody.setAttribute('style', 'height: 6pt;width: '+(smallestTime+biggesttime)*scale+'px;position: relative;margin-left: 20pt;margin-top: 15pt;');
		el.appendChild(timelineBody);
		timelineparent.insertBefore(el, timelineparent.firstChild);
		
	}
}

function CreateTimeCalculator(){
    calculatorBody = document.getElementById('mw-time-calculator');
	if (calculatorBody)
	{
		
		var earthDate = document.getElementById('mw-time-calculator-h');
		var timeEarthY = CreateTimeElement('t-earth-y', 2015);
		earthDate.appendChild(timeEarthY);
		
		var raharrDate = document.getElementById('mw-time-calculator-r');
		var timeRaharrY = CreateTimeElement('t-raharr-y', 285);		
		raharrDate.appendChild(timeRaharrY);
		
		var standardDate = document.getElementById('mw-time-calculator-s');		
		var timeStdY = CreateTimeElement('t-standard-y', 285);	
		standardDate.appendChild(timeStdY);
		

		
		timeEarthY.addEventListener("input", function () 
		{
			timeStdY.value = Math.round(EarthToStd(timeEarthY.value)*100)/100;
			timeRaharrY.value = Math.round(StdToRaharr(EarthToStd(timeEarthY.value))*100)/100;
		});
		timeRaharrY.addEventListener("input", function () 
		{
			timeStdY.value = Math.round(RaharrToStd(timeRaharrY.value)*100)/100;
			timeEarthY.value = Math.round(StdToEarth(RaharrToStd(timeRaharrY.value))*100)/100;
		});
		timeStdY.addEventListener("input", function () 
		{
			timeEarthY.value = Math.round(StdToEarth(timeStdY.value)*100)/100;
			timeRaharrY.value = Math.round(StdToRaharr(timeStdY.value)*100)/100;
		});
		timeEarthY.value = new Date().getFullYear();
		timeEarthY.dispatchEvent(new Event('input'));		
	}	
}

function CreateTimeElement(title,defval){
		var el = document.createElement('input');
		el.setAttribute('type', 'number');
		el.setAttribute('name', title);
		el.setAttribute('id', title);
		el.setAttribute('placeholder', defval);
		el.setAttribute('step',0.01);
		el.setAttribute('style', 'height: 30px;width: 70px;');
		el.value = defval;
		return el;
}

//Years corresponding with 0 of the Standard Calendar
var raharYearZero = 690;
var earthYearZero = 1368.05;

var humanYearsinStdYear = 4.02;
var raharrYearsinStdYear = 0.59;

function RaharrToStd(time){
	var newTime = time  - raharYearZero;
	newTime = newTime / raharrYearsinStdYear ;
	return newTime;
}
function EarthToStd(time){
	var newTime = time - earthYearZero;
	newTime = newTime / humanYearsinStdYear ;
	return newTime;
}
function StdToRaharr(time){
	var newTime = time * raharrYearsinStdYear ;
	newTime = newTime + raharYearZero;
	return newTime;
}
function StdToEarth(time){
	var newTime = time * humanYearsinStdYear ;
	newTime = newTime + earthYearZero;
	return newTime;
}