Wikisłownikarz:Alkamid/newArticle.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
// Author: [[pl:User:Beau]]
// Requirements:
// * lib-beau

function setCookie(cookieName, cookieValue) {
	var today = new Date();
	var expire = new Date();
	var nDays = 30;
	expire.setTime( today.getTime() + (3600000 * 24 * nDays) );
	document.cookie = cookieName + "=" + escape(cookieValue)
			+ ";path=" + mw.config.get('wgScriptPath')
			+ ";expires="+expire.toGMTString();
	document.cookie = cookieName + "=" + escape(cookieValue)
			+ ";path=" + mw.config.get('wgArticlePath').replace("/$1", "")
			+ ";expires="+expire.toGMTString();
}

function getCookie(cookieName) {
	if (!document.cookie || document.cookie.length==0)
		return "";
	var start = document.cookie.indexOf( cookieName + "=" );
	if ( start<0 ) return "";
	var len = start + cookieName.length + 1;
	if ( ( !start ) && ( cookieName != document.cookie.substring( 0, cookieName.length ) ) )
		return "";
	var end = document.cookie.indexOf( ";", len );
	if ( end<0 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

var newArticleGadget = {};

newArticleGadget.version = 10;

newArticleGadget.languages = new Array(
        'esperanto',
        'język angielski',
        'język czeski',
        'język francuski',
        'język grecki',
        'język hiszpański',
        'język litewski',
        'język niemiecki',
        'język polski',
        'język rosyjski',
        'język szwedzki',
        'język ukraiński',
        'japoński'
);

newArticleGadget.parts = new Array(
        'czasownik',
        'liczebnik',
        'partykuła',
        'przyimek',
        'przymiotnik',
        'przysłówek',
        'rzeczownik',
        'spójnik',
        'wykrzyknik',
        'zaimek',
        'związek frazeologiczny'
);

newArticleGadget.tips = {
	'język polski': {
		'czasownik': {
			question: 'co można robić?',
			examples: new Array('pisać', 'gotować')
		},
		'liczebnik': {
			question: 'ile? który z kolei?',
			examples: new Array('osiem', 'trzeci')
		},
		'partykuła': {
			examples: new Array('bodaj', 'oby')
		},
		'przyimek': {
			examples: new Array('do', 'na', 'bez')
		},
		'przymiotnik': {
			question: 'jaki?, czyj?, który?',
			examples: new Array('wesoły')
		},
		'przysłówek': {
			question: 'jak?',
			examples: new Array('szybko')
		},
		'rzeczownik': {
			question: 'kto?, co?',
			examples: new Array('rolnik', 'usta')
		},
		'spójnik': {
			examples: new Array('oraz', 'czyli')
		},
		'wykrzyknik': {
			examples: new Array('ach', 'plusk')
		},
		'zaimek': {
			examples: new Array('ja', 'mój')
		},
		'związek frazeologiczny': {
			examples: new Array('pies ogrodnika')
		}
	}
};

newArticleGadget.panel = null;
newArticleGadget.languageSelect = null;
newArticleGadget.partSelect = null;
newArticleGadget.tipsLine = null;

newArticleGadget.showPanel = function() {
	if (this.panel) {
		if (this.panel.style.display == 'none')
			this.panel.style.display = 'block';
		else
			this.panel.style.display = 'none';
		return;
	}

	this.panel = document.createElement('div');
	this.panel.style.paddingTop = '0.2em';
	this.panel.style.paddingBottom = '0.2em';

	var firstLine = document.createElement('div');

	this.languageSelect = document.createElement("select");
	this.languageSelect.onchange = function () { newArticleGadget.selectionChanged() }
	this.languageSelect.style.verticalAlign = 'middle';

	var selected = getCookie("newArticleLanguage");
	if (!selected)
		selected = "język polski";

	for (var i in this.languages) {
		var language = this.languages[i];
		var option = document.createElement('option');
		option.value = language;
		if (language == selected)
			option.selected = true;
		option.appendChild( document.createTextNode(language) );
		this.languageSelect.appendChild(option);
	}

	firstLine.appendChild( document.createTextNode('Nowa sekcja językowa: '));
	firstLine.appendChild( this.languageSelect );

	this.partSelect = document.createElement("select");
	this.partSelect.onchange = function () { newArticleGadget.selectionChanged() }
	this.partSelect.style.verticalAlign = 'middle';

	selected = getCookie("newArticlePart");

	for (var i in this.parts) {
		var part = this.parts[i];
		var option = document.createElement('option');
		option.value = part;
		if (part == selected)
			option.selected = true;

		option.appendChild( document.createTextNode(part) );
		this.partSelect.appendChild(option);
	}

	firstLine.appendChild( document.createTextNode('Część mowy: '));
	firstLine.appendChild( this.partSelect );

	this.button = document.createElement("input");
	this.button.type = "button";
	this.button.value = "Wstaw szablon";
	this.button.onclick = function() { newArticleGadget.insertTemplate(); };

	firstLine.appendChild( this.button );

	this.panel.appendChild(firstLine);

	this.tipsLine = document.createElement("p");
	this.panel.appendChild(this.tipsLine);

	var textarea = document.getElementById('wpTextbox1');
	if (!textarea)
		return;

	textarea.parentNode.insertBefore(this.panel, textarea);
	this.selectionChanged();
}

newArticleGadget.selectionChanged = function() {
	// zapamiętaj w ciasteczku ustawienia dodawanych haseł
	setCookie("newArticleLanguage", this.languageSelect.value);
	setCookie("newArticlePart", this.partSelect.value);

	this.tipsLine.innerHTML = '';

	var tips = this.tips[this.languageSelect.value];
	if (!tips) return;
	tips = tips[this.partSelect.value];
	if (!tips) return;

	if (tips.question) {
		this.tipsLine.appendChild( document.createTextNode("Pytanie: ") );
		var span = document.createElement('span');
		span.style.fontWeight = 'bold';
		span.appendChild( document.createTextNode(tips.question) );
		this.tipsLine.appendChild(span);
		this.tipsLine.appendChild( document.createTextNode(". ") );
	}
	if (tips.examples) {
		this.tipsLine.appendChild( document.createTextNode("Zobacz przykładowe hasło: ") );
		for (var i = 0; i < tips.examples.length; i++) {
			var example = tips.examples[i];
			var link = document.createElement('a');
			link.title = example;
			link.href = wgArticlePath.replace("$1", example);
			link.appendChild(document.createTextNode(example));
			if (i > 0)
				this.tipsLine.appendChild( document.createTextNode(", ") );
			this.tipsLine.appendChild( link );
		}
		this.tipsLine.appendChild( document.createTextNode(". ") );
	}
}

newArticleGadget.ucfirst = function(str) {
	return str.substr(0, 1).toUpperCase() + str.substr(1, str.length);
}

newArticleGadget.insertTemplate = function() {
	var lines = new Array();

	var language = this.languageSelect.value;
	var part = this.partSelect.value;

	var langshort = language.replace("język ", ""); // usuń prefiks

	if (language == 'japoński') {
		lines.push("== {{ja|" + wgTitle + "}} ({{język japoński}}) ==");
		lines.push("{{wymowa}} {{hep|}}");
		lines.push("{{znaczenia}}");
		lines.push("''rzeczownik''");
		lines.push(": (1.1) {{zob|[[]]}} ''([[]])''\n{{przykłady}}\n: (1.1)\n{{źródła}}");
	}
	else {
		lines.push("== " + wgTitle + " ({{" + language + "}}) ==");
	
	
		if (language == 'język rosyjski' || language == 'język ukraiński') {
			lines.push("{{transliteracja}}");
		}
		if (language == 'język francuski') {
			lines.push("{{wymowa}} {{IPA3|}}");
		}
		else {
			lines.push("{{wymowa}}");
		}	
		lines.push("{{znaczenia}}");

		if (part == "rzeczownik" && language == "język angielski") {
			lines.push("''" + part + " policzalny/niepoliczalny''");
		}
		else {
			lines.push("''" + part + "''");
		}

		lines.push(": (1.1) ");
		var infl = "{{odmiana}}"
		if (part == "zaimek") {
			infl += " {{zob|[[Aneks:" + this.ucfirst(language) + " - zaimki]]}}"
		}
		if (part == "rzeczownik" && language == "język francuski") {
			infl += " {{lp}}, {{lm}}"
		}
		if (part == "przymiotnik" && language == "język francuski") {
			infl += " {{lp}} {{m}}, {{f}}; {{lm}} {{m}}, {{f}}; {{stopn|plus " + wgTitle + "|le plus " + wgTitle + "}}"
		}
		lines.push(infl);

		lines.push("{{przykłady}}");
		if (language == "język polski") {
			lines.push(": (1.1) ");
		}
		else {
			lines.push(": (1.1) ");
		}
		lines.push("{{składnia}}");
		lines.push("{{kolokacje}}");
		lines.push("{{synonimy}}");
		lines.push("{{antonimy}}");
		lines.push("{{pokrewne}}");
		lines.push("{{frazeologia}}");
		lines.push("{{etymologia}}");
		var notes = "{{uwagi}}";

		if (part == "związek frazeologiczny") {
			notes += " zobacz też: [[Indeks:" + this.ucfirst(langshort) +" - Związki frazeologiczne]]";
		}
		else if (part == "liczebnik" && language != "język polski") {
			notes += " zobacz też: [[Indeks:" + this.ucfirst(langshort) +" - Liczebniki]]";
		}
		lines.push(notes);

		if (language == "język polski") {
			lines.push("{{tłumaczenia}}");
			lines.push("* angielski: (1.1) [[ ]]");
		}
		lines.push("{{źródła}}");
		lines.push("<references/>");
	}
	
	var text = lines.join("\n");
	// znajdź definicję i podziel tekst
	var m = text.match(/^([\s\S]+?\(1\.1\) )([^\n]*)([\s\S]+)$/);
	if (m) {
		insertTags(m[1], m[3], m[2]);
	}
	else {
		insertTags(text, "", "");
	}
	
}

newArticleGadget.init = function() {
	mw.libs.toolbarGadget.addButton({
		icon	: "http://upload.wikimedia.org/wikipedia/en/f/fd/Button_blockquote.png",
		title	: 'Nowa sekcja językowa (wersja ' + this.version + ')',
		alt	: 'Nowa sekcja językowa',
		id	: 'newArticleGadgetButton',
		onclick	: function() { newArticleGadget.showPanel() }
	});

	// pokaż panel jeśli tworzone jest nowe hasło w przestrzeni głównej
	if (!wgArticleId && !wgNamespaceNumber)
		this.showPanel();
}

if (wgAction == 'edit' || wgAction == 'submit') {
	addOnloadHook( function() { newArticleGadget.init() } );
}