Diferencia entre revisiones de «MediaWiki:Gadget-ExcerptTree.js»

De Wikipedia, la enciclopedia libre
Contenido eliminado Contenido añadido
m Sophivorus trasladó la página Usuario:Sophivorus/ToK.js a MediaWiki:ToK.js sin dejar una redirección: Siguiendo Wikipedia:Café/Portal/Archivo/Propuestas/2019/08#Script_para_ver_el_árbol_de_herencia_de_contenidos y a falta de oposición y riesgos
Sin resumen de edición
(No se muestran 3 ediciones intermedias del mismo usuario)
Línea 16: Línea 16:
title = $( node ).children( 'a' ).text();
title = $( node ).children( 'a' ).text();
ToK.getChildren( title ).done( function ( data ) {
ToK.getChildren( title ).done( function ( data ) {
var pages = data.query.pages,
var page = data.query.pages[0],
key = Object.keys( pages )[0],
children = [];
children = pages[ key ].templates;
redirects = [];
page.redirects.forEach( function ( redirect ) {
if ( children ) {
redirects.push( redirect.title );
});
page.templates.forEach( function ( template ) {
if ( redirects.indexOf( template.title ) === -1 ) {
children.push( template.title );
}
});
if ( children.length ) {
ToK.addButton( node );
ToK.addButton( node );
var list = $( '<ul>' ).hide().data( 'virgin', true );
var list = $( '<ul>' ).hide().data( 'virgin', true );
children.forEach( function ( child ) {
children.forEach( function ( title ) {
var title = child.title,
var link = $( '<a>', { 'href': '/wiki/' + title, 'title': title } ).text( title ),
link = $( '<a>', { 'href': '/wiki/' + title, 'title': title } ).text( title ),
item = $( '<li>' ).html( link );
item = $( '<li>' ).html( link );
list.append( item );
list.append( item );
Línea 74: Línea 81:
titles: title,
titles: title,
action: 'query',
action: 'query',
formatversion: 2,
prop: 'templates',
prop: 'templates|redirects',
tlnamespace: '0|104',
tlnamespace: '0|104',
tllimit: 'max'
tllimit: 'max',
});
});
}
}

Revisión del 21:34 17 oct 2019

/**
 * ToK, for Tree of Knowledge, is a script for visualizing the tree of content inheritance in Wikipedia
 * By Sophivorus
 */
var ToK = {

	/**
	 * Initialization script
	 */
	init: function () {
		$( '.ToK li' ).each( ToK.addChildren );
	},

	addChildren: function () {
		var node = $( this ),
			title = $( node ).children( 'a' ).text();
		ToK.getChildren( title ).done( function ( data ) {
			var page = data.query.pages[0],
				children = [];
				redirects = [];
			page.redirects.forEach( function ( redirect ) {
				redirects.push( redirect.title );
			});
			page.templates.forEach( function ( template ) {
				if ( redirects.indexOf( template.title ) === -1 ) {
					children.push( template.title );
				}
			});
			if ( children.length ) {
				ToK.addButton( node );
				var list = $( '<ul>' ).hide().data( 'virgin', true );
				children.forEach( function ( title ) {
					var link = $( '<a>', { 'href': '/wiki/' + title, 'title': title } ).text( title ),
						item = $( '<li>' ).html( link );
					list.append( item );
				});
				node.append( list );
			}
		});
	},

	/**
	 * Add a clickable right-arrow to the given node
	 */
	addButton: function ( node ) {
		var button = $( '<span>' ).text( '►' ).css({
			'cursor': 'pointer',
			'position': 'absolute',
			'left': '-1em'
		});
		node.css({
			'list-style': 'none',
			'position': 'relative'
		}).prepend( button );
		button.click( node, ToK.toggleChildren );
	},

	toggleChildren: function ( event ) {
		var node = event.data,
			button = $( node ).children( 'span' ),
			list = $( node ).children( 'ul' ),
			virgin = list.data( 'virgin' );

		list.toggle();
		if ( virgin ) {
			list.data( 'virgin', false );
			list.children( 'li' ).each( ToK.addChildren );
			button.text( '▼' );
		} else {
			if ( list.is( ':visible' ) ) {
				button.text( '▼' );
			} else {
				button.text( '►' );
			}
		}
	},

	getChildren: function ( title ) {
		var api = new mw.Api();
		return api.get({
			titles: title,
			action: 'query',
			formatversion: 2,
			prop: 'templates|redirects',
			tlnamespace: '0|104',
			tllimit: 'max',
		});
	}
};

mw.loader.using( 'mediawiki.api', ToK.init );