MediaWiki:Gadget-hide-empty-fields.js: Różnice pomiędzy wersjami

Z Wikisłownika – wolnego słownika wielojęzycznego
Usunięta treść Dodana treść
jest jakis problem z mw.util.$content[ 0 ]
niech iteruje po wezlach kolekcji $content - zdaje sie, ze zabraklo jakiejs niezbednej zaleznosci albo cos sie zmienilo ostatnio w mw.util; wlasciwosc $content wciaz ma wartosc 'null' po odswiezeniu cache strony pod Chrome, zwykle F5 dziala bez zarzutow
Linia 3: Linia 3:


window.hide_empty = {
window.hide_empty = {
version: '7.7dbg2',
version: '7.8',
show: false,
show: false,
foundEmpty: false,
foundEmpty: false,
lang: {}
lang: {},
contentNode: null
};
};


Linia 15: Linia 16:


hide_empty.browse = function ( node ) {
hide_empty.browse = function ( node ) {
var subNode, nodeName, start, end, text, j;
for ( var j = 0; j < node.childNodes.length; j++ ) {
var subNode = node.childNodes[ j ];
for ( j = 0; j < node.childNodes.length; j++ ) {
var nodeName = subNode.nodeName.toLowerCase();
subNode = node.childNodes[ j ];
nodeName = subNode.nodeName.toLowerCase();
if ( subNode.nodeType !== 1 ) {
if ( subNode.nodeType !== 1 ) {
Linia 26: Linia 29:
}
}
if ( nodeName !== 'dl' /*&& !subNode.className*/ ) {
if ( nodeName !== 'dl' /*&& !subNode.className*/ ) {
var start = subNode.className.indexOf( 'fldn-' );
start = subNode.className.indexOf( 'fldn-' );
if ( start < 0 ) {
if ( start < 0 ) {
Linia 32: Linia 35:
}
}
var end = ( subNode.className + ' ' ).indexOf( ' ', start );
end = ( subNode.className + ' ' ).indexOf( ' ', start );
if ( end < 0 ) {
if ( end < 0 ) {
Linia 38: Linia 41:
}
}
var text = subNode.textContent || subNode.innerText;
text = subNode.textContent || subNode.innerText;
if (
if (
Linia 54: Linia 57:


hide_empty.process = function () {
hide_empty.process = function () {
var fields, subNode, start, end, i, n, text;
this.fields = {};
this.fields = {};
this.browse( this.contentNode );
console.log(10);
console.log(mw.util);
console.log(mw.util.$content);
console.log(mw.util.$content[ 0 ]);
var content = mw.util.$content[ 0 ];
console.log(11);
this.browse( content );
console.log(12);
this.foundEmpty = false;
this.foundEmpty = false;
var fields = document.getElementsByTagName( 'dt' );
fields = document.getElementsByTagName( 'dt' );
console.log(3);
console.log(fields);
for ( var i = 0; i < fields.length; i++ ) {
for ( i = 0; i < fields.length; i++ ) {
var subNode = fields[ i ];
subNode = fields[ i ];
if ( !subNode.className ) {
if ( !subNode.className ) {
Linia 74: Linia 72:
}
}
var start = subNode.className.indexOf( 'fldn-' );
start = subNode.className.indexOf( 'fldn-' );
if ( start < 0 ) {
if ( start < 0 ) {
Linia 80: Linia 78:
}
}
var end = ( subNode.className + ' ' ).indexOf( ' ', start );
end = ( subNode.className + ' ' ).indexOf( ' ', start );
if ( end < 0 ) {
if ( end < 0 ) {
Linia 92: Linia 90:
fields[ i ].parentNode.style.display = ( this.show ? 'block' : 'none' );
fields[ i ].parentNode.style.display = ( this.show ? 'block' : 'none' );
}
}
console.log(14);
var n = document.getElementById( 'ca-empty-fields' );
n = document.getElementById( 'ca-empty-fields' );
console.log(15);
console.log(n);
if ( n ) {
if ( n ) {
var text = this.lang[ this.foundEmpty ? this.show ? 0 : 1 : 2 ];
text = this.lang[ this.foundEmpty ? this.show ? 0 : 1 : 2 ];
if ( n.firstChild.nodeName.toLowerCase() === 'span' ) {
if ( n.firstChild.nodeName.toLowerCase() === 'span' ) {
Linia 107: Linia 104:
n.className = this.foundEmpty ? '' : 'new';
n.className = this.foundEmpty ? '' : 'new';
}
}
console.log(16);
$.cookie( 'hideSections', ( this.show ? '0' : '1' ), {
$.cookie( 'hideSections', ( this.show ? '0' : '1' ), {
expires: 30,
expires: 30,
path: '/'
path: '/'
} );
} );
console.log(17);
};
};


Linia 120: Linia 116:
};
};


hide_empty.init = function () {
hide_empty.init = function ( node ) {
var hist, url;
var hist, url, cookie;
console.log(5);
if ( !( hist = document.getElementById( 'ca-history' ) ) ) return;
if ( !( hist = document.getElementById( 'ca-history' ) ) ) return;
if ( !( url = hist.getElementsByTagName( 'a' )[ 0 ] ) ) return;
if ( !( url = hist.getElementsByTagName( 'a' )[ 0 ] ) ) return;
if ( !( url = url.href ) ) return;
if ( !( url = url.href ) ) return;
console.log(6);
this.contentNode = node;
this.lang = this.strings[ mw.config.get( 'wgUserLanguage' ) ];
this.lang = this.strings[ mw.config.get( 'wgUserLanguage' ) ];
Linia 146: Linia 144:
hide_empty.switchEmptyFields();
hide_empty.switchEmptyFields();
} );
} );
console.log(7);
var cookie = $.cookie( 'hideSections' );
cookie = $.cookie( 'hideSections' );
this.show = ( cookie == '0' );
this.show = ( cookie == '0' );
this.process();
this.process();
console.log(8);
// zapobiega wskokowi w niewlasciwe miejsce wskutek ukrycia pustych sekcji po zaladowaniu strony
// zapobiega wskokowi w niewlasciwe miejsce wskutek ukrycia pustych sekcji po zaladowaniu strony
if ( location.hash ) location.hash = location.hash;
if ( location.hash ) location.hash = location.hash;
console.log(9);
};
};


Linia 160: Linia 157:
mw.config.get( 'wgAction' ) === 'view'
mw.config.get( 'wgAction' ) === 'view'
) {
) {
console.log(1);
mw.hook( 'sectionLinks.ready' ).add( function ( $content ) {
mw.hook( 'sectionLinks.ready' ).add( function ( $content ) {
hide_empty.init( $content[ 0 ] );
console.log(2);
hide_empty.init();
console.log(3);
mw.hook( 'hideEmptyFields.ready' ).fire( $content );
mw.hook( 'hideEmptyFields.ready' ).fire( $content );
console.log(4);
} );
} );
}
}

Wersja z 21:14, 29 lip 2017

// Skrypt ukrywający puste pola w artykułach. Przeróbka kodu do rozwijania skrótów autorstwa [[user:Sp5uhe]].
// Autor przeróbki: [[user:Olaf]]

window.hide_empty = {
	version: '7.8',
	show: false,
	foundEmpty: false,
	lang: {},
	contentNode: null
};

hide_empty.strings = {
	'en': [ 'Hide empty fields', 'Show empty fields', 'No empty fields', 'Show/hide empty fields' ],
	'pl': [ 'Ukryj puste pola', 'Pokaż puste pola', 'Brak pustych pól', 'Pokaż/ukryj puste pola' ]
};

hide_empty.browse = function ( node ) {
	var subNode, nodeName, start, end, text, j;
	
	for ( j = 0; j < node.childNodes.length; j++ ) {
		subNode = node.childNodes[ j ];
		nodeName = subNode.nodeName.toLowerCase();
		
		if ( subNode.nodeType !== 1 ) {
			continue;
		}
		if ( nodeName !== 'dt' ) {
			this.browse( subNode );
		}
		if ( nodeName !== 'dl' /*&& !subNode.className*/ ) {
			start = subNode.className.indexOf( 'fldn-' );
			
			if ( start < 0 ) {
				continue;
			}
			
			end = ( subNode.className + ' ' ).indexOf( ' ', start );
			
			if ( end < 0 ) {
				continue;
			}
			
			text = subNode.textContent || subNode.innerText;
			
			if (
				nodeName !== 'dt' &&
				nodeName !== 'a' &&			// patrollink
				nodeName !== 'noscript' &&	// koncowy element 'noscript' u niezalogowanych uzytkownikow
				text &&
				'(1.1) '.indexOf( text ) !== 0
			) {
				this.fields[ subNode.className.substring( start, end ) ] = true;
			}
		}
	}
};

hide_empty.process = function () {
	var fields, subNode, start, end, i, n, text;
	
	this.fields = {};
	this.browse( this.contentNode );
	this.foundEmpty = false;
	
	fields = document.getElementsByTagName( 'dt' );
	
	for ( i = 0; i < fields.length; i++ ) {
		subNode = fields[ i ];
		
		if ( !subNode.className ) {
			continue;
		}
		
		start = subNode.className.indexOf( 'fldn-' );
		
		if ( start < 0 ) {
			continue;
		}
		
		end = ( subNode.className + ' ' ).indexOf( ' ', start );
		
		if ( end < 0 ) {
			continue;
		}
		if ( this.fields[ subNode.className.substring( start, end ) ] ) {
			continue;
		}

		this.foundEmpty = true;
		fields[ i ].parentNode.style.display = ( this.show ? 'block' : 'none' );
	}
	
	n = document.getElementById( 'ca-empty-fields' );
	
	if ( n ) {
		text = this.lang[ this.foundEmpty ? this.show ? 0 : 1 : 2 ];
		
		if ( n.firstChild.nodeName.toLowerCase() === 'span' ) {
			n.firstChild.firstChild.innerHTML = text;
		} else {
			n.firstChild.innerHTML = text;
		}
		
		n.className = this.foundEmpty ? '' : 'new';
	}
	
	$.cookie( 'hideSections', ( this.show ? '0' : '1' ), {
		expires: 30,
		path: '/'
	} );
};

hide_empty.switchEmptyFields = function () {
	this.show = !this.show;
	this.process();
};

hide_empty.init = function ( node ) {
	var hist, url, cookie;
	
	if ( !( hist = document.getElementById( 'ca-history' ) ) ) return;
	if ( !( url = hist.getElementsByTagName( 'a' )[ 0 ] ) ) return;
	if ( !( url = url.href ) ) return;
	
	this.contentNode = node;
	
	this.lang = this.strings[ mw.config.get( 'wgUserLanguage' ) ];
	
	if ( !this.lang ) {
		this.lang = this.strings.en;
	}
	
	$( mw.util.addPortletLink(
		'p-cactions',
		'#',
		hide_empty.lang[ 1 ],
		'ca-empty-fields',
		hide_empty.lang[ 3 ],
		'F',
		hist
	) )
	.on( 'click', function ( e ) {
		e.preventDefault();
		hide_empty.switchEmptyFields();
	} );
	
	cookie = $.cookie( 'hideSections' );
	this.show = ( cookie == '0' );
	this.process();
	
	// zapobiega wskokowi w niewlasciwe miejsce wskutek ukrycia pustych sekcji po zaladowaniu strony
	if ( location.hash ) location.hash = location.hash;
};

if (
	mw.config.get( 'wgNamespaceNumber' ) === 0 &&
	mw.config.get( 'wgAction' ) === 'view'
) {
	mw.hook( 'sectionLinks.ready' ).add( function ( $content ) {
		hide_empty.init( $content[ 0 ] );
		mw.hook( 'hideEmptyFields.ready' ).fire( $content );
	} );
}