MediaWiki:MapMenu-GR.js

/** MapMenu-GR.js **/ /** See bottom for functions **/ /****** menu data structure *******/ const menus = { "europe_countries": { "class": "countrymenu", "title": "Europe", "list": [ "Denmark", "England ", "Finland", "Ireland", "Isle of Man", "Italy", "Jersey", "Netherlands", "Norway", "Scotland", "Sweden", "Wales" ] },  "north_america_countries": { "class": "countrymenu", "title": "North America", "list": [ "Canada ", "Costa Rica", "Mexico", "United States " ] },  "canada_states": { "parent": "north_america_countries", "class": "statemenu", "title": "Canada", "list": [ "Canada", "Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland & Labrador", "Northwest Territories", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon" ] },  "england_states": { "parent": "europe_countries", "class": "statemenu", "title": "England", "list": [ "England", "Bedfordshire", "Berkshire", "Buckinghamshire", "Cambridgeshire", "Cheshire", "Cornwall", "Cumberland", "Derbyshire", "Devon", "Dorset", "Durham", "Essex", "Gloucestershire", "Hampshire", "Herefordshire", "Hertfordshire", "Huntingdonshire", "Kent", "Lancashire", "Leicestershire", "Lincolnshire", "London", "Middlesex", "Norfolk", "Northamptonshire", "Northumberland", "Nottinghamshire", "Oxfordshire", "Rutland", "Shropshire", "Somerset", "Staffordshire", "Suffolk", "Surrey", "Sussex", "Warwickshire", "Westmorland", "Wiltshire", "Worcestershire", "Yorkshire" ] },  "united_states_states": { "parent": "north_america_countries", "class": "statemenu", "title": "United States", "list": [ "United States", "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming" ] } }; /** * When you click a button, show the list for that country * Note that button ids must match country identifiers in the JSON */ function showList(name) { var html = ''; var exit = ' [x] '; // There is no break in JavaScript for (var i in menus) { if (i === name) { var title = linkify(menus[i].title); // add in the id and class attributes html += '' + title + exit + ' '; html += ''; for (var j in menus[i].list) { var link = linkify(menus[i].list[j]); html += '' + link + ''; }           html += ''; }   }    // place the generated html in the menu $("#menu").html(html); }

/** * NB There is no matching JSON object for "all countries"... we just loop * through the whole menu object. We alphabetize according to the 'sort value' * that we create in the list2 object */ function showAllCountries { var exit = ' [x] '; var html = ' List of all Localities' + exit + ' '; var list = []; // initialize our list variables var list2 = []; for (var i in menus) { // only output the _countries menus if (/_countries$/.test(i)) { list = list.concat(menus[i].list); }   }    for (var j in list) { var link = linkify(list[j]); var sortv = getSortV(list[j]); list2[j] = { link: link, sortv: sortv }; }   // sort our list structure in-place list2.sort(function(a, b) {     // a comes before b      if (a.sortv < b.sortv) { return -1;}      // b comes before a      if (a.sortv > b.sortv) { return 1;}      // they're equal      return 0;    }); // assemble our html html += ""; for (var k in list2) { html += "" + list2[k].link + ""; }   html += "</ul";

// console.log(list2); // place the generated html in the menu $("#menu").html(html); }

/** * Take a string value and strip characters to get it's * "sort value". The two types of strings we expect are either * a) a wiki link * b) a element * In the case of a wiki link, we want the link text * In the case of a span element, we want the innerHtml */ function getSortV(str) { var sortv = null; var n = str.indexOf('['); if (n !== -1) { var sortv = str.split("|"); for (var i = 0; i < sortv.length; i++) { sortv[i] = sortv[i].replace(/\[/g, ""); sortv[i] = sortv[i].replace(/\]/g, ""); }       sortv = (sortv.length > 1) ? sortv[1] : sortv[0]; } else { sortv = str.substring(str.indexOf(">") + 1, str.indexOf("<", 2)); }   if (sortv === null) { console.log("ERROR: could not parse " + str + " for a sort value") } return sortv; } /** * Take a string value and if it's a wikitext link turn it into * an html link * @param {string} str */ function linkify(str) { // e.g. "https://beta.familysearch.org/wiki/en/"; const lang = mw.config.get('wgContentLanguage'); const base = document.location.origin + '/wiki/' + lang +'/'; var n = str.indexOf('['); // indexOf returns -1 if not found if (n == -1) { // console.log ("No link in " + str); // return the string unharmed return str; } else { var link = str.split("|"); // link could be just one element, or two if it is piped for (var i = 0; i < link.length; i++) { // get rid of brackets globally link[i] = link[i].replace(/\[/g, ""); link[i] = link[i].replace(/\]/g, ""); }       // find out what we need to use for text in our anchor var text = (link.length > 1) ? link[1] : link[0]; // encode spaces and such var html = '' + text + ""; return html; } }

$(document).ready(function {    // start out by showing all menu items    showAllCountries; }); /** End of Map Menus code */