MediaWiki:Common.js

From GGBN Wiki
Revision as of 15:49, 4 November 2020 by A.Plank (talk | contribs) (init_special_listfiles_add_file_usage() double save)
Jump to: navigation, search

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: 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.
/* Any JavaScript here will be loaded for all users on every page load. */
/**
  * Add file’s usage to Special:ListFiles
  * @requires mw.config
  * @requires mw.util
  * @requires mw.Api
  */
window.init_special_listfiles_add_file_usage = function () {

  if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){ // "Special:ListFiles"
    /*
    * {batchcomplete: "", query: {…}}
        batchcomplete: ""
        query:
          normalized: [{…}]
          pages:
            402:
              fileusage: Array(1)
              0:
                ns: 0
                pageid: 90
                title: "News"
                __proto__: Object
              length: 1
              __proto__: Array(0)
              ns: 6
              pageid: 402
              title: "File:INVEMAR logo.png"
            __proto__: Object
          __proto__: Object
        __proto__: Object
        __proto__: Object
            * */
    var api = new mw.Api();
    // $('td.TablePager_col_img_name').first().find('a').attr('href').replace(mw.config.get('wgArticlePath').replace('$1', ''), '');
    $('td.TablePager_col_img_name').each(function () {
      var $thisTdImgName = $(this)
          , thisWikiFile=$thisTdImgName.find('a').first().attr('href').replace(
          mw.config.get('wgArticlePath').replace('$1', ''), ''
          )
      ;
      if (thisWikiFile.length) {
        api.get({
          action: 'query',
          prop: 'fileusage',
          titles: thisWikiFile
        }).done(function (data) {
          // console.log(data);
          if (typeof data['query'] ['pages'] != "undefined") {
            $.each(data.query.pages, function(i_1, el_pages) {
              if (typeof  el_pages['fileusage'] != "undefined") {
                $list_tag=$('<ol/>').addClass('list-files-usage');
                $.each(el_pages['fileusage'], function(i_2, el_fileusage) {
                  var $tag=$('<a/>')
                      .attr('href', mw.util.getUrl(el_fileusage.title))
                      .append(el_fileusage.title);
                  $list_tag.append($('<li/>').append($tag));
                });
                $thisTdImgName.append('&#32;<span>File is used on:</span>');
                $thisTdImgName.append($list_tag);
              }
            });
          } else {
            // console.log("DEBUG: data ['query'] ['pages'] not found ");
          }
        });
      } else{
        console.warn('DEBUG: thisWikiFile seems empty: ');
        console.warn(thisWikiFile);
      }
    });
  }// if "Special:ListFiles"
};// end init_special_listfiles_add_file_usage

document.getElementsByTagName("body")[0].classList.remove("vector-animateLayout");

if(skin=="ggbn"){
	toolbox = document.getElementById("p-tb");
	headmenu = document.getElementById("mw-head");
	if(toolbox && headmenu){
		toolbox.parentNode.removeChild(toolbox);
		toolbox.setAttribute("class","vectorTabs");
		headmenu.appendChild(toolbox);
	}
	portalLinks = document.getElementsByClassName("portal");
	for(i=0;i<portalLinks.length;i++){
		var portalLink = portalLinks[i];
		portalLink.setAttribute('title',portalLink.getAttribute('id').substring(2).replace(new RegExp('_', 'g'), ' '));
	}
}

jQuery(document).ready(function($) {
  if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){
    init_special_listfiles_add_file_usage();
  }
});