Vés al contingut

MediaWiki:Gadget-DocTabs.js: diferència entre les revisions

De Viccionari
Contingut suprimit Contingut afegit
canvi de funcions obsoletes (mw:ResourceLoader/JavaScript Deprecations)
deprecated: wgPageName, wgScriptPath, wgArticlePath, wgCanonicalNamespace
Línia 34: Línia 34:
function add_namespace_tab(page, id) {
function add_namespace_tab(page, id) {
var insert = get_insert_position(id);
var insert = get_insert_position(id);
addPortletLink(insert[0], wgArticlePath.replace("$1", page), texts[id][0], id, texts[id][1], texts[id][2], insert[1]);
addPortletLink(insert[0], wgArticlePath.replace("$1", page), texts[id][0], id, texts[id][1], texts[id][2], insert[1]);
if (page == wgPageName) { // Can by-pass lookup
if (page == wgPageName) { // Can by-pass lookup
var tab = document.getElementById(id);
var tab = document.getElementById(id);
tab.className = 'selected';
tab.className = 'selected';
Línia 50: Línia 50:
ct.parentNode.removeChild(ct);
ct.parentNode.removeChild(ct);
// Add new tab.
// Add new tab.
add_namespace_tab(wgPageName, new_id);
add_namespace_tab(wgPageName, new_id);


// Change destination of talk tab.
// Change destination of talk tab.
var dt = document.getElementById('ca-talk');
var dt = document.getElementById('ca-talk');
var a = dt.getElementsByTagName('a')[0]
var a = dt.getElementsByTagName('a')[0]
a.setAttribute('href', wgArticlePath.replace("$1", talk_page));
a.setAttribute('href', wgArticlePath.replace("$1", talk_page));
lookup[talk_page] = dt;
lookup[talk_page] = dt;
if(dt.className) dt.className = dt.className.replace('new','');
if(dt.className) dt.className = dt.className.replace('new','');
Línia 72: Línia 72:
}
}
if( wgCanonicalNamespace == 'Template' && /\/ús$/.test(wgTitle) ){
if( wgCanonicalNamespace == 'Template' && /\/ús$/.test(wgTitle) ){
var baseTitle = wgTitle.replace(/\/ús$/, "");
var baseTitle = wgTitle.replace(/\/ús$/, "");
change_main_tabs('ca-nstab-template', 'ca-nstab-documentation', 'Plantilla Discussió:' + baseTitle);
change_main_tabs('ca-nstab-template', 'ca-nstab-documentation', 'Plantilla Discussió:' + baseTitle);
add_namespace_tab('Plantilla:' + baseTitle, 'ca-nstab-template');
add_namespace_tab('Plantilla:' + baseTitle, 'ca-nstab-template');


}else if( wgCanonicalNamespace == 'Template' || wgCanonicalNamespace == 'Template_talk' ){
}else if( wgCanonicalNamespace == 'Template' || wgCanonicalNamespace == 'Template_talk' ){
add_namespace_tab('Plantilla:'+wgTitle+'/ús', 'ca-nstab-documentation');
add_namespace_tab('Plantilla:'+wgTitle+'/ús', 'ca-nstab-documentation');


Línia 89: Línia 89:
if(! ajaxer) return false;
if(! ajaxer) return false;
var url = wgScriptPath+ '/api.php?format=json&action=query&prop=info&titles=';
var url = wgScriptPath+ '/api.php?format=json&action=query&prop=info&titles=';
var spl = '';
var spl = '';
for(var page in lookup){
for(var page in lookup){

Revisió del 01:06, 30 jul 2015

// Adaptat de http://en.wiktionary.org/w/index.php?title=MediaWiki:Common.js&oldid=10888691
//    wikt:en:Mediawiki:common.js  #Make Citations: tabs
// Optimitzat per User:Coet
//
// Afegix una pestanya "Documentació" en l'espai de noms Plantilla, enllaçant a la subpàgina /ús


function documentation_tab(){

  var texts = {
      // tab id : [ label, hover-text, access-key (c if tab comes first, 3 if last) ]
      'ca-nstab-template': ['Codi', 'Vegeu el codi de la plantilla', 'c'],
      'ca-nstab-documentation': ['Documentació', 'Vegeu la documentació', '3']
  }

  var lookup = new Object(); // {page-title: tab-node} these are looked up and changed to red if missing.

  // Returns [id-of-portlet, node-to-insert-before or null] for inserting the tab.
  function get_insert_position(id) {
    var portlet = document.getElementById('p-namespaces') ? 'p-namespaces' : 'p-cactions';
    var insbef = null;

    if (texts[id][2] == 'c') { // otherwise '3'
       insbef = document.getElementById(portlet).getElementsByTagName('ul')[0].firstChild;
    } else if (portlet == 'p-cactions' ) { // 'last' in Monobook means "before Edit"
      var insbef = document.getElementById('ca-edit');
      if(! insbef) insbef = document.getElementById('ca-viewsource');
    }

    return [portlet, insbef];
  }

  // Add a new namespace with addPortletLink using the lookup table above.
  function add_namespace_tab(page, id) {
    var insert = get_insert_position(id);
    addPortletLink(insert[0], mw.config.get("wgArticlePath").replace("$1", page), texts[id][0], id, texts[id][1], texts[id][2], insert[1]); 
    if (page == mw.config.get("wgPageName")) { // Can by-pass lookup
        var tab = document.getElementById(id);
        tab.className = 'selected';
        if (wgArticleId == 0) make_tab_red(tab);
    } else { // lookup the redness with the ajax below
        lookup[page]=document.getElementById(id);
    }
  }

  // Replace the two current tabs with new ones, used when in the third namespace.
  function change_main_tabs(old_id, new_id, talk_page) {
    // Remove old tab.
    var ct = document.getElementById(old_id);
    ct.parentNode.removeChild(ct);
    // Add new tab.
    add_namespace_tab(mw.config.get("wgPageName"), new_id);

    // Change destination of talk tab.
    var dt = document.getElementById('ca-talk');
    var a = dt.getElementsByTagName('a')[0]
    a.setAttribute('href', mw.config.get("wgArticlePath").replace("$1", talk_page));
    lookup[talk_page] = dt;
    if(dt.className) dt.className = dt.className.replace('new','');
  }

  // Change a blue-link to a red-link
  function make_tab_red(tab){
    tab.className = tab.className+' new';
    var a = tab.getElementsByTagName('a')[0];
    var href = a.getAttribute('href');
    if( tab.getAttribute('id') == 'ca-nstab-documentation' ) {
      a.setAttribute('href',href+(href.indexOf('?')>0?'&':'?')+'action=edit&preload=Plantilla%3AÚs+de+la+plantilla%2Fprecàrrega');
    }else {
      a.setAttribute('href',href+(href.indexOf('?')>0?'&':'?')+'action=edit&redlink=1');
    }
  }
  
  if( mw.config.get("wgCanonicalNamespace") == 'Template' && /\/ús$/.test(wgTitle) ){
    var baseTitle = wgTitle.replace(/\/ús$/, "");
    change_main_tabs('ca-nstab-template', 'ca-nstab-documentation', 'Plantilla Discussió:' + baseTitle);
    add_namespace_tab('Plantilla:' + baseTitle, 'ca-nstab-template');

  }else if( mw.config.get("wgCanonicalNamespace") == 'Template' || mw.config.get("wgCanonicalNamespace") == 'Template_talk' ){
    add_namespace_tab('Plantilla:'+wgTitle+'/ús', 'ca-nstab-documentation');

  }else{ //Nothing to see here...
    return false;
  
  }
  
  //Now check for red pages
  var ajaxer = sajax_init_object();
  if(! ajaxer) return false;
  
  var url = mw.config.get("wgScriptPath") + '/api.php?format=json&action=query&prop=info&titles=';
  var spl = '';
  for(var page in lookup){
    url+=spl+encodeURIComponent(page);
    spl='|';
  }
  
  ajaxer.onreadystatechange = function(){
    if( ajaxer.readyState == 4 ){
      if( ajaxer.status == 200 ){
        var resps = eval('(' + ajaxer.responseText + ')');
        for (var x in resps.query.pages) {
          if (Number(x) < 0) {
            make_tab_red(lookup[resps.query.pages[x].title]);
          }
        }
      }
    }
  }

  ajaxer.open("GET", url);
  ajaxer.send('');

}

$(document).ready( documentation_tab );