MediaWiki:Gadget-collegamentipersonali.js e Utente:Ricordisamoa/Gadget-collegamentipersonali.js: differenze tra le pagine
(Differenze fra le pagine)
Contenuto cancellato Contenuto aggiunto
m Jalo ha spostato la pagina Utente:Rotpunkt/Gadgets/Gadget-collegamentipersonali.js a MediaWiki:Gadget-collegamentipersonali.js senza lasciare redirect |
uso di mw:API:Options invece di una pagina wiki per le impostazioni; applicate convenzioni |
||
Riga 8: | Riga 8: | ||
* |
* |
||
* @author [[Utente:Rotpunkt]] |
* @author [[Utente:Rotpunkt]] |
||
* @author [[Utente:Ricordisamoa]] |
|||
*/ |
*/ |
||
/* global mediaWiki, jQuery */ |
|||
/*jslint unparam: true */ |
|||
( function ( mw, $ ) { |
|||
/*global window: false, mediaWiki: false, jQuery: false */ |
|||
'use strict'; |
|||
var api = new mw.Api(), |
|||
(function (mw, $) { |
|||
optionKey = 'userjs-personallinks-links', // codice dell'opzione MediaWiki per i collegamenti personali |
|||
'use strict'; |
|||
userLinks; // collegamenti personali dell'utente |
|||
function writeUserConfig( config ) { |
|||
// Configurazione utente |
|||
api.postWithToken( 'options', { |
|||
var userConfig = 'Utente:' + mw.config.get('wgUserName') + |
|||
action: 'options', |
|||
'/' + mw.config.get('skin') + '.js', |
|||
optionname: optionKey, |
|||
// Collegamenti personali dell'utente (chiamato myLinks nel vector/monobook.js dell'utente) |
|||
optionvalue: config |
|||
userLinks; |
|||
} ) |
|||
.done( function ( data ) { |
|||
if ( data && data.options && data.options.result === 'Success' ) { |
|||
mw.notify( 'La configurazione dei collegamenti personali è stata aggiornata.' ); |
|||
} else { |
|||
var msg = data && data.error ? ': ' + data.error.code + ' ' + data.error.info : ''; |
|||
mw.notify( 'Errore nell\'aggiornare la configurazione dei collegamenti personali' + |
|||
' ' + msg ); |
|||
} |
|||
} ); |
|||
} |
|||
function updatePortletLinks() { |
|||
var url; |
|||
$.ajax({ |
|||
$( 'li[id^="t-gcp"]' ).remove(); |
|||
url: mw.config.get('wgScript'), |
|||
$.each( userLinks, function ( i, userLink ) { |
|||
data: { |
|||
url = userLink[1].replace( '%TITOLO%', mw.config.get( 'wgPageName' ) ); |
|||
title: userConfig, |
|||
url = url.replace( '%TITOLO2%', mw.config.get( 'wgTitle' ) ); |
|||
action: 'raw' |
|||
mw.util.addPortletLink( 'p-navigation', url, userLink[0], 't-gcp' + i ); |
|||
}, |
|||
} ); |
|||
cache: false, |
|||
} |
|||
dataType: 'script' |
|||
}) |
|||
.done(function (data) { |
|||
readHandler(data); |
|||
}) |
|||
.fail(function (jqXHR, textStatus, errorThrown) { |
|||
mw.notify('Errore nel leggere ' + userConfig + ': ' + errorThrown); |
|||
}); |
|||
} |
|||
function buildRow( userLink ) { |
|||
function writeUserConfig(content) { |
|||
var $span, $input, $tr = $( '<tr>' ), |
|||
$.ajax({ |
|||
properties = { |
|||
url: mw.util.wikiScript('api'), |
|||
'width': '100%', |
|||
data: { |
|||
'box-sizing': 'border-box', |
|||
action: 'edit', |
|||
'-moz-box-sizing': 'border-box', |
|||
title: userConfig, |
|||
'-webkit-box-sizing': 'border-box' |
|||
text: content, |
|||
}; |
|||
summary: 'collegamenti personali', |
|||
// icona spostamento |
|||
token: mw.user.tokens.get('editToken'), |
|||
$span = $( '<span>' ).addClass( 'ui-icon ui-icon-arrowthick-2-n-s' ); |
|||
format: 'json' |
|||
$( '<td>' ).append( $span ).appendTo( $tr ); |
|||
}, |
|||
// input text |
|||
dataType: 'json', |
|||
$input = $( '<input>' ).attr( 'type', 'text' ).val( userLink[0] ).css( properties ); |
|||
type: 'POST' |
|||
$( '<td>' ).css( 'width', '20%' ).append( $input ).appendTo( $tr ); |
|||
}) |
|||
$input = $( '<input>' ).attr( 'type', 'text' ).val( userLink[1] ).css( properties ); |
|||
.done(function (data) { |
|||
$( '<td>' ).css( 'width', '78%' ).append( $input ).appendTo( $tr ); |
|||
var url, msg; |
|||
// checkbox |
|||
if (data && data.edit && data.edit.result === 'Success') { |
|||
$( '<td>' ).append( $( '<input>' ).attr( 'type', 'checkbox' ) ).appendTo( $tr ); |
|||
url = mw.config.get('wgArticlePath').replace('$1', userConfig); |
|||
return $tr; |
|||
msg = 'La configurazione dei collegamenti personali è stata aggiornata in <a href="' + |
|||
} |
|||
url + '" title="' + userConfig + '">' + userConfig + '</a>'; |
|||
mw.notify($.parseHTML(msg)); |
|||
} else { |
|||
msg = data && data.error ? ': ' + data.error.code + ' ' + data.error.info : ''; |
|||
mw.notify('Errore nell\'aggiornare ' + userConfig + ' ' + msg); |
|||
} |
|||
}); |
|||
} |
|||
// Visualizza la finestra di dialogo per modificare i "collegamenti personali" |
|||
function updatePortletLinks() { |
|||
function showConfigDialog() { |
|||
var url; |
|||
var $table, $thead, $tbody, $tr, $checkbox; |
|||
$("li[id^=t-gcp]").remove(); |
|||
$.each(userLinks, function (i, userLink) { |
|||
url = userLink[1].replace('%TITOLO%', mw.config.get('wgPageName')); |
|||
url = url.replace('%TITOLO2%', mw.config.get('wgTitle')); |
|||
mw.util.addPortletLink('p-navigation', url, userLink[0], 't-gcp' + i); |
|||
}); |
|||
} |
|||
$table = $( '<table>' ); |
|||
function buildRow(userLink) { |
|||
$thead = $( '<thead>' ).appendTo( $table ); |
|||
var $span, $input, $tr = $('<tr>'), |
|||
$tr = $( '<tr>' ).attr( 'bgcolor', 'lightgrey' ).appendTo( $thead ); |
|||
properties = { |
|||
$( '<th>' ).css( 'width', '2%' ).text( '' ).appendTo( $tr ); |
|||
'width': '100%', |
|||
$checkbox = $( '<input>' ) |
|||
'box-sizing': 'border-box', |
|||
.attr( 'id', 'gcp-selectall' ) |
|||
'-moz-box-sizing': 'border-box', |
|||
.attr( 'type', 'checkbox' ) |
|||
'-webkit-box-sizing': 'border-box' |
|||
.attr( 'tabindex', '-1' ) |
|||
}; |
|||
.click( function () { |
|||
// icona spostamento |
|||
$( '#gcp-dialog :checkbox' ).prop( 'checked', this.checked ); |
|||
$span = $('<span>').addClass('ui-icon ui-icon-arrowthick-2-n-s'); |
|||
} ); |
|||
$('<td>').append($span).appendTo($tr); |
|||
$( '<th>' ).css( 'width', '20%' ).text( 'Nome' ).appendTo( $tr ); |
|||
// input text |
|||
$( '<th>' ).css( 'width', '78%' ).text( 'Indirizzo' ).appendTo( $tr ); |
|||
$( '<th>' ).append( $checkbox ).appendTo( $tr ); |
|||
$tbody = $( '<tbody>' ).appendTo( $table ); |
|||
$input = $('<input>').attr('type', 'text').val(userLink[1]).css(properties); |
|||
$.each( userLinks, function ( i, userLink ) { |
|||
$('<td>').css('width', '78%').append($input).appendTo($tr); |
|||
$tbody.append( buildRow( userLink ) ); |
|||
// checkbox |
|||
} ); |
|||
$('<td>').append($('<input>').attr('type', 'checkbox')).appendTo($tr); |
|||
$tbody.sortable( { handle: 'span' } ); |
|||
return $tr; |
|||
$( '#gcp-dialog' ).html( $table ); |
|||
} |
|||
// visualizza il dialog |
|||
// Visualizza la finestra di dialogo per modificare i "collegamenti personali" |
|||
$( '#gcp-dialog' ).dialog( { |
|||
function showConfigDialog() { |
|||
title: 'Configurazione collegamenti personali', |
|||
var $table, $thead, $tbody, $tr, $checkbox; |
|||
width: 800, |
|||
height: 300, |
|||
modal: true, |
|||
buttons: { |
|||
'Aggiungi riga': function () { |
|||
$table.append( buildRow( ['', ''] ) ); |
|||
}, |
|||
'Cancella riga': function () { |
|||
$( '#gcp-dialog :checkbox:checked:not(#gcp-selectall)' ).parents( 'tr' ).remove(); |
|||
$( '#gcp-selectall' ).attr( 'checked', false ); |
|||
}, |
|||
'Salva': function () { |
|||
// genera la nuova configurazione |
|||
var confLinks = $table.find( 'tr:has(td)' ).map( function () { |
|||
var $input = $( this ).find( 'input' ); |
|||
return $.trim( $input.eq( 0 ).val() ) !== '' && |
|||
$.trim( $input.eq( 1 ).val() ) !== '' ? |
|||
[[ $input.eq( 0 ).val(), $input.eq( 1 ).val() ]] : null; |
|||
} ).get(), |
|||
confLinksJSON = JSON.stringify( confLinks ), |
|||
userLinksJSON = JSON.stringify( userLinks ); |
|||
// se necessario memorizza la configurazione nelle opzioni |
|||
if ( confLinksJSON === userLinksJSON ) { |
|||
mw.notify( 'I collegamenti personali non sono stati modificati.' ); |
|||
} else { |
|||
writeUserConfig( confLinksJSON ); |
|||
userLinks = confLinks; |
|||
// aggiorna i link |
|||
updatePortletLinks(); |
|||
} |
|||
$( this ).dialog( 'close' ); |
|||
}, |
|||
'Annulla': function () { |
|||
$( this ).dialog( 'close' ); |
|||
} |
|||
} |
|||
} ); |
|||
} |
|||
$( window ).load( function () { |
|||
$table = $('<table>'); |
|||
mw.loader.using( ['user.options', 'jquery.ui.dialog', 'jquery.ui.sortable'], function () { |
|||
$thead = $('<thead>').appendTo($table); |
|||
userLinks = mw.user.options.exists( optionKey ) ? JSON.parse( mw.user.options.get( optionKey ) ) : []; |
|||
$tr = $('<tr>').attr('bgcolor', 'lightgrey').appendTo($thead); |
|||
// setup portletLink configurazione |
|||
$('<th>').css('width', '2%').text('').appendTo($tr); |
|||
$( mw.util.addPortletLink( 'p-tb', '#', 'Collegamenti personali' ) ) |
|||
$checkbox = $('<input>') |
|||
.click( function ( event ) { |
|||
.attr('id', 'gcp-selectall') |
|||
event.preventDefault(); |
|||
.attr('type', 'checkbox') |
|||
showConfigDialog(); |
|||
.attr('tabindex', '-1') |
|||
} ); |
|||
.click(function () { |
|||
// setup dialog |
|||
$('#gcp-dialog :checkbox').prop('checked', this.checked); |
|||
$( '<div>' ).attr( 'id', 'gcp-dialog' ).appendTo( 'body' ); |
|||
}); |
|||
// setup collegamenti |
|||
$('<th>').css('width', '20%').text('Nome').appendTo($tr); |
|||
if ( userLinks.length > 0 ) { |
|||
$('<th>').css('width', '78%').text('Indirizzo').appendTo($tr); |
|||
updatePortletLinks(); |
|||
$('<th>').append($checkbox).appendTo($tr); |
|||
} |
|||
$tbody = $('<tbody>').appendTo($table); |
|||
} ); |
|||
$.each(userLinks, function (i, userLink) { |
|||
} ); |
|||
$tbody.append(buildRow(userLink)); |
|||
}( mediaWiki, jQuery ) ); |
|||
}); |
|||
$tbody.sortable({ handle: 'span' }); |
|||
$('#gcp-dialog').html($table); |
|||
// visualizza il dialog |
|||
$('#gcp-dialog').dialog({ |
|||
title: 'Configurazione collegamenti personali', |
|||
width: 800, |
|||
height: 300, |
|||
modal: true, |
|||
buttons: { |
|||
'Aggiungi riga': function () { |
|||
$table.append(buildRow(['', ''])); |
|||
}, |
|||
'Cancella riga': function () { |
|||
$('#gcp-dialog :checkbox:checked:not(#gcp-selectall)').parents('tr').remove(); |
|||
$('#gcp-selectall').attr('checked', false); |
|||
}, |
|||
'Salva': function () { |
|||
var confLinks, confLinksJSON, userLinksJSON, |
|||
expr = /var\s+myLinks\s*=[\s\S]*?;/; |
|||
// genera la nuova configurazione |
|||
confLinks = $table.find('tr:has(td)').map(function () { |
|||
var $input = $(this).find('input'); |
|||
return $.trim($input.eq(0).val()) !== '' && |
|||
$.trim($input.eq(1).val()) !== '' ? |
|||
[[ $input.eq(0).val(), $input.eq(1).val() ]] : null; |
|||
}).get(); |
|||
confLinksJSON = JSON.stringify(confLinks); |
|||
userLinksJSON = JSON.stringify(userLinks); |
|||
// se necessario scrive la configurazione nella pagina dell'utente |
|||
if (confLinksJSON === userLinksJSON) { |
|||
mw.notify('I collegamenti personali non sono stati modificati.'); |
|||
} else { |
|||
confLinksJSON = 'var myLinks = ' + confLinksJSON + ';'; |
|||
readUserConfig(function (content) { |
|||
content = content.match(expr) ? |
|||
content.replace(expr, confLinksJSON) : |
|||
content + '\n' + confLinksJSON; |
|||
writeUserConfig(content); |
|||
userLinks = confLinks; |
|||
// aggiorna i link |
|||
updatePortletLinks(); |
|||
}); |
|||
} |
|||
$(this).dialog('close'); |
|||
}, |
|||
'Annulla': function () { |
|||
$(this).dialog('close'); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
$(window).load(function () { |
|||
mw.loader.using(['user.options', 'jquery.ui.dialog', 'jquery.ui.sortable'], function () { |
|||
userLinks = window.myLinks === undefined ? [] : window.myLinks; |
|||
// setup portletLink configurazione |
|||
var portletLink = mw.util.addPortletLink('p-tb', '#', 'Collegamenti personali'); |
|||
$(portletLink).click(function (event) { |
|||
event.preventDefault(); |
|||
showConfigDialog(); |
|||
}); |
|||
// setup dialog |
|||
$('<div>').attr('id', 'gcp-dialog').appendTo('body'); |
|||
// setup collegamenti |
|||
if (userLinks.length > 0) { |
|||
updatePortletLinks(); |
|||
} |
|||
}); |
|||
}); |
|||
}(mediaWiki, jQuery)); |
Versione delle 07:46, 4 set 2014
/**
* Gadget-collegamentipersonali.js
* Aggiunge dei collegamenti personali al menu navigazione della sidebar.
* I collegamenti sono configurabili con una finestra di dialogo che si attiva dal menu strumenti.
* Riscritto da zero a partire da:
* http://it.wikipedia.org/w/index.php?title=MediaWiki:Collegamenti_personali.js&oldid=47557920
* http://it.wikipedia.org/w/index.php?title=MediaWiki:Gadget-collegamentipersonali.js&oldid=38993132
*
* @author [[Utente:Rotpunkt]]
* @author [[Utente:Ricordisamoa]]
*/
/* global mediaWiki, jQuery */
( function ( mw, $ ) {
'use strict';
var api = new mw.Api(),
optionKey = 'userjs-personallinks-links', // codice dell'opzione MediaWiki per i collegamenti personali
userLinks; // collegamenti personali dell'utente
function writeUserConfig( config ) {
api.postWithToken( 'options', {
action: 'options',
optionname: optionKey,
optionvalue: config
} )
.done( function ( data ) {
if ( data && data.options && data.options.result === 'Success' ) {
mw.notify( 'La configurazione dei collegamenti personali è stata aggiornata.' );
} else {
var msg = data && data.error ? ': ' + data.error.code + ' ' + data.error.info : '';
mw.notify( 'Errore nell\'aggiornare la configurazione dei collegamenti personali' +
' ' + msg );
}
} );
}
function updatePortletLinks() {
var url;
$( 'li[id^="t-gcp"]' ).remove();
$.each( userLinks, function ( i, userLink ) {
url = userLink[1].replace( '%TITOLO%', mw.config.get( 'wgPageName' ) );
url = url.replace( '%TITOLO2%', mw.config.get( 'wgTitle' ) );
mw.util.addPortletLink( 'p-navigation', url, userLink[0], 't-gcp' + i );
} );
}
function buildRow( userLink ) {
var $span, $input, $tr = $( '<tr>' ),
properties = {
'width': '100%',
'box-sizing': 'border-box',
'-moz-box-sizing': 'border-box',
'-webkit-box-sizing': 'border-box'
};
// icona spostamento
$span = $( '<span>' ).addClass( 'ui-icon ui-icon-arrowthick-2-n-s' );
$( '<td>' ).append( $span ).appendTo( $tr );
// input text
$input = $( '<input>' ).attr( 'type', 'text' ).val( userLink[0] ).css( properties );
$( '<td>' ).css( 'width', '20%' ).append( $input ).appendTo( $tr );
$input = $( '<input>' ).attr( 'type', 'text' ).val( userLink[1] ).css( properties );
$( '<td>' ).css( 'width', '78%' ).append( $input ).appendTo( $tr );
// checkbox
$( '<td>' ).append( $( '<input>' ).attr( 'type', 'checkbox' ) ).appendTo( $tr );
return $tr;
}
// Visualizza la finestra di dialogo per modificare i "collegamenti personali"
function showConfigDialog() {
var $table, $thead, $tbody, $tr, $checkbox;
$table = $( '<table>' );
$thead = $( '<thead>' ).appendTo( $table );
$tr = $( '<tr>' ).attr( 'bgcolor', 'lightgrey' ).appendTo( $thead );
$( '<th>' ).css( 'width', '2%' ).text( '' ).appendTo( $tr );
$checkbox = $( '<input>' )
.attr( 'id', 'gcp-selectall' )
.attr( 'type', 'checkbox' )
.attr( 'tabindex', '-1' )
.click( function () {
$( '#gcp-dialog :checkbox' ).prop( 'checked', this.checked );
} );
$( '<th>' ).css( 'width', '20%' ).text( 'Nome' ).appendTo( $tr );
$( '<th>' ).css( 'width', '78%' ).text( 'Indirizzo' ).appendTo( $tr );
$( '<th>' ).append( $checkbox ).appendTo( $tr );
$tbody = $( '<tbody>' ).appendTo( $table );
$.each( userLinks, function ( i, userLink ) {
$tbody.append( buildRow( userLink ) );
} );
$tbody.sortable( { handle: 'span' } );
$( '#gcp-dialog' ).html( $table );
// visualizza il dialog
$( '#gcp-dialog' ).dialog( {
title: 'Configurazione collegamenti personali',
width: 800,
height: 300,
modal: true,
buttons: {
'Aggiungi riga': function () {
$table.append( buildRow( ['', ''] ) );
},
'Cancella riga': function () {
$( '#gcp-dialog :checkbox:checked:not(#gcp-selectall)' ).parents( 'tr' ).remove();
$( '#gcp-selectall' ).attr( 'checked', false );
},
'Salva': function () {
// genera la nuova configurazione
var confLinks = $table.find( 'tr:has(td)' ).map( function () {
var $input = $( this ).find( 'input' );
return $.trim( $input.eq( 0 ).val() ) !== '' &&
$.trim( $input.eq( 1 ).val() ) !== '' ?
[[ $input.eq( 0 ).val(), $input.eq( 1 ).val() ]] : null;
} ).get(),
confLinksJSON = JSON.stringify( confLinks ),
userLinksJSON = JSON.stringify( userLinks );
// se necessario memorizza la configurazione nelle opzioni
if ( confLinksJSON === userLinksJSON ) {
mw.notify( 'I collegamenti personali non sono stati modificati.' );
} else {
writeUserConfig( confLinksJSON );
userLinks = confLinks;
// aggiorna i link
updatePortletLinks();
}
$( this ).dialog( 'close' );
},
'Annulla': function () {
$( this ).dialog( 'close' );
}
}
} );
}
$( window ).load( function () {
mw.loader.using( ['user.options', 'jquery.ui.dialog', 'jquery.ui.sortable'], function () {
userLinks = mw.user.options.exists( optionKey ) ? JSON.parse( mw.user.options.get( optionKey ) ) : [];
// setup portletLink configurazione
$( mw.util.addPortletLink( 'p-tb', '#', 'Collegamenti personali' ) )
.click( function ( event ) {
event.preventDefault();
showConfigDialog();
} );
// setup dialog
$( '<div>' ).attr( 'id', 'gcp-dialog' ).appendTo( 'body' );
// setup collegamenti
if ( userLinks.length > 0 ) {
updatePortletLinks();
}
} );
} );
}( mediaWiki, jQuery ) );