MediaWiki:Interwiki-links.js

   

Fra Villmark

Gå til: navigasjon, søk

Merk: Etter lagring vil det kanskje være nødvendig at nettleseren sletter mellomlageret sitt for at endringene skal tre i kraft. Mozilla og Firefox: trykk Ctrl-Shift-R, Internet Explorer: Ctrl-F5, Safari: Cmd-Shift-R i engelskspråklig versjon, Cmd-Alt-E i norskspråklig versjon, Konqueror og Opera: F5.

/** Sort iw-links according to a preference table ****************************************************** 
 *
 *  Description: [[:no:Wikipedia:Sortering av interwiki-lenker]]
 *  Maintainers: [[:no:User:Jeblad]]
 *  Note: As a default for unregistered users, use someting like
 *    var wgDefaultLangPrefs = ["nn", "da", "sv", "en", "de", "fr", "se"];
 */

var wgDebugIWlang;
var wgUseUserlanguage;
var wgInterwikiDone = false;
var wgDefaultLangPrefs = [];

// make sure we in fact has a array
var wgLangPrefs;
if (typeof wgLangPrefs == 'undefined') wgLangPrefs = Array();
wgLangPrefs.push(wgUserLanguage);

// choose and adjust lists for prefered languages
if (wgUserName) {
    wgLangPrefs = wgLangPrefs;
}
else {
    wgLangPrefs = wgDefaultLangPrefs;
}
var wgLangIWlinks = Object();
for (i=0;i<wgLangPrefs.length;i++) {
    wgLangPrefs[wgLangPrefs[i]] = 1;
    wgLangIWlinks["interwiki-" + wgLangPrefs[i]] = 1;
}

// get data structures for nameing and sorting of languages
// this will block any languages that isn't defined
var wgLanguages = { 'nn' : true, 'no' : true };
if ( wgUserName == null ? wgUserLanguage != wgContentLanguage : true ) {
    if ( wgLanguages[ wgUserLanguage ] )
        importScript('MediaWiki:User-lang-' + wgUserLanguage + '.js');
}
var wgLangNames;
if ( typeof wgLangNames != 'object' ) wgLangNames = Object();

// get data structure for babel services
var wgBabelTrans;
var wgUseTranslations = true;
if ( wgUserName == null ? wgUserLanguage != wgContentLanguage : true ) {
    if ( wgUseTranslations )
        importScript('MediaWiki:Babel-translations.js');
}
var wgBabelTrans;
if ( typeof wgBabelTrans != 'object' ) wgBabelTrans = Object();

