MediaWiki:Gadget-LiveRC 1x.js/InserisciTemplate.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m dot notation
Altre semplificazioni piccoline
 
(7 versioni intermedie di uno stesso utente non sono mostrate)
Riga 14:
LiveRC_Config.InstalledLiveRCExtensions.push( "InserisciTemplate" );
 
/////////////////////////////// CSS ///////////////////////////////////////////////////////
importStylesheetURI( '//it.wikipedia.org/w/index.php?title=MediaWiki:Gadget-LiveRC 1x.js/InserisciTemplate.css&action=raw&ctype=text/css' );
 
///////////////////////// VARIABLES ////////////////////////////////////////////////////////
 
//// PARAMÈTRES PERSONNALISABLES ////
 
var templateArguments = {
Riga 626 ⟶ 621:
}
};
 
function getTemplateList() {
return Object.keys( Custom_lstMyTemplate ).length
? Custom_lstMyTemplate
: lstMyTemplate;
}
 
// ####################################################################################################################
// ####################################################################################################################
 
/**
// Add options in the "Tag" form
* Add options in the "Tag" form
 
*/
function lrcRunInsertTemplate( data ) {
var TagSelect = document.getElementById( 'LiveTagReason' );,
TemplateList = getTemplateList();
 
var TemplateList = Custom_lstMyTemplate;
if ( !lrcGetObjectLength( TemplateList ) ) {
TemplateList = lstMyTemplate;
}
$.each( TemplateList, function( i, val ) {
var optTag = document.createElement( 'option' );
Riga 646 ⟶ 645:
TagSelect.appendChild( optTag );
} );
 
// setup dialog
mw.loader.using$( 'jquery.ui<div>' )
.attr( 'id', 'gtb-dialog' )
.done( function() {
$.appendTo( '<div>body' );
.attr( 'id', 'gtb-dialog' )
.appendTo( 'body' );
} )
.fail( function() {
console.warn( 'Errore del RL con l\'estensione InserisciTemplate di LiveRC' );
} );
}
LiveRC_AddHook( "AfterPreviewArticle", lrcRunInsertTemplate );
 
/**
// Function launched when choosing one of this extension options
* Function launched when choosing one of this extension options
 
*/
getLiveTagFunctions.InserisciTemplateExtension = function( page, option ) {
lrcDisableLink( "LiveTagReason" );
lrcDisableLink( "LiveTagLink" );
 
var TemplateList = Custom_lstMyTemplate;getTemplateList(),
if message ( !lrcGetObjectLength(= TemplateList[ ) )option.value {];
 
TemplateList = lstMyTemplate;
new mw.Api().get( {
}
format: 'json',
var message = TemplateList[ option.value ];
action: 'query',
wpajax.http( {
prop: 'info',
url: mw.config.get( 'wgServer' ) + wgScriptPath + '/api.php?format=xml' +
inprop: 'protection',
'&action=query&prop=info&intoken=edit' +
titles: page
'&inprop=protection' +
} )
'&titles=' + encodeURIComponent( page ),
.done( function ( res ) {
onSuccess: InserisciTemplate_PostTagPage,
InserisciTemplate_PostTagPage( res, page, message );
page: page,
} );
message: message
} );
return false;
};
 
