Aller au contenu

MediaWiki:Gadget-0xBlockMessage.js

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 11 novembre 2017 à 19:44 et modifiée en dernier par 0x010C (discuter | contributions) (modernisation). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
var bm_texts = {
	"default-header" : "Bloquer l’utilisateur",
	"success-header" : "Blocage réussi",
	"form-legend": "Avertir \'$1\' de son blocage",
	"template-label": "Modèle",
	"subst-label": "Subster le modèle",
	"clean-label": "Effacer la PdD avant de mettre le modèle",
	"extra-label": "Rajouter un message sous le modèle",
	"submit-label": "Avertir",
	"message-summary": "Blocage",
	"section-title": "Blocage",
	"successnotif": "La pose du message a réussi",
	"time-label": "Durée",
	"unit-label": "Unité",
};

var templatesList = {
  finite: [
    {
      label: "Bloqué",
      data: '{ "template": "Bloqué|$1|$2" }'
    }, {
      label: "Bloqué sans avertissement",
      data: '{ "template": "Bloqué sans avertissement|$1|$2" }'
    }, {
      label: "Insultes",
      data: '{ "template": "Insultes|$1|$2" }'
    }, {
      label: "IP partagée bloquée",
      data: '{ "template": "IP partagée bloquée|$1|$2" }'
    }, {
      label: "Vandale scolaire bloqué",
      data: '{ "template": "Vandale scolaire bloqué" }',
      'autoselect-time': '15 july 2018'
    }, {
      label: "Proxy",
      data: '{ "template": "Proxy", "default-subst": false }',
      'autoselect-reason': '[[Wikipédia:Proxy ouvert|Proxy ouvert]]'
    }, {
      label: "IP bloquée pour 3 jours",
      data: '{ "template": "Adresse IP bloquée pour trois jours|$3|$1|$2" }'
    },
  ],
  infinite: [
    {
      label: "Compte bloqué indéfiniment",
      data: '{ "template": "Compte bloqué indéfiniment|$3", "default-clean": true }'
    }, {
      label: "Vandale banni",
      data: '{ "template": "Vandale banni", "default-clean": true }'
    }, {
      label: "Vandale banni sans avertissement",
      data: '{ "template": "Vandale banni sans avertissement", "default-clean": true }'
    }, {
      label: "Bot banni",
      data: '{ "template": "Bot banni" }'
    },
  ]
};


$( function() {
  if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Special' && mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Block' ) {
    mw.loader.using( [ 'mediawiki.cookie' ], function() {
      // On the Block page
      if ( $( '#firstHeading' ).html() === bm_texts[ 'default-header' ] ) {
        $( 'form.mw-htmlform' ).submit( function( event ) {
          if ( $( '#mw-input-wpExpiry select' ).val() === 'other' ) {
            mw.cookie.set( 'blockmessage-expiry', $( 'input[name="wpExpiry-other"]' ).val() );
          }
          else {
            mw.cookie.set( 'blockmessage-expiry', $( '#mw-input-wpExpiry select' ).val() );
          }
          if ( $( '#mw-input-wpReason-select select' ).val() === 'other' ) {
            mw.cookie.set( 'blockmessages-reason', $( 'input[name="wpReason-other"]' ).val() );
          }
          else if ( $( 'input[name="wpReason-other"]' ).val() !== '' ) {
            mw.cookie.set( 'blockmessage-reason', $( '#mw-input-wpReason-select select' ).val() + ' : ' + $( 'input[name="wpReason-other"]' ).val() );
          }
          else {
            mw.cookie.set( 'blockmessage-reason', $( '#mw-input-wpReason-select select' ).val() );
          }
        });
      }

      // When the user has successfully blocked someone
      else if ($("#firstHeading").html() == bm_texts['success-header'] && $.cookie("blockmessages-duree") !== null) {
        window.blockMessage = new BlockMessage( mw.config.get( 'wgRelevantUserName' ), mw.cookie.get( 'blockmessage-reason' ), mw.cookie.get( 'blockmessage-expiry' ) );
        window.blockMessage.init();
      }
    } );
	}
});

