Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
Created page with "Any JavaScript here will be loaded for all users on every page load.: Landrace map bootstrap (Common.js): (function () { var LVER = '1.9.4', CDN = 'https://unpkg.com/leaflet@' + LVER + '/dist/'; function addCSS(href,id){if(id&&document.getElementById(id))return;var l=document.createElement('link');l.rel='stylesheet';l.href=href;if(id)l.id=id;document.head.appendChild(l);} function addJS(src,cb){if(window.L)return cb();var s=document.createElement('script..."
 
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Landrace.wiki – Interactive map bootstrap (Common.js) */
(function () {
  var LVER = '1.9.4';
  var CDN  = 'https://unpkg.com/leaflet@' + LVER + '/dist/';


/* Landrace map bootstrap (Common.js) */
  /* --- tiny helpers to load Leaflet --- */
(function () {
   function addCSS(href, id) {
  var LVER = '1.9.4', CDN = 'https://unpkg.com/leaflet@' + LVER + '/dist/';
    if (id && document.getElementById(id)) return;
   function addCSS(href,id){if(id&&document.getElementById(id))return;var l=document.createElement('link');l.rel='stylesheet';l.href=href;if(id)l.id=id;document.head.appendChild(l);}
    var l = document.createElement('link');
   function addJS(src,cb){if(window.L)return cb();var s=document.createElement('script');s.src=src;s.onload=cb;document.head.appendChild(s);}
    l.rel = 'stylesheet'; l.href = href; if (id) l.id = id;
   function statusColor(s){return({Stable:'#2ecc71',Vulnerable:'#f1c40f',Endangered:'#e67e22',Critical:'#e74c3c',Lost:'#95a5a6'})[s]||'#3498db';}
    document.head.appendChild(l);
   function popup(kind,p){
  }
     if(kind==='accessions'){
   function addJS(src, cb) {
       var t=[p.accession_id,p.local_name].filter(Boolean).join(' — ');
    if (window.L) return cb();
       var link=p.page_url?`<br><a href="${p.page_url}">Open accession</a>`:'';
    var s = document.createElement('script');
    s.src = src; s.onload = cb;
    document.head.appendChild(s);
  }
 
  /* --- UI helpers --- */
   function statusColor(s) {
    return ({
      Stable:'#2ecc71', Vulnerable:'#f1c40f',
      Endangered:'#e67e22', Critical:'#e74c3c', Lost:'#95a5a6'
    })[s] || '#3498db';
  }
   function popup(kind, p) {
     if (kind === 'accessions') {
       var t = [p.accession_id, p.local_name].filter(Boolean).join(' — ');
       var link = p.page_url ? `<br><a href="${p.page_url}">Open accession</a>` : '';
       return `<b>${t}</b><br>${p.status||''}${link}`;
       return `<b>${t}</b><br>${p.status||''}${link}`;
     }
     }
     return `<b>${p.name||''}</b><br>${(p.level||kind)} • ${p.status||''}`;
     return `<b>${p.name||''}</b><br>${(p.level||kind)} • ${p.status||''}`;
   }
   }
   function initOne(el){
 
     if(el.dataset.init) return; el.dataset.init='1';
   function initOne(el) {
     var map=L.map(el).setView([20,85],3);
     if (el.dataset.init) return; el.dataset.init = '1';
     L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png',{attribution:'© OSM'}).addTo(map);
 
     var cfg={
     var map = L.map(el).setView([20, 85], 3);
      regions:{ style:f=>({color:statusColor(f.properties.status),weight:1,fillOpacity:0.15}) },
     L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution:'© OSM' }).addTo(map);
      populations:{ pointToLayer:(f,ll)=>L.circleMarker(ll,{radius:5,color:statusColor(f.properties.status)}) },
 
      accessions:{ pointToLayer:(f,ll)=>L.circleMarker(ll,{radius:4,color:statusColor(f.properties.status)}) }
     // in-map legend (matches site colors)
    };
    var legend = L.control({ position:'bottomleft' });
     ['regions','populations','accessions'].forEach(function(kind){
     legend.onAdd = function () {
       var url=el.dataset[kind]; if(!url) return;
       var div = L.DomUtil.create('div', 'lw-legend-map');
       fetch(url).then(r=>r.json()).then(g=>{
       var items = [
         L.geoJSON(g,Object.assign({
         ['#2ecc71','Stable'], ['#f1c40f','Vulnerable'],
          onEachFeature:(f,ly)=>ly.bindPopup(popup(kind,f.properties||{}))
         ['#e67e22','Endangered'], ['#e74c3c','Critical'],
         },cfg[kind])).addTo(map);
        ['#95a5a6','Lost']
      }).catch(console.error);
      ].map(([c,l]) => `<span class=
    });
  }
  function init(root){(root||document).querySelectorAll('.lw-map').forEach(initOne);}
  addCSS(CDN+'leaflet.css','leaflet-css');
  addJS(CDN+'leaflet.js',function(){
    init();
    if(window.mw && mw.hook){mw.hook('wikipage.content').add($c=>init($c[0]||document));}
  });
})();

Revision as of 07:43, 24 August 2025

/* Landrace.wiki – Interactive map bootstrap (Common.js) */
(function () {
  var LVER = '1.9.4';
  var CDN  = 'https://unpkg.com/leaflet@' + LVER + '/dist/';

  /* --- tiny helpers to load Leaflet --- */
  function addCSS(href, id) {
    if (id && document.getElementById(id)) return;
    var l = document.createElement('link');
    l.rel = 'stylesheet'; l.href = href; if (id) l.id = id;
    document.head.appendChild(l);
  }
  function addJS(src, cb) {
    if (window.L) return cb();
    var s = document.createElement('script');
    s.src = src; s.onload = cb;
    document.head.appendChild(s);
  }

  /* --- UI helpers --- */
  function statusColor(s) {
    return ({
      Stable:'#2ecc71', Vulnerable:'#f1c40f',
      Endangered:'#e67e22', Critical:'#e74c3c', Lost:'#95a5a6'
    })[s] || '#3498db';
  }
  function popup(kind, p) {
    if (kind === 'accessions') {
      var t = [p.accession_id, p.local_name].filter(Boolean).join(' — ');
      var link = p.page_url ? `<br><a href="${p.page_url}">Open accession</a>` : '';
      return `<b>${t}</b><br>${p.status||''}${link}`;
    }
    return `<b>${p.name||''}</b><br>${(p.level||kind)} • ${p.status||''}`;
  }

  function initOne(el) {
    if (el.dataset.init) return; el.dataset.init = '1';

    var map = L.map(el).setView([20, 85], 3);
    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution:'© OSM' }).addTo(map);

    // in-map legend (matches site colors)
    var legend = L.control({ position:'bottomleft' });
    legend.onAdd = function () {
      var div = L.DomUtil.create('div', 'lw-legend-map');
      var items = [
        ['#2ecc71','Stable'], ['#f1c40f','Vulnerable'],
        ['#e67e22','Endangered'], ['#e74c3c','Critical'],
        ['#95a5a6','Lost']
      ].map(([c,l]) => `<span class=