MediaWiki:Common.js: Różnice pomiędzy wersjami
Z LawRus
auto-sync z repo |
auto-sync z repo |
||
| Linia 1: | Linia 1: | ||
/* | /* --- LawRus -- Common.js --------------------------------------------------- | ||
1. Przenoszenie sekcji | 1. Przenoszenie sekcji uczestnikow pod wlasciwe sekcje Umow w formularzu. | ||
2. Walidacja formularza WpisKroniki przed zapisem (status | 2. Walidacja formularza WpisKroniki przed zapisem (status >= 4). | ||
3. Skracanie streszczenia w tabelach Cargo do 100 | 3. Skracanie streszczenia w tabelach Cargo do 100 znakow + rozwin. | ||
-------------------------------------------------------------------------- */ | |||
/* | /* DataTables: wylacz autoWidth zanim Cargo zainicjuje tabele. | ||
Kolumna streszczenie = zawsze td:nth-child(2) w | preInit.dt odpala przed kazdym new DataTable() -- niezaleznie od kolejnosci | ||
Przypisanie do window.lawrusTruncateAll pozwala | ladowania modulow. Bez tego DataTables nadpisuje inline-style'em nasze | ||
CSS-owe szerokosci kolumn. */ | |||
$( document ).on( 'preInit.dt', function ( e, settings ) { | |||
settings.oFeatures.bAutoWidth = false; | |||
if ( !$( settings.nTable ).hasClass( 'cargoDynamicTable' ) ) { return; } | |||
/* Usun separatory tysiecy z kolumn roku (dodawane przez formatNum w EN locale). | |||
Lapi "1,888" / "1 888" / "1 888" -- zwraca "1888". */ | |||
var stripThousands = function ( data ) { | |||
if ( !data && data !== 0 ) { return data; } | |||
return String( data ).replace( /^(\d{1,3})[, ](\d{3})$/, '$1$2' ); | |||
}; | |||
settings.aoColumnDefs = settings.aoColumnDefs || []; | |||
settings.aoColumnDefs.push( { | |||
targets: [ 3, 4 ], | |||
render: stripThousands | |||
} ); | |||
} ); | |||
/* -- Skracanie streszczenia ------------------------------------------------- | |||
Kolumna streszczenie = zawsze td:nth-child(2) w kazdym LawRus cargo query. | |||
Przypisanie do window.lawrusTruncateAll pozwala wywolac ja z konsoli. */ | |||
window.lawrusTruncateAll = function () { | window.lawrusTruncateAll = function () { | ||
| Linia 34: | Linia 57: | ||
}; | }; | ||
/* draw.dt | /* draw.dt -- kazdy redraw DataTables (init, sort, filtr, paginacja) */ | ||
$( document ).on( 'draw.dt', window.lawrusTruncateAll ); | $( document ).on( 'draw.dt', window.lawrusTruncateAll ); | ||
/* | /* Klikniecia -- delegowane, niezalezne od DataTables */ | ||
$( document ).on( 'click', '.lawrus-more', function ( e ) { | $( document ).on( 'click', '.lawrus-more', function ( e ) { | ||
e.preventDefault(); | e.preventDefault(); | ||
| Linia 51: | Linia 74: | ||
} ); | } ); | ||
/* window.load | /* window.load -- odpala po zaladowaniu WSZYSTKICH zasobow strony (DataTables | ||
na pewno gotowe do tego momentu). */ | na pewno gotowe do tego momentu). */ | ||
$( window ).on( 'load', function () { | $( window ).on( 'load', function () { | ||
| Linia 119: | Linia 142: | ||
function lawrusReorderParticipants() { | function lawrusReorderParticipants() { | ||
/* PageForms wymaga, | /* PageForms wymaga, zeby bloki {{{for template|...|multiple}}} byly poza | ||
glownym szablonem -- przez to div#lawrus-uczestnicy-* laduje na dole | |||
formularza. Przenosimy je | formularza. Przenosimy je bezposrednio pod odpowiednie sekcje 3/4. | ||
Uzywamy detach()+insertAfter() zeby zachowac event-handlery jQuery. | |||
v2 | v2 -- wywolanie przez setTimeout(0) gwarantuje uruchomienie po PageForms. */ | ||
var $sec3 = $( '#lawrus-sec3' ); | var $sec3 = $( '#lawrus-sec3' ); | ||
var $sec4 = $( '#lawrus-sec4' ); | var $sec4 = $( '#lawrus-sec4' ); | ||
| Linia 136: | Linia 159: | ||
} | } | ||
} | } | ||
/* -- LawRusWorkflow: CSRF token injection ------------------------------------ | |||
Wypelnia pole wptoken w formularzach .lawrus-action-form tokenem sesji. | |||
Musi dzialac przed wyslaniem formularza -- ladowanie synchroniczne z mw.loader. */ | |||
mw.loader.using( 'mediawiki.user' ).done( function () { | |||
var token = mw.user.tokens.get( 'csrfToken' ); | |||
$( document ).on( 'submit', '.lawrus-action-form', function () { | |||
$( this ).find( 'input[name="wptoken"]' ).val( token ); | |||
} ); | |||
// Wypelnij od razu przy ladowaniu (dla formularzy juz widocznych) | |||
$( '.lawrus-action-form input[name="wptoken"]' ).val( token ); | |||
} ); | |||
$( function () { | $( function () { | ||
| Linia 152: | Linia 187: | ||
} | } | ||
/* Timeouty | /* Timeouty -- pokrywaja rozne scenariusze async ladowania Cargo/DataTables */ | ||
window.lawrusTruncateAll(); | window.lawrusTruncateAll(); | ||
setTimeout( window.lawrusTruncateAll, 300 ); | setTimeout( window.lawrusTruncateAll, 300 ); | ||
| Linia 158: | Linia 193: | ||
setTimeout( window.lawrusTruncateAll, 2000 ); | setTimeout( window.lawrusTruncateAll, 2000 ); | ||
/* | /* -- Walidacja przed zapisem (tylko formularz WpisKroniki) -- */ | ||
var $form = $( '#pfForm' ); | var $form = $( '#pfForm' ); | ||
if ( !$form.length ) return; | if ( !$form.length ) return; | ||
| Linia 164: | Linia 199: | ||
var REQUIRED_AT_4 = [ | var REQUIRED_AT_4 = [ | ||
{ id: 'input_datacja_rok_lacinski', label: 'Rok AD' }, | { id: 'input_datacja_rok_lacinski', label: 'Rok AD' }, | ||
{ id: 'input_jezyk_oryginalu', label: ' | { id: 'input_jezyk_oryginalu', label: 'Jezyk oryginalu' }, | ||
{ id: 'input_kraj', label: 'Kraj' }, | { id: 'input_kraj', label: 'Kraj' }, | ||
{ id: 'input_tekst_zrodlowy', label: 'Tekst | { id: 'input_tekst_zrodlowy', label: 'Tekst zrodlowy' }, | ||
{ id: 'input_bibliografia', label: 'Bibliografia' }, | { id: 'input_bibliografia', label: 'Bibliografia' }, | ||
{ id: 'input_streszczenie_pl', label: 'Streszczenie PL' }, | { id: 'input_streszczenie_pl', label: 'Streszczenie PL' }, | ||
{ id: 'input_streszczenie_en', label: 'Streszczenie EN' }, | { id: 'input_streszczenie_en', label: 'Streszczenie EN' }, | ||
{ id: 'input_tlumaczenie_pl', label: ' | { id: 'input_tlumaczenie_pl', label: 'Tlumaczenie PL' }, | ||
{ id: 'input_tlumaczenie_en', label: ' | { id: 'input_tlumaczenie_en', label: 'Tlumaczenie EN' } | ||
]; | ]; | ||
var HIGH_STATUS = [ '4-do-publikacji', '5-opublikowane' ]; | var HIGH_STATUS = [ '4-do-publikacji', '5-opublikowane' ]; | ||
| Linia 193: | Linia 228: | ||
mw.loader.using( 'mediawiki.notification' ).then( function () { | mw.loader.using( 'mediawiki.notification' ).then( function () { | ||
mw.notify( | mw.notify( | ||
'Przy statusie | 'Przy statusie "' + status + '" wymagane sa: ' + names + '. Uzupelnij przed zapisem.', | ||
{ type: 'error', autoHide: false, tag: 'lawrus-validation' } | { type: 'error', autoHide: false, tag: 'lawrus-validation' } | ||
); | ); | ||
Wersja z 14:07, 27 maj 2026
/* --- LawRus -- Common.js ---------------------------------------------------
1. Przenoszenie sekcji uczestnikow pod wlasciwe sekcje Umow w formularzu.
2. Walidacja formularza WpisKroniki przed zapisem (status >= 4).
3. Skracanie streszczenia w tabelach Cargo do 100 znakow + rozwin.
-------------------------------------------------------------------------- */
/* DataTables: wylacz autoWidth zanim Cargo zainicjuje tabele.
preInit.dt odpala przed kazdym new DataTable() -- niezaleznie od kolejnosci
ladowania modulow. Bez tego DataTables nadpisuje inline-style'em nasze
CSS-owe szerokosci kolumn. */
$( document ).on( 'preInit.dt', function ( e, settings ) {
settings.oFeatures.bAutoWidth = false;
if ( !$( settings.nTable ).hasClass( 'cargoDynamicTable' ) ) { return; }
/* Usun separatory tysiecy z kolumn roku (dodawane przez formatNum w EN locale).
Lapi "1,888" / "1 888" / "1 888" -- zwraca "1888". */
var stripThousands = function ( data ) {
if ( !data && data !== 0 ) { return data; }
return String( data ).replace( /^(\d{1,3})[, ](\d{3})$/, '$1$2' );
};
settings.aoColumnDefs = settings.aoColumnDefs || [];
settings.aoColumnDefs.push( {
targets: [ 3, 4 ],
render: stripThousands
} );
} );
/* -- Skracanie streszczenia -------------------------------------------------
Kolumna streszczenie = zawsze td:nth-child(2) w kazdym LawRus cargo query.
Przypisanie do window.lawrusTruncateAll pozwala wywolac ja z konsoli. */
window.lawrusTruncateAll = function () {
$( 'table.cargoDynamicTable tbody tr' ).each( function () {
var $td = $( this ).children( 'td' ).eq( 1 );
if ( !$td.length ) { return; }
if ( $td.find( '.lawrus-short' ).length ) { return; }
var text = $td.text().replace( /\s+/g, ' ' ).trim();
if ( text.length <= 100 ) { return; }
var e = function ( s ) {
return s.replace( /&/g, '&' )
.replace( /</g, '<' )
.replace( />/g, '>' );
};
$td.html(
'<span class="lawrus-short">' +
e( text.substring( 0, 100 ) ) +
'<a href="#" class="lawrus-more" style="color:#3366cc;cursor:pointer;margin-left:2px">…</a>' +
'</span>' +
'<span class="lawrus-full-wrap" style="display:none">' +
e( text ) +
' <a href="#" class="lawrus-less" style="color:#3366cc;cursor:pointer">↑</a>' +
'</span>'
);
} );
};
/* draw.dt -- kazdy redraw DataTables (init, sort, filtr, paginacja) */
$( document ).on( 'draw.dt', window.lawrusTruncateAll );
/* Klikniecia -- delegowane, niezalezne od DataTables */
$( document ).on( 'click', '.lawrus-more', function ( e ) {
e.preventDefault();
var $td = $( this ).closest( 'td' );
$td.find( '.lawrus-short' ).hide();
$td.find( '.lawrus-full-wrap' ).show();
} );
$( document ).on( 'click', '.lawrus-less', function ( e ) {
e.preventDefault();
var $td = $( this ).closest( 'td' );
$td.find( '.lawrus-full-wrap' ).hide();
$td.find( '.lawrus-short' ).show();
} );
/* window.load -- odpala po zaladowaniu WSZYSTKICH zasobow strony (DataTables
na pewno gotowe do tego momentu). */
$( window ).on( 'load', function () {
window.lawrusTruncateAll();
setTimeout( window.lawrusTruncateAll, 300 );
} );
function lawrusInitLangSwitch() {
var $entries = $( '.wpis-kroniki' );
if ( !$entries.length ) return;
var isEn = ( mw.config.get( 'wgUserLanguage' ) === 'en' );
$entries.each( function () {
var $entry = $( this );
if ( !$entry.find( '.lawrus-lang-pl, .lawrus-lang-en' ).length ) return;
if ( isEn ) {
$entry.addClass( 'lang-en' );
}
var $sw = $(
'<div class="lawrus-lang-switch">' +
'<button data-lang="pl"' + ( !isEn ? ' class="active"' : '' ) + '>PL</button>' +
'<button data-lang="en"' + ( isEn ? ' class="active"' : '' ) + '>EN</button>' +
'</div>'
);
$entry.prepend( $sw );
$sw.on( 'click', 'button', function () {
var lang = $( this ).data( 'lang' );
var url = new URL( window.location.href );
if ( lang === 'en' ) {
url.searchParams.set( 'uselang', 'en' );
} else {
url.searchParams.delete( 'uselang' );
}
window.location.href = url.toString();
} );
} );
}
function lawrusInitDrilldownLangSwitch() {
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Drilldown' ) return;
var isEn = ( mw.config.get( 'wgUserLanguage' ) === 'en' );
var $sw = $(
'<div class="lawrus-lang-switch" style="margin-bottom:1em;">' +
'<button data-lang="pl"' + ( !isEn ? ' class="active"' : '' ) + '>PL</button>' +
'<button data-lang="en"' + ( isEn ? ' class="active"' : '' ) + '>EN</button>' +
'</div>'
);
$( '#mw-content-text' ).prepend( $sw );
$sw.on( 'click', 'button', function () {
var lang = $( this ).data( 'lang' );
var url = new URL( window.location.href );
if ( lang === 'en' ) {
url.searchParams.set( 'uselang', 'en' );
} else {
url.searchParams.delete( 'uselang' );
}
window.location.href = url.toString();
} );
}
function lawrusReorderParticipants() {
/* PageForms wymaga, zeby bloki {{{for template|...|multiple}}} byly poza
glownym szablonem -- przez to div#lawrus-uczestnicy-* laduje na dole
formularza. Przenosimy je bezposrednio pod odpowiednie sekcje 3/4.
Uzywamy detach()+insertAfter() zeby zachowac event-handlery jQuery.
v2 -- wywolanie przez setTimeout(0) gwarantuje uruchomienie po PageForms. */
var $sec3 = $( '#lawrus-sec3' );
var $sec4 = $( '#lawrus-sec4' );
var $krajowa = $( '#lawrus-uczestnicy-krajowa' );
var $miedz = $( '#lawrus-uczestnicy-miedz' );
if ( $sec3.length && $krajowa.length ) {
$krajowa.detach().insertAfter( $sec3 );
}
if ( $sec4.length && $miedz.length ) {
$miedz.detach().insertAfter( $sec4 );
}
}
/* -- LawRusWorkflow: CSRF token injection ------------------------------------
Wypelnia pole wptoken w formularzach .lawrus-action-form tokenem sesji.
Musi dzialac przed wyslaniem formularza -- ladowanie synchroniczne z mw.loader. */
mw.loader.using( 'mediawiki.user' ).done( function () {
var token = mw.user.tokens.get( 'csrfToken' );
$( document ).on( 'submit', '.lawrus-action-form', function () {
$( this ).find( 'input[name="wptoken"]' ).val( token );
} );
// Wypelnij od razu przy ladowaniu (dla formularzy juz widocznych)
$( '.lawrus-action-form input[name="wptoken"]' ).val( token );
} );
$( function () {
console.log( '[LawRus] Common.js: document.ready, ns=' + mw.config.get( 'wgNamespaceNumber' ) );
lawrusInitLangSwitch();
lawrusInitDrilldownLangSwitch();
if ( mw.config.get( 'wgUserName' ) ) {
$( 'body' ).addClass( 'lawrus-zalogowany' );
}
if ( $( '#lawrus-uczestnicy-krajowa' ).length ) {
setTimeout( lawrusReorderParticipants, 0 );
}
/* Timeouty -- pokrywaja rozne scenariusze async ladowania Cargo/DataTables */
window.lawrusTruncateAll();
setTimeout( window.lawrusTruncateAll, 300 );
setTimeout( window.lawrusTruncateAll, 800 );
setTimeout( window.lawrusTruncateAll, 2000 );
/* -- Walidacja przed zapisem (tylko formularz WpisKroniki) -- */
var $form = $( '#pfForm' );
if ( !$form.length ) return;
var REQUIRED_AT_4 = [
{ id: 'input_datacja_rok_lacinski', label: 'Rok AD' },
{ id: 'input_jezyk_oryginalu', label: 'Jezyk oryginalu' },
{ id: 'input_kraj', label: 'Kraj' },
{ id: 'input_tekst_zrodlowy', label: 'Tekst zrodlowy' },
{ id: 'input_bibliografia', label: 'Bibliografia' },
{ id: 'input_streszczenie_pl', label: 'Streszczenie PL' },
{ id: 'input_streszczenie_en', label: 'Streszczenie EN' },
{ id: 'input_tlumaczenie_pl', label: 'Tlumaczenie PL' },
{ id: 'input_tlumaczenie_en', label: 'Tlumaczenie EN' }
];
var HIGH_STATUS = [ '4-do-publikacji', '5-opublikowane' ];
$form.on( 'submit', function ( e ) {
var status = $( '#input_status_wpisu' ).val() || '';
if ( HIGH_STATUS.indexOf( status ) === -1 ) return;
var missing = REQUIRED_AT_4.filter( function ( f ) {
return ( $( '#' + f.id ).val() || '' ).trim() === '';
} );
if ( $( '[id^="input_typ_wpisu_"]:checked' ).length === 0 ) {
missing.push( { id: null, label: 'Typ wpisu' } );
}
if ( missing.length === 0 ) return;
e.preventDefault();
var names = missing.map( function ( f ) { return f.label; } ).join( ', ' );
mw.loader.using( 'mediawiki.notification' ).then( function () {
mw.notify(
'Przy statusie "' + status + '" wymagane sa: ' + names + '. Uzupelnij przed zapisem.',
{ type: 'error', autoHide: false, tag: 'lawrus-validation' }
);
} );
missing.forEach( function ( f ) {
if ( f.id ) { $( '#' + f.id ).css( 'border-color', '#d33' ); }
} );
missing.forEach( function ( f ) {
if ( f.id ) {
$( '#' + f.id ).one( 'input', function () {
$( this ).css( 'border-color', '' );
} );
}
} );
} );
} );
