Wikipedysta:Tufor/refToolbar.js

/* ------------------------------------------------------------------------ *\
  Copyright: en:User:Mr.Z-man (en), Wikipedysta:Holek (pl), pl:User:Nux
   Licencja: GFDL oraz CC-BY-SA

  Original:
	http://en.wikipedia.org/wiki/User:Mr.Z-man/refToolbar.js

  Version:
    (see below) = refsTB.version
\* ------------------------------------------------------------------------ */
// <nowiki>


//
// Object Init
//
if (document.cookie.indexOf("js_refsTB_critical=1")==-1 && window.refsTB!=undefined)
{
	alert('Błąd krytyczny - konflikt nazw!'+
		'\n\n'+
		'Jeden ze skryptów używa już nazwy "refsTB" jako nazwę zmiennej globalnej.');
	document.cookie = "js_refsTB_critical=1; path=/";
	if (document.cookie.indexOf("js_refsTB_critical=1")!=-1)
	{
		alert('Poprzedni komunikat jest wyświetlany tylko raz w ciągu sesji.'+
		'\n\n'+
		'Musisz rozwiązać konflikt nazw lub usunąć jeden ze skryptów w całości.');
	}

}

window.refsTB = {
	/** Version of the gadget */
	version: '1.2.7',
	/** Number of forms */
	numforms: 0,

	/** Sets up the gadget */
	init: function() {
		var that = this;

		toolbarGadget.addButton( {
			title: 'Wstaw szablon cytowania (wer. ' + that.version + ')',
			alt: 'Wstaw szablon cytowania',
			oldIcon: '//upload.wikimedia.org/wikipedia/commons/b/bf/Button_easy_cite_pl.png',
			newIcon: '//upload.wikimedia.org/wikipedia/commons/thumb/c/c0/Curly_Brackets_cytuj.svg/22px-Curly_Brackets_cytuj.svg.png',
			onclick: function() {
				that.easyCiteMain();
			}
		} );
	},
	/** Shows and hides the form */
	easyCiteMain: function() {
		var citemain = document.getElementById( 'citeselect' );

		if ( !citemain ) {
			// Create the buttons
			citemain = document.createElement( 'div' );
			citemain.style.display = 'none';
			citemain.setAttribute( 'id', 'citeselect' );
			citemain.appendChild( this.addOption( "refsTB.citeWeb()", "Strona WWW" ) );
			citemain.appendChild( this.addOption( "refsTB.citeNamedRef()", "Istniejące przypisy" ) );
			citemain.appendChild( this.addOption( "refsTB.dispErrors()", "Sprawdzenie błędów" ) );
			citemain.appendChild( this.addOption( "refsTB.hideInitial()", "Anuluj" ) );
			var txtarea = document.getElementById( 'wpTextbox1' );
			txtarea.parentNode.insertBefore( citemain, txtarea );
		}
		if ( citemain.style.display == 'none' ) {
			citemain.style.display = '';
		} else {
			citemain.style.display = 'none';
		}
	}
};


//
// Methods
//

refsTB.addOption = function (script, text) {
	var option = document.createElement('input');
	option.setAttribute('type', 'button');
	option.onclick = new Function(script);
	option.setAttribute("value", text);
	return option;
}

refsTB.hideInitial = function () {
	document.getElementById('citeselect').style.display = 'none';
	refsTB.oldFormHide();
}

refsTB.oldFormHide = function () {
	if (refsTB.numforms != 0) {
		document.getElementById('citediv'+refsTB.numforms).style.display = 'none';
	}
	if (document.getElementById('errorform') != null) {
		document.getElementById('citeselect').removeChild(document.getElementById('errorform'));
	}
}

refsTB.getTime = function () {
	var time = new Date();
	var nowdate = time.getUTCDate();
	if (nowdate<10) { nowdate = "0"+ nowdate.toString(); }
	var nowmonth = time.getUTCMonth()+1;
	if (nowmonth<10) { nowmonth = "0"+ nowmonth.toString(); }
	var nowyear = time.getUTCFullYear();
	newtime =	nowyear + '-' + nowmonth + '-' + nowdate;
	return (newtime);
}