function InserisciTemplate_PostTagPage( res, page, message ) {
// Get page edit token and protection status
var resPage = Object.values( res.query.pages )[0];
 
if ( !resPage ) {
function InserisciTemplate_PostTagPage( xmlreq, data ) {
return;
var page = data.page;
}
var message = data.message;
for ( var prEntry in resPage.protection ) {
ObjetXML = xmlreq.responseXML;
if ( prEntry.type === "edit" && mw.config.get( 'wgUserGroups' ).indexOf( prEntry.level ) === -1 ) {
var Isprotected = false;
LiveRC_alert( "<b>" + lrcMakeText( "PROTECTEDPAGE" ).split( "$1" ).join( page ) + "</b>" );
var PR = ObjetXML.getElementsByTagName( "pr" );
return;
for ( var a = 0, l = PR.length; a < l; a++ ) {
var Type = PR[ a ].getAttribute( "type" );
var Level = PR[ a ].getAttribute( "level" );
if ( Type == "edit" && mw.config.get( 'wgUserGroups' ).indexOf( Level ) == -1 ) {
Isprotected = true;
}
}
InserisciTemplate_showDialog( message, page );
if ( Isprotected ) {
LiveRC_alert( "<b>" + lrcMakeText( "PROTECTEDPAGE" ).split( "$1" ).join( page ) + "</b>" );
return;
}
var Page = ObjetXML.getElementsByTagName( "page" )[ 0 ];
LiveRC_Config.edittoken = Page.getAttribute( "edittoken" );
var tpl = InserisciTemplate_showDialog( message, page );
}
 
/**
// Show the dialog in order to ask for the template parameters.
* Show the dialog in order to ask for the template parameters.
 
*/
function InserisciTemplate_showDialog( data, page ) {
var $dialog, $fieldset;
 
// create the dialog html
$dialog = $( '#gtb-dialog' ).html( lrcMakeText( "IT_InsertTemplate1" ) );
$( '<a>' )
.attr( 'href', mw.configutil.getgetUrl( 'wgArticlePathTemplate:' ).split( "$1" ).join( lrcGetNamespaceName( 10 ) ) + data.template ) )
.attr( 'target', '_blank' )
.attr( 'tabindex', '-1' )
Riga 733 ⟶ 717:
.append( '<br/>' );
} );
 
// show the dialog
var ButtonsOKText = {};lrcMakeText( 'OK' ),
var OKText CancelText = lrcMakeText( 'OKCancel' );
 
var CancelText = lrcMakeText( 'Cancel' );
var Buttons[ OKText ] = function() {
varOKText: params =function() {};
var params = {};
$dialog.find( 'input:text,select' ).each( function() {
params[ $( this ).attr( 'id' ) ] = $.trim( $( this ).val() );
} );
text = InserisciTemplate_dumpTemplate( data, params );
$( this ).dialog( 'close' );
InserisciTemplate_postTemplate( data, text, page );
};,
Buttons[ CancelText ] =: function() {
$( this ).dialog( 'close' );
}
};
$dialog.dialog( {
Riga 760 ⟶ 746:
 
function InserisciTemplate_buildInputEl( id, data ) {
var label, inputEl;= data.name,
inputEl;
if ( data.type == 'string' ) {
label = data.name;
inputEl = $( '<input/>' )
.attr( 'id', id )
Riga 769 ⟶ 755:
.attr( 'value', ( data.value || '' ) );
} else if ( data.type == 'select' ) {
label = data.name;
inputEl = $( '<select>' )
.attr( 'id', id )
Riga 785 ⟶ 770:
}
 
/**
// Check the dialog box and create the wikitext from the template and its params
* Check the dialog box and create the wikitext from the template and its params
 
*/
function InserisciTemplate_dumpTemplate( template, params ) {
return ( template.noinclude ? '<noinclude>' : '' ) +
var text, templateParams = "";
templateParams = LiveRC_FormatTemplateParams( params || {} );
text = ( template.noinclude ? '<noinclude>' : '' ) +
'{{' + ( template.subst ? 'subst:' : '' ) +
template.template + '|' +
LiveRC_FormatTemplateParams( params ) +
( templateParams ) +
'}}' +
( template.noinclude ? '</noinclude>' : '' ) + '\n';
return text;
}
 
Riga 809 ⟶ 792:
}
 
/**
// Save the edit
* Save the edit
 
*/
function InserisciTemplate_postTemplate( data, text, page ) {
lrcDisableLink( "LiveTagReason" );
lrcDisableLink( "LiveTagLink" );
var EditParam = [];{
action: 'edit',
EditParam.token = LiveRC_Config[ "edittoken" ];
summary: lrcMakeText( "RESUMESTART" ) + lrcMakeText( "TAG_RESUME" ) + ' ' + data.template,
if ( data.where == 'top' ) {
title: page,
EditParam.prependtext = text + "\n";
watchlist: lrcMakeParam( "BypassWatchdefault" ) ? 'nochange' : "preferences",
} else if ( data.where == 'bottom' ) {
notminor: 1,
nocreate: 1
};
 
if ( data.where == 'bottom' ) {
EditParam.appendtext = "\n" + text;
} else { // top, or default = top
EditParam.prependtext = text + "\n";
}
EditParam.summary = lrcMakeText( "RESUMESTART" ) + lrcMakeText( "TAG_RESUME" ) + ' ' + data.template;
EditParam.title = page;
EditParam.watchlist = "preferences";
EditParam.notminor = "1";
EditParam.nocreate = "1";
if ( lrcMakeParam( "BypassWatchdefault" ) ) {
EditParam.watchlist = "nochange";
}
var Params = [];
for ( var Param in EditParam ) {
Params.push( Param + "=" + encodeURIComponent( EditParam[ Param ] ) );
}
Params = Params.join( "&" );
var headers = [];
headers[ 'Content-Type' ] = 'application/x-www-form-urlencoded';
wpajax.http( {
url: mw.config.get( 'wgServer' ) + wgScriptPath + '/api.php?action=edit',
method: "POST",
headers: headers,
data: Params,
onSuccess: InserisciTemplate_PostTagPageDone,
params: EditParam,
where: data.where
} );
}
 
new mw.Api().postWithEditToken( EditParam )
function InserisciTemplate_PostTagPageDone( Req, data ) {
.done( function () {
var params = data.params;
LiveRC_alert( "<b>" + page + " : " + lrcMakeText( "TAG_DONE" ) + "</b> <small>(" + text + ")</small>" );
var where = data.where;
} );
var text = "<b>" + params[ "title" ] + " : " + lrcMakeText( "TAG_DONE" ) + "</b> <small>(" + params[ ( where == 'bottom' ) ? "appendtext" : "prependtext" ] + ")</small>";
LiveRC_alert( text );
}
 
Riga 859 ⟶ 822:
 
 
/**
// ===== Create a fieldset in the configuration panel =====
* Create a fieldset in the configuration panel
*/
function InserisciTemplate_CreateConfigPanel() {
var InserisciTemplateUl = LiveRC_ManageParams_CreateNewListMenu( "InserisciTemplateLegend", LiveRC_ManageParams_CreateActionButtons() ),
Target = InserisciTemplateUl.parentNode,
Templates = getTemplateList();
 
function InserisciTemplate_CreateConfigPanel() {
var InserisciTemplateUl = LiveRC_ManageParams_CreateNewListMenu( "InserisciTemplateLegend", LiveRC_ManageParams_CreateActionButtons() );
var Target = InserisciTemplateUl.parentNode;
var Templates = Custom_lstMyTemplate;
if ( !Templates || lrcGetObjectLength( Templates ) == 0 ) {
Templates = lstMyTemplate;
}
for ( var temp in Templates ) {
if ( !Templates.hasOwnProperty( temp ) ) {
Target.appendChild( InserisciTemplate_CreateTemplateConfigPanel( Templates[ temp ] ) );
continue;
}
var Template = Templates[ temp ];
var TemplateForm = InserisciTemplate_CreateTemplateConfigPanel( Template );
Target.appendChild( TemplateForm );
}
var NewTemplate = document.createElement( 'p' );
Riga 916 ⟶ 875:
var Div = DeleteLink;
while ( Div ) {
if ( lrcHasClass( Div,.classList.contains( "InserisciTemplate_TemplateDiv" ) ) {
break;
}
Riga 924 ⟶ 883:
return;
}
Div.parentNode.removeChild$( Div ).remove();
}
 
Riga 931 ⟶ 890:
TemplateDiv.className = "InserisciTemplate_TemplateDiv ParamMenuLi";
var Legend = document.createElement( 'p' );
var Title = document.createElement( 'a' );
Title.className = "InserisciTemplate_template";
Title.innerHTML = "{{" + Template.template + "}} ";
Title.target = "_blank";
Title.href = lrcGetPageURLmw.util.getUrl( lrcGetNamespaceName( 10 ) + "Template:" + Template.template );
Title.title = lrcGetNamespaceName( 10 ) + "Template:" + Template.template;
Title.onclick = function() {
liveArticle( lrcGetNamespaceName( 10 ) + "Template:" + Template.template );
return false;
};
Riga 1 027 ⟶ 986:
Input_subst.id = 'subst';
Input_subst.type = "checkbox";
if ( Template.subst ) {
Input_subst.checked = "checked";
}
LI_Template.appendChild( Input_subst );
 
Riga 1 061 ⟶ 1 022:
 
function InserisciTemplate_AddParamToTemplate( AddParamLink ) {
var Li = AddParamLink.parentNode;
var NewParams = {
name: '',
Riga 1 068 ⟶ 1 028:
};
var NewParamLi = InserisciTemplate_CreateNewParam( "", NewParams );
Li.parentNode.insertBefore$( NewParamLi, Li).insertBefore( AddParamLink.parentNode );
}
 
function InserisciTemplate_CreateNewParam( param, Params ) {
var ParamName = Params.name;
var ParamType = Params.type;
var ParamValue = Params.value;
Riga 1 083 ⟶ 1 042:
DeleteParamLink.href = "javascript:;";
DeleteParamLink.onclick = function() {
$( this.parentNode ).remove();
InserisciTemplate_DeleteParamFromTemplate( this );
};
LI_Param.appendChild( DeleteParamLink );
Riga 1 108 ⟶ 1 067:
Input_name.id = 'Param_name';
Input_name.type = "text";
Input_name.value = ( ParamNameParams.name || "" );
LI_Param.appendChild( Input_name );
LI_Param.appendChild( document.createTextNode( " - " ) );
Riga 1 125 ⟶ 1 084:
Opt.innerHTML = OptValue;
Opt.value = OptValue;
if ( OptValue == ParamType ) {
Opt.selected = "selected";
}
Select_type.appendChild( Opt );
}
Select_type.onchange = function() {
InserisciTemplate_ChageValueTypeInserisciTemplate_ChangeValueType( this );
};
LI_Param.appendChild( Select_type );
LI_Param.appendChild( document.createTextNode( " - " ) );
Riga 1 182 ⟶ 1 143:
}
 
function InserisciTemplate_ChageValueTypeInserisciTemplate_ChangeValueType( Select ) {
var NewTypeLabels = Select.valueparentNode.getElementsByTagName( 'label' );
var Li = Select.parentNode;
var Labels = Li.getElementsByTagName( 'label' );
var LastLabel = Labels[ ( Labels.length - 1 ) ];
while ( LastLabel.nextSibling ) {
LastLabel.nextSibling.parentNode.removeChild$( LastLabel.nextSibling ).remove();
}
if ( NewTypeSelect.value == "string" ) {
var Input_value = document.createElement( 'input' );
Input_value.id = 'Param_value';
Riga 1 228 ⟶ 1 187:
 
function InserisciTemplate_DeleteOptionFromSelect( DeleteOptionLink ) {
var LiSelects = DeleteOptionLink.parentNode.getElementsByTagName( 'select' );
var Selects = Li.getElementsByTagName( 'select' );
for ( var a = 0, l = Selects.length; a < l; a++ ) {
var Select = Selects[ a ];
Riga 1 279 ⟶ 1 237:
var Span = AddOptionInputCancel.parentNode;
var Link = Span.nextSibling;
Span.parentNode.removeChild$( Span ).remove();
Link.style.display = "";
}
Riga 1 298 ⟶ 1 256:
Select.appendChild( Option );
}
InserisciTemplate_AddOptionToSelect_Cancel( AddOptionInputOK );
}
 
/**
function InserisciTemplate_DeleteParamFromTemplate( DeleteParamLink ) {
* Check the fieldset and add a text to be saved in /LiveRCparam.js page
var Li = DeleteParamLink.parentNode;
*/
Li.parentNode.removeChild( Li );
}
 
// ===== Check the fieldset and add a text to be saved in /LiveRCparam.js page =====
 
function InserisciTemplate_CheckConfigPanel() {
var $ITConfigPanel = document.getElementById$( '#LiveRC_OptionsContent_InserisciTemplateLegend' );
if ( !$ITConfigPanel.length ) {
return;
}
var ElementForms = lrcGetElementsByClass( "InserisciTemplate_TemplateForm", ITConfigPanel, "form" );
var Items = {};
$ITConfigPanel.find( 'form.InserisciTemplate_TemplateForm' ).each( function () {
for ( var a = 0, l = ElementForms.length; a < l; a++ ) {
var ThisFormNewItem = ElementForms[ a ];{
template: this.getElementById( 'template' ).value,
var NewItem = {};
string: this.getElementById( 'string' ).value,
NewItem.template = getElementWithId( 'template', 'input', ThisForm ).value;
where: this.getElementById( 'where' ).value,
NewItem.string = getElementWithId( 'string', 'input', ThisForm ).value;
noinclude: this.getElementById( 'noinclude' ).checked,
NewItem.where = getElementWithId( 'where', 'select', ThisForm ).value;
subst: this.getElementById( 'subst' ).checked
NewItem.noinclude = ( getElementWithId( 'noinclude', 'input', ThisForm ).checked ? true : false );
};
NewItem.subst = ( getElementWithId( 'subst', 'input', ThisForm ).checked ? true : false );
NewItem.parameters = {};
var ParamLIs = lrcGetElementsByClass$( "LI_parameters_li",this ThisForm,).find( "'li".LI_parameters_li' );
for ( var b = 0, m = ParamLIs.length;each( b < m; b++function () {
var ParamLiNewParam = ParamLIs[ b ];{
name: this.getElementById( 'Param_name' ).value,
var NewParam = {};
type: this.getElementById( 'Param_type' ).value
var NewParamId = getElementWithId( 'Param_id', 'input', ParamLi ).value;
};
NewParam.name = getElementWithId( 'Param_name', 'input', ParamLi ).value;
NewParam.type = getElementWithId( 'Param_type', 'select', ParamLi ).value;
if ( NewParam.type == "string" ) {
NewParam.var value = getElementWithIdthis.getElementById( 'Param_value', 'input', ParamLi ).value.unhtmlize();
if ( !NewParam.value ) {
delete NewParam.value = value;
}
} else {
NewParam.value = [];
var ParamValueSelect$Opt = getElementWithId$( this.getElementById( 'Param_value', ) ).find( 'select.option', ParamLi );
Opt.each( function () {
var Opt = ParamValueSelect.getElementsByTagName( 'option' );
NewParam.value.push( this.value.unhtmlize() );
for ( var c = 0, k = Opt.length; c < k; c++ ) {
} );
NewParam.value.push( Opt[ c ].value.unhtmlize() );
}
}
NewItem.parameters[ NewParamIdthis.getElementById( 'Param_id' ).value ] = NewParam;
} );
Items[ NewItem.template ] = NewItem;
} );
var TextToSave = InserisciTemplate_CompareNewParams( Items );
if ( !TextToSave ) {
Riga 1 397 ⟶ 1 348:
function InserisciTemplate_CompareNewParams( NewItems ) {
var OldItems = lstMyTemplate;
 
if ( JSON.stringify( Object.keys( OldItems ).sort() ) !== JSON.stringify( Object.keys( NewItems ).sort() ) ) {
return true;
}
 
for ( var template in OldItems ) {
iffor ( typeof(var NewItemsitem in OldItems[ template ] ) === "undefined" ) {
if ( item !== "parameters" && OldItems[ template ][ item ] !== NewItems[ template ][ item ] ) {
return true;
}
}
 
var oldParams = OldItems[ template ].parameters,
newParams = NewItems[ template ].parameters;
 
if ( Object.keys( oldParams ).length !== Object.keys( newParams ).length ) {
return true;
}
for ( var itemparam in OldItems[ template ]oldParams ) {
if ( item !=newParams[ param "parameters"] ) {
return true;
if ( OldItems[ template ][ item ] !== NewItems[ template ][ item ] ) {
}
return true;
for ( var paramitem in oldParams[ param ] ) {
}
if ( paramitem != "value" || ( typeof( oldParams[ param ][ paramitem ] ) != "object" && typeof( newParams[ param ][ paramitem ] ) != "object" ) ) {
} else {
if ( lrcGetObjectLength( OldItemsoldParams[ templateparam ][ itemparamitem ] ) !== lrcGetObjectLength( NewItemsnewParams[ templateparam ][ itemparamitem ] ) ) {
return true;
}
for ( var param in OldItems[ template ][ item ] ) {
if ( !NewItems[ template ][ item ][ param ] ) {
return true;
}
} else {
for ( var paramitem in OldItems[ template ][ item ][ param ] ) {
if ( paramitem != "value" || ( typeof( OldItems[ template ][ item ]oldParams[ param ][ paramitem ] ).length != "object" && typeof( NewItems[ template ][ item ]newParams[ param ][ paramitem ] ) != "object" ).length ) {
return true;
if ( OldItems[ template ][ item ][ param ][ paramitem ] !== NewItems[ template ][ item ][ param ][ paramitem ] ) {
return true;}
for ( var a = 0, l = oldParams[ param ][ paramitem ].length; a < l; a++ ) {
}
if ( oldParams[ param ][ paramitem ][ a ] != newParams[ param ][ paramitem ][ a ] ) {
} else {
return true;
if ( OldItems[ template ][ item ][ param ][ paramitem ].length != NewItems[ template ][ item ][ param ][ paramitem ].length ) {
return true;
}
for ( var a = 0, l = OldItems[ template ][ item ][ param ][ paramitem ].length; a < l; a++ ) {
if ( OldItems[ template ][ item ][ param ][ paramitem ][ a ] != NewItems[ template ][ item ][ param ][ paramitem ][ a ] ) {
return true;
}
}
}
}
}
}
}
}
for ( var template in NewItems ) {
if ( typeof( OldItems[ template ] ) === "undefined" ) {
return true;
}
}
Riga 1 442 ⟶ 1 391:
}
 
// =====*** Vars for configuration panel fieldset =====*/
 
// Texts
 
lrcTexts.IT_InsertTemplate1 = "Inserisce il template {{";
Riga 1 456 ⟶ 1 403:
lrcTexts.IT_AddOption = "Aggiungi una nuova opzione";
 
 
// Descriptions
lrcParamDesc.DescIT_InsertTemplate1 = "[InserisciTemplate] Inserisci la parte 1 della frase del template";
lrcParamDesc.DescIT_InsertTemplate2 = "[InserisciTemplate] Inserisci la parte 2 della frase del template";
Riga 1 473 ⟶ 1 418:
// Hide/Show tabs panel item
addParamMenuTab( "InserisciTemplateLegend", true );
 
/* ************************************************************************************************************************************************ */