MediaWiki:Common.js: различия между версиями
Перейти к навигации
Перейти к поиску
Нет описания правки |
Нет описания правки |
||
Строка 98: | Строка 98: | ||
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]] | * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]] | ||
*/ | */ | ||
// Collapsiblе: [[ВП:СБ]] | |||
function collapsibleTables() { | |||
var $btn, | |||
$a, | |||
tblIdx = 0, | |||
colTables = []; | |||
$( 'table.collapsible' ).each( function() { | |||
var $table = $( this ), | |||
$row = $table.find( 'tr' ).first(), | |||
$cell = $row.find( 'th' ).first(); | |||
if ( !$cell.length ) { | |||
return; | |||
} | |||
$table.attr( 'id', 'collapsibleTable' + tblIdx ); | |||
$btn = $( '<span>' ) | |||
.css( { | |||
'float': 'right', | |||
'font-weight': 'normal', | |||
'font-size': 'smaller' | |||
} ); | |||
$a = $( '<a>' ) | |||
.attr( 'id', 'collapseButton' + tblIdx ) | |||
.attr( 'href', 'javascript:collapseTable(' + tblIdx + ');' ) | |||
.css( 'color', $cell.css( 'color' ) ) | |||
.text( NavigationBarHide ) | |||
.appendTo( $btn ); | |||
if ( $cell.contents().length ) { | |||
$btn.insertBefore( $cell.contents().first() ); | |||
} else { | |||
$btn.appendTo( $cell ); | |||
} | |||
colTables[tblIdx++] = $table; | |||
} ); | |||
for ( var i = 0; i < tblIdx; i++ ) { | |||
if ( colTables[i].hasClass( 'collapsed' ) || | |||
( tblIdx > NavigationBarShowDefault && | |||
colTables[i].hasClass( 'autocollapse' ) | |||
) | |||
) { | |||
collapseTable( i ); | |||
} | |||
} | |||
} | |||
function collapseTable ( idx ) { | |||
var $table = $( '#collapsibleTable' + idx ), | |||
$rows = $table.find( 'tr' ), | |||
$btn = $( '#collapseButton' + idx ); | |||
if ( !$table.length || !$rows.length || !$btn.length ) { | |||
return false; | |||
} | |||
var isShown = ( $btn.text() === NavigationBarHide ), | |||
cssDisplay = isShown ? 'none' : $rows.first().css( 'display' ); | |||
$btn.text( isShown ? NavigationBarShow : NavigationBarHide ); | |||
$rows.slice( 1 ).each( function() { | |||
$( this ).css( 'display', cssDisplay ); | |||
} ); | |||
} | |||
function collapsibleDivs() { | |||
var navIdx = 0, | |||
colNavs = [], | |||
i; | |||
$( '#mw-content-text div.NavFrame' ).each( function() { | |||
var $navFrame = $( this ); | |||
$navFrame.attr( 'id', 'NavFrame' + navIdx ); | |||
var $a = $( '<a>' ) | |||
.addClass( 'NavToggle' ) | |||
.attr( 'id', 'NavToggle' + navIdx ) | |||
.attr( 'href', 'javascript:collapseDiv(' + navIdx + ');' ) | |||
.text( NavigationBarHide ); | |||
$navFrame.children( '.NavHead' ).append( $a ); | |||
colNavs[navIdx++] = $navFrame; | |||
} ); | |||
for ( i = 0; i < navIdx; i++ ) { | |||
if ( colNavs[i].hasClass( 'collapsed' ) || | |||
( navIdx > NavigationBarShowDefault && | |||
!colNavs[i].hasClass( 'expanded' ) | |||
) | |||
) { | |||
collapseDiv( i ); | |||
} | |||
} | |||
} | |||
function collapseDiv ( idx ) { | |||
var $div = $( '#NavFrame' + idx ), | |||
$btn = $( '#NavToggle' + idx ); | |||
if ( !$div.length || !$btn.length ) { | |||
return false; | |||
} | |||
var isShown = ( $btn.text() === NavigationBarHide ); | |||
$btn.text( isShown ? NavigationBarShow : NavigationBarHide ); | |||
$div.children( '.NavContent,.NavPic' ).each( function() { | |||
$( this ).css( 'display', isShown ? 'none' : 'block' ); | |||
} ); | |||
} | |||
function checkEmptyCreatebox() { | |||
$( 'form.createbox' ).on( 'submit', function() { | |||
if ( !$( this ).find( 'input.createboxInput' ).val() ) { | |||
window.alert( 'Не указано название новой статьи!' ); | |||
return false; | |||
} | |||
return true; | |||
} ); | |||
} | |||
var hasClass = ( function() { | var hasClass = ( function() { |
Версия от 07:20, 24 марта 2015
/* Размещённый здесь JavaScript код будет загружаться всем пользователям при обращении к каждой странице */
/** Collapsible tables *********************************************************
*
* Description: Allows tables to be collapsed, showing only the header. See
* http://www.mediawiki.org/wiki/Manual:Collapsible_tables.
* Maintainers: [[en:User:R. Koot]]
*/
var autoCollapse = 2;
var collapseCaption = 'скрыть';
var expandCaption = 'показать';
function collapseTable( tableIndex ) {
var Button = document.getElementById( 'collapseButton' + tableIndex );
var Table = document.getElementById( 'collapsibleTable' + tableIndex );
if ( !Table || !Button ) {
return false;
}
var Rows = Table.rows;
if ( Button.firstChild.data == collapseCaption ) {
for ( var i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = 'none';
}
Button.firstChild.data = expandCaption;
} else {
for ( var i = 1; i < Rows.length; i++ ) {
Rows[i].style.display = Rows[0].style.display;
}
Button.firstChild.data = collapseCaption;
}
}
function createCollapseButtons() {
var tableIndex = 0;
var NavigationBoxes = new Object();
var Tables = document.getElementsByTagName( 'table' );
for ( var i = 0; i < Tables.length; i++ ) {
if ( hasClass( Tables[i], 'collapsible' ) ) {
/* only add button and increment count if there is a header row to work with */
var HeaderRow = Tables[i].getElementsByTagName( 'tr' )[0];
if ( !HeaderRow ) {
continue;
}
var Header = HeaderRow.getElementsByTagName( 'th' )[0];
if ( !Header ) {
continue;
}
NavigationBoxes[tableIndex] = Tables[i];
Tables[i].setAttribute( 'id', 'collapsibleTable' + tableIndex );
var Button = document.createElement( 'span' );
var ButtonLink = document.createElement( 'a' );
var ButtonText = document.createTextNode( collapseCaption );
Button.className = 'collapseButton'; // Styles are declared in [[MediaWiki:Common.css]]
ButtonLink.style.color = Header.style.color;
ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
ButtonLink.setAttribute( 'href', "javascript:collapseTable(" + tableIndex + ");" );
ButtonLink.appendChild( ButtonText );
Button.appendChild( document.createTextNode( '[' ) );
Button.appendChild( ButtonLink );
Button.appendChild( document.createTextNode( ']' ) );
Header.insertBefore( Button, Header.childNodes[0] );
tableIndex++;
}
}
for ( var i = 0; i < tableIndex; i++ ) {
if ( hasClass( NavigationBoxes[i], 'collapsed' ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], 'autocollapse' ) ) ) {
collapseTable( i );
} else if ( hasClass( NavigationBoxes[i], 'innercollapse' ) ) {
var element = NavigationBoxes[i];
while ( element = element.parentNode ) {
if ( hasClass( element, 'outercollapse' ) ) {
collapseTable( i );
break;
}
}
}
}
}
addOnloadHook( createCollapseButtons );
/** Test if an element has a certain class **************************************
*
* Description: Uses regular expressions and caching for better performance.
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
*/
// Collapsiblе: [[ВП:СБ]]
function collapsibleTables() {
var $btn,
$a,
tblIdx = 0,
colTables = [];
$( 'table.collapsible' ).each( function() {
var $table = $( this ),
$row = $table.find( 'tr' ).first(),
$cell = $row.find( 'th' ).first();
if ( !$cell.length ) {
return;
}
$table.attr( 'id', 'collapsibleTable' + tblIdx );
$btn = $( '<span>' )
.css( {
'float': 'right',
'font-weight': 'normal',
'font-size': 'smaller'
} );
$a = $( '<a>' )
.attr( 'id', 'collapseButton' + tblIdx )
.attr( 'href', 'javascript:collapseTable(' + tblIdx + ');' )
.css( 'color', $cell.css( 'color' ) )
.text( NavigationBarHide )
.appendTo( $btn );
if ( $cell.contents().length ) {
$btn.insertBefore( $cell.contents().first() );
} else {
$btn.appendTo( $cell );
}
colTables[tblIdx++] = $table;
} );
for ( var i = 0; i < tblIdx; i++ ) {
if ( colTables[i].hasClass( 'collapsed' ) ||
( tblIdx > NavigationBarShowDefault &&
colTables[i].hasClass( 'autocollapse' )
)
) {
collapseTable( i );
}
}
}
function collapseTable ( idx ) {
var $table = $( '#collapsibleTable' + idx ),
$rows = $table.find( 'tr' ),
$btn = $( '#collapseButton' + idx );
if ( !$table.length || !$rows.length || !$btn.length ) {
return false;
}
var isShown = ( $btn.text() === NavigationBarHide ),
cssDisplay = isShown ? 'none' : $rows.first().css( 'display' );
$btn.text( isShown ? NavigationBarShow : NavigationBarHide );
$rows.slice( 1 ).each( function() {
$( this ).css( 'display', cssDisplay );
} );
}
function collapsibleDivs() {
var navIdx = 0,
colNavs = [],
i;
$( '#mw-content-text div.NavFrame' ).each( function() {
var $navFrame = $( this );
$navFrame.attr( 'id', 'NavFrame' + navIdx );
var $a = $( '<a>' )
.addClass( 'NavToggle' )
.attr( 'id', 'NavToggle' + navIdx )
.attr( 'href', 'javascript:collapseDiv(' + navIdx + ');' )
.text( NavigationBarHide );
$navFrame.children( '.NavHead' ).append( $a );
colNavs[navIdx++] = $navFrame;
} );
for ( i = 0; i < navIdx; i++ ) {
if ( colNavs[i].hasClass( 'collapsed' ) ||
( navIdx > NavigationBarShowDefault &&
!colNavs[i].hasClass( 'expanded' )
)
) {
collapseDiv( i );
}
}
}
function collapseDiv ( idx ) {
var $div = $( '#NavFrame' + idx ),
$btn = $( '#NavToggle' + idx );
if ( !$div.length || !$btn.length ) {
return false;
}
var isShown = ( $btn.text() === NavigationBarHide );
$btn.text( isShown ? NavigationBarShow : NavigationBarHide );
$div.children( '.NavContent,.NavPic' ).each( function() {
$( this ).css( 'display', isShown ? 'none' : 'block' );
} );
}
function checkEmptyCreatebox() {
$( 'form.createbox' ).on( 'submit', function() {
if ( !$( this ).find( 'input.createboxInput' ).val() ) {
window.alert( 'Не указано название новой статьи!' );
return false;
}
return true;
} );
}
var hasClass = ( function() {
var reCache = {};
return function( element, className ) {
return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
};
})();
//Collapsiblе: [[ВП:СБ]]
var hasClass = (function (){
var reCache = {}
return function (element, className){
return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className)
}
})()
function collapsibleTables(){
var Table, HRow, HCell, btn, a, tblIdx = 0, colTables = []
var allTables = document.getElementsByTagName('table')
for (var i=0; Table = allTables[i]; i++){
if (!hasClass(Table, 'collapsible')) continue
if (!(HRow=Table.rows[0])) continue
if (!(HCell=HRow.getElementsByTagName('th')[0])) continue
Table.id = 'collapsibleTable' + tblIdx
btn = document.createElement('span')
btn.style.cssText = 'float:right; font-weight:normal; font-size:smaller'
a = document.createElement('a')
a.id = 'collapseButton' + tblIdx
a.href = 'javascript:collapseTable(' + tblIdx + ');'
a.style.color = HCell.style.color
a.appendChild(document.createTextNode(NavigationBarHide))
btn.appendChild(a)
HCell.insertBefore(btn, HCell.childNodes[0])
colTables[tblIdx++] = Table
}
for (var i=0; i < tblIdx; i++)
if ((tblIdx > NavigationBarShowDefault && hasClass(colTables[i], 'autocollapse')) || hasClass(colTables[i], 'collapsed'))
collapseTable(i)
}
function collapseTable (idx){
var Table = document.getElementById('collapsibleTable' + idx)
var btn = document.getElementById('collapseButton' + idx)
if (!Table || !btn) return false
var Rows = Table.rows
var isShown = (btn.firstChild.data == NavigationBarHide)
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
var disp = isShown ? 'none' : Rows[0].style.display
for (var i=1; i < Rows.length; i++)
Rows[i].style.display = disp
}
function collapsibleDivs(){
var navIdx = 0, colNavs = [], i, NavFrame
var divs = document.getElementById('content').getElementsByTagName('div')
for (i=0; NavFrame = divs[i]; i++) {
if (!hasClass(NavFrame, 'NavFrame')) continue
NavFrame.id = 'NavFrame' + navIdx
var a = document.createElement('a')
a.className = 'NavToggle'
a.id = 'NavToggle' + navIdx
a.href = 'javascript:collapseDiv(' + navIdx + ');'
a.appendChild(document.createTextNode(NavigationBarHide))
for (var j=0; j < NavFrame.childNodes.length; j++)
if (hasClass(NavFrame.childNodes[j], 'NavHead'))
NavFrame.childNodes[j].appendChild(a)
colNavs[navIdx++] = NavFrame
}
for (i=0; i < navIdx; i++)
if ((navIdx > NavigationBarShowDefault && !hasClass(colNavs[i], 'expanded')) || hasClass(colNavs[i], 'collapsed'))
collapseDiv(i)
}
function collapseDiv(idx) {
var div = document.getElementById('NavFrame' + idx)
var btn = document.getElementById('NavToggle' + idx)
if (!div || !btn) return false
var isShown = (btn.firstChild.data == NavigationBarHide)
btn.firstChild.data = isShown ? NavigationBarShow : NavigationBarHide
var disp = isShown ? 'none' : 'block'
for (var child = div.firstChild; child != null; child = child.nextSibling)
if (hasClass(child, 'NavPic') || hasClass(child, 'NavContent'))
child.style.display = disp
}
var NavigationBarHide = '[скрыть]';
var NavigationBarShow = '[показать]';
var NavigationBarShowDefault = 2;
addOnloadHook( collapsibleDivs );