MediaWiki:MapMenu-GR.js

From FamilySearch Wiki
Revision as of 16:34, 17 December 2020 by Ahancey (talk | contribs) (List of GuidedResearch location pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, 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: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/** MapMenu-GR.js **/
/** See bottom for functions **/
/****** menu data structure *******/
const menus = 
{
  "europe_countries": {
    "class": "countrymenu",
    "title": "Europe",
    "list": [
      "[[GuidedResearch:Denmark|Denmark]]",
      "<span class=\"fakelink menulink\">England</span>",
      "[[GuidedResearch:Finland|Finland]]",
      "[[GuidedResearch:Ireland|Ireland]]",
      "[[GuidedResearch:Isle of Man|Isle of Man]]",
      "[[GuidedResearch:Italy|Italy]]",
      "[[GuidedResearch:Jersey|Jersey]]",
      "[[GuidedResearch:Netherlands|Netherlands]]",
      "[[GuidedResearch:Norway|Norway]]",
      "[[GuidedResearch:Scotland|Scotland]]",
      "[[GuidedResearch:Sweden|Sweden]]",
      "[[GuidedResearch:Wales|Wales]]"
    ]
  },
  "north_america_countries": {
    "class": "countrymenu",
    "title": "North America",
    "list": [
      "<span class=\"fakelink menulink\">Canada</span>",
      "[[GuidedResearch:Costa Rica|Costa Rica]]",
      "[[GuidedResearch:Mexico|Mexico]]",
      "<span class=\"fakelink menulink\">United States</span>"
    ]
  },
  "canada_states": {
    "parent": "north_america_countries",
    "class": "statemenu",
    "title": "Canada",
    "list": [
      "[[GuidedResearch:Canada|Canada]]",
      "[[GuidedResearch:Alberta|Alberta]]",
      "[[GuidedResearch:British Columbia|British Columbia]]",
      "[[GuidedResearch:Manitoba|Manitoba]]",
      "[[GuidedResearch:New Brunswick|New Brunswick]]",
      "[[GuidedResearch:Newfoundland and Labrador|Newfoundland & Labrador]]",
      "[[GuidedResearch:Northwest Territories|Northwest Territories]]",
      "[[GuidedResearch:Nova Scotia|Nova Scotia]]",
      "[[GuidedResearch:Nunavut|Nunavut]]",
      "[[GuidedResearch:Ontario|Ontario]]",
      "[[GuidedResearch:Prince Edward Island|Prince Edward Island]]",
      "[[GuidedResearch:Quebec|Quebec]]",
      "[[GuidedResearch:Saskatchewan|Saskatchewan]]",
      "[[GuidedResearch:Yukon|Yukon]]"
    ]
  },
  "england_states": {
    "parent": "europe_countries",
    "class": "statemenu",
    "title": "England",
    "list": [
      "[[GuidedResearch:England|England]]",
      "[[GuidedResearch:Bedfordshire|Bedfordshire]]",
      "[[GuidedResearch:Berkshire|Berkshire]]",
      "[[GuidedResearch:Buckinghamshire|Buckinghamshire]]",
      "[[GuidedResearch:Cambridgeshire|Cambridgeshire]]",
      "[[GuidedResearch:Cheshire|Cheshire]]",
      "[[GuidedResearch:Cornwall|Cornwall]]",
      "[[GuidedResearch:Cumberland|Cumberland]]",
      "[[GuidedResearch:Derbyshire|Derbyshire]]",
      "[[GuidedResearch:Devon|Devon]]",
      "[[GuidedResearch:Dorset|Dorset]]",
      "[[GuidedResearch:Durham|Durham]]",
      "[[GuidedResearch:Essex|Essex]]",
      "[[GuidedResearch:Gloucestershire|Gloucestershire]]",
      "[[GuidedResearch:Hampshire|Hampshire]]",
      "[[GuidedResearch:Herefordshire|Herefordshire]]",
      "[[GuidedResearch:Hertfordshire|Hertfordshire]]",
      "[[GuidedResearch:Huntingdonshire|Huntingdonshire]]",
      "[[GuidedResearch:Kent|Kent]]",
      "[[GuidedResearch:Lancashire|Lancashire]]",
      "[[GuidedResearch:Leicestershire|Leicestershire]]",
      "[[GuidedResearch:Lincolnshire|Lincolnshire]]",
      "[[GuidedResearch:London|London]]",
      "[[GuidedResearch:Middlesex|Middlesex]]",
      "[[GuidedResearch:Norfolk|Norfolk]]",
      "[[GuidedResearch:Northamptonshire|Northamptonshire]]",
      "[[GuidedResearch:Northumberland|Northumberland]]",
      "[[GuidedResearch:Nottinghamshire|Nottinghamshire]]",
      "[[GuidedResearch:Oxfordshire|Oxfordshire]]",
      "[[GuidedResearch:Rutland|Rutland]]",
      "[[GuidedResearch:Shropshire|Shropshire]]",
      "[[GuidedResearch:Somerset|Somerset]]",
      "[[GuidedResearch:Staffordshire|Staffordshire]]",
      "[[GuidedResearch:Suffolk|Suffolk]]",
      "[[GuidedResearch:Surrey|Surrey]]",
      "[[GuidedResearch:Sussex|Sussex]]",
      "[[GuidedResearch:Warwickshire|Warwickshire]]",
      "[[GuidedResearch:Westmorland|Westmorland]]",
      "[[GuidedResearch:Wiltshire|Wiltshire]]",
      "[[GuidedResearch:Worcestershire|Worcestershire]]",
      "[[GuidedResearch:Yorkshire|Yorkshire]]"
    ]
  },
  "united_states_states": {
    "parent": "north_america_countries",
    "class": "statemenu",
    "title": "United States",
    "list": [
      "[[GuidedResearch:United States|United States]]",
      "[[GuidedResearch:Alabama|Alabama]]",
      "[[GuidedResearch:Alaska|Alaska]]",
      "[[GuidedResearch:Arizona|Arizona]]",
      "[[GuidedResearch:Arkansas|Arkansas]]",
      "[[GuidedResearch:California|California]]",
      "[[GuidedResearch:Colorado|Colorado]]",
      "[[GuidedResearch:Connecticut|Connecticut]]",
      "[[GuidedResearch:Delaware|Delaware]]",
      "[[GuidedResearch:District of Columbia|District of Columbia]]",
      "[[GuidedResearch:Florida|Florida]]",
      "[[GuidedResearch:Georgia|Georgia]]",
      "[[GuidedResearch:Hawaii|Hawaii]]",
      "[[GuidedResearch:Idaho|Idaho]]",
      "[[GuidedResearch:Illinois|Illinois]]",
      "[[GuidedResearch:Indiana|Indiana]]",
      "[[GuidedResearch:Iowa|Iowa]]",
      "[[GuidedResearch:Kansas|Kansas]]",
      "[[GuidedResearch:Kentucky|Kentucky]]",
      "[[GuidedResearch:Louisiana|Louisiana]]",
      "[[GuidedResearch:Maine|Maine]]",
      "[[GuidedResearch:Maryland|Maryland]]",
      "[[GuidedResearch:Massachusetts|Massachusetts]]",
      "[[GuidedResearch:Michigan|Michigan]]",
      "[[GuidedResearch:Minnesota|Minnesota]]",
      "[[GuidedResearch:Mississippi|Mississippi]]",
      "[[GuidedResearch:Missouri|Missouri]]",
      "[[GuidedResearch:Montana|Montana]]",
      "[[GuidedResearch:Nebraska|Nebraska]]",
      "[[GuidedResearch:Nevada|Nevada]]",
      "[[GuidedResearch:New Hampshire|New Hampshire]]",
      "[[GuidedResearch:New Jersey|New Jersey]]",
      "[[GuidedResearch:New Mexico|New Mexico]]",
      "[[GuidedResearch:New York|New York]]",
      "[[GuidedResearch:North Carolina|North Carolina]]",
      "[[GuidedResearch:North Dakota|North Dakota]]",
      "[[GuidedResearch:Ohio|Ohio]]",
      "[[GuidedResearch:Oklahoma|Oklahoma]]",
      "[[GuidedResearch:Oregon|Oregon]]",
      "[[GuidedResearch:Pennsylvania|Pennsylvania]]",
      "[[GuidedResearch:Rhode Island|Rhode Island]]",
      "[[GuidedResearch:South Carolina|South Carolina]]",
      "[[GuidedResearch:South Dakota|South Dakota]]",
      "[[GuidedResearch:Tennessee|Tennessee]]",
      "[[GuidedResearch:Texas|Texas]]",
      "[[GuidedResearch:Utah|Utah]]",
      "[[GuidedResearch:Vermont|Vermont]]",
      "[[GuidedResearch:Virginia|Virginia]]",
      "[[GuidedResearch:Washington|Washington]]",
      "[[GuidedResearch:West Virginia|West Virginia]]",
      "[[GuidedResearch:Wisconsin|Wisconsin]]",
      "[[GuidedResearch:Wyoming|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 = '<span class="exit">[x]</span>';
    // 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 += '<div id="' + i + '" class="' + menus[i].class + ' menuTitle"';
            // add in the custom "parent" attribute if its defined in the data
            if (menus[i].parent) {
              html += ' parent="' + menus[i].parent + '"'; 
            }
            html += '>' + title + exit + '</div>';
            html += '<ul>';
            for (var j in menus[i].list) {
                var link = linkify(menus[i].list[j]);
                html += '<li>' + link + '</li>';
            }
            html += '</ul>';
        }
    }
    // 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 = '<span class="exit">[x]</span>';
    var html = '<div class="menuTitle">List of all Localities' + exit + '</div>';
    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 += "<ul>";
    for (var k in list2) {
    	html += "<li>" + list2[k].link + "</li>";
    }
    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 <span></span> 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 = '<a href="' + base + encodeURI(link[0]) + '">' + text + "</a>";
        return html;
    }
}

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