MediaWiki

LanguagesGallery.js

Revision as of 21:24, 19 January 2022 by Yug (talk | contribs)

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.
/* *************************************************************** */
/* LanguageGallery script **************************************** */
// Description: given json data provided in MediaWiki pages, merge those data, project language cards.
// Usage: [[Template:LanguagesGallery]]
// Usage: open [[Template:LanguagesGallery]] > Open, loads > Click for more.
// Hack pad:https://jsfiddle.net/hugolpz/vnz238xq/ 
// Documentations:  
// Author: Yug
 
/* *************************************************************** */
/* DATA ********************************************************** */
// Sources
var sources = [
    { page: 'LanguageGalleryData.js'},
    { page: 'LanguageGalleryData.js'},/*
    { page: 'LanguageSpeakersFemaleData.js'},
    { page: 'LanguageSpeakersMaleData.js'},
    { page: 'LanguageWikidataData.js'},*/
];

/* *************************************************************** */
// Clean data
var cleanResponseDataBindings = function(dataBindings){
    var res = dataBindings.map(item => {
       var keys = Object.keys(item);
       var obj = {};
       for(var i=0;i<keys.length;i++){
           var key = keys[i],
               val = item[key].value;
           obj[key]=val;
       }
       return obj 
    });
    return res;
}

/* *************************************************************** */
// Merge data by common property
var merge2ArraysBySameId = function(arr1,arr2,id1){
	return arr1.map( item1 => { 
  	var identical = arr2.find(obj => obj[id1] === item1[id1]); 
  	return Object.assign(identical, item1) 
  } );
}


/* *************************************************************** */
// SORT ?
 
/* *************************************************************** */
/* HTML TEMPLATE ************************************************* */
// Beautity numbers
//console.log(10000.toLocaleString());    // "10,000"

// Template
var tpl = function(lang){
    return 
    `<div>
        <h2>${lang.label} (${lang.iso||lang.qid})</h2>
        <span>Recordings: ${lang.recordsF+lang.recordsM} (${lang.recordsF}|${lang.recordsM})</span>
        <span>Speakers: ${lang.speakersF+lang.speakersM} (${lang.speakersF}|${lang.speakersM})</span>
        <span>Population: ${lang.population.toLocaleString()}</span>        
    <div>`
}
 
/* *************************************************************** */
/* INJECTIONS **************************************************** */



// Fetch data        -> NOTE : this is asynchroneous so the data is available too late. Need fix.
var result = {}; // final object containing consolidated result
$.when(
    $.getJSON('/index.php?title=MediaWiki:'+sources[0].page+'.js&action=raw&ctype=text/javascript', 
    function (data) { console.log(sources[0].page); result[sources[0].page] = data; }),
    $.getJSON('https://lingualibre.org/index.php?title=MediaWiki:'+sources[0].page+'.js&action=raw&ctype=text/javascript', 
        function (data) { console.log(sources[1].page); result[sources[0].page+'2'] = data; }),
).done(function() {
    console('Yug: ', result)
    // use `result`
});

//Temporary fake data, clean:
const languages = [{ qid: 'Q209', label: 'Breton', iso:'bre' }, { qid: 'Q21', label: 'French', iso: 'fra' }],
    speakersFemales = [{ qid: 'Q209', genderF: 3, recordsF: 60 }, { qid: 'Q21', genderF: 21, recordsF:15046 }],
    speakersMales = [{ qid: 'Q209', genderM: 7, recordsM: 112 }, { qid: 'Q21', genderM: 85, recordsM:82964 }],
    natives = [{ qid: 'Q209', population: 300000 }, { qid: 'Q21', population:80000000 }];

// Mergings
var merge1 = merge2ArraysBySameId(languages,speakersFemales,'qid');
var merge2 = merge2ArraysBySameId(merge1,speakersMales,'qid');
var final = merge2ArraysBySameId(merge2,natives,'qid');
alert(JSON.stringify(final))

var _10k = final.filter(item => item.recordsF+item.recordsM > 9999);
_10k.forEach(item => $('#lg-10k').append(tpl(item)) )

var _1k = final.filter(item => item.recordsF+item.recordsM > 999 && item.recordsF+item.recordsM <10000);
_1k.forEach(item => $('#lg-1k').append(tpl(item)) )

var _1 = final.filter(item => item.recordsF+item.recordsM > 0 && item.recordsF+item.recordsM <1000);
_1.forEach(item => $('#lg-1').append(tpl(item)) )


/* *************************************************************** */
/* PAGE INTERACTIONS ********************************************* */
// Show more/less cards for this section
// Show more/less infor overall