MediaWiki:Gadget-Substubb.js

Fra Wikipedia, den frie encyklopedi

Merk: Etter publisering vil det kanskje være nødvendig å slette mellomlageret i nettleseren din for å se endringene.

  • Firefox / Safari: Hold Shift mens du klikker på Oppdater, eller trykk enten Ctrl+F5 eller Ctrl+R (⌘+R på Mac)
  • Google Chrome: Trykk Ctrl+Shift+R (⌘+Shift+R på Mac)
  • Internet Explorer / Edge: Hold Ctrl mens du trykker på Oppdater eller trykk Ctrl+F5
  • Opera: Ttrykk Ctrl+F5.
/**
 * Description: Checks if there are pages in the categories 
 *  - 'Sider som må utvides' (Substubber)
 * and displays the number of pages in the toolbar if there are.
 * If the category is empty, this gadget is completely invisible.
 * 
 * Definition: Substubb[ResourceLoader|dependencies=mediawiki.api,mediawiki.title,mediawiki.uri,mediawiki.util]|Substubb.js
 * Maintainer: Danmichaelo
 */ 

( function() {

    "use strict";

    $(function(){

        var api, items;

        api = new mw.Api();
        items = [{
            id: 'pt-hurtigslett',
            category: 'Sider som må utvides',
            label: 'Substubber',
            tooltip: 'Gå til kategorien'
        }];

        $.each( items, function( key, config ) {

            var title, url, request;

            title = new mw.Title( config.category, 14 );
            try {
            	url = new mw.Uri( title.getUrl() );
            } catch (e) {
            	return; // malformed URI
            }
            request = {
                format: 'json',
                action: 'query',
                list: 'categorymembers',
                cmtitle: title.toString(),
                cmlimit: 11,
                cmtype: 'page'  // excludes subcategories
            };

            api.get( request ).done( function( data ){

                var count, label, li, currentUrl;

                count = data.query.categorymembers.length;
                if (count === 0) {
                	// If the category's empty, there's no need to show the link
                	return;
                }
                if (count === request.cmlimit) count = '> ' + (count - 1);

                label = config.label + ': ' + count;

                li = mw.util.addPortletLink(
                    'p-views',
                    url,
                    label,
                    config.id,
                    config.tooltip
                    // null,
                    // '#pt-preferences'
                );

                currentUrl = new mw.Uri();

                if ( currentUrl.toString() === url.toString() ) {
                	$(li).addClass('selected');
                }

            } );

        } );

    } );

} )();