Difference between revisions of "MediaWiki:Common.js"

From GGBN Wiki
Jump to: navigation, search
(debug mw.api dependency)
(init_special_listfiles_add_file_usage try with colum added)
 
(6 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
+
  * Add file’s usage to Special:ListFiles
* @requires mw.config
+
  * @requires mw.config
* @requires mw.util
+
  * @requires mw.util
* @requires mw.Api
+
  * @requires mw.Api
*/
+
  */
    window.init_special_listfiles_add_file_usage = function () {
+
window.init_special_listfiles_add_file_usage = function () {
  
      if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){ // "Special:ListFiles"
+
  if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){ // "Special:ListFiles"
        /*
+
    /*
                * {batchcomplete: "", query: {…}}
+
    * {batchcomplete: "", query: {…}}
                    batchcomplete: ""
+
        batchcomplete: ""
                    query:
+
        query:
                        normalized: [{…}]
+
          normalized: [{…}]
                        pages:
+
          pages:
                            402:
+
            402:
                                fileusage: Array(1)
+
              fileusage: Array(1)
                                0:
+
              0:
                                    ns: 0
+
                ns: 0
                                    pageid: 90
+
                pageid: 90
                                    title: "News"
+
                title: "News"
                                    __proto__: Object
+
                __proto__: Object
                                length: 1
+
              length: 1
                                __proto__: Array(0)
+
              __proto__: Array(0)
                                ns: 6
+
              ns: 6
                                pageid: 402
+
              pageid: 402
                                title: "File:INVEMAR logo.png"
+
              title: "File:INVEMAR logo.png"
                            __proto__: Object
+
            __proto__: Object
                        __proto__: Object
+
          __proto__: Object
                    __proto__: Object
+
        __proto__: Object
                    __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', ''), ''
 
              )
 
          ;
 
          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
 
  
 +
$('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 91: Line 98:
 
}
 
}
  
$.when(
+
jQuery(document).ready(function($) {
   mw.loader.using( ['mediawiki.api', 'mediawiki.util'] ),
+
   if(mw.config.get("wgCanonicalSpecialPageName")==="Listfiles"){
  $.ready
+
    init_special_listfiles_add_file_usage();
)
+
  }
.then( 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();
  }
});