_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

User:Splarka/togglegallery.js

Talking about User:Splarka/togglegallery.js is a topic that arouses the interest of people of all ages and profiles. Whether due to its historical relevance, its impact on current society or its influence in different areas, User:Splarka/togglegallery.js is a topic that deserves to be explored and understood in its entirety. Over the years, User:Splarka/togglegallery.js has been the subject of debate, analysis and study, demonstrating its importance and significance. In this article, we will delve into the fascinating world of User:Splarka/togglegallery.js, exploring its different facets, its evolution over time and its impact on everyday life.
/* __NOGALLERY__ toggler, version 
Originally from: http://en.wikipedia.orghttps://wiki386.com/en/User:Splarka/togglegallery.js

Notes:
* To internationalize, change gtMatchPath to local articlepath and Image namespace.
* Test bigger, faster, harder

Operation:
* Checks the page for <div id="mw-category-media">
** If found, remove the table and build a list of all the images
* Else, checks for <div id="mw-pages">
** Iterates over these for at least one Image: link. If found, adds button.
** Button iterates over all image links again, builds lists in groups of 50.
** Lists are submitted to callback.
** Callback return embeds thumbnailed image tag into each link.

Todo:
* Have galleryToggleOff() make 3 nice rows, makes a single list currently.
* Testywesty.
*/

var gtImageListWidth = 100;
var gtMatchPath = 'view_image.php?q=User:Splarka/togglegallery.js&sq=User:Splarka/togglegallery.js&lang=en&file=File:';
if(wgCanonicalNamespace == 'Category') addOnloadHook(galleryToggleCheck)
function galleryToggleCheck() {
  var mds = document.getElementById('mw-category-media');
  if(mds) {
    mw.util.addPortletLink('p-tb','javascript:galleryToggleOff()','Toggle gallery off','t-toggal','show gallery as a text list');
    return;
  }

  var pgs = document.getElementById('mw-pages');
  if(!pgs) return;
  var ul = pgs.getElementsByTagName('ul');
  for(var i=0;i<ul.length;i++) {
    var a = ul.getElementsByTagName('a');
    for(var j=0;j<a.length;j++) {
      if(a.href.indexOf(gtMatchPath) != -1) {  
        mw.util.addPortletLink('p-tb','javascript:galleryToggleOn()','Toggle gallery on','t-toggal','show category Image: links as a gallery');
        return;
      }
    }
  }
}

function galleryToggleOn() {
  injectSpinner(document.getElementById('t-toggal'),'tog');
  var pgs = document.getElementById('mw-pages');
  if(!pgs) return;
  var ul = pgs.getElementsByTagName('ul');
  var imgs = ;
  var imgrps = ;  //limit of 50 per query
  for(var i=0;i<ul.length;i++) {
    var a = ul.getElementsByTagName('a');
    for(var j=0;j<a.length;j++) {
      if(a.href.indexOf(gtMatchPath) != -1) {  
        imgs.push(encodeURIComponent(getText(a).replace(/_/ig,' ')));
        if(imgs.length >= 50) {
          imgrps.push(imgs.join('|'));
          imgs = ;
        }
      }
    }
  }
  imgrps.push(imgs.join('|'));

  for(var i=0;i<imgrps.length;i++) {
    var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?maxage=300&smaxage=300&format=json&callback=gtImageListCB&action=query&prop=imageinfo&iiprop=url&iiurlwidth=' + gtImageListWidth + '&iiurlheight=' + parseInt(gtImageListWidth * 1.5) + '&titles=' + imgrps;
    mw.loader.load(url);
  }
}

function gtImageListCB(obj) {
  document.getElementById('t-toggal').style.display = 'none';
  removeSpinner('tog');
  if(!obj || !obj) return
  var thumbs = obj;
  var a = document.getElementById('mw-pages').getElementsByTagName('a');

  for(var i in thumbs) {
    var title = thumbs;
    for(var j=0;j<a.length;j++) {
      var imgtitle = getText(a).replace(/_/ig,' ');
      if(title.indexOf(imgtitle) != -1 && imgtitle != '') {
        var img = document.createElement('img');
        img.style.width = thumbs + 'px'; 
        img.style.height = thumbs + 'px';
        img.style.border = '1px solid #999999';
        img.style.background = 'url("http://upload.wikimedia.org/wikipedia/commons/5/5d/Checker-16x16.png") repeat';
        img.setAttribute('title',title);
        img.setAttribute('src',thumbs)
        a.appendChild(document.createElement('br'));
        a.appendChild(img);
      }
    }
  }
}

function galleryToggleOff() {
  document.getElementById('t-toggal').style.display = 'none';
  var mds = document.getElementById('mw-category-media');
  var ul = document.createElement('ul');
  var imgs = mds.getElementsByTagName('img');
  for(var i=0;i<imgs.length;i++) {
    var par = imgs.parentNode;
    if(par.tagName == 'A') {
      var li = document.createElement('li');
      var a = par.cloneNode(false);
      a.appendChild(document.createTextNode(a.title));
      li.appendChild(a);
      ul.appendChild(li);
    }
  }
  mds.appendChild(ul);
 
  var tables = getElementsByClassName(mds,'table','gallery');
  for(var i=tables.length-1;i>-1;i--) tables.parentNode.removeChild(tables)
}

function getText(object) {
  if (object.nodeType == 3) return object.nodeValue;
  var txt = ;
  var i=0;
  while(object.childNodes) {
    txt = getText(object.childNodes);
    i++;
  }
  return txt.join('');
}