refsTB.parseCiteForm = function (form_id) {
	var els = document.getElementById(form_id).getElementsByTagName('input');
	for (var i=0; i<els.length; i++)
	{
		if (els[i].getAttribute('type')!='hidden')
		{
			els[i].setAttribute('tabindex', 100+i);
		}
		if (els[i].getAttribute('type')=='text')
		{
			els[i].onkeypress = function(e) {
				if(window.event) // IE
				{
					e = window.event;
				}
				if (e.keyCode == '13') {
					refsTB.addcites();
					return false;
				}
			};
		}
	}
}

refsTB.citeWeb = function () {
	refsTB.oldFormHide();
	template = "Cytuj stronę";
	var legend = "Cytowanie strony internetowej";
	newtime = refsTB.getTime();
	refsTB.numforms++;
	var form_el = document.createElement('div');
	form_el.id = 'citediv'+refsTB.numforms;
	form_el.innerHTML =
		'<fieldset><legend>'+legend+'</legend>'+
		'<table cellspacing="5">'+
		'<input type="hidden" value="'+template+'" id="template">'+
		'<tr><td width="120"><label for="url">&nbsp;URL: </label></td>'+
			'<td width="400"><input type="text" style="width:100%" id="url"></td>'+
		'<td width="120"><label for="tytuł">&nbsp;Tytuł: </label></td>'+
			'<td width="400"><input type="text" style="width:100%" id="tytuł"></td></tr>'+
		'<tr><td width="120"><label for="opublikowany">&nbsp;Wydawca: </label></td>'+
			'<td width="200"><input type="text" style="width:100%" id="opublikowany"></td>'+
		'<td width="120"><label for="język">&nbsp;Język: </label></td>'+
			'<td width="200"><input type="text" style="width:100%" id="język" value="en"></td>'+
		'<td width="120"><label for="data dostępu">&nbsp;Data dostępu: </label></td>'+
			'<td width="200"><input type="text" style="width:100%" id="data dostępu" value="'+ newtime +'"></td></tr>'+
		'</table>'+
		'<input type="button" value="Dodaj przypis" onClick="refsTB.addcites()">'+
	'</fieldset>';
	document.getElementById('citeselect').appendChild(form_el);
	refsTB.parseCiteForm(form_el.id);
}

refsTB.addcites = function (template) {
	cites = document.getElementById('citediv'+refsTB.numforms).getElementsByTagName('input');
	var citebegin = '<ref';
	var citename = '';
	var citeinner = '';
	for (var i=0; i<cites.length-1; i++) {
		if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id != "template") {
			citeinner += " | " + cites[i].id + " = " + cites[i].value;
		}
		else if (cites[i].value != '' && cites[i].id == "refname" && cites[i].id != "template") {
			citebegin += ' name="' + cites[i].value + '"';
		}
		else if (cites[i].value != '' && cites[i].id != "refname" && cites[i].id == "template") {
			citename = '>{{' + cites[i].value;
		}
	}
	cite = citebegin + citename + citeinner + "}}</ref>";
	document.getElementById('wpTextbox1').focus();	// focus first
	insertTags(cite, '', '');
	document.getElementById('citediv'+refsTB.numforms).style.display = 'none';
}

refsTB.getNamedRefs = function (calls) {
	if (typeof(wikEdUseWikEd) != 'undefined') {
		if (wikEdUseWikEd == true) {
			WikEdUpdateTextarea();
		}
	}
	text = document.getElementById('wpTextbox1').value;
	var regex;
	if (calls) {
		regex = /< *?ref +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)) *?\/ *?>/gi //'
	} else {
		regex = /< *?ref +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)) *?>/gi //'
	}
	var namedrefs = new Array();
	var i=0;
	var nr=true;
	do {
		ref = regex.exec(text);
		if(ref != null){
			if (ref[5]) {
				namedrefs[i] = ref[5];
			} else if (ref[3]) {
				namedrefs[i] = ref[3];
			} else {
				namedrefs[i] = ref[6];
			}
			i++;
		} else {
			nr=false;
		}
	} while (nr==true);
	return namedrefs;
}

