Difference between revisions of "MediaWiki:Common.js"

From GGBN Wiki
Jump to: navigation, search
(debug mw.api dependency (jQuery.ready))
Line 92: Line 92:
 
jQuery(document).ready(function($) {
 
jQuery(document).ready(function($) {
 
   init_special_listfiles_add_file_usage();
 
   init_special_listfiles_add_file_usage();
}
+
});
 
/*
 
/*
 
$.when(
 
$.when(

Revision as of 15:40, 4 November 2020

/* 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($) {
  init_special_listfiles_add_file_usage();
});
/*
$.when(
  mw.loader.using( ['mediawiki.api', 'mediawiki.util'] ),
  $.ready
)
.then( mw.loader.using( 'mediawiki.api' ).then( init_special_listfiles_add_file_usage() ) );
*/