Difference between revisions of "MediaWiki:Common.js"

From GGBN Wiki
Jump to: navigation, search
(init_special_listfiles_add_file_usage() double save)
(init_special_listfiles_add_file_usage try with colum added)
 
(One intermediate revision by the same user not shown)
Line 32: Line 32:
 
         __proto__: Object
 
         __proto__: Object
 
             * */
 
             * */
    var api = new mw.Api();
+
var api = new mw.Api(),
    // $('td.TablePager_col_img_name').first().find('a').attr('href').replace(mw.config.get('wgArticlePath').replace('$1', ''), '');
+
zero_indexColImgName = parseInt($('table.listfiles tr:nth-child(2)').children('td.TablePager_col_img_name').index());
    $('td.TablePager_col_img_name').each(function () {
+
// $('td.TablePager_col_img_name').first().find('a').attr('href').replace(mw.config.get('wgArticlePath').replace('$1', ''), '');
      var $thisTdImgName = $(this)
+
$('table.listfiles tr:nth-child(1) th:nth-child('+ ( zero_indexColImgName + 1 ) + ')').after($('<th/>').append('File Usage'));
          , thisWikiFile=$thisTdImgName.find('a').first().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(
      if (thisWikiFile.length) {
+
mw.config.get('wgArticlePath').replace('$1', ''), ''
        api.get({
+
),
          action: 'query',
+
$td=$('<td/>').addClass('TablePager_col_img_fileusage')
          prop: 'fileusage',
+
 
          titles: thisWikiFile
+
;
        }).done(function (data) {
+
if (thisWikiFile.length) {
          // console.log(data);
+
api.get({
          if (typeof data['query'] ['pages'] != "undefined") {
+
action: 'query',
            $.each(data.query.pages, function(i_1, el_pages) {
+
prop: 'fileusage',
              if (typeof  el_pages['fileusage'] != "undefined") {
+
titles: thisWikiFile
                $list_tag=$('<ol/>').addClass('list-files-usage');
+
}).done(function (data) {
                $.each(el_pages['fileusage'], function(i_2, el_fileusage) {
+
// console.log(data);
                  var $tag=$('<a/>')
+
if (typeof data['query'] ['pages'] != "undefined") {
                      .attr('href', mw.util.getUrl(el_fileusage.title))
+
$.each(data.query.pages, function(i_1, el_pages) {
                      .append(el_fileusage.title);
+
if (typeof  el_pages['fileusage'] != "undefined") {
                  $list_tag.append($('<li/>').append($tag));
+
$list_tag=$('<ol/>');
                });
+
// $list_tag=$('<span/>').append('File is used on:').add('<ol/>');
                $thisTdImgName.append('&#32;<span>File is used on:</span>');
+
$.each(el_pages['fileusage'], function(i_2, el_fileusage) {
                $thisTdImgName.append($list_tag);
+
var $a=$('<a/>')
              }
+
.attr('href', mw.util.getUrl(el_fileusage.title))
            });
+
.append(el_fileusage.title);
          } else {
+
$list_tag.append($('<li/>').append($a));
            // console.log("DEBUG: data ['query'] ['pages'] not found ");
+
});
          }
+
$td.append($('<span/>').append('File is used on:'))
        });
+
.append($list_tag);
      } else{
+
}
        console.warn('DEBUG: thisWikiFile seems empty: ');
+
});
        console.warn(thisWikiFile);
+
$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"
 
   }// if "Special:ListFiles"
 
};// end init_special_listfiles_add_file_usage
 
};// end 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();
  }
});