var BlockMessage = function(targetUser, reason, duration) {
  this.targetUser = targetUser;
  this.reason = reason;
  this.duration = duration;
  this.isDurationInfinite = ( this.duration === 'infinite' );
  
  this.usernameInput;
  this.templateDropdownInput;
  this.expiryInput;
  this.substCheckboxInput;
  this.cleanCheckboxInput;
  this.extraCheckboxInput;
  this.extraTextarea;
  this.submitInput;
  
  this.expiryField;
  this.extraTextareaField;
  this.form;
};

BlockMessage.prototype.init = function() {
  this.createWidgets();
  this.addEventHandlers();
  this.initializeWidgets();
  
  $( '#mw-content-text' ).append( this.form.$element );
};

BlockMessage.prototype.createWidgets = function() {
  this.usernameInput = new OO.ui.TextInputWidget( {
    name: 'username',
    disabled: 'disabled'
  } );
  this.templateDropdownInput = new OO.ui.DropdownInputWidget();
  this.expiryInput = new mw.widgets.SelectWithInputWidget( {
    or: false,
    dropdowninput: {
      label: 'Unité',
      options: [
        { data: 'heure', label: 'Heure' },
        { data: 'heures', label: 'Heures' },
        { data: 'jour', label: 'Jour' },
        { data: 'jours', label: 'Jours' },
        { data: 'semaine', label: 'Semaine' },
        { data: 'semaines', label: 'Semaines' },
        { data: 'mois', label: 'Mois' },
        { data: 'année', label: 'Année' },
        { data: 'années', label: 'Années' }
      ]
    },
    textinput: {
      placeholder: 'nombre'
    }
  } );
  this.substCheckboxInput = new OO.ui.CheckboxInputWidget( {
    name: '',
    selected: true
  } );
  this.cleanCheckboxInput = new OO.ui.CheckboxInputWidget( {
    name: '',
    selected: false
  } );
  this.extraCheckboxInput = new OO.ui.CheckboxInputWidget( {
    name: '',
    selected: false
  } );
  this.extraTextarea = new OO.ui.MultilineTextInputWidget( {
    rows: 10,
    autosize: true,
  } );
  this.submitInput = new OO.ui.ButtonInputWidget( {
    type: 'submit',
    label: 'Avertir',
    flags: [ 'primary', 'destructive' ],
  } );
  
  this.expiryField = new OO.ui.FieldLayout( this.expiryInput, {
    align: 'top',
    label: 'Expiration'
  } );
  this.extraTextareaField = new OO.ui.FieldLayout( this.extraTextarea, {
    align: 'top',
    label: $( '<small>(attention, la signature n\'est plus apposé automatiquement)</small>' )
  } );
  
  this.form = new OO.ui.FieldsetLayout( {
    id: 'demo-section-formLayout',
    label: 'Avertir du blocage',
    items: [
      new OO.ui.FieldLayout( this.usernameInput, {
        align: 'top',
        label: 'User name'
      } ),
      new OO.ui.FieldLayout( this.templateDropdownInput, {
        align: 'top',
        label: 'Modèle'
      } ),
      this.expiryField,
      new OO.ui.FieldLayout( this.substCheckboxInput, {
        align: 'inline',
        label: 'Subster le modèle'
      } ),
      new OO.ui.FieldLayout( this.cleanCheckboxInput, {
        align: 'inline',
        label: 'Effacer la PdD avant de mettre le modèle'
      } ),
      new OO.ui.FieldLayout( this.extraCheckboxInput, {
        align: 'inline',
        label: 'Rajouter un message avec le modèle'
      } ),
      this.extraTextareaField,
      new OO.ui.FieldLayout( this.submitInput )
    ]
  } );
};

BlockMessage.prototype.addEventHandlers = function() {
  var self = this;
  
  this.submitInput.on( 'click', function() {
    self.submitInput.setDisabled(true);
    self.postMessage();
  } );
  
  this.templateDropdownInput.on( 'change', function (value) {
    value = JSON.parse(value);
    if ( value.hasOwnProperty( 'default-subst' ) ) {
      self.substCheckboxInput.setSelected( value[ 'default-subst' ] );
    }
    if ( value.hasOwnProperty( 'default-clean' ) ) {
      self.cleanCheckboxInput.setSelected( value[ 'default-clean' ] );
    }
  } );

  this.extraCheckboxInput.on( 'change', function ( checked ) {
    self.extraTextareaField.toggle( checked );
  } );
};

