MediaWiki:Gadget-catinsert.js
Megjegyzés: közzététel után frissítened kell a böngésződ gyorsítótárát, hogy lásd a változásokat.
- Firefox / Safari: tartsd lenyomva a Shift gombot és kattints a Frissítés gombra a címsorban, vagy használd a Ctrl–F5 vagy Ctrl–R (Macen ⌘–R) billentyűkombinációt
- Google Chrome: használd a Ctrl–Shift–R (Macen ⌘–Shift–R) billentyűkombinációt
- Internet Explorer / Edge: tartsd nyomva a Ctrl-t, és kattints a Frissítés gombra, vagy nyomj Ctrl–F5-öt
- Opera: Nyomj Ctrl–F5-öt
/**
* Category insert script
*/
( function catinsert( $, mw ) {
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) === -1 ) {
return;
}
var messages = [
'categorytree-collapse',
'categorytree-expand',
'categorytree-loading',
'categorytree-no-subcategories'
];
var ciDefaultCategory = 'Tartalom';
var optionname = 'userjs-catinsert-start';
/** @type {string} Starting category, possibly from user option */
var ciStartingCategory = mw.user.options.get( optionname, ciDefaultCategory );
// FIXME
var $ciFrame;
/** @type {string} */
var ciKey;
/** @type {string} */
var ciTrail;
var ciError = false;
var ciUseWikiEditor = ( mw.user.options.get( 'usebetatoolbar' ) == 1 );
// var ciWikEdObj;
function ciGetDialog() {
$ciFrame = $( '#catInsertFrame' );
if ( !$ciFrame.length ) {
$ciFrame = $( '<div>' )
.attr( 'id', 'catInsertFrame' )
.keypress( function ( e ) {
if ( e.which === 27 ) {
ciCloseDialog();
}
} )
.insertBefore( /* ( typeof wikEd !== 'undefined' && wikEd.useWikEd ) ? '#wikEdFrameOuter' : */ '#wpTextbox1' );
$( '<div>' )
.attr( 'id', 'catInsertClose' )
.text( 'x' )
.click( ciCloseDialog )
.appendTo( $ciFrame );
}
var $ciDiv = $( '#catInsertBody' );
if ( !$ciDiv.length ) {
$ciDiv = $( '<div>' )
.attr( 'id', 'catInsertBody' )
.appendTo( $ciFrame );
}
$ciFrame.show();
return $ciDiv;
}
function ciCloseDialog() {
$ciFrame.hide();
/* if ( typeof wikEd !== 'undefined' && wikEd.useWikEd ) {
wikEd.frameWindow.focus();
} else { */
$( '#wpTextbox1' ).focus();
// }
}
/**
* Handle clicking on a bullet.
* @this {HTMLElement}
*/
function ciToggleNode() {
var expand = this.dataset['ctState'] === 'collapsed';
var $div = $( this ).parents( '.CategoryTreeSection:first' ).children( '.CategoryTreeChildren' )
$div.toggle( expand );
// Possible values: categorytree-collapse, categorytree-expand
this.title = mw.msg( 'categorytree-' + ( expand ? 'collapse' : 'expand' ) );
this.dataset['ctState'] = expand ? 'expanded' : 'collapsed';
if ( expand && this.dataset['ctLoaded'] === undefined ) {
this.dataset['ctLoaded'] = '';
ciLoadChildren( this.dataset['ctTitle'], $div );
}
}
/**
* Load child categories of a given category
* @param {string} cat Category to load children of
* @param {jQuery} $div Insert result in this `<div>`
* @param {PromiseLike<void>} [cssPromise] Promise representing the loading of the CSS styles.
* The result is not shown until it’s fulfilled.
*/
function ciLoadChildren( cat, $div, cssPromise ) {
$( '<span>' )
.addClass( 'CategoryTreeNotice' )
.text( mw.msg( 'categorytree-loading' ) )
.appendTo( $div.empty() );
new mw.Api().get( {
action: 'categorytree',
category: cat,
formatversion: 2
} ).done( function ciAjaxCallback( data ) {
var result = data.categorytree.html;
( cssPromise || Promise.resolve() ).then( function ciCssLoadedCallback() {
if ( result ) {
$div.html( result );
$div.find( '.CategoryTreeToggle' ).click( ciToggleNode );
$div.find( 'a' ).click( ciInsertCategory );
} else {
$( '<span>' )
.addClass( 'CategoryTreeNotice' )
.text( mw.msg( 'categorytree-no-subcategories' ) )
.appendTo( $div.empty() );
}
} );
} ).fail( function ciAjaxFail( error, xhr, statusText, errorThrown ) {
// Show default list on error
if ( !ciError ) {
mw.log.warn( 'Error loading ' + cat + ': ' + error );
mw.log.warn( xhr );
ciLoadChildren( ciStartingCategory, ciGetDialog() );
ciError = true;
} else { // avoid infinite cycle
$( '<div>' )
.addClass( 'error' )
.text( xhr.status + ' ' + xhr.statusText + ': ' + errorThrown )
.appendTo( $div.empty() );
ciError = false;
}
} );
}
function ciInsertCategory( e ) {
e.preventDefault();
var categoryLink = '[' + '[Kategória:' + this.text + ciKey + ']]' + ciTrail;
/*
if ( typeof wikEd !== 'undefined' && wikEd.useWikEd ) {
ciWikEdObj.changed.plain = categoryLink;
ciWikEdObj.changed.keepSel = true;
wikEdLastVersion = null;
ciWikEdObj.html = ciWikEdObj.changed.plain;
if ( wikEdHighlightSyntax ) {
WikEdHighlightSyntax( ciWikEdObj );
} else {
ciWikEdObj.html = ciWikEdObj.html.replace( /(\t)/g, '<span class="wikEdTabPlain">$1</span><!--wikEdTabPlain-->' );
}
ciWikEdObj.sel.removeAllRanges();
ciWikEdObj.sel.addRange( ciWikEdObj.changed.range );
wikEd.frameDocument.execCommand( 'inserthtml', ciWikEdObj.html );
wikEdFrameDOMCache = null;
wikEd.frameWindow.focus();
/*if ( wikEdHighlightSyntax ) {
wikEdFollowLinks();
}
} else {*/
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: categoryLink,
peri: '',
post: ''
} );
//}
$ciFrame.hide();
}
/**
* Handle clicking on the toolbar button in the legacy toolbar
* @param {MouseEvent} e The click event
*/
function ciLegacyToolbarButtonClicked( e ) {
// Set the default starting category
if ( e.shiftKey ) {
var start = prompt( 'Indulókategória (üres: visszaállítás az alapértelmezettre):', ciStartingCategory );
if ( start ) {
ciStartingCategory = start;
new mw.Api().saveOption( optionname, start );
} else if ( start === '' ) {
ciStartingCategory = ciDefaultCategory;
// Delete option
new mw.Api().saveOption( optionname, null );
}
} else {
ciButtonClicked();
}
}
/**
* Handle clicking on the toolbar button in the WikiEditor toolbar,
* or in the legacy toolbar without pressing Shift
*/
function ciButtonClicked() {
var cssPromise = mw.loader.using( 'ext.categoryTree.styles' );
/*
if ( typeof wikEd !== 'undefined' && wikEd.useWikEd ) {
ciWikEdObj = {};
ciWikEdObj.changed = {};
WikEdGetText( ciWikEdObj, 'selection' );
ciWikEdObj.changed = ciWikEdObj.selection;
var selection = ciWikEdObj.selection.plain;
} else { */
var selection = $( '#wpTextbox1' ).textSelection( 'getSelection' );
// }
ciKey = ciTrail = '';
if ( selection ) {
var match = selection.match( /(\s+)$/ );
// Allow trailing whitespace
if ( match && match.length >= 2 ) {
selection = selection.slice( 0, -( match[ 1 ].length ) );
ciTrail = match[ 1 ];
}
match = selection.match( /\[\[.*(\|.*)\]\]$/ );
if ( match && match.length >= 2 ) {
ciKey = match[ 1 ];
}
selection = selection.replace( /^\[\[[Kk]ategória:/, '' );
selection = selection.replace( /(\|.*)?\]\]$/, '' );
} else {
selection = ciStartingCategory;
}
var $div = ciGetDialog();
$ciFrame.focus();
ciLoadChildren( selection, $div, cssPromise );
}
function ciInstallButton() {
if ( !$( '#mw-editbutton-catinsert, .oo-ui-icon-catinsert' ).length ) {
var label = 'Kategória kiválasztása';
if ( ciUseWikiEditor ) {
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
section: 'main',
group: 'insert',
tools: {
'insertCategory': {
label: label,
type: 'button',
oouiIcon: 'catinsert',
action: {
type: 'callback',
execute: ciButtonClicked
}
}
}
} );
} else {
mw.toolbar.addButton( {
imageId: 'mw-editbutton-catinsert',
speedTip: label,
onClick: ciLegacyToolbarButtonClicked
} );
}
}
}
$.when(
mw.loader.using( ciUseWikiEditor ? 'ext.wikiEditor' : 'mediawiki.toolbar' ),
new mw.Api().loadMessages( messages ),
$.ready
).then( ciInstallButton );
} ( jQuery, mediaWiki ) );