MediaWiki:Common.js: Różnice pomiędzy wersjami

Z Nonsensopedii, polskiej encyklopedii humoru
(świeży skrypt galeriowy z Wikipedii)
Linia 236: Linia 236:
}
}
function ImageGroup() {
function ImageGroup() {
if (mw.config.values.skin === "minerva") return; //nie powinno się ładować na mobilce
jQuery('div.ImageGroup').each(function(i, group) {
jQuery('div.ImageGroup').each(function(i, group) {
var unitnode = jQuery('div.ImageGroupUnits', group).get(0);
var unitnode = jQuery('div.ImageGroupUnits', group).get(0);

Wersja z 21:20, 18 lip 2019

/* <pre><nowiki> */
/** COMMON.JS **
 * Plik zawiera funkcje używane w innych skryptach. Edytuj ostrożnie!
 **/

/** PODSTAWOWE FUNKCJE **/
// FIXME: Zastąpić to poprzez użycie jQuery: $("#id")
function returnObjById(a) {
    var b = null;
    document.getElementById ? b = document.getElementById(a) : document.all ? b = document.all[a] : document.layers && (b = document.layers[a]);
    return b;
}
// FIXME: Zastąpić to poprzez użycie jQuery: $(b).hasClass(c)
function hasClass(e, c) {
    return $(e).hasClass(c);
}

// Odpowiednik PLURAL w JS-ie
function war(l, j, k, w) {
    return (l == 1) ? j : (l % 100 >= 10 && l % 100 <= 20) ? w : (l % 10 > 1 && l % 10 < 5) ? k : w;
}
// Odpowiednik GENDER w JS-ie
function gnd(g, m, z, n) {
    var X = {
        "male": m,
        "female": z,
        "unknown": n
    };
    return (g) ? X[g] : n;
}
// FIXME: Zastąpić to poprzez użycie jQuery Timeago: $.timeago.inWords(l)
function dTemu(l) {
    var N = Math.floor(l / 1000),
        D = Math.floor(N / 86400),
        H = Math.floor((N - D * 86400) / 3600),
        I = Math.floor((N - D * 86400 - H * 3600) / 60);
    return {
        d: D,
        h: H,
        i: I,
        text: (D ? D + " d" + war(D, "zień", "ni", "ni") + " " : "") + (H ? H + " godz. " : "") + (I ? I + " min" + (!H ? "ut" + war(I, "ę", "y", "") : "") + " temu" : "przed chwilą")
    };
}

/* Archiwizacja Porum */
function zipForum() {
    if (!("Forum" != mw.config.get('wgCanonicalNamespace') || "view" != wgAction || "Strona główna" == wgTitle))
        if (!document.getElementById("naglowekforum") || document.getElementById("nieodkopuj")) {
            var cae = $("#ca-edit a")[0];
            cae.style.color = "DarkKhaki";
            cae.href = "";
            cae.title = "Ten wątek jest archiwalny, prosimy o nieedytowanie go.";
        }
}

/* Import CSS */
function Zab() {
		if (mw.config.get('wgRestrictionEdit')[0] === undefined && mw.config.get('wgRestrictionMove')[0] === undefined) return;
		var zabezpieczenie = '';
		var editRestriction = 'brak';
		var moveRestriction = "brak";
		if (mw.config.get('wgRestrictionEdit')[0] !== undefined) editRestriction = mw.config.get('wgRestrictionEdit')[0];
		if (mw.config.get('wgRestrictionMove')[0] !== undefined) moveRestriction = mw.config.get('wgRestrictionMove')[0];
		var klucz = {autoconfirmed: "półzabezpieczona", sysop: "zabezpieczona"};
		if (editRestriction == moveRestriction) {
			zabezpieczenie = klucz[editRestriction];
		} else {
			if (editRestriction != 'brak') {
				zabezpieczenie += klucz[editRestriction] + " przed edycją";
				if (moveRestriction != "brak") zabezpieczenie += ", ";
			}
			if (moveRestriction != 'brak') {
				zabezpieczenie += klucz[moveRestriction] + " przed przeniesieniem"
			}
		}
	
	    $("<span class='restr'>Strona " + zabezpieczenie + ".</span>").css({
	        float: "right"
	    }).appendTo("#contentSub");
}
if (mw.config.get('wgRestrictionEdit') !== null && mw.config.get('wgRestrictionMove') !== null) jQuery(document).ready(Zab);

/*** Skrypty dotyczące Gry ***/

/** Collapsible tables **
 *  Description: Allows tables to be collapsed, showing only the header. See
 *  [[en:Wikipedia:NavFrame]].
 *  Maintainers: [[User:R. Koot]]
 *  Zmodyfikowane przez [[User:Polskacafe]]
 */

/*
	Wartości collapseCaption i expandCaption można zmieniać z poziomu tabeli z użyciem data-expand-text, data-collapse-text i data-width (w przypadku gdy tekst jest za długi).
*/
var autoCollapse = 2;
var collapseCaption = "Ukryj";
var expandCaption = "Pokaż";

function collapseTable(tableIndex) {
    var i;
    var Button = document.getElementById("collapseButton" + tableIndex);
    var Table = document.getElementById("collapsibleTable" + tableIndex);

    if (!Table || !Button) { return false; }
    var Rows = Table.rows;

    if (Button.firstChild.data == collapseCaption || Button.firstChild.data == Table.dataset.collapseText) {
        for (i = 1; i < Rows.length; i++) {
            Rows[i].style.display = "none";
        }
        if (!Table.dataset.expandText) {
        	Button.firstChild.data = expandCaption;
        } else { 
        	Button.firstChild.data = Table.dataset.expandText;
        }
    } else {
        for (i = 1; i < Rows.length; i++) {
            Rows[i].style.display = Rows[0].style.display;
        }
        if (!Table.dataset.collapseText) {
        	Button.firstChild.data = collapseCaption;
        } else { 
        	Button.firstChild.data = Table.dataset.collapseText;
        }
    }
}

function createCollapseButtons() {
    var i;
    var tableIndex = 0;
    var NavigationBoxes = {};
    var Tables = document.getElementsByTagName("table");

    for (i = 0; i < Tables.length; i++) {
        if (hasClass(Tables[i], "collapsible")) {

            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName("tr")[0];
            if (!HeaderRow) continue;
            var Header = HeaderRow.getElementsByTagName("th")[0];
            if (!Header) continue;

            NavigationBoxes[tableIndex] = Tables[i];
            Tables[i].setAttribute("id", "collapsibleTable" + tableIndex);

            var Button = document.createElement("span");
            var ButtonLink = document.createElement("a");
            var ButtonText = document.createTextNode(collapseCaption);

            Button.style.styleFloat = "right";
            Button.style.cssFloat = "right";
            Button.style.fontWeight = "normal";
            Button.style.textAlign = "right";
            if (!Tables[i].dataset.width) {
            	Button.style.width = "6em";
            } else {
            	Button.style.width = Tables[i].dataset.width;
            }
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute("id", "collapseButton" + tableIndex);
            ButtonLink.setAttribute("href", "javascript:collapseTable(" + tableIndex + ");");
            ButtonLink.appendChild(ButtonText);

            Button.appendChild(document.createTextNode("["));
            Button.appendChild(ButtonLink);
            Button.appendChild(document.createTextNode("]"));

            Header.insertBefore(Button, Header.childNodes[0]);
            tableIndex++;
        }
    }

    for (i = 0; i < tableIndex; i++) {
        if (hasClass(NavigationBoxes[i], "collapsed") || (tableIndex >= autoCollapse && hasClass(NavigationBoxes[i], "autocollapse"))) {
            collapseTable(i);
        } else if (hasClass(NavigationBoxes[i], "innercollapse")) {
            var element = NavigationBoxes[i];
            while (element = element.parentNode) {
                if (hasClass(element, "outercollapse")) {
                    collapseTable(i);
                    break;
                }
            }
        }
    }
}
jQuery(document).ready(createCollapseButtons);

/* Zmiana tytułu strony, autor: [[user:Vae]], Public Domain   */
/* Użycie: {{nibytytul|Przykladowy tytul}}                    */
/*         {{nibytytul|Przykladowy tytul|sub=Podtytul}}       */
// TODO: Przerobić na jQuery, żeby ktoś chociaż wiedział, jak to ma działać
function uChangeTitle() {
    var staryTytul = null;
    staryTytul = returnObjById("firstHeading"); /* monobook */
    if (!staryTytul) {
    	if (mw.config.get('skin') === 'minerva') {
 			staryTytul = returnObjById("section_0"); /* minerva */
    	} else {
        	var h1 = document.body.getElementsByTagName("h1");
        	staryTytul = h1[0];
    	}
    }
    var nowyTytul = null;
    nowyTytul = returnObjById("uTytulStrony");
    if (nowyTytul && staryTytul) {
        if (staryTytul.innerHTML.match(/<span>/i))
            staryTytul.innerHTML = staryTytul.innerHTML.replace(/^(.*?)<span>/, returnObjById("uTytulStrony").innerHTML + "<span>");
        else
            staryTytul.innerHTML = returnObjById("uTytulStrony").innerHTML;
        staryTytul.style.backgroundColor = nowyTytul.style.backgroundColor;
        staryTytul.style.color = nowyTytul.style.color;
        // sprawdzamy, czy podtytul tez jest do zamiany
        var ssub = returnObjById("uPodtytulStrony");
        var sub = returnObjById("siteSub");
        if (ssub && ssub.innerHTML && sub) {
            sub.innerHTML = ssub.innerHTML;
        }
    }

    if (returnObjById("uLogoStrony")) {
        var a = (skin == "monobook") ? returnObjById("p-logo").getElementsByTagName('a')[0] : returnObjById("wiki_logo");
        a.style.backgroundImage = "url(" + returnObjById("uLogoStrony").getElementsByTagName('img')[0].src + ")";
    }
    return;
}
jQuery(document).ready(uChangeTitle);

/**
 * Skrypt dla Szablon:Galeria
 * Źródło: [[wikipedia:pl:MediaWiki:Common.js]]
 */
function toggleImage (group, remindex, shwindex) {
	jQuery("#ImageGroupsGr" + group + "Im" + remindex).hide();
	jQuery("#ImageGroupsGr" + group + "Im" + shwindex).show();
}
function ImageGroup() {
	if (mw.config.values.skin === "minerva") return;	//nie powinno się ładować na mobilce
	jQuery('div.ImageGroup').each(function(i, group) {
		var unitnode = jQuery('div.ImageGroupUnits', group).get(0);
		if (unitnode == undefined) {
			return 1;
		}
		var units = jQuery(unitnode).children('.center');
		var count = units.get().length;
		if (count <= 1) {
			return 1;
		}
		units.each(function(j, currentimage) {
			jQuery(currentimage).attr('id', "ImageGroupsGr" + i + "Im" + j);
			var leftlink = jQuery('<a href="#"/>');
			if (j != 0) {
				leftlink.text('◀').click(function() {
					toggleImage(i, j, j - 1); return false;
				});
			}
			var rightlink = jQuery('<a href="#"/>');
			if (j != count - 1) {
				rightlink.text('▶').click(function() {
					toggleImage(i, j, j + 1); return false;
				});
			}
			jQuery('<div/>').css({ 'font-size' : '110%', 'font-weight' : 'bold' })
				.addClass('disabled-user-selection')
				.append(leftlink)
				.append('<tt>(' + (j + 1) + '/' + count +  ')</tt>')
				.append(rightlink)
				.prependTo(jQuery(currentimage));
			if (j != 0) {
				jQuery(currentimage).hide().addClass('noprint');
			}
		});
	});
}
jQuery(ImageGroup);

mw.loader.using( ["mediawiki.util", "jquery.cookie"] ).then( function() {
	if ( mw.config.get( 'wgUserName' ) == null ) {
		mw.util.addCSS( '.anon_hide_block{display:none}' );

		// Skrypt dla anonimowych użytkowników umożliwiający odznaczenie wiadomości jako przeczytanych (na daną sesję).
		if ( mw.config.get( 'wgNamespaceNumber' ) === 3 ) {
			jQuery( function() {
				re = new RegExp( 'title=[^:&]+:[0-9.]+\&diff=cur' );
				if ( re.test( location.search ) ) {
					jQuery.cookie( 'read_memail_go_away', '1', {
						path: '/'
					} );
				}
			} );
		}

		if ( jQuery.cookie( 'read_memail_go_away' ) == '1' ) {
			mw.util.addCSS( '.usermessage {display:none;}' );
		}

	} else {
		mw.util.addCSS( '.nonanon_hide_block{display:none}' );
	}
} );

/* Robienie rzeczy */
$('.purgeNoConfirm').on( 'click', function (e) {
	new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then(function () {
		location.reload();
	}, function () {
		mw.notify( 'Purge failed', { type: 'error' } );
	});
	e.preventDefault();
});

/* </pre></nowiki> */