BlockMessage.prototype.initializeWidgets = function() {
  var templates;
  if ( this.isDurationInfinite ) {
    this.expiryField.toggle( false );
    templates = templatesList.infinite;
  }
  else {
    templates = templatesList.finite;
    this.computeDuration();
  }
  this.templateDropdownInput.setOptions( templates );
  
  // Autoselect a template depending of the reasons or the duration
  for ( var i=0; i<templates.length; i++ ) {
    if ( 'autoselect-reason' in templates[ i ] ) {
      if ( templates[ i ][ 'autoselect-reason' ] === this.reason ) {
        this.templateDropdownInput.setValue( templates[ i ].data );
        break;
      }
    }
    if ( 'autoselect-duration' in templates[ i ] ) {
      if ( templates[ i ][ 'autoselect-duration' ] === this.duration ) {
        this.templateDropdownInput.setValue( templates[ i ].data );
        break;
      }
    }
  }
  
  this.usernameInput.setValue( this.targetUser );
  this.extraTextareaField.toggle( false );
};

BlockMessage.prototype.computeDuration = function() {
	if ( this.duration !== null ) {
		var splitedDuration = this.duration.split( ' ' );
    if ( splitedDuration.length === 2 && !isNaN( splitedDuration[ 0 ] ) ) {
      time_set = true;
			switch ( splitedDuration[ 1 ] ) {
				case "hour":
					this.expiryInput.dropdowninput.setValue( 'heure' );
					break;
				case "hours":
					this.expiryInput.dropdowninput.setValue( 'heures' );
					break;
				case "day":
					this.expiryInput.dropdowninput.setValue( 'jour' );
					break;
				case "days":
					this.expiryInput.dropdowninput.setValue( 'jours' );
					break;
				case "week":
					this.expiryInput.dropdowninput.setValue( 'semaine' );
					break;
				case "weeks":
					this.expiryInput.dropdowninput.setValue( 'semaines' );
					break;
				case "month":
					this.expiryInput.dropdowninput.setValue( 'mois' );
					break;
				case "months":
					this.expiryInput.dropdowninput.setValue( 'mois' );
					break;
				case "year":
					this.expiryInput.dropdowninput.setValue( 'année' );
					break;
				case "years":
					this.expiryInput.dropdowninput.setValue( 'années' );
					break;
				default:
					time_set = false;
			}
			if (time_set) {
				this.expiryInput.textinput.setValue( splitedDuration[ 0 ] );
			}
		}
	}
};

BlockMessage.prototype.postMessage = function() {
  var subst = '';
  if ( this.substCheckboxInput.isSelected() ) {
    subst = 'subst:';
  }
  var extra = ' ~~' + '~~';
  if ( this.extraCheckboxInput.isSelected() ) {
    extra = '\n\n' + this.extraTextarea.getValue();
  }

  var self = this;
  new mw.Api().edit( 'User talk:' + self.targetUser, function( revision ) {
    var template = JSON.parse( self.templateDropdownInput.getValue() ).template;
    template = template.replace( '$1', self.expiryInput.textinput.getValue() );
    template = template.replace( '$2', self.expiryInput.dropdowninput.getValue() );
    template = template.replace( '$3', self.reason );
    
    var data = {
      summary: bm_texts['message-summary'],
      text: '{{' + subst + template + '}}' + extra,
    };
    
    if ( ! self.substCheckboxInput.isSelected() ) {
      data.section = "new";
      data.sectiontitle = bm_texts['section-title'];
    }
    
    return data;
  } ).then( function ( data ) {
    mw.notify(bm_texts['successnotif']);
    mw.cookie.set( 'blockmessage-user', null );
    mw.cookie.set( 'blockmessage-duree', null );
    mw.cookie.set( 'blockmessage-reason', null );
    setTimeout( function() {
      document.location.href = mw.util.getUrl( 'User talk:' + self.targetUser + '#footer' );
    }, 750 );
  } );
};