MediaWiki:Gadget-blpEditNotice.js (MediaWiki&Gadget-blpEditNoticeQjs)

Перейти к навигации Перейти к поиску
JS-код ниже относится к гаджету: Отображать предупреждение о правиле о биографиях современников при редактировании соответствующих статей (не отключайте, не ознакомившись) (править описание). Он включён по умолчанию.

После сохранения или недавних изменений очистите кэш браузера.

// Magic editintros by [[:en:User:RockMFR]] & [[:ru:User:Jack who built the house]] (VE part)
( function () {
function addVeEditNotice( code ) {
	var target = ve.init.target;
	var actionsToolbar = target.actionsToolbar;
	if ( !actionsToolbar ) {
		// avoid TypeError: Cannot read properties of undefined (reading 'tools')
		return;
	}
	var wasNoEditNotices = !actionsToolbar.tools.notices;
	if ( wasNoEditNotices ) {
		if ( actionsToolbar.items[ 0 ] &&
			actionsToolbar.items[ 0 ].items &&
			actionsToolbar.items[ 0 ].items[ 1 ] &&
			actionsToolbar.items[ 0 ].items[ 1 ] instanceof ve.ui.MWNoticesPopupTool &&
			actionsToolbar.items[ 0 ].items[ 0 ] &&
			actionsToolbar.items[ 0 ].items[ 0 ] instanceof ve.ui.MWHelpPopupTool
		) {
			var noticesPopupTool = new ve.ui.MWNoticesPopupTool( actionsToolbar.items[ 0 ] );
			actionsToolbar.tools.notices = actionsToolbar.items[ 0 ].items[ 1 ] = noticesPopupTool;
			noticesPopupTool.$element.insertAfter( $( '.oo-ui-tool-name-help' ) );
			actionsToolbar.connect( noticesPopupTool, {
				updateState: 'onUpdateState'
			} );
		} else {
			console.error( 'Unable to add the edit notice tool to the VisualEditor toolbar' );
			return;
		}
	}
	target.remoteNotices.unshift( code );
	target.editNotices.unshift( code );
	actionsToolbar.tools.notices.setNotices( target.getEditNotices() );
	if ( wasNoEditNotices ) {
		// setTimeout for IE
		setTimeout( function () {
			actionsToolbar.tools.notices.getPopup().toggle( true );
		}, 0 );
	}
}

function addEditIntro( name ) {
	$( '.mw-editsection, #ca-edit, #ca-ve-edit' ).find( 'a' ).each( function ( i, el ) {
		el.href = $( this ).attr( 'href' ) + '&editintro=' + name;
	} );

	if ( mw.util.getParamValue( 'editintro' ) !== name ) {
		var editNoticeLoaded = false;
		mw.libs.ve.addPlugin( function () {
			if ( !editNoticeLoaded ) {
				new mw.Api().get( {
					action: 'parse',
					page: name,
					prop: 'text',
					formatversion: 2,
				} ).done( function ( data ) {
					if ( data && data.parse && data.parse.text !== undefined ) {
						editNoticeLoaded = true;
						if ( data.parse.text ) {
							mw.hook( 've.activationComplete' ).add( function () {
								addVeEditNotice( data.parse.text );
							} );
						}
					}
				} );
			}
		} );
	}
}

if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
	// Use runAsEarlyAsPossible (defined in MediaWiki:Common.js) if available
	// If not (e.g. mobile) use jQuery.ready
	( typeof runAsEarlyAsPossible !== 'undefined' ? runAsEarlyAsPossible : $ )( function () {
		var cats = mw.config.get( 'wgCategories' );
		if ( !cats ) return;
		if ( $.inArray( 'Статьи с эдитнотисом об осторожности при редактировании', cats ) !== -1 ) {
			addEditIntro( 'Шаблон:Editnotice/RU-BY' );
		} else if ( $.inArray( 'Википедия:Биографии современников', cats ) !== -1 ) {
			addEditIntro( 'Шаблон:Editnotice/Ныне_живущие' );
		} else if ( $.inArray( 'Персоналии, умершие менее года назад', cats ) !== -1 ) {
			addEditIntro( 'Шаблон:Editnotice/Недавно_умершие' );
		}
	}, $( '#ca-history' ) );
}

}() );