var wgTransFunctions = Object();
// the babel service specific metods
wgTransFunctions[ 'bac' ] = function (c, url, from, to, rec ) {
    url = url.replace(/\//g, '%2f');
    url = url.replace(/:/g, '%3a');
    var innerHTML = '<a href="http://babelfish.altavista.com/babelfish/trurl_pagecontent?lp='
              + from + '_' + to + '&trurl=' + url + '" title="' + rec[ 2 ] + '">'
              + (( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage) ? wgLangNames[ wgUserLanguage ][ to ][ 0 ] : rec[ 1 ])
              + ' (Altavista)</a>';
    if (innerHTML.length) {
        innerHTML = '<br />' + innerHTML;
        var span = document.createElement("span");
        span.className = "iw-babel";
        span.innerHTML = innerHTML;
        c.appendChild(span);
    }
}
// the babel service specific metods
wgTransFunctions[ 'tgc' ] = function (c, url, from, to, rec ) {
    url = url.replace(/\//g, '%2f');
    url = url.replace(/:/g, '%3a');
    var innerHTML = '<a href="http://translate.google.com/translate?langpair=' 
              + (rec[ 3 ] ? rec[ 3 ] : from) + '%7C' + (rec[ 4 ] ? rec[ 4 ] : to)
              + '&u=' + url + '&hl=' + wgUserLanguage + '" title="' + rec[ 2 ] + '">'
              + (( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage) ? wgLangNames[ wgUserLanguage ][ to ][ 0 ] : rec[ 1 ])
              + ' (Google)</a>';
    if (innerHTML.length) {
        innerHTML = '<br />' + innerHTML;
        var span = document.createElement("span");
        span.className = "iw-babel";
        span.innerHTML = innerHTML;
        c.appendChild(span);
    }
}
// the babel service specific metods
wgTransFunctions[ 'wgc' ] = function (c, url, from, to, rec ) {
    url = url.replace(/\//g, '%2f');
    url = url.replace(/:/g, '%3a');
    var innerHTML = '<a href="http://www.gramtrans.com/gt/url/?pair=' 
              + (rec[ 3 ] ? rec[ 3 ] : from) + '2' + (rec[ 4 ] ? rec[ 4 ] : to)
              + '&url=' + url + '&x-form-id=translate_url" title="' + rec[ 2 ] + '">'
              + (( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage) ? wgLangNames[ wgUserLanguage ][ to ][ 0 ] : rec[ 1 ])
              + ' (Gramtrans)</a>';
    if (innerHTML.length) {
        innerHTML = '<br />' + innerHTML;
        var span = document.createElement("span");
        span.className = "iw-babel";
        span.innerHTML = innerHTML;
        c.appendChild(span);
    }
}

function sortIWlinks(a,b)
{
    return wgLangNames[ wgUserLanguage ][ a.className ][ 1 ] - wgLangNames[ wgUserLanguage ][ b.className ][ 1 ];
}

addOnloadHook( function () {
    //try {
    if (wgInterwikiDone) return;
    wgInterwikiDone = true;
    if ( typeof wgDebugIWlang != 'undefined' && wgDebugIWlang ) return; // bail out for debugging

    // reorganize wgLangNames so we can use it for sorting
    var j = 0;
    var ta = Array();
    for (x in wgLangNames[ wgUserLanguage ]) {
        ta.push(x);
    }
    for (i=0;i<ta.length;i++) {
        wgLangNames[ wgUserLanguage ][ 'interwiki-' + ta[i] ] = [ wgLangNames[ wgUserLanguage ][ ta[i] ], j ];
        wgLangNames[ wgUserLanguage ][ ta[i] ] = [ wgLangNames[ wgUserLanguage ][ ta[i] ], j ];
        j++
    }

    // if we don't have anything from wgLangNames we give up
    if (!j) return;

    // locate iw-links, and the container
    var container = document.getElementById("p-lang");
    if (!container) return;
    var ul = container.getElementsByTagName("ul");
    if (!ul) return;
    if (ul.length != 1) return;
    ul = ul[0];
    var c = container.getElementsByTagName("li");
    if (!c) return;

    // loop over the iw-links, translate names and
    // separate those we know how to sort from the rest
    // and add babel links
    var childs = Array();
    var keeps = Array();
    var adds = Array();
    for (i=0;i<c.length;i++) {
        if (wgLangNames) {
            var s = c[ i ].className;
            if (0>s.indexOf('interwiki-')) continue;
            s = s.replace(/^.*?interwiki-/, '');
            s = s.replace(/\s.*$/, '');
            var a = c[ i ].getElementsByTagName("a");
            // add babel links
            try {
                if ( typeof wgUseBabelLinks != 'undefined' && wgUseBabelLinks
	              &&typeof wgLangIWlinks[ c[i].className ] == 'undefined' ) {
                    if ( typeof wgBabelTrans[ s ] != 'undefined') {
                        for ( x in wgBabelTrans[ s ] ) {
                            if ( typeof wgLangPrefs[ x ] != 'undefined' && typeof wgBabelTrans[ s ][ x ] != 'undefined' ) {
                                for (k=0;k<wgBabelTrans[ s ][ x ].length;k++) {
                                    if ( wgTransFunctions[ wgBabelTrans[ s ][ x ][ k ][ 0 ] ] != 'undefined') {
                                        wgTransFunctions[ wgBabelTrans[ s ][ x ][ k ][ 0 ] ]( c[ i ],
                                            a[0].getAttribute("href"), s, x, wgBabelTrans[ s ][ x ][ k ]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (e) { /* just skip this */ }
            // add localized names
            try {
                if ( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage
                     && typeof wgLangNames[ wgUserLanguage ] != 'undefined' ) {
                    if ( typeof wgLangNames[ wgUserLanguage ][ s ] != 'undefined'
                         && typeof wgLangNames[ wgUserLanguage ][ s ][ 0 ] == 'string' ) {
                        c[ i ].setAttribute( 'title', wgLangNames[ wgUserLanguage ][ s ][ 0 ] );
                        if (a.length) a[0].innerHTML = wgLangNames[ wgUserLanguage ][ s ][ 0 ];
                    }
                }
            }
            catch (e) { /* just skip this */ }
            // check if we have any odd link}
            try {
                if ( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage
                     && typeof wgLangNames[ wgUserLanguage ][ s ] != 'undefined'
                     && typeof wgLangNames[ wgUserLanguage ][ s ][ 1 ] == 'number' ) {
                    childs.push(c[ i ]);
                    //wgLangNames[ c[ i ].className ] = wgLangNames[ wgUserLanguage ][ s ];
                }
                else {
	              adds.push(c[ i ]);
                }
            }
            catch (e) { /* just skip this */ }
        }
        else {
	      childs.push(c[ i ]);
        }
    }

    // sort the childs according to definitions used by sortIWlinks
    if ( typeof wgUseUserLanguage != 'undefined' && wgUseUserLanguage
         && typeof wgLangNames[ wgUserLanguage ] != 'undefined' ) {
        childs = childs.sort(sortIWlinks);
    }

    // reinsert those we didn't know how to sort into the list of children
    for (i=0;i<adds.length;i++) {
	      childs.push(adds[ i ]);
    }
    
    // move one block to the top
    try {
        // separate out those that shall be moved to the top
        for (i=0;i<childs.length;i++) {
            // the following test fails if there are several classes for the actual child
            if ( typeof wgLangIWlinks[ childs[i].className ] != 'undefined' && wgLangIWlinks[ childs[i].className ]) {
                keeps.push(childs[i]);
            }
        }

        // reorganize the list of children
        var n = childs.length;
        for (i=childs.length-1;i>=0;i--) {
            if ( typeof wgLangIWlinks[ childs[i].className ] != 'undefined' && wgLangIWlinks[ childs[i].className ] ) {
                childs[i].className += ' iw-focus';
            }
            else {
                keeps[--n] = childs[i];
            }
        }
    }
    catch (e) { /* just skip this */ }

    // remove all existing children and reinsert from our own list
    if (keeps.length) {
        var child;
        while (child = ul.firstChild) {
            ul.removeChild(child);
        }
        for (i=0;i<keeps.length;i++) {
            ul.appendChild(keeps[i]);
        }
    }
    // }
    // catch (e) { /* just skip this */ }
});
Personlige verktøy