refsTB.citeNamedRef = function () {
	var namedrefs = refsTB.getNamedRefs(false);
	refsTB.oldFormHide();
	refsTB.numforms++;
	var form_el = document.createElement('div');
	form_el.id = 'citediv'+refsTB.numforms;
	if (namedrefs == '') {
		form_el.innerHTML =
			'<fieldset>'+
			'<legend>Przypisy z artykułu</legend>'+
			'Nie znaleziono żadnych przypisów z przypisanymi nazwami (<tt>&lt;ref name="nazwa"&gt;</tt>)'+
			'</fieldset>';
	}
	else
	{
		var form =
			'<fieldset><legend>Przypisy z artykułu</legend>'+
			'<table cellspacing="5">'+
			'<tr><td><label for="namedrefs">&nbsp;Nazwane przypisy</label></td>'+
						'<td><select name="namedrefs" id="namedrefs">';
		for (var i=0;i<namedrefs.length;i++) {
			form+= '<option value="'+namedrefs[i]+'">'+namedrefs[i]+'</option>';
		}
		form+= '</select>'+
			'</td></tr></table>'+
			'<input type="button" value="Dodaj przypis" onClick="refsTB.addnamedcite()">'+
			'</fieldset>';
		form_el.innerHTML = form;
	}
	document.getElementById('citeselect').appendChild(form_el);
}

refsTB.addnamedcite = function () {
	name = document.getElementById('citediv'+refsTB.numforms).getElementsByTagName('select')[0].value;
	ref = '{{r|'+name+'}}';
	document.getElementById('wpTextbox1').focus();	// focus first
	insertTags(ref, '', '');
	document.getElementById('citediv'+refsTB.numforms).style.display = 'none';
}

refsTB.getAllRefs = function () {
	if (typeof(wikEdUseWikEd) != 'undefined') {
		if (wikEdUseWikEd == true) {
			WikEdUpdateTextarea();
		}
	}
	text = document.getElementById('wpTextbox1').value;
	regex = /< *?ref( +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)))? *?>((.|\n)*?)< *?\/? *?ref *?>/gim //"
	var allrefs = new Array();
	var i=0;
	var nr=true;
	do {
		ref = regex.exec(text);
		if(ref != null){
			var orig_code = ref[0];
			if (ref[0].search(/[^\s]{150}/) != -1) {
				ref[0] = ref[0].replace(/\|([^\s])/g, "| $1");
			}
			ref[0] = ref[0].replace(/</g, "&lt;");
			ref[0] = ref[0].replace(/>/g, "&gt;");
			allrefs[i] = {code : ref[0], index : ref.index, orig_code: orig_code};
			i++;
		} else {
			nr=false;
		}
	} while (nr==true);
	return allrefs;
}

refsTB.NRcallError = function (namedrefs, refname) {
	for (var i=0; i<namedrefs.length; i++) {
		if (namedrefs[i] == refname) {
			return true;
		}
	}
	return false;
}

