Wikipedysta:Matma Rex/kodmapy.js
// Wstawia zaznaczony tekst w polu edycji jako parametr „kod mapy” do infoboksu,
// w razie potrzeby dodając go. (Przy braku zaznaczenia wstawia pusty.)
// Po dodaniu parametru zaznacza nowo wstawiony kod (lub ustawia kursor w pustym miejscu).
// [[w:pl:User:Matma Rex]] 2013-2014, dual-licensed under MIT license and CC-BY-SA 3.0.
/*globals mw, toolbarGadget*/
mw.loader.using( "ext.gadget.lib-toolbar", function() {
toolbarGadget.addButton( {
id: 'wstawkodmapy',
title: 'Wstaw kod mapy',
alt: 'kod mapy',
icon: '//upload.wikimedia.org/wikipedia/commons/3/3e/Disambiguate_charset_button.png',
onclick: function() {
var tb = document.getElementById('wpTextbox1');
var text = tb.value;
var newMapCode = text.substring(tb.selectionStart, tb.selectionEnd);
var mapCodeIndex = text.search(/\|\s*kod mapy\s*=/);
if(mapCodeIndex != -1) {
// juz jest pole w infoboksie - wpisujemy
text = text.replace(/(\|\s*kod mapy\s*=[ \t]*)[^\n|]*/, '$1'+newMapCode);
} else {
// nie ma pola - dodajemy po wspolrzednych
// imitujemy obecny styl infoboksu
var match = text.match(/\s*\|\s*((sekund|minut|stopni)[NEWS]|współrzędne|szerokość|długość)\s*=[ \t]*/i);
var newLine;
if(match) {
newLine = match[0].replace(/(?:sekund[NEWS]|minut[NEWS]|stopni[NEWS]|współrzędne|szerokość|długość)(\s*)/i, function(match, spaces) {
var param = 'kod mapy';
// jesli jest wyrownywany, wyrownujemy; jak nie, to nie
if(spaces.length == 1) {
param += ' ';
} else if(spaces.length > 1) {
while(param.length < match.length) param += ' ';
}
return param;
});
newLine += newMapCode;
} else {
// ugh
newLine = '\n| kod mapy = ' + newMapCode;
}
var rev = function(str){
return str.split('').reverse().join('');
};
var prevText = text;
// wstawiamy po ostatnich wspolrzednych
// to jest strasznie glupie, ale coz
if(match) {
text = rev( rev(text).replace(
/[^\n|]*=\s*([NEWS](dnukes|tunim|inpots)|endęzrłópsw|ćśogułd|ćśokorezs)\s*\|/i,
rev(newLine) + '$&'
) );
} else {
// nie ma podanych wspolrzednych? wstawmy na koncu
text = text.replace(/\n\}\}\n/, newLine + '$&');
}
}
tb.value = text;
// i jeszcze zaznaczmy nowa wartosc
var idx = tb.value.search(/(\|\s*kod mapy\s*=[ \t]*)/);
var offset = tb.value.match(/(\|\s*kod mapy\s*=[ \t]*)/)[0].length;
var len = newMapCode.length;
tb.selectionStart = idx+offset;
tb.selectionEnd = idx+offset+len;
tb.focus();
}
} );
} );
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.
- 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:
- 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.
- 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.
- 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.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.