مدیاویکی:Gadget-organize-language-links.js

از مشروطه
پرش به ناوبری پرش به جستجو

نکته: پس از ذخیره کردن ممکن است برای دیدن تغییرات نیاز باشد که حافظهٔ نهانی مرورگر خود را پاک کنید.

  • فایرفاکس / سافاری: کلید Shift را نگه دارید و روی دکمهٔ Reload کلیک کنید، یا کلید‌های Ctrl-F5 یا Ctrl-R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-R)
  • گوگل کروم: کلیدهای Ctrl+Shift+R را با هم فشار دهید (در رایانه‌های اپل مکینتاش کلید‌های ⌘-Shift-R)
  • اینترنت اکسپلورر: کلید Ctrl را نگه‌دارید و روی دکمهٔ Refresh کلیک کنید، یا کلید‌های Ctrl-F5 را با هم فشار دهید
  • اپرا: بروید به Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
/**
 * Description: Adapt the focused language links to those the user in fact uses
 * Maintainers: [[User:Jeblad]]
 */
(function(mw, $, undefined){
    // only web browsers supporting local storage
    if (!window.localStorage) return;
    
    // override our default portlet language selector
    var skins = {
         //'modern' : '#p-lang'
    };
    
    // this is the number of entries we tries to approximate
    var num = 3;
    
    // this is the fraction of the overall calls to increase the focused items
    var frac = 0.05;
    
    // note that this is also positional dependent
    var defaults = {
        // we kickstart at this number of overall clicks
        'all' : 21,
        'langs' : {
            // these are focused and very sticky
            'en' : 4, 'fr' : 4, 'de' : 4, 'it' : 4,
            // these are also focused but less sticky
            'ar' : 2, 'tr' : 2, 'ja' : 2,
            // these easilly gets focus
            'nl' : 1, 'pl' : 1, 'ru' : 1, 'fi' : 1
        }
    }
 
    // get the locally stored objects, clear out the objects on errors
    var max = 32;   // limit the number of entries, this will eventually clear obsolete entries
    var language = {}; // holds the number of accesses
    try {
        // we try to convert what we have in local storage
        language = $.parseJSON( window.localStorage.getItem("sidebar-languages")) || defaults;
    }
    catch (e) {
        // bummer, use defaults
        language = defaults;
    }
    
    // locate the language links container
    var ul = $(skins[skin] ? skins[skin] : '#p-lang').find('ul').first();
    
    // worker to be run after load
    $(function(){
        // skin name is used to set default selector
        var skin = mw.user.options.get( 'skin' );
        // should we sort the list
        if (!/no-sort/.test(mw.config.get('wgLanglinks'))) {
            // figure out what to sort
            var items = {};
            ul.find('li').each(function(i, el){
                if ( el.className ) {
                    var matches = el.className.match(/interwiki-([-\w]*)/);
                    if (matches && matches.length == 2) {
                        items[matches[1]] = this;
                    }
                }
            });
            // we need a list of prefixes (yeah, there is better ways to do this in new browsers)
            var prefixes = [];
            for (var x in items) prefixes.push(x);
            // sort the prefixes and move the items up front
            for (var y in prefixes.sort().reverse()) {
                var x = prefixes[y];
                ul.prepend(items[x]);
            }
        }
        // should we reorder the list
        if (!/no-reorder/.test(mw.config.get('wgLanglinks'))) {
            // we need a keys list (yeah, there is better ways to do this in new browsers)
            var keys = [];
            for (var x in language.langs) keys.push(x);
            // sort out the candidates
            var candidates = [];
            for (var y in keys.sort(function(a,b){return language.langs[a] <= language.langs[b]})) {
                var x = keys[y];
                if (language.langs[x] && (frac*language.all<language.langs[x] || 0<num)) {
                    num--;
                    candidates.push(x);
                }
            }
            // build a selector for each of the candidates
            var selectors = [];
            for (var x in candidates) {
                selectors.push('.interwiki-' + candidates[x]);
            }
            // move the found lang links
            var items = ul.find(selectors.join(','));
            items.detach();
            items.addClass('iw-focus');
            ul.prepend(items);
        }
        // add an additional handler on all langlinks
        ul.find('li a').click(function(){
            // should we analyze the click behaviour
            if (!/no-analyze/.test(mw.config.get('wgLanglinks'))) {
                try {
                    var cname = $(this).parent().attr('class');
                    if (cname) {
                        // rebuild for local storage
                        var a = [];
                        var matches = cname.match(/interwiki-([-\w]*)/);
                        a.push('"'+matches[1]+'":'+(language.langs[matches[1]] ? language.langs[matches[1]]+1 : 1));
                        for (var x in language.langs) {
                            if (x !== matches[1]) {
                                a.push('"'+x+'":'+language.langs[x]);
                            }
                        }
                        // note that this also resets the structure if it previously croacked
                        window.localStorage.setItem("sidebar-languages", '{"all":'+(language.all+1)+',"langs":{'+a.join(',')+'}}' );
                    }
                }
                catch (e) {
                    // die silently
                }
            }
            // continue on the link, we're done
            return true;
        });
    });
})(mediaWiki, jQuery);