ssa-gov/policy/js/rspa.nav.js?v1
2025-02-19 12:17:21 -08:00

152 lines
No EOL
8.6 KiB
Text

// MOBILE MENU
const rMenu=()=>{
if (document.getElementById('rMenu') && window.addEventListener) { // check for top menu & browser support
const menuFly=document.querySelectorAll('.rMenu>UL>LI');
const closeFly=()=>menuFly.forEach(fly=>fly.classList.remove('keyed'));
const menuTogs=document.querySelectorAll('LI.hasFly>A');
const closeMenuTogs=()=>menuTogs.forEach(tog=>tog.parentElement.classList.remove('open'));
const closeAll=(event)=>{
document.documentElement.classList.remove('menuOn');
closeFly();
closeMenuTogs();
}
// close menu if click or focus away from menu
document.addEventListener('click', function(event) {
if (!event.target.closest('#rMenu') && !event.target.closest('#rMenuBtn')) { closeAll(event) }
}, true);
document.addEventListener('focus', function(event) {
if (!event.target.closest('#rMenu')) { closeAll(event) }
}, true);
// menu toggle
if (document.getElementById('rMenuBtn')) {
document.getElementById('rMenuBtn').addEventListener('click', function(event) {
event.preventDefault();
if (document.querySelector('.menuOn')) { closeAll(event);
} else { document.documentElement.classList.add('menuOn') }
}, false);
}
// loop through flyouts for keyboard access
menuFly.forEach(fly=>{
if (fly.classList.contains('hasFly')) {
fly.tabIndex=0;
fly.addEventListener('focus', function(event) {
event.preventDefault();
closeFly();
this.firstElementChild.setAttribute('aria-expanded', 'true');
this.classList.add('keyed');
}, false);
} else {
fly.firstChild.addEventListener('focus', function(event) {
event.preventDefault();
closeFly();
}, false);
}
});
// loop through toggle links
menuTogs.forEach(tog=>{
tog.tabIndex=-1;
tog.addEventListener('click', function(event) {
event.preventDefault();
if (this.parentElement.classList.contains('open')) {
closeMenuTogs();
this.setAttribute('aria-expanded', 'false');
} else {
closeMenuTogs();
this.setAttribute('aria-expanded', 'true');
this.parentElement.classList.add('open');
}
}, false);
});
}
}
// SERIES SIDEBARS
const sbTime=Math.floor($.now()/18e5); // half hour turnovers
const sbPub=window.location.pathname.split('/')[3]=='ssb'?'ssb':window.location.pathname.split('/')[4];
let sbEdition=window.location.pathname.replace('.html','').split('/')[5];
const iuSidebars=()=>{
$.getJSON('/policy/json/research.json?v'+sbTime, function(data) {
data=data.filter(a=>a.Type=='International Update');
let countries=[...new Set(data.filter(a=>a.Countries!=null).map(pub=>pub.Countries).flat())];
countries=countries.filter(a=>!a.match('European Commission|OECD|World Bank'));
countries.sort((a, b)=>a<b?-1:1);
data.sort((a,b)=>a.Url>b.Url?-1:1);
if (sbEdition=='index') { sbEdition=data[0].Url.substr(26,7) }
const date=new Date(data[0].Released);
date.setMonth(date.getMonth()+1);
$('#js-exp').html(date.toLocaleString('en-US', {month:'long',year:'numeric'}));
let issues=[], issueDate='';
data.forEach(a=>{
if (a.Url.substr(26,7)!=sbEdition) {
issueDate=a.Title.replace('International Update, ','');
issues.push(`<option value="${a.Url.substr(26,7)}">${issueDate.substring(issueDate.length-4)} ${issueDate.substring(0,issueDate.length-5)}</option>`);
}
});
let countryList=[];
countries.forEach(c=>{ countryList.push(`<option value="/policy/research.html?type=International%20Update&keyword=${c}">${c}</option>`)});
$('#js-ed').html(`<form class="other" name="jump"><label for="archive">Other Issues</label><select id="archive" name="menu"><option selected="selected" value="index.html">Choose date</option>${issues.join('')}</select>&nbsp;<input type="button" onClick="location='/policy/docs/progdesc/intl_update/' + document.jump.menu.value + '/index.html';" value="GO" /></form><form class="other" name="countries"><label for="countrySelector">Find Issues for:</label> <select id="countrySelector" name="menu"><option selected="selected">Country or region</option>${countryList.join('')}</select>&nbsp;<input type="button" onclick="location=document.countries.menu.value;" value="GO"></form><p><i>See also the <a href="/policy/country-index.html">Country Index</a> for all Research and Analysis.</i></p>`);
});
}
const ssbSidebars=()=>{
const months=['','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
$.getJSON('/policy/json/research.json?v'+sbTime, function(data) {
data=[... new Set(data.filter(a=>a.Type.match('Social Security Bulletin')).map(a=>a.Released+a.Issue))];
data.sort((a,b)=>a>b?-1:1);
data=data.map(a=>{ return {'Released':a.substring(0,10),'Issue':a.substring(10)} });
const date=new Date(data[0].Released);
date.setMonth(date.getMonth()+3);
$('#js-exp').html(date.toLocaleString('en-US', {month:'long',year:'numeric'}));
let issues=[], dates=[];
data.forEach(a=>{
issues.push(`<option value="/policy/docs/ssb/${a.Issue.replace('Vol. ','v').replace(', No. ','n')}/index.html">${a.Issue}</option>`);
const date=a.Released.substring(0,4)+' '+months[Number(a.Released.substring(5,7))];
dates.push(`<option value="/policy/docs/ssb/${a.Issue.replace('Vol. ','v').replace(', No. ','n')}/index.html">${date}</option>`);
});
$('#otherIssues').html(`<form class="other" name="jump"><label for="archive">All Issues</label><select id="archive" name="menu"><option selected="selected">Choose by volume</option>${issues.join('')}</select>&nbsp;<input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="GO" /></form>`);
$('#otherDates').html(`<form class="other" name="jumpByYear"><select id="archiveByYear" name="menuByYear" title="Other Issues by Year"><option selected="selected">Choose by date</option>${dates.join('')}</select>&nbsp;<input type="button" onClick="location=document.jumpByYear.menuByYear.options[document.jumpByYear.menuByYear.selectedIndex].value;" value="GO" /></form>`);
});
}
const statSidebars=()=>{
let editions=[], final='';
$.getJSON('/policy/json/statistics.json?v'+sbTime, function(pub) {
pub=pub.find(a=>a.Url.split('/')[2]==sbPub);
if ($('#js-exp')) {$('#js-exp').html(pub.Expected);}
if (pub.Editions.length>0) {
if (sbPub=='ssi_monthly'||sbPub=='stat_snapshot') {
const months=['','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
const seriesURL=sbPub=='ssi_monthly'?'/index':'';
const seriesChoose=sbPub=='ssi_monthly'?'':'index';
pub.Editions.forEach(e=>{
let mm=months.indexOf(e.split(' ')[0]), numEdition='';
if (mm>0) { // if e contains a month name, which some SSIM do not
if (mm<10) { mm='0'+mm }
numEdition=e.split(' ')[1]+'-'+mm;
} else {
numEdition=e; // for annual SSIMs
}
if (numEdition!=sbEdition) { editions.push(`<option value="${numEdition}">${e.substring(e.length-4)} ${e.substring(0,e.length-5)}</option>`) }
});
if (sbEdition=='index') { editions.shift() }
final=`<form class="other" name="jump"><label for="archive">Other Editions</label><select id="archive" name="menu"><option selected="selected" value="${seriesChoose}">Choose edition</option>${editions.join('')}</select>&nbsp;<input type="button" onClick="location='/policy/${pub.Url}' + document.jump.menu.value + '${seriesURL}.html';" value="GO" /></form>`;
} else {
pub.Editions.forEach(e=>{
const thisURL=e=='2015 (Expanded Edition)'?'2015ee':e;
const thisText=e=='2015 (Expanded Edition)'?'2015&nbsp;(Expanded)':e;
if (thisURL!==sbEdition) {
editions.push(`<a href="/policy/${pub.Url}${thisURL}/index.html">${thisText}</a>&ensp; `);
}
});
if (sbEdition == 'index') { editions.shift() }
final=`<h4 class="sidebar-gray">Other Editions</h4><p>${editions.join('')}</p>`;
}
$('#js-ed').html(final);
}
});
}
$(function () {
rMenu();
if (sbPub=='intl_update') { iuSidebars()
} else if (sbPub=='ssb') { ssbSidebars()
} else if ($('body').hasClass('js-sb')) { statSidebars() }
});