MediaWiki:Livros/Wikilivros:Biblioteca.js

Nota: Depois de publicar, poderá ter de contornar a cache do seu navegador para ver as alterações.

  • Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
  • Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
  • Internet Explorer / Edge: Pressione Ctrl enquanto clica Recarregar, ou pressione Ctrl-F5
  • Opera: Pressione Ctrl-F5.
/**
 * Permite ocultar/exibir livros da biblioteca por nível de desenvolvimento
 * @author: Helder (https://github.com/he7d3r)
 */
/*jslint browser: true, white: true, plusplus: true*/
/*global jQuery, mediaWiki */
( function ( mw, $ ) {
'use strict';

function Bookshelf() {
    var that = this,
		done,
		progress = {};
	this.minLevel = 0;
	this.maxLevel = 8;
	done = this.minLevel - 1;

	function formatList() {
		var $div = mw.util.$content.find( '.bb .coluna div, .bb-finalizados div.etapa' );
		$div.addClass( 'etapa' );
		$div.find( 'li' ).each( function() {
			var $this = $(this),
				book = $this.text();
			if ( progress[ book ] >= that.minLevel && progress[ book ] <= that.maxLevel ) {
				$this.addClass( 'et-' + progress[ book ]);
			}
		});
	}
	this.doQuery = function ( category, level ) {
		function processCurrentQuery( resObj ) {
			var i,
				books = resObj.query.categorymembers,
				bookCount = books.length;
			for ( i = 0; i < bookCount; i++ ) {
				progress[ books[ i ].title ] = level;
			}
			done++;
			// Check if all categories were processed
			if ( done >= that.maxLevel ) {
				$( formatList );
			}
		}
		$.getJSON(
			mw.util.wikiScript( 'api' ), {
				'format': 'json',
				'action': 'query',
				'list': 'categorymembers',
				'cmprop': 'title',
				'cmlimit': '500',
				'cmtitle': category
			}, processCurrentQuery
		);
	};
	this.insertCheckboxes = function ( ) {
		$(function() {
			var $body = $( '#bodyContent' ),
			    //Estilo copiado de [[Wikilivros:Portal comunitário]].
				//Pode ser melhor definir uma classe para este tipo de caixa
				html = '<div class="caixaPP azul-limpo" style="text-align:center; font-size: 85%;">Mostrar livros na etapa: ',
				min = 2,
				check, i;
			for ( i = that.maxLevel; i >= that.minLevel; i-- ) {
				if ( i >= min || 'Wikilivros:Biblioteca/Etapa_de_desenvolvimento' === mw.config.get( 'wgPageName' ) ) {
					check = 'checked="checked" ';
				} else {
					check = '';
				}
				html+= '<input type="checkbox" ' + check +
					'id="mw-inputbox-et' + i + '" value="1" name="et' + i + '">'
					+ '<label for="mw-inputbox-et' + i + '" class="et-' + i +
					'" style="margin-right:1em; padding-left:15px;">' + i + ' de 8</label>';
				$body.toggleClass( 'hide-et-' + i, (i < min) );
			}
			html += '</div>';
			$body.find( 'div.caixaPP' ).first().after( html );
			$body.find( ':checkbox' ).each( function() {
				var $this = $(this),
					num = $this.attr('id');
				num = num.charAt( num.length-1 );
				$this.change( function() {
					$body.toggleClass( 'hide-et-' + num, !$this.is(':checked') );
				} );
			} );
		});
	};
}
var wbBookshelf = new Bookshelf();
if ( $.inArray( mw.config.get('wgAction'), [ 'view', 'purge' ] ) > -1 ) {
	mw.loader.using( ['mediawiki.util'], function() {
		var cat = 'Categoria:Livros_na_etapa_$1_de_8',
			i;
		for ( i = wbBookshelf.minLevel; i <= wbBookshelf.maxLevel; i++ ) {
			wbBookshelf.doQuery( cat.replace('$1', i), i );
		}
		wbBookshelf.insertCheckboxes();
	} );
}

}( mediaWiki, jQuery ) );