MediaWiki:Common.js: различия между версиями
Admin (обсуждение | вклад) Мобильное меню |
Admin (обсуждение | вклад) Класс готовности меню |
||
| Строка 67: | Строка 67: | ||
var sidebar = document.querySelector( '.sidebar' ); | var sidebar = document.querySelector( '.sidebar' ); | ||
if ( !sidebar ) { return; } | if ( !sidebar ) { return; } | ||
document.documentElement.classList.toggle( 'ms-nav-ready', mq.matches ); | |||
var button = document.getElementById( 'ms-nav-toggle' ); | var button = document.getElementById( 'ms-nav-toggle' ); | ||
Версия от 02:01, 24 сентября 2026
/* Кнопка «Скопировать» у документов (страница «Бумажная работа») */
( function () {
'use strict';
function copyText( text, onDone ) {
if ( navigator.clipboard && navigator.clipboard.writeText ) {
navigator.clipboard.writeText( text ).then( onDone, function () { fallback( text, onDone ); } );
return;
}
fallback( text, onDone );
}
function fallback( text, onDone ) {
var area = document.createElement( 'textarea' );
area.value = text;
area.setAttribute( 'readonly', '' );
area.style.position = 'fixed';
area.style.top = '-1000px';
document.body.appendChild( area );
area.select();
try { document.execCommand( 'copy' ); onDone(); } catch ( e ) { /* ничего */ }
document.body.removeChild( area );
}
function init() {
var items = document.querySelectorAll( 'table.doc-item' );
Array.prototype.forEach.call( items, function ( item ) {
var paper = item.querySelector( 'pre.doc-paper' );
var host = item.querySelector( 'th' ) || item.querySelector( 'td' );
if ( !paper || !host || item.querySelector( '.doc-copy' ) ) { return; }
var button = document.createElement( 'button' );
button.type = 'button';
button.className = 'doc-copy';
button.textContent = 'Скопировать';
button.title = 'Скопировать текст документа';
button.addEventListener( 'click', function () {
copyText( paper.textContent, function () {
button.textContent = 'Скопировано';
button.classList.add( 'is-done' );
window.setTimeout( function () {
button.textContent = 'Скопировать';
button.classList.remove( 'is-done' );
}, 1500 );
} );
} );
host.appendChild( button );
} );
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', init );
} else {
init();
}
}() );
/* Мобильное меню вики: кнопка над статьёй открывает/закрывает сайдбар */
( function () {
'use strict';
var mq = window.matchMedia( '(max-width: 960px)' );
function update() {
var sidebar = document.querySelector( '.sidebar' );
if ( !sidebar ) { return; }
document.documentElement.classList.toggle( 'ms-nav-ready', mq.matches );
var button = document.getElementById( 'ms-nav-toggle' );
if ( mq.matches ) {
if ( !button ) {
button = document.createElement( 'button' );
button.id = 'ms-nav-toggle';
button.type = 'button';
button.className = 'ms-nav-toggle';
button.textContent = 'Меню';
sidebar.parentNode.insertBefore( button, sidebar );
button.addEventListener( 'click', function () {
var open = sidebar.classList.toggle( 'is-open' );
button.textContent = open ? 'Скрыть' : 'Меню';
} );
}
} else {
if ( button ) { button.remove(); }
sidebar.classList.remove( 'is-open' );
}
}
function start() { update(); }
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', start );
} else {
start();
}
if ( mq.addEventListener ) { mq.addEventListener( 'change', update ); }
}() );