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]]",
"<span class=\"fakelink menulink\">United States</span>",
]
},
"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]]"
]
},
"united_states_states": {
"parent": "north_america_countries",
"class": "statemenu",
"title": "United States",
"list": [
"[[United States Genealogy|United States]]",
"[[Alabama, United States Genealogy|Alabama]]",
"[[Alaska, United States Genealogy|Alaska]]",
"[[Arizona, United States Genealogy|Arizona]]",
"[[Arkansas, United States Genealogy|Arkansas]]",
"[[California, United States Genealogy|California]]",
"[[Colorado, United States Genealogy|Colorado]]",
"[[Connecticut, United States Genealogy|Connecticut]]",
"[[Delaware, United States Genealogy|Delaware]]",
"[[District of Columbia, United States Genealogy|District of Columbia]]",
"[[Florida, United States Genealogy|Florida]]",
"[[Georgia, United States Genealogy|Georgia]]",
"[[Hawaii, United States Genealogy|Hawaii]]",
"[[Idaho, United States Genealogy|Idaho]]",
"[[Illinois, United States Genealogy|Illinois]]",
"[[Indiana, United States Genealogy|Indiana]]",
"[[Iowa, United States Genealogy|Iowa]]",
"[[Kansas, United States Genealogy|Kansas]]",
"[[Kentucky, United States Genealogy|Kentucky]]",
"[[Louisiana, United States Genealogy|Louisiana]]",
"[[Maine, United States Genealogy|Maine]]",
"[[Maryland, United States Genealogy|Maryland]]",
"[[Massachusetts, United States Genealogy|Massachusetts]]",
"[[Michigan, United States Genealogy|Michigan]]",
"[[Minnesota, United States Genealogy|Minnesota]]",
"[[Mississippi, United States Genealogy|Mississippi]]",
"[[Missouri, United States Genealogy|Missouri]]",
"[[Montana, United States Genealogy|Montana]]",
"[[Nebraska, United States Genealogy|Nebraska]]",
"[[Nevada, United States Genealogy|Nevada]]",
"[[New Hampshire, United States Genealogy|New Hampshire]]",
"[[New Jersey, United States Genealogy|New Jersey]]",
"[[New Mexico, United States Genealogy|New Mexico]]",
"[[New York, United States Genealogy|New York]]",
"[[North Carolina, United States Genealogy|North Carolina]]",
"[[North Dakota, United States Genealogy|North Dakota]]",
"[[Ohio, United States Genealogy|Ohio]]",
"[[Oklahoma, United States Genealogy|Oklahoma]]",
"[[Oregon, United States Genealogy|Oregon]]",
"[[Pennsylvania, United States Genealogy|Pennsylvania]]",
"[[Rhode Island, United States Genealogy|Rhode Island]]",
"[[South Carolina, United States Genealogy|South Carolina]]",
"[[South Dakota, United States Genealogy|South Dakota]]",
"[[Tennessee, United States Genealogy|Tennessee]]",
"[[Texas, United States Genealogy|Texas]]",
"[[Utah, United States Genealogy|Utah]]",
"[[Vermont, United States Genealogy|Vermont]]",
"[[Virginia, United States Genealogy|Virginia]]",
"[[Washington, United States Genealogy|Washington]]",
"[[West Virginia, United States Genealogy|West Virginia]]",
"[[Wisconsin, United States Genealogy|Wisconsin]]",
"[[Wyoming, United States Genealogy|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">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 */