« MediaWiki:Gadget-citeBox.js » : différence entre les versions

La bibliothèque libre.
Contenu supprimé Contenu ajouté
+ paramètre "wikisource" pour {{Ouvrage}}
Change le modèle utilisé pour le Wiktionnaire
Ligne 187 : Ligne 187 :


ws.citeBox.add('Wiktionnaire', function(data) {
ws.citeBox.add('Wiktionnaire', function(data) {
content = '<pre>{{cit réf';
content = '<pre>{{Source';
content += '|' + data.title;
content += data.author;
content += '|' + data.year;
content += ", ''{{ws|" + mw.config.get('wgTitle') + '|' + data.title + "}}''";
content += '|' + data.author;
if(data.publisher)
content += '|lien=' + document.location.href;
content += ', ' + data.publisher;
if(data.year)
content += ', ' + data.year;
if(data.pages)
content += ', pp. ' + data.pages;
content += '}}</pre>';
content += '}}</pre>';
return content;
return content;

Version du 30 juin 2015 à 02:40

/*********************
Citation d'un livre
Auteur: Tpt
*********************/
if(typeof ws == 'undefined') {
    ws = {};
}

ws.citeBox = {
  defaultTemplate: 'Liste',
  templates: {},
  data: {},
  i18n: { 
    title: 'Quote this book',
    description: 'Quote this book.',
    display_mode: 'Display mode:'
  },

  add: function(name, content) {
    this.templates[name] = content;
  },

  init: function( i18np ) {
    if( $('div#citeBox').length > 0) {
        return;
    }
    $.extend(this.i18n, i18np);

    $.getScript('//wikisource.org/w/index.php?title=MediaWiki:Wikisource.data.js&action=raw&ctype=text/javascript', function() {
      mw.loader.using(['jquery.ui.dialog'], function() {
        var message = $('<div id="citeBox" title="' + ws.citeBox.i18n.title + '"></div>').appendTo("#content");
        message.dialog({
          autoOpen: false,
          modal: true,
          width: 500,
          buttons: {
            Ok: function() {
              $(this).dialog("close");
            }
          }
        });
        var portletLink = mw.util.addPortletLink('p-tb', '#', ws.citeBox.i18n.title, 't-cite', ws.citeBox.i18n.description);
        ws.citeBox.data = ws.data.getAll();
        $(portletLink).click(function() {
          ws.citeBox.open();
        });
      });
    });
  },

  getContent: function() {
    var content = '<p><label for="citeBox-select">' + this.i18n.display_mode + '</label> <select id="citeBox-select">';
    for(var id in this.templates) {
       content += '<option value="' + id + '">' + id + '</option>';
    }
    content += '</select></p><div id="citeBox-content">' + this.format(this.defaultTemplate) + '</div>';
    return content;
  },

  format: function(id) {
    var func = this.templates[id];
    return func(this.data);
  },

  open: function() {
    var citeBox = $('div#citeBox');
    if(citeBox.text() == '') {
      citeBox.html(ws.citeBox.getContent());
      $("select#citeBox-select").change(function () {
        var node = $("select#citeBox-select option:selected");
        if(node.size())
          $('#citeBox-content').html(ws.citeBox.format(node.text()));
      });
    }
    citeBox.dialog("open");
  }
};

/*
Configuration : utiliser la fonction ws.citeBox.add prenant en premier paramètre le nom du type de référence et sen second une fonction prenant en paramètre un tableau associatif des métadonnées et retournant la référence.
*/
if((mw.config.get('wgNamespaceNumber') == 0 || mw.config.get('wgNamespaceNumber') == 112) && (mw.config.get('wgAction') == 'view' || mw.config.get('wgAction') == 'purge')) {
  $(function() {
    ws.citeBox.add('Liste', function(data) {
      content = '<ul>';
      if(data.title)
        content += '<li>Titre : ' + data.title + '</li>';
      if(data.periodical)
        content += '<li>Périodique : ' + data.periodical + '</li>';
      if(data.volume)
        content += '<li>Volume : ' + data.volume + '</li>';
      if(data.author)
        content += '<li>Auteur(s) : ' + data.author + '</li>';
      if(data.translator)
        content += '<li>Traducteur(s) : ' + data.translator + '</li>';
      if(data.illustrator)
        content += '<li>Illustrateurs(s) : ' + data.illustrator + '</li>';
      if(data.publisher)
        content += '<li>Éditeur : ' + data.publisher + '</li>';
      if(data.school)
        content += '<li>École : ' + data.school + '</li>';
      if(data.year)
        content += '<li>Publication : ' + data.year + '</li>';
      if(data.place)
        content += '<li>Lieu d\'édition : ' + data.place + '</li>';
      if(data.chapter)
        content += '<li>Chapitre : ' + data.chapter + '</li>';
      if(data.pages) {
        if(data.pages.length > 4) {
          content += '<li>Pages : ' + data.pages + '</li>';
        } else {
          content += '<li>Page : ' + data.pages + '</li>';
        }
      }
      content += '</ul>';
      return content;
    });

    ws.citeBox.add('BibTeX', function(data) {
      content = '<pre style="display:block;">@Book{\n';
      if(data.type)
        content += '  type = "' + data.type + '",\n';
      if(data.title)
        content += '  title = "' + data.title + '",\n';
      if(data.volume)
        content += '  volume = "' + data.volume + '",\n';
      if(data.author)
        content += '  author = "' + data.author + '",\n';
      if(data.publisher)
        content += '  publisher = "' + data.publisher + '",\n';
      if(data.school)
        content += '  school = "' + data.school + '",\n';
      if(data.year)
        content += '  year = "' + data.year + '",\n';
      if(data.place)
        content += '  place = "' + data.place + '",\n';
      if(data.chapter)
        content += '  chapter = "' + data.chapter + '",\n';
      if(data.pages)
        content += '  pages = "' + data.pages + '",\n';
      content += '  url = "' + document.location.href + '",\n';
      content += '}</pre>';
      return content;
    });

    ws.citeBox.add('Wikipédia', function(data) {
      content = '<pre>{{Ouvrage\n';
      content += '|titre= ' + data.title + '\n';
      content += '|auteur= ' + data.author + '\n';
      if(data.translator)
        content += '|traducteur= ' + data.translator + '\n';
      if(data.illustrator)
        content += '|illustrateur= ' + data.illustrator + '\n';
      content += '|éditeur= ' + data.publisher + '\n';
      content += '|année= ' + data.year + '\n';
      if(data.place)
        content += '|lieu= ' + data.place + '\n';
      if(data.volume)
        content += '|tome= ' + data.volume + '\n';
      if(data.chapter)
        content += '|chapitre= ' + data.chapter + '\n';
      if(data.pages)
        content += '|passage= ' + data.pages + '\n';
      content += '|langue= fr\n';
      content += '|wikisource= ' + mw.config.get('wgTitle') + '\n';
      content += '}}</pre>';
      return content;
    });

    ws.citeBox.add('Wikiquote', function(data) {
      content = '<pre>{{Réf Livre\n';
      content += '|titre= ' + data.title + '\n';
      content += '|auteur= ' + data.author + '\n';
      content += '|éditeur= ' + data.publisher + '\n';
      content += '|année= ' + data.year + '\n';
      content += '|page= ' + data.pages + '\n';
      if(data.volume)
        content += '|tome= ' + data.volume + '\n';
      if(data.chapter)
        content += '|chapitre= ' + data.chapter + '\n';
      if(data.translator)
        content += '|traducteur= ' + data.translator + '\n';
      content += '|s= ' + mw.config.get('wgTitle') + '\n';
      content += '}}</pre>';
      return content;
    });

    ws.citeBox.add('Wiktionnaire', function(data) {
      content = '<pre>{{Source';
      content += data.author;
      content += ", ''{{ws|" + mw.config.get('wgTitle') + '|' + data.title + "}}''";
      if(data.publisher)
        content += ', ' + data.publisher;
      if(data.year)
        content += ', ' + data.year;
      if(data.pages)
        content += ', pp. ' + data.pages;
      content += '}}</pre>';
      return content;
    });

    var i18n = {
      title: 'Citer ce texte',
      description: 'Citer ce texte en différents formats.',
      display_mode: 'Mode d\'affichage :'
    };
    ws.citeBox.init( i18n );
  });
}