Utilisateur:AB Louis/chelper.js

Aller à la navigation Aller à la recherche

Note : après avoir enregistré tes préférences, tu devras forcer le rechargement complet du cache de ton navigateur pour voir les changements. Mozilla Firefox / Safari : maintiens la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou presse Maj-Ctrl-R (Maj-Cmd-R sur Apple Mac) ; Internet Explorer / Opera : maintiens la touche Ctrl en cliquant sur le bouton Actualiser ou presse Ctrl-F5.

//<nowiki>
C.tag = {
  dialog: null,
  config: {
    'modal-title': 'C helper - Ajouter/retirer des bandeaux de maintenance',
    'submit': 'Valider la requête',
    'c-tag-checkbox-class': 'c-tag-checkbox',
    'c-tag-radio-class': 'c-tag-radio',
    'notify-success': 'Les bandeaux ont bien été ajoutés.',
    'notify-no-reason': 'Vous n\'avez fait aucun changement.',
    'notify-not-a-page': 'Impossible de gérer les bandeaux d\'une page inexistante.',
    'notify-protected': 'Impossible de gérer les bandeaux de cette page, celle-ci est protégée.',
    'add-summary': 'Ajout de $1',
    'template-link-summary': '{{[[Modèle:$1|$1]]}}',
  },
  categories: [
  	{display:"Sourçage", node:null}, //0
  	{display:"Maintenance", node:null}, //3
  	{display:"Mise en forme", node:null}, //4

  ],
  /*
    {category:, display:'', template:'', help:'', reason:'', extra:''},
	Magic words :
		* $(page)
		* $(diff)
		* $(user)
		* $(day) $(month) $(year)
		* $(reason)
		* $(extra)
  */
  templates: [
	{category:3, display:'À désangliciser', template:'{{À désangliciser}}', help:''},
	{category:3, display:'À fusionner', template:'{{À fusionner|$(extra)}}', extra:'Liste des articles à fusionner, séparés par des « | » :', help:''},
	{category:3, display:'À illustrer', template:'{{À illustrer}}', help:''},
	{category:0, display:'À sourcer', template:'{{À sourcer|date=$(month) $(year)}}', help:''},
	{category:4, display:'À vérifier', template:'{{À vérifier}}', help:''},
	{category:0, display:'À vikifier', template:'{{À vikifier|date=$(month) $(year)}}', help:''},
	{category:2, display:'Article non neutre', template:'{{Article non neutre|date=$(month) $(year)}}', help:''},
	{category:3, display:'Copie de site', template:'{{Copie de site|1=$(reason)}}', reason:'Adresse du site copié : ', help:''},
	{category:0, display:'En travaux', template:'{{En travaux|$(user)|date=$(day) $(month) $(year)|commentaire=$(extra)}}', extra:'Commentaire facultatif :', help:''},
	{category:3, display:'Fusion d\'Historique', template:'{{Fusion d\'Historique|$(reason)|~~~}}', extra:'', reason:'Motif de la fusion :', help:''},
	{category:3, display:'Fusion technique', template:'{{Fusion technique|$(extra)}}', extra:'Liste des articles qui font l\'objet de la fusion, sparés par des « | » :', help:''},
	{category:0, display:'Orthographe', template:'{{Orthographe|date=$(month) $(year)}}', help:''},
	{category:0, display:'Suppression', template:'{{Suppression|date=$(month) $(year)}}', help:'Attention, il faut à la suite de la pose de ce bandeau ouvrir manuellement la procédure de PàS. Suivez les explications qui apparaîtront sur le bandeau.'},
  ],
  categories_container: null,
  alphabetic_container: null,
  current_display_mode: "categories",

  init: function() {
  },
  launch: function() {
  	if(mw.config.get('wgArticleId') === 0) {
    	mw.notify(C.tag.config['notify-not-a-page'], {title:'C-helper', type:'error'});
    	return;
  	}
  	if(mw.config.get('wgRestrictionEdit') == ["sysop"]) {
    	mw.notify(C.tag.config['notify-protected'], {title:'C-helper', type:'error'});
    	return;
  	}
	if(this.dialog === null) {
		this.build_dialog();
	}
	this.dialog.dialog("open");
  },
  "build_dialog": function() {
    this.dialog = $('<div>', {title:this.config['modal-title']});
    var form = $('<form>');
    this.dialog.append(form);
    //Radio
    $("<div>").text("Trier par ")
               .append($("<input>", {type:"radio",name:"C-tag-display-mode", value:"categories", id:"C-tag-display-mode-categories", class:this.config['c-tag-radio-class'], checked: "checked"}))
               .append($("<label>", {"for":"C-tag-display-mode-categories"}).text("Catégories"))
               .append($("<input>", {type:"radio",name:"C-tag-display-mode", value:"alphabetic", id:"C-tag-display-mode-alphabetic", class:this.config['c-tag-radio-class']}))
               .append($("<label>", {"for":"C-tag-display-mode-alphabetic"}).text("Ordre alphabétique"))
               .appendTo(form);

    //Create sections
    this.alphabetic_container = $("<div>", {id:"C-tag-alphabetic-container"});
    this.categories_container = $("<div>", {id:"C-tag-categories-container"});
    for(var i=0; i<this.categories.length; i++) {
    	this.categories[i].node = $("<div>").append($("<h3>").text(this.categories[i].display)).appendTo(this.categories_container);
    }
    for(var i=0; i<this.templates.length; i++) {
		this.templates[i].node = $("<div>");
		this.templates[i].node.append($("<input>", {type:"checkbox", id:"C-tag-"+i, tag_id:i, class:this.config['c-tag-checkbox-class']}));
		this.templates[i].node.append($("<label>", {"for":"C-tag-"+i}).text(this.templates[i].display));
		
		if(this.templates[i].help !== '') {
			this.templates[i].node.append(C.util.construct_help_icon(this.templates[i].help));
		}
		if(this.templates[i].hasOwnProperty('reason')) {
			this.templates[i].node.append($('<span>', {id:'C-tag-reason-'+i, class:this.config['c-tag-reason-class']})
			                      		.hide()
			                      		.append($('<br>'))
			                            .append($('<label>', {"for":'C-tag-reason-input-'+i}).html(this.templates[i].reason))
			                            .append("&nbsp;")
			    						.append($('<input>', {type:'text', id:'C-tag-reason-input-'+i})));
		}
		if(this.templates[i].hasOwnProperty('extra')) {
			this.templates[i].node.append($('<span>', {id:'C-tag-extra-'+i, class:this.config['c-tag-extra-class']})
			                      		.hide()
			                      		.append($('<br>'))
			                            .append($('<label>', {"for":'C-tag-extra-input'+i}).html(this.templates[i].extra))
			                            .append("&nbsp;")
			    						.append($('<input>', {type:'text', id:'C-tag-extra-input'+i})));
		}
		
		this.categories[this.templates[i].category].node.append(this.templates[i].node);
    }
    form.append(this.alphabetic_container);
    form.append(this.categories_container);
    
    this.dialog.dialog({
      autoOpen: false,
      height: 400,
      width: 600,
      modal: true:,
      buttons: [
        {
          text C.tag.config['submit'],
          click: function() {
            C.tag.dialog.dialog("close");
            C.tag.validate();
          },
        },
      ],
    dialogClass: 'c-helper-dialog',
    });
    $('.'+this.config['c-tag-radio-class']).change(function() {
    	C.tag.switch_display_mode($(this).val());
    });
	$('.'+this.config['c-tag-checkbox-class']).change(function() {
		if($(this).is(":checked")) {
			$('#C-tag-reason-'+$(this).attr('tag_id')).show();
			$('#C-tag-extra-'+$(this).attr('tag_id')).show();
		}
		else {
			$('#C-tag-reason-'+$(this).attr('tag_id')).hide();
			$('#C-tag-extra-'+$(this).attr('tag_id')).hide();
		}
	});
  },
  switch_display_mode: function(new_mode) {
  	if(this.current_display_mode != new_mode) {
  		if(new_mode == "categories") {
  			$("#C-tag-categories-container").show();
  			for(var i=0; i<this.templates.length; i++) {
				this.categories[this.templates[i].category].node.append(this.templates[i].node);
			}
			$("#C-tag-alphabetic-container").hide();
  		}
  		else {
			$("#C-tag-alphabetic-container").show();
  			for(var i=0; i<this.templates.length; i++) {
				this.alphabetic_container.append(this.templates[i].node);
			}
  			$("#C-tag-categories-container").hide();
  		}
  		this.current_display_mode = new_mode;
  	}
  },
  validate: function() {
  	var to_prepend = "";
  	var summary_array = [];
  	for(var i=0; i<this.templates.length; i++) {
  		if($('#C-tag-'+i).is(':checked')) {
  			var tmp = this.templates[i].template.replace(/\$\(day\)/g, "{{subst:CURRENTDAY}}")
  			                             .replace(/\$\(month\)/g, "{{subst:CURRENTMONTHNAME}}")
  			                             .replace(/\$\(year\)/g, "{{subst:CURRENTYEAR}}")
  			                             .replace(/\$\(page\)/g, mw.config.get('wgPageName'))
  			                             .replace(/\$\(diff\)/g, mw.config.get('wgRevisionId'))
  			                             .replace(/\$\(user\)/g, mw.config.get('wgUserName'));
  			if($('#C-tag-reason-input-'+i).length > 0) {
  				tmp = tmp.replace(/\$\(reason\)/g, $('#C-tag-reason-input-'+i).val());
  			}
  			if($('#C-tag-extra-input'+i).length > 0)
  				tmp = tmp.replace(/\$\(extra\)/g, $('#C-tag-extra-input'+i).val());
  				
  			to_prepend += tmp+'\n';
  			summary_array.push(this.templates[i].display);
  		}
  	}
  	if(summary_array.length > 0) {
  		var template_links = summary_array.map(function(item) {
  			return this.config['template-link-summary'].replace(/\$1/g, item);
  		}, this);
  		var summary = this.config['add-summary'].replace(/\$1/g, C.util.array_to_text(template_links));
  		C.util.prepend(null, to_prepend, summary, function() {
  			mw.notify(C.tag.config['notify-success'], {title:'C-helper', type:'info'});
	    	C.util.reload();
  		});
  	}
  },
};

C.modules.tag.callback = C.tag;
//</nowiki>