Utilisateur:François Melchior/MediaWiki:Gadget-AdvancedTitle.js

Un livre de Wikilivres.

Attention : Depuis MediaWiki 1.18 les pages se terminant avec l'extension .js ou .css sont interprétées comme des pages wiki ! En particulier les modèles (subst ou non) et les liens. Vous devez donc migrer le code source et effectuer vos changements en évitant ces éléments de syntaxe wiki (peu importe leurs emplacements dans le code source : commentaire, chaine) :

  • Double accolades ouvrantes (en particulier avec subst:) : séparer les deux accolades "{"+"{" du reste de la chaine
  • Double crochets ouvrants : même technique de séparation.
  • Signature (tildes ~ multiples) : même technique de séparation.

Note : après avoir enregistré vos préférences, vous devrez attendre que le serveur mette à jour la feuille de style globale avant de forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
  • Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
  • Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
  • Konqueror : Cliquez sur Actualiser ou pressez F5 ;
  • Opera : Videz le cache dans Outils → Préférences.
/*

 Code for AdvancedTitle gadget. Inspirated (and similar, but with added functionalities)
  by TitreHierarchique and TitreDeluxe, written by DavidL and Tavernier (wikibooks:fr).

 License: GNU LGPL or Apache 2.0

 Author(s): François Melchior

 Configuration variables:
  * advtitleNamespaces = array of integers
       list of namespace indexes where the script will modify subpage titles (default: only mainspace).
  * advtitleSlashInName = regexp
       matches (sub-)pagenames that may contain a slash (default: none).
  * advtitleSeparator = string
       used as separator between the parts of parent's title (default: " / ").
  * advtitleAllowReplace = boolean
       if true, will use the content of element #customPageTitle (if present) as (sub-)page name. (default: false)
*/

$(document).ready( function ()
  {
  var firstHeadingElement = $('#firstHeading')
  if(!firstHeadingElement.length)
    {
    firstHeadingElement = $('h1:first') //For skins not defining #firstHeading.
    if(!firstHeadingElement.length) return
    }
  
  //This must be changed if changed in MediaWiki
  var dirAttr = firstHeadingElement[0].firstChild.dir //Easier without jQuery.
  if(dirAttr) dirAttr = ' dir="' + dirAttr + '"'
  
  var titleInThePageElement = $('#titleInThePage')
  if(titleInThePageElement.length)
    {
    firstHeadingElement.css('display', 'none') //not .hide() - no need to save old value.
    firstHeadingElement[0].id = 'advtitleOldFirstHeading' //Easier without jQuery.
    titleInThePageElement[0].id = 'firstHeading'
    titleInThePageElement.addClass('firstHeading')
    }
  else titleInThePageElement = null
  
  var customPageSubtitleElement = $('#customPageSubtitle'), customPageSubtitle
  if(customPageSubtitleElement.length)
    {
    customPageSubtitle = mw.html.escape(customPageSubtitleElement[0].title)
                         || $.trim(customPageSubtitleElement.html())
              //Easier to get title attr without jQuery.
    customPageSubtitleElement.css('display', 'none') //not .hide() - no need to save old value.
    }
  customPageSubtitleElement = null //Not needed anymore.
  
  var customPageTitle = ''
  if(window.advtitleAllowReplace)
    {
    var customPageTitleElement = $('#customPageTitle')
    if(customPageTitleElement.length)
      {
      customPageTitle = mw.html.escape(customPageTitleElement[0].title)
                        || $.trim(customPageTitleElement.html())
      customPageTitleElement.css('display', 'none')
      }
    customPageTitleElement = null //Not needed anymore.
    }

  do
    {
    if(mw.config.get('wgAction') != 'view') break
    if($.inArray(mw.config.get('wgNamespaceNumber'), window.advtitleNamespaces || [ 0 ]) < 0) break
    
    var subpagesElements = mw.util.$content.find('.subpages')
    
    if(!subpagesElements.length) break //Not in a subpage, or in a namespace that
                                       // does not recognize subpages.
    
    if(window.advtitleSeparator == undefined) advtitleSeparator = ' / '
    
    var fullPageTitle = $.trim(firstHeadingElement.text())
    var chunkedPageTitle = fullPageTitle.split('/')
    
    //Restore pagenames that contain a slash
    if(window.advtitleSlashInName != undefined)
      for(var i = chunkedPageTitle.length - 1, previousChunk = chunkedPageTitle[i]; i--; )
        {
        var reglued = chunkedPageTitle[i] + '/' + previousChunk
        previousChunk = chunkedPageTitle[i]
        if(advtitleSlashInName.test(reglued))
          chunkedPageTitle.splice(i, 2, chunkedPageTitle[i] + '/' + chunkedPageTitle[i+1])
                    //Does not use 'reglued', in case 2 (or more) slashs in name
        }
    
    if(chunkedPageTitle.length < 2) //Not a subpage anymore!
      {
      subpagesElements.css('display', 'none')
      break
      }
    
    if(customPageTitle) chunkedPageTitle[chunkedPageTitle.length - 1] = customPageTitle
    
    newTitleHtml = '<span class="advtitleLastSeparator">' + advtitleSeparator +
                   '</span></div><div class="advtitlePage"' + dirAttr + '>' + chunkedPageTitle.pop() +
                   '</div>'
    while(chunkedPageTitle.length)
      {
      var part = '<a href="' + mw.util.getUrl(chunkedPageTitle.join('/')) + '">'
      part += chunkedPageTitle.pop() + '</a>'
      newTitleHtml = (chunkedPageTitle.length ? '<span>' + advtitleSeparator + '</span>' : '') +
                     part + newTitleHtml
      }
    newTitleHtml = '<div class="advtitleParents"' + dirAttr + '>' + newTitleHtml +
                   (customPageSubtitle ? '<div class="advtitleSubtitle"' + dirAttr + '>' +
                                         customPageSubtitle + '</div>' : '')
    
    if(titleInThePageElement)
      {
      firstHeadingElement = titleInThePageElement
      subpagesElements.addClass('advtitleSmallTitle')
      }
    
    //Finaly modify the DOM.
    subpagesElements.html(fullPageTitle)
    firstHeadingElement.html(newTitleHtml)
    
    return
    } while(false)
  
  // If we are here, then we are not in a subpage.
  
  if(titleInThePageElement)
    {
    // Move title at the new place.
    titleInThePageElement.append(customPageTitle || firstHeadingElement.contents())
    
    // Add a small title around the old one.
    var smallTitle = '<span class="advtitleSmallTitle">' +
                     $.trim(titleInThePageElement.text()) + '</span>'
    var contentSubElement = $('#contentSub')
    if(contentSubElement.length)
      contentSubElement.prepend(smallTitle)
    else firstHeadingElement.after('<div id="contentSub">' + smallTitle + '</div>')
    
    // Let following step not fail
    firstHeadingElement = titleInThePageElement
    }
  else if(customPageTitle) firstHeadingElement.html(customPageTitle)
  
  if(customPageSubtitle)
    firstHeadingElement.append('<div class="advtitleSubtitle"' + dirAttr + '>' +
                               customPageSubtitle + '</div>')
  
  });