MediaWiki:Gadget-insert-inflection.js: Różnice pomiędzy wersjami
Usunięta treść Dodana treść
ext.gadget.lib-toolbar bedzie ladowany z poziomu konfiguracji RL |
nie tworz nowego formularza jeszcze w trakcie ladowania pierwszego; dr. |
||
Linia 4: | Linia 4: | ||
var iig = mw.libs.insertInflection = { |
var iig = mw.libs.insertInflection = { |
||
version: 13, |
|||
templates: null, // MediaWiki:Gadget-insert-inflection-data.js |
|||
$panel: null, |
|||
$languageSelector: null, |
|||
$templateSelector: null, |
|||
$formContainer: null, |
|||
modules: [ |
|||
'ext.gadget.insert-inflection-data', |
'ext.gadget.insert-inflection-data', |
||
'mediawiki.confirmCloseWindow', |
'mediawiki.confirmCloseWindow', |
||
'jquery.textSelection', |
'jquery.textSelection', |
||
'jquery.cookie' |
'jquery.cookie' |
||
], |
|||
messages: { |
|||
'inflection-insert-noun-subject': 'Wstaw temat', |
'inflection-insert-noun-subject': 'Wstaw temat', |
||
'inflection-noun-subject': 'Temat rzeczownika: |
'inflection-noun-subject': 'Temat rzeczownika:', |
||
'inflection-insert-button-title': 'Wstaw odmianę (wersja $1)', |
'inflection-insert-button-title': 'Wstaw odmianę (wersja $1)', |
||
'inflection-insert-button-alt': 'Wstaw odmianę', |
'inflection-insert-button-alt': 'Wstaw odmianę', |
||
'inflection-language': 'Język: |
'inflection-language': 'Język:', |
||
'inflection-template': 'Szablon: |
'inflection-template': 'Szablon:', |
||
'inflection-cancel-button': 'Anuluj', |
'inflection-cancel-button': 'Anuluj', |
||
'inflection-insert-template': 'Wstaw szablon' |
'inflection-insert-template': 'Wstaw szablon' |
||
} |
|||
} |
}, |
||
isLoading = false; |
|||
⚫ | |||
/** Sets up the gadget */ |
/** Sets up the gadget */ |
||
iig.init = function () { |
iig.init = function () { |
||
⚫ | |||
mw.libs.toolbarGadget.addButton( { |
mw.libs.toolbarGadget.addButton( { |
||
icon: '//upload.wikimedia.org/wikipedia/commons/0/04/Button_array.png', |
icon: '//upload.wikimedia.org/wikipedia/commons/0/04/Button_array.png', |
||
Linia 39: | Linia 40: | ||
onclick: function () { |
onclick: function () { |
||
if ( !iig.$panel ) { |
if ( !iig.$panel ) { |
||
if ( !isLoading ) { |
|||
⚫ | |||
isLoading = true; |
|||
⚫ | |||
} |
|||
} else { |
} else { |
||
iig.$panel.toggle(); |
iig.$panel.toggle(); |
||
Linia 75: | Linia 79: | ||
.append( [ |
.append( [ |
||
mw.msg( 'inflection-language' ), |
mw.msg( 'inflection-language' ), |
||
' ', |
|||
iig.$languageSelector, |
iig.$languageSelector, |
||
mw.msg( 'inflection-template' ), |
mw.msg( 'inflection-template' ), |
||
' ', |
|||
iig.$templateSelector |
iig.$templateSelector |
||
] ) |
] ) |
||
Linia 110: | Linia 116: | ||
iig.selectLanguage( parseInt( $.cookie( 'inflectionTemplate' ) ) || 0 ); |
iig.selectLanguage( parseInt( $.cookie( 'inflectionTemplate' ) ) || 0 ); |
||
isLoading = false; |
|||
}; |
}; |
||
Linia 183: | Linia 191: | ||
.append( [ |
.append( [ |
||
mw.msg( 'inflection-noun-subject' ), |
mw.msg( 'inflection-noun-subject' ), |
||
' ', |
|||
$( '<input>' ).attr( 'type', 'text' ), |
$( '<input>' ).attr( 'type', 'text' ), |
||
$( '<input>' ) |
$( '<input>' ) |
Wersja z 20:53, 11 maj 2017
/*
* @author: [[:pl:User:Beau]]
*/
var iig = mw.libs.insertInflection = {
version: 13,
templates: null, // MediaWiki:Gadget-insert-inflection-data.js
$panel: null,
$languageSelector: null,
$templateSelector: null,
$formContainer: null,
modules: [
'ext.gadget.insert-inflection-data',
'mediawiki.confirmCloseWindow',
'jquery.textSelection',
'jquery.cookie'
],
messages: {
'inflection-insert-noun-subject': 'Wstaw temat',
'inflection-noun-subject': 'Temat rzeczownika:',
'inflection-insert-button-title': 'Wstaw odmianę (wersja $1)',
'inflection-insert-button-alt': 'Wstaw odmianę',
'inflection-language': 'Język:',
'inflection-template': 'Szablon:',
'inflection-cancel-button': 'Anuluj',
'inflection-insert-template': 'Wstaw szablon'
}
},
isLoading = false;
/** Sets up the gadget */
iig.init = function () {
mw.messages.set( iig.messages );
mw.libs.toolbarGadget.addButton( {
icon: '//upload.wikimedia.org/wikipedia/commons/0/04/Button_array.png',
title: mw.msg( 'inflection-insert-button-title', iig.version ),
alt: mw.msg( 'inflection-insert-button-alt' ),
id: 'insertInflectionButton',
onclick: function () {
if ( !iig.$panel ) {
if ( !isLoading ) {
isLoading = true;
mw.loader.using( iig.modules ).done( iig.loadPanel );
}
} else {
iig.$panel.toggle();
}
}
} );
};
iig.loadPanel = function () {
var selectedLanguage = parseInt( $.cookie( 'inflectionLanguage' ) ) || 0;
iig.$panel = $( '<div>' ).addClass( 'inflection-gadget-panel' );
iig.$languageSelector = $( '<select>' )
.addClass( 'inflection-gadget-language-selector' )
.on( 'change', iig.selectLanguage );
iig.$templateSelector = $( '<select>' )
.addClass( 'inflection-gadget-template-selector' )
.on( 'change', iig.selectTemplate );
$.each( iig.templates, function ( i, template ) {
var $option = $( '<option>' )
.val( i )
.text( template.title )
.appendTo( iig.$languageSelector );
if ( i === selectedLanguage ) {
$option.prop( 'selected', true );
}
} );
// top line
$( '<div>' )
.append( [
mw.msg( 'inflection-language' ),
' ',
iig.$languageSelector,
mw.msg( 'inflection-template' ),
' ',
iig.$templateSelector
] )
.appendTo( iig.$panel );
iig.$formContainer = $( '<div>' ).appendTo( iig.$panel );
// bottom line
$( '<div>' )
.append( [
$( '<input>' )
.attr( 'type', 'button' )
.val( mw.msg( 'inflection-insert-template' ) )
.on( 'click', iig.insertTemplate )
.on( 'click', mw.confirmCloseWindow( {
test: function () {
return iig.$panel.find( 'input[type="text"]' ).get()
.some( function ( input ) {
return input.value !== '';
} );
},
namespace: 'insert-inflection'
} ).release ),
' ',
$( '<input>' )
.attr( 'type', 'button' )
.val( mw.msg( 'inflection-cancel-button' ) )
.on( 'click', iig.$panel.toggle )
] )
.appendTo( iig.$panel );
$( '#wpTextbox1' ).before( iig.$panel );
iig.selectLanguage( parseInt( $.cookie( 'inflectionTemplate' ) ) || 0 );
isLoading = false;
};
iig.selectLanguage = function ( selectedTemplate ) {
var selectedLanguage = iig.$languageSelector.val(),
language = iig.templates[ selectedLanguage ];
$.cookie( 'inflectionLanguage', selectedLanguage, {
expires: 30
} );
iig.$templateSelector.empty();
$.each( language.templates, function ( i, template ) {
var $option = $( '<option>' )
.val( i )
.text( template.title )
.appendTo( iig.$templateSelector );
if ( i === selectedTemplate ) {
$option.prop( 'selected', true );
}
} );
iig.selectTemplate();
};
iig.selectTemplate = function () {
var $form, $table, elements, $row,
language = iig.templates[ iig.$languageSelector.val() ],
selectedTemplate = iig.$templateSelector.val(),
template = language.templates[ selectedTemplate ];
$.cookie( 'inflectionTemplate', selectedTemplate, {
expires: 30
} );
iig.$templateSelector.data( 'selectedTemplate', template );
iig.$formContainer.children().hide();
if ( template.$element ) {
template.$element.show();
return;
}
elements = [];
$.each( template.fields, function ( i, field ) {
var $input;
if ( field.caption !== null ) {
$input = $( '<input>' ).attr( 'type', 'text' );
elements.push( field.caption, $input );
field.$input = $input;
}
} );
$table = $( '<table>' );
while ( elements.length > 0 ) {
$row = $( '<tr>' );
$.each( elements.splice( 0, 4 ), function ( i, rowElement ) {
$( '<td>' )
.append( rowElement )
.appendTo( $row );
} );
$table.append( $row );
}
$form = $( '<div>' );
if ( template.title === 'rzeczownik' ) {
$( '<div>' )
.append( [
mw.msg( 'inflection-noun-subject' ),
' ',
$( '<input>' ).attr( 'type', 'text' ),
$( '<input>' )
.attr( 'type', 'button' )
.val( mw.msg( 'inflection-insert-noun-subject' ) )
.on( 'click', function () {
$form.find( 'input[type="text"]' )
.val( $( this ).prev().val() );
} )
] )
.prependTo( $form );
}
template.$element = $form
.append( $table )
.appendTo( iig.$formContainer );
};
iig.insertTemplate = function () {
var data = iig.$templateSelector.data( 'selectedTemplate' ),
text = mw.format( '{{$1\n', data.template ),
fields = {},
padding = 0;
$.each( data.fields, function ( i, field ) {
if ( field.$input ) {
fields[ field.name ] = field.$input.val();
} else {
fields[ field.name ] = field.value;
}
if ( field.name.length > padding ) {
padding = field.name.length;
}
} );
padding++;
$.each( fields, function ( name, value ) {
while ( name.length < padding ) {
name += ' ';
}
if ( name[ 0 ] >= '0' && name[ 0 ] <= '9' ) {
text += mw.format( '|$1\n', value );
} else {
text += mw.format( '|$1 = $2\n', name, value );
}
} );
text += '}}';
$( '#wpTextbox1' ).focus().textSelection( 'encapsulateSelection', {
pre: text
} );
};
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
$( iig.init );
}