Difference between revisions of "MediaWiki:Common.js"

From GGBN Wiki
Jump to: navigation, search
(move toolbox and set portal link texts as title attributes (for mouse-over))
 
(init_special_listfiles_add_file_usage try with colum added)
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
/* Any JavaScript here will be loaded for all users on every page load. */
 
/* 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(),
 +
zero_indexColImgName = parseInt($('table.listfiles tr:nth-child(2)').children('td.TablePager_col_img_name').index());
 +
// $('td.TablePager_col_img_name').first().find('a').attr('href').replace(mw.config.get('wgArticlePath').replace('$1', ''), '');
 +
$('table.listfiles tr:nth-child(1) th:nth-child('+ ( zero_indexColImgName + 1 ) + ')').after($('<th/>').append('File Usage'));
 +
 +
$('td.TablePager_col_img_name').each(function () {
 +
var $thisTdImgName = $(this)
 +
, thisWikiFile=$thisTdImgName.find('a').first().attr('href').replace(
 +
mw.config.get('wgArticlePath').replace('$1', ''), ''
 +
),
 +
$td=$('<td/>').addClass('TablePager_col_img_fileusage')
 +
 +
;
 +
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/>');
 +
// $list_tag=$('<span/>').append('File is used on:').add('<ol/>');
 +
$.each(el_pages['fileusage'], function(i_2, el_fileusage) {
 +
var $a=$('<a/>')
 +
.attr('href', mw.util.getUrl(el_fileusage.title))
 +
.append(el_fileusage.title);
 +
$list_tag.append($('<li/>').append($a));
 +
});
 +
$td.append($('<span/>').append('File is used on:'))
 +
.append($list_tag);
 +
}
 +
});
 +
$thisTdImgName.after($td);
 +
} else {
 +
// console.log("DEBUG: data ['query'] ['pages'] not found ");
 +
$thisTdImgName.after($td);
 +
}
 +
});
 +
} 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");
 
document.getElementsByTagName("body")[0].classList.remove("vector-animateLayout");
  
Line 16: Line 97:
 
}
 
}
 
}
 
}
 +
 +
jQuery(document).ready(function($) {
 +
  if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){
 +
    init_special_listfiles_add_file_usage();
 +
  }
 +
});

Latest revision as of 17:29, 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(),
				zero_indexColImgName = parseInt($('table.listfiles tr:nth-child(2)').children('td.TablePager_col_img_name').index());
		// $('td.TablePager_col_img_name').first().find('a').attr('href').replace(mw.config.get('wgArticlePath').replace('$1', ''), '');
		$('table.listfiles tr:nth-child(1) th:nth-child('+ ( zero_indexColImgName + 1 ) + ')').after($('<th/>').append('File Usage'));

		$('td.TablePager_col_img_name').each(function () {
			var $thisTdImgName = $(this)
					, thisWikiFile=$thisTdImgName.find('a').first().attr('href').replace(
					mw.config.get('wgArticlePath').replace('$1', ''), ''
					),
					$td=$('<td/>').addClass('TablePager_col_img_fileusage')

			;
			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/>');
								// $list_tag=$('<span/>').append('File is used on:').add('<ol/>');
								$.each(el_pages['fileusage'], function(i_2, el_fileusage) {
									var $a=$('<a/>')
											.attr('href', mw.util.getUrl(el_fileusage.title))
											.append(el_fileusage.title);
									$list_tag.append($('<li/>').append($a));
								});
								$td.append($('<span/>').append('File is used on:'))
								.append($list_tag);
							}
						});
						$thisTdImgName.after($td);
					} else {
						// console.log("DEBUG: data ['query'] ['pages'] not found ");
						$thisTdImgName.after($td);
					}
				});
			} 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();
  }
});