MediaWiki:Gadget-hide-empty-fields.js i Wikisłownikarz:Peter Bowman/hide-empty-fields.js: Różnica pomiędzy stronami
(Różnica między stronami)
Usunięta treść Dodana treść
filtrowanie wg przestrzeni teraz w gadgets-definitions |
dr. |
||
Linia 1: | Linia 1: | ||
//Skrypt ukrywający puste pola w artykułach. Przeróbka kodu do rozwijania skrótów autorstwa [[user:Sp5uhe]]. |
|||
/* |
|||
//Autor przeróbki: [[user:Olaf]] |
|||
* Skrypt ukrywający puste pola w artykułach. Przeróbka kodu do rozwijania |
|||
var gadget = window.hide_empty = { |
|||
* skrótów autorstwa [[User:Sp5uhe]]. |
|||
version: '8', |
|||
* Autorzy przeróbek: [[User:Olaf]], [[User:Peter Bowman]]. |
|||
show: false, |
|||
*/ |
|||
foundEmpty: false, |
|||
lang: {} |
|||
}; |
|||
var $content =$( '#mw-content-text' ).children(); |
|||
var hef = mw.libs.hideEmptyFields = { |
|||
state: null, |
|||
hasEmptyFields: null, |
|||
$caEmptyFields: $( [] ) |
|||
}, |
|||
L10n = { |
|||
'qqx': [ 'hide-empty-fields-hide', 'hide-empty-fields-show', 'hide-empty-fields-no-empty', 'hide-empty-fields-tooltip' ], |
|||
'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' ] |
|||
}, |
|||
config = mw.config.get( [ |
|||
'wgTitle', |
|||
'wgUserLanguage' |
|||
] ), |
|||
state = { |
|||
HIDE: 'hide', |
|||
SHOW: 'show' |
|||
}, |
|||
storageKey = 'ext.gadget.hide-empty-fields.state'; |
|||
hide_empty.strings = { |
|||
mw.libs.hideEmptyFields.browseContent = function ( $content ) { |
|||
'en': [ 'Hide empty fields', 'Show empty fields', 'No empty fields', 'Show/hide empty fields' ], |
|||
$content.find( '.field' ).each( function ( i, el ) { |
|||
'pl': [ 'Ukryj puste pola', 'Pokaż puste pola', 'Brak pustych pól', 'Pokaż/ukryj puste pola' ] |
|||
var $dt = $( el ).parent( 'dt' ), |
|||
}; |
|||
$dd = $dt.next( 'dd' ), |
|||
$dl = $dt.parent( 'dl' ); |
|||
// http://stackoverflow.com/questions/6548748/portability-of-nextelementsibling-nextsibling |
|||
function nextElementSibling( el ) { |
|||
do { el = el.nextSibling } while ( el && el.nodeType !== 1 ); |
|||
return el; |
|||
} |
|||
hide_empty.browse = function() { |
|||
$content.filter( 'dl:has(dt > span.field)' ).each( function() { |
|||
var nxtSbl = this.nextElementSibling || nextElementSibling( this ); |
|||
if ( nxtSbl && nxtSbl.nodeName !== 'DL' && ( |
|||
// [[Dyskusja szablonu:pole#Dwukropek]] |
|||
nxtSbl.nodeName === 'P' || // sekcja 'znaczenia' |
|||
nxtSbl.nodeName === 'UL' || // lista tłumaczeń |
|||
$dd = $dd.next( 'dd' ); |
|||
nxtSbl.nodeName === 'OL' // lista przypisów |
|||
} |
|||
) ) |
|||
return true; |
|||
var dd = this.getElementsByTagName( 'dd' )[ 0 ]; |
|||
var text = dd |
|||
? ( dd.textContent || dd.innerText ) |
|||
: null; |
|||
if ( !dd || !text || '(1.1) '.indexOf( text ) === 0 ) { |
|||
gadget.elements.push( this ); |
|||
gadget.foundEmpty = true; |
|||
} |
|||
} ); |
|||
}; |
|||
hide_empty.label = function() { |
|||
this.fields = {}; |
|||
$content.find( 'dt' ).each( function() { |
|||
if ( |
if ( |
||
$.inArray( this.parentNode, gadget.elements ) < 0 && |
|||
// puste pole |
|||
this.className && |
|||
this.className.indexOf( 'fldn-' ) !== -1 |
|||
// element pod polem, odpowiednio: znaczenia, tłumaczenia, warianty, źródła |
|||
!$dl.next().is( 'p, ul, table, .mw-references-wrap' ) && |
|||
// wyjątek dla tabelek typu `{| ... |}` w polu odmiany |
|||
// [[Specjalna:Niezmienny link/7541865#barszczowy]] |
|||
!$dl.next().is( 'dl:has(table.fldt-odmiana)' ) |
|||
) { |
) { |
||
var start = this.className.indexOf( 'fldn-' ); |
|||
var end = ( this.className + ' ' ).indexOf( ' ', start ); |
|||
mw.libs.hideEmptyFields.hasEmptyFields = true; |
|||
gadget.fields[ this.className.substring( start, end ) ] = true; |
|||
} |
} |
||
} ); |
} ); |
||
}; |
}; |
||
hide_empty.process = function() { |
|||
if ( this.elements.length === 0 ) { |
|||
$( document.body ).toggleClass( 'gadget-hide-empty-fields-hidden' ); |
|||
this.browse(); |
|||
if ( mw.libs.hideEmptyFields.state === state.SHOW ) { |
|||
mw.hook( 'sectionLinks.ready' ).add( function() { |
|||
mw.libs.hideEmptyFields.state = state.HIDE; |
|||
gadget.label(); |
|||
} else { |
|||
} ); |
|||
mw.libs.hideEmptyFields.state = state.SHOW; |
|||
} |
} |
||
window.scroller.scroll( 'h2', function() { |
|||
for ( var i = 0; i < gadget.elements.length; i++ ) { |
|||
gadget.elements[ i ].style.display = gadget.show |
|||
? 'block' |
|||
: 'none'; |
|||
} |
|||
} ); |
|||
var n = document.getElementById( 'ca-empty-fields' ); |
|||
mw.storage.set( storageKey, mw.libs.hideEmptyFields.state ); |
|||
}; |
|||
mw.libs.hideEmptyFields.updatePortletLink = function () { |
|||
var newText = ''; |
|||
if ( |
if ( n ) { |
||
var text = this.lang[ this.foundEmpty ? this.show ? 0 : 1 : 2 ]; |
|||
if ( mw.libs.hideEmptyFields.state === state.SHOW ) { |
|||
newText = mw.msg( 'hide-empty-fields-hide' ); |
|||
if ( n.firstChild.nodeName == 'SPAN' ) { |
|||
n.firstChild.firstChild.innerHTML = text; |
|||
} else { |
} else { |
||
n.firstChild.innerHTML = text; |
|||
newText = mw.msg( 'hide-empty-fields-show' ); |
|||
} |
} |
||
} else { |
|||
n.className = this.foundEmpty ? '' : 'new'; |
|||
newText = mw.msg( 'hide-empty-fields-no-empty' ); |
|||
} |
} |
||
jQuery.cookie( 'hideSections', ( this.show ? '0' : '1' ), { |
|||
mw.libs.hideEmptyFields.$caEmptyFields.find( 'a:not(:has(*)), a > span' ).text( newText ); |
|||
expires: 30, |
|||
path: '/' |
|||
} ); |
|||
}; |
}; |
||
hide_empty.switchEmptyFields = function() { |
|||
// filtrowanie wg przestrzeni nazw: zob. [[Słownik języka ukraińskiego]] + mechanizm słowa dnia |
|||
this.show = !this.show; |
|||
this.process(); |
|||
$.each( L10n[ config.wgUserLanguage ] || L10n.en, function ( i, message ) { |
|||
}; |
|||
mw.messages.set( L10n.qqx[ i ], message ); |
|||
} ); |
|||
mw.libs.hideEmptyFields.state = state.SHOW; |
|||
mw.libs.hideEmptyFields.hasEmptyFields = false; |
|||
hide_empty.init = function() { |
|||
$( function () { |
|||
var hist, url; |
|||
mw.libs.hideEmptyFields.$caEmptyFields = $( mw.util.addPortletLink( |
|||
'p-cactions', |
|||
'#', |
|||
mw.msg( 'hide-empty-fields-no-empty' ), |
|||
'ca-empty-fields', |
|||
mw.msg( 'hide-empty-fields-tooltip' ), |
|||
'1', |
|||
'#ca-history' |
|||
) ) |
|||
.on( 'click', function ( e ) { |
|||
mw.libs.hideEmptyFields.toggleState(); |
|||
mw.libs.hideEmptyFields.updatePortletLink(); |
|||
return false; |
|||
} ); |
|||
if ( !( hist = document.getElementById( 'ca-history' ) ) ) return; |
|||
if ( mw.storage.get( storageKey ) !== state.SHOW ) { |
|||
if ( !( url = hist.getElementsByTagName( 'a' )[ 0 ] ) ) return; |
|||
mw.libs.hideEmptyFields.toggleState(); |
|||
if ( !( url = url.href ) ) return; |
|||
this.lang = this.strings[ mw.config.get( 'wgUserLanguage' ) ]; |
|||
if ( !this.lang ) { |
|||
this.lang = this.strings.en; |
|||
} |
} |
||
mw.util.addPortletLink( |
|||
// mw.hook('wikipage.content') uruchamia sie czasem *przed* document.ready |
|||
'p-cactions', |
|||
mw.libs.hideEmptyFields.updatePortletLink(); |
|||
'javascript:hide_empty.switchEmptyFields();', |
|||
} ); |
|||
hide_empty.lang[ 1 ], |
|||
'ca-empty-fields', |
|||
mw.hook( 'wikipage.content' ).add( function ( $content ) { |
|||
hide_empty.lang[ 3 ], |
|||
mw.libs.hideEmptyFields.hasEmptyFields = false; |
|||
'F', |
|||
mw.libs.hideEmptyFields.browseContent( $content ); |
|||
hist |
|||
mw.libs.hideEmptyFields.updatePortletLink(); |
|||
); |
|||
var cookie = jQuery.cookie( 'hideSections' ); |
|||
// zapobiega wskokowi w niewlasciwe miejsce wskutek ukrycia pustych sekcji po zaladowaniu strony |
|||
this.show = ( cookie == '0' ); |
|||
//if ( location.hash ) location.hash = location.hash; |
|||
this.elements = []; |
|||
} ); |
|||
this.process(); |
|||
}; |
|||
if ( |
|||
mw.config.get( 'wgNamespaceNumber' ) === 0 && |
|||
mw.config.get( 'wgAction' ) == 'view' |
|||
) { |
|||
mw.hook( 'wikipage.content' ).add( function() { |
|||
gadget.init(); |
|||
} ); |
|||
} |