MediaWiki:MapMenu.js
Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
 - Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
 - Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
 - Opera: Pressione Ctrl-F5.
 
/** See bottom for functions **/
/****** menu data structure *******/
const menus = 
{
  "africa_countries": {
    "class": "countrymenu",
    "title": "Africa",
    "list": [
     "[[África do Sul|África do Sul]]",
     "[[Angola|Angola]]",
     "[[Cabo Verde|Cabo Verde]]",
     "[[Guiné-Bissau|Guiné-Bissau]]",
     "[[Guiné Equatorial|Guiné Equatorial]]",
     "[[Moçambique|Moçambique]]",
     "[[São Tomé e Príncipe|São Tomé e Príncipe]]"
      
    ]
  },
  "asia_countries": {
    "class": "countrymenu",
    "title": "Asia",
    "list": [
      "[[Armênia|Armênia]]",
      "[[China Genealogy|China]]",
      "[[Japão|Japão]]",
      "[[Macau|Macau]]",
      "[[Timor-Leste|Timor-Leste]]"
    ]
  },
  "europe_countries": {
    "class": "countrymenu",
    "title": "Europa",
    "list": [
    "[[Albânia|Albânia]]",
    "[Alemanha|Alemanha]]",
    "[[Áustria|Áustria]]",
    "[[Bélgica|Bélgica]]",
    "[[Dinamarca|Dinamarca]]",
    "[[Espanha|Espanha]]",
    "[[França|França]]",
    "[[Inglaterra|Inglaterra]]",
     "[[Itália]]",
    "[[Irlanda|Irlanda]]",
    "[[Islândia|Islândia]]",
    "[[Luxemburgo|Luxemburgo]]",
    "[[Noruega|Noruega]]",
    "[[Polônia|Polônia]]",
    "[[Portugal|Portugal]]",
    "[[Suécia|Suécia]]",
      
    ]
  },
  "north_america_countries": {
    "class": "countrymenu",
    "title": "North America",
    "list": [
      "[[Canadá|Canadá]]",
      "[[México|México]]",
      "[[United States|United States]]",
    ]
  },
  "pacific_countries": {
    "class": "countrymenu",
    "title": "Australia/Oceania",
    "list": [
      "[[Austrália|Austrália]]",
   ]
  },
  "south_america_countries": {
    "class": "countrymenu",
    "title": "South America",
    "list": [
      "[[Argentina|Argentina]]",
      "[[Bolívia|Bolívia]]",
      "[[Brasil]]",
      "[[Chile|Chile]]",
      "[[Colômbia|Colômbia]]",
      "[[Paraguai|Paraguai]]",
      "[[Peru|Peru]]",
      "[[Uruguai|Uruguai]]",
      "[[Venezuela|Venezuela]]"
    
    ]
  }
};  
  
/**
 * 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">Lista de todas as localidades' + 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/pt/";
    const base = document.location.origin + '/wiki/pt/';
    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 */