مدیاویکی:Common.js: تفاوت میان نسخهها
از آیتالله گنجی
بدون خلاصۀ ویرایش |
بدون خلاصۀ ویرایش |
||
| خط ۱: | خط ۱: | ||
/* | /* پایگاه اطلاعرسانی آیتالله گنجی — MediaWiki:Common.js */ | ||
$( function () { | |||
'use strict'; | |||
/* تشخیص مطمئن کاربر مهمان و واردشده */ | |||
var isAnonymous = !mw.config.get( 'wgUserName' ); | |||
document.body.classList.toggle( 'ganji-anonymous', isAnonymous ); | |||
document.body.classList.toggle( 'ganji-authenticated', !isAnonymous ); | |||
/* یکسانسازی حروف و اعداد برای جستوجوی فارسی */ | |||
function normalize( value ) { | |||
return String( value || '' ) | |||
.replace( /ي/g, 'ی' ) | |||
.replace( /ك/g, 'ک' ) | |||
.replace( /[۰-۹]/g, function ( digit ) { | |||
return String( '۰۱۲۳۴۵۶۷۸۹'.indexOf( digit ) ); | |||
} ) | |||
.replace( /[٠-٩]/g, function ( digit ) { | |||
return String( '٠١٢٣٤٥٦٧٨٩'.indexOf( digit ) ); | |||
} ) | |||
.replace( /\s+/g, ' ' ) | |||
.trim() | |||
.toLowerCase(); | |||
} | |||
/* جستوجوی زنده در جدولهای آرشیو */ | |||
document.querySelectorAll( '.ganji-archive' ).forEach( function ( archive ) { | |||
var table = archive.querySelector( '.ganji-archive-table' ); | |||
if ( !table || archive.querySelector( '.ganji-table-search' ) ) { | |||
return; | |||
} | |||
var input = document.createElement( 'input' ); | |||
input.type = 'search'; | |||
input.className = 'ganji-table-search'; | |||
input.placeholder = 'جستوجو در جلسه، تاریخ و موضوعها…'; | |||
input.setAttribute( 'aria-label', 'جستوجو در جدول جلسات' ); | |||
var empty = document.createElement( 'div' ); | |||
empty.className = 'ganji-table-no-results'; | |||
empty.textContent = 'نتیجهای پیدا نشد.'; | |||
empty.hidden = true; | |||
archive.insertBefore( input, archive.firstChild ); | |||
archive.insertBefore( empty, input.nextSibling ); | |||
input.addEventListener( 'input', function () { | |||
var query = normalize( input.value ); | |||
var visible = 0; | |||
table.querySelectorAll( 'tbody tr' ).forEach( function ( row ) { | |||
var matched = !query || | |||
normalize( row.textContent ).includes( query ); | |||
row.hidden = !matched; | |||
if ( matched ) { | |||
visible++; | |||
} | |||
} ); | |||
empty.hidden = visible !== 0; | |||
} ); | |||
} ); | |||
/* ساخت فهرست مطالب در منوی کناری پوستهٔ Medik */ | |||
var sidebar = | |||
}); | document.querySelector( '#mw-navigation nav' ) || | ||
document.querySelector( '#mw-navigation' ) || | |||
document.querySelector( '.sidebar' ); | |||
var content = document.querySelector( '.mw-parser-output' ); | |||
if ( | |||
!sidebar || | |||
!content || | |||
sidebar.querySelector( '.ganji-sidebar-toc' ) | |||
) { | |||
return; | |||
} | |||
var headings = Array.prototype.slice.call( | |||
content.querySelectorAll( 'h2, h3, h4' ) | |||
).filter( function ( heading ) { | |||
return ( | |||
!heading.closest( '.toc' ) && | |||
!heading.closest( 'table' ) && | |||
!heading.classList.contains( 'mw-empty-elt' ) | |||
); | |||
} ); | |||
if ( !headings.length ) { | |||
return; | |||
} | |||
var toc = document.createElement( 'div' ); | |||
toc.className = 'ganji-sidebar-toc'; | |||
var tocTitle = document.createElement( 'div' ); | |||
tocTitle.className = 'ganji-sidebar-toc-title'; | |||
tocTitle.textContent = 'فهرست مطالب'; | |||
toc.appendChild( tocTitle ); | |||
var list = document.createElement( 'ul' ); | |||
headings.forEach( function ( heading, index ) { | |||
var headline = heading.querySelector( '.mw-headline' ); | |||
var target = headline || heading; | |||
if ( !target.id ) { | |||
target.id = 'ganji-section-' + ( index + 1 ); | |||
} | |||
var text = target.textContent | |||
.replace( /\[ویرایش\]/g, '' ) | |||
.trim(); | |||
if ( !text ) { | |||
return; | |||
} | |||
var item = document.createElement( 'li' ); | |||
item.className = | |||
'ganji-toc-level-' + heading.tagName.toLowerCase(); | |||
var link = document.createElement( 'a' ); | |||
link.href = '#' + target.id; | |||
link.textContent = text; | |||
link.addEventListener( 'click', function () { | |||
document.querySelectorAll( | |||
'.ganji-sidebar-toc a.is-active' | |||
).forEach( function ( activeLink ) { | |||
activeLink.classList.remove( 'is-active' ); | |||
} ); | |||
link.classList.add( 'is-active' ); | |||
} ); | |||
item.appendChild( link ); | |||
list.appendChild( item ); | |||
} ); | |||
toc.appendChild( list ); | |||
sidebar.appendChild( toc ); | |||
} ); | |||
نسخهٔ کنونی تا ۱۰ سپتامبر ۲۰۲۶، ساعت ۱۷:۵۴
/* پایگاه اطلاعرسانی آیتالله گنجی — MediaWiki:Common.js */
$( function () {
'use strict';
/* تشخیص مطمئن کاربر مهمان و واردشده */
var isAnonymous = !mw.config.get( 'wgUserName' );
document.body.classList.toggle( 'ganji-anonymous', isAnonymous );
document.body.classList.toggle( 'ganji-authenticated', !isAnonymous );
/* یکسانسازی حروف و اعداد برای جستوجوی فارسی */
function normalize( value ) {
return String( value || '' )
.replace( /ي/g, 'ی' )
.replace( /ك/g, 'ک' )
.replace( /[۰-۹]/g, function ( digit ) {
return String( '۰۱۲۳۴۵۶۷۸۹'.indexOf( digit ) );
} )
.replace( /[٠-٩]/g, function ( digit ) {
return String( '٠١٢٣٤٥٦٧٨٩'.indexOf( digit ) );
} )
.replace( /\s+/g, ' ' )
.trim()
.toLowerCase();
}
/* جستوجوی زنده در جدولهای آرشیو */
document.querySelectorAll( '.ganji-archive' ).forEach( function ( archive ) {
var table = archive.querySelector( '.ganji-archive-table' );
if ( !table || archive.querySelector( '.ganji-table-search' ) ) {
return;
}
var input = document.createElement( 'input' );
input.type = 'search';
input.className = 'ganji-table-search';
input.placeholder = 'جستوجو در جلسه، تاریخ و موضوعها…';
input.setAttribute( 'aria-label', 'جستوجو در جدول جلسات' );
var empty = document.createElement( 'div' );
empty.className = 'ganji-table-no-results';
empty.textContent = 'نتیجهای پیدا نشد.';
empty.hidden = true;
archive.insertBefore( input, archive.firstChild );
archive.insertBefore( empty, input.nextSibling );
input.addEventListener( 'input', function () {
var query = normalize( input.value );
var visible = 0;
table.querySelectorAll( 'tbody tr' ).forEach( function ( row ) {
var matched = !query ||
normalize( row.textContent ).includes( query );
row.hidden = !matched;
if ( matched ) {
visible++;
}
} );
empty.hidden = visible !== 0;
} );
} );
/* ساخت فهرست مطالب در منوی کناری پوستهٔ Medik */
var sidebar =
document.querySelector( '#mw-navigation nav' ) ||
document.querySelector( '#mw-navigation' ) ||
document.querySelector( '.sidebar' );
var content = document.querySelector( '.mw-parser-output' );
if (
!sidebar ||
!content ||
sidebar.querySelector( '.ganji-sidebar-toc' )
) {
return;
}
var headings = Array.prototype.slice.call(
content.querySelectorAll( 'h2, h3, h4' )
).filter( function ( heading ) {
return (
!heading.closest( '.toc' ) &&
!heading.closest( 'table' ) &&
!heading.classList.contains( 'mw-empty-elt' )
);
} );
if ( !headings.length ) {
return;
}
var toc = document.createElement( 'div' );
toc.className = 'ganji-sidebar-toc';
var tocTitle = document.createElement( 'div' );
tocTitle.className = 'ganji-sidebar-toc-title';
tocTitle.textContent = 'فهرست مطالب';
toc.appendChild( tocTitle );
var list = document.createElement( 'ul' );
headings.forEach( function ( heading, index ) {
var headline = heading.querySelector( '.mw-headline' );
var target = headline || heading;
if ( !target.id ) {
target.id = 'ganji-section-' + ( index + 1 );
}
var text = target.textContent
.replace( /\[ویرایش\]/g, '' )
.trim();
if ( !text ) {
return;
}
var item = document.createElement( 'li' );
item.className =
'ganji-toc-level-' + heading.tagName.toLowerCase();
var link = document.createElement( 'a' );
link.href = '#' + target.id;
link.textContent = text;
link.addEventListener( 'click', function () {
document.querySelectorAll(
'.ganji-sidebar-toc a.is-active'
).forEach( function ( activeLink ) {
activeLink.classList.remove( 'is-active' );
} );
link.classList.add( 'is-active' );
} );
item.appendChild( link );
list.appendChild( item );
} );
toc.appendChild( list );
sidebar.appendChild( toc );
} );