refsTB.gotoErrorCode = function (code) {
	code = decodeURIComponent(code);
	var input = document.getElementById('wpTextbox1');
	var pos=-1;
	// already selected? => get next
	if (sel_t.getSelStr(input, false)==code)
	{
		sel_pos = sel_t.getSelBound(input);
		pos = input.value.indexOf(code, sel_pos.start+1);
	}
	// not selected yet or last found => get first
	if (pos==-1)
	{
		pos = input.value.indexOf(code);
	}
	// select if found
	if (pos!=-1)
	{
		sel_t.setSelBound(input, {start:pos, end:(pos+code.length)}, true);
	}
}
refsTB.gotoErrorCodeHTML = function (code) {
	var search_icon = '//upload.wikimedia.org/wikipedia/commons/thumb/3/33/Crystal_Clear_action_viewmag.png/20px-Crystal_Clear_action_viewmag.png';
	var el = document.getElementById('wpTextbox1')
	return '<img'
		+ ' style="margin:0 .3em; float:right;"'
		+ ' src="'+search_icon+'" alt="szukaj"'
		+ ' onclick="refsTB.gotoErrorCode(\''+encodeURIComponent(code)+'\')"'
		+ ' />'
	;
}
refsTB.errorCheck = function () {
	var allrefs = refsTB.getAllRefs();
	var allrefscontent = new Array();
	var samecontentexclude = new Array();
	var sx=0;
	var templateexclude = new Array();
	var tx=0;
	var skipcheck = false;
	var namedrefcalls = refsTB.getNamedRefs(true);
	for (var i=0; i<allrefs.length; i++) {
		allrefscontent[i] = allrefs[i].code.replace(/&lt; *?ref( +?name *?= *?(('([^']*?)')|("([^"]*?)")|([^'"\s]*?[^\/]\b)))? *?&gt;((.|\n)*?)&lt; *?\/? *?ref *?&gt;/gim, "$8");	//"
	}
	var namedrefs = refsTB.getNamedRefs(false);
	var errorlist = new Array();
	var q=0;
	unclosed = document.getElementById('unclosed').checked;
	samecontent = document.getElementById('samecontent').checked;
	templates = document.getElementById('templates').checked;
	repeated = document.getElementById('repeated').checked;
	undef = document.getElementById('undef').checked;
	for (var i=0; i<allrefs.length; i++) {
		if (allrefs[i].code.search(/&lt; *?\/ *?ref *?&gt;/) == -1 && unclosed) {
			errorlist[q] = '<tr><td width="75%"><tt>'+allrefs[i].code+'</tt>'+refsTB.gotoErrorCodeHTML(allrefs[i].orig_code)+'</td>';
			errorlist[q] += '<td width="25%">Niedomknięty tag <tt>&lt;ref&gt;</tt></td></tr>';
			q++;
		}
		if (samecontent) {
			for (var d=0; d<samecontentexclude.length; d++) {
				if (allrefscontent[i] == samecontentexclude[d]) {
					skipcheck = true;
				}
			}
			var p=0;
			while (p<allrefs.length && !skipcheck) {
				if (allrefscontent[i] == allrefscontent[p] && i != p) {
					errorlist[q] = '<tr><td width="75%"><tt>'+allrefscontent[i]+'</tt>'+refsTB.gotoErrorCodeHTML(allrefs[i].orig_code)+'</td>';
					errorlist[q] += '<td width="25%">Wiele przypisów posiada tę zawartość. Do tego przypisu powinna zostać przypisana <a target="_blank" href="//pl.wikipedia.org/wiki/Pomoc:Przypisy#Wielokrotne_u.C5.BCycie_jednego_odno.C5.9Bnika">nazwa</a>.</td></tr>';
					q++;
					samecontentexclude[sx] = allrefscontent[i]
					sx++;
					break;
				}
				p++;
			}
		skipcheck=false;
		}
		if (templates) {
			if (allrefscontent[i].search(/\{\{cytuj/i) == -1 && allrefscontent[i].search(/\{\{cite/i) == -1) {
				for (var x=0; x<templateexclude.length; x++) {
					if (allrefscontent[i] == templateexclude[x]) {
						skipcheck = true;
					}
				}
				if (!skipcheck) {
					errorlist[q] = '<tr><td width="75%"><tt>'+allrefs[i].code+'</tt>'+refsTB.gotoErrorCodeHTML(allrefs[i].orig_code)+'</td>';
					errorlist[q] += '<td width="25%">Przypis nie wykorzystuje szablonów cytowania</td></tr>';
					q++;
					templateexclude[tx] = allrefscontent[i];
					tx++;
				}
				skipcheck = false;
			}
		}
	}
	if (repeated) {
		var repeatnameexclude = new Array();
		var rx=0;
		for (var k=0; k<namedrefs.length; k++) {
			for (var d=0; d<repeatnameexclude.length; d++) {
				if (namedrefs[k] == repeatnameexclude[d]) {
					skipcheck = true;
				}
			}
			var z=0;
			while (z<namedrefs.length && !skipcheck) {
				if (namedrefs[k] == namedrefs[z] && k != z) {
					errorlist[q] = '<tr><td width="75%"><tt>'+namedrefs[k]+'</tt></td>';
					errorlist[q] += '<td width="25%">Kilka różnych przypisów posiada <a target="_blank" href="//pl.wikipedia.org/wiki/Pomoc:Przypisy#Wielokrotne_u.C5.BCycie_jednego_odno.C5.9Bnika">tę samą nazwę</a>.</td></tr>';
					q++;
					repeatnameexclude[rx] = namedrefs[z];
					rx++;
					break;
				}
				z++;
			}
			skipcheck = false;
		}
	}
	if (undef) {
		var undefexclude = new Array();
		var ux=0;
		for (var p=0; p<namedrefcalls.length; p++) {
			for (var d=0; d<undefexclude.length; d++) {
				if (allrefscontent[i] == undefexclude[d]) {
					skipcheck = true;
				}
			}
			if (!skipcheck) {
				if (!refsTB.NRcallError(namedrefs, namedrefcalls[p])) {
					errorlist[q] = '<tr><td width="75%"><tt>'+namedrefcalls[p]+'</tt></td>';
					errorlist[q] += '<td width="25%">Użyty przypis nie został wcześniej <a target="_blank" href="//pl.wikipedia.org/wiki/Pomoc:Przypisy#Wielokrotne_u.C5.BCycie_jednego_odno.C5.9Bnika">zdefiniowany</a>.</td></tr>';
					q++;
					undefexclude[ux] = namedrefs[p];
					ux++;
				}
			}
			skipcheck = false;
		}
 }
	if (q > 0) {
		return errorlist;
	} else {
		return 0;
	}
}

refsTB.dispErrors = function () {
	refsTB.oldFormHide();
	var form_el = document.createElement('div');
	form_el.id = 'errorform';
	form_el.innerHTML = '<fieldset>'+
		'<legend>Sprawdzanie błędów</legend>'+
		'<b>Sprawdź:</b><br/>'+
		'<input type="checkbox" id="unclosed" checked="checked" /> Niedomknięte tagi <tt>&lt;ref&gt;</tt><br/>'+
		'<input type="checkbox" id="samecontent" checked="checked" /> Przypisy z tymi samymi danymi<br/>'+
		'<input type="checkbox" id="templates" checked="checked" /> Przypisy niewykorzystujące szablonów cytowania<br/>'+
		'<input type="checkbox" id="repeated" checked="checked" /> Powtórzone przypisy o tej samej nazwie<br/>'+
		'<input type="checkbox" id="undef" checked="checked" /> Użycie nazwanych przypisów bez treści/definicji<br/>'+
		'<input type="button" id="errorchecksubmit" value="Sprawdzenie pod kątem wybranych błędów" onclick="refsTB.doErrorCheck()"/>'+
		'</fieldset>';
	document.getElementById('citeselect').appendChild(form_el);
}

refsTB.doErrorCheck = function () {
	var errors = refsTB.errorCheck();
	document.getElementById('citeselect').removeChild(document.getElementById('errorform'));
	if (errors == 0) {
		if (refsTB.numforms != 0) {
			document.getElementById('citediv'+refsTB.numforms).style.display = 'none';
		}
		refsTB.numforms++;
		var form_el = document.createElement('div');
		form_el.id = 'citediv'+refsTB.numforms;
		form_el.innerHTML = '<fieldset>'+
			'<legend>Sprawdzanie błędów</legend>Nie znaleziono żadnych błędów.</fieldset>';
		document.getElementById('citeselect').appendChild(form_el);
	}
	else {
		if (refsTB.numforms != 0) {
			document.getElementById('citediv'+refsTB.numforms).style.display = 'none';
		}
		refsTB.numforms++;
		var form_el = document.createElement('div');
		form_el.id = 'citediv'+refsTB.numforms;
		var form =
			'<fieldset><legend>Sprawdzanie błędów</legend>'+
			'<table border="1px">';
		for (var i=0; i<errors.length; i++) {
			form+=errors[i];
		}
		form+= '</table>'+
			'</fieldset>';
		form_el.innerHTML = form
		document.getElementById('citeselect').appendChild(form_el);
	}
}

mw.loader.using( "ext.gadget.lib-toolbar", function() {
	refsTB.init();
} );
// </nowiki>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
Kembali kehalaman sebelumnya