User:Dschwen/coordinates.js

// // Script to embed Coordinate template generation in MediaWiki's edit page

// // defaults // var coord_templatename = 'Coord'; // can be overridden with 'coor_dms'

// // globals // var wpSummary = null; var optionalBox = null; var optionalBoxLabel = null; var optionalNumber = null; var lat,lon;

// // configuration // cohel_config = { data : { listRegions : [ 'US','DE','US-CA' ], nbsp       : false, template   : 0, region     : 0 },

prefix : 'cohel_',

restore : function { var name; var pos;

if(document.cookie) {  var cookies = document.cookie.split('; '); for (var i = 0; i < cookies.length; i++) {   // gehoert der Keks uns? if( cookies[i].substr(0,this.prefix.length) == this.prefix ) {    pos = cookies[i].indexOf('='); name = cookies[i].substring(this.prefix.length, pos);

// nur existierende felder wiederherstellen! if( typeof(this.data[name]) != 'undefined' ) {     // skalar if( cookies[i].substr(pos+1,2) == 'V:' ) this.data[name] = cookies[i].substr(pos+3);

// array if( cookies[i].substr(pos+1,2) == 'A:' ) this.data[name] = cookies[i].substr(pos+3).split('+');

// boolean if( cookies[i].substr(pos+1,2) == 'B:' ) if( cookies[i].substr(pos+3) > 0 ) this.data[name] = true; else this.data[name] = false; }   }   } //endfor } //endif_cookie },

save : function { var today = new Date; var nextyear = new Date( today.getTime + 30758400000 ); var params = '; expires=' + nextyear.toGMTString + '; path=/';

for (var key in this.data) {   if( ( typeof(this.data[key]) == 'object' ) && ( this.data[key].length > 0 ) ) {   // join geht nicht, weil ich uriencoden will var dummy = encodeURIComponent( this.data[key][0] ); for (var i = 1; i < this.data[key].length; i++) dummy += '+' + encodeURIComponent( this.data[key][i] ); document.cookie = this.prefix + key + '=' + 'A:' + dummy + params; }  else if( typeof(this.data[key]) == 'boolean' ) {    if(this.data[key]) document.cookie = this.prefix + key + '=' + 'B:1' + params; else document.cookie = this.prefix + key + '=' + 'B:0' + params; }  else {    document.cookie = this.prefix + key + '=' + 'V:' + encodeURIComponent(this.data[key]) + params; } }

return true; }

};

var listTemplates = new Array( 'Koordinate_Artikel' ); var listRegions = new Array( 'US','DE','US-CA' ); var listTypes = new Array( 'landmark', 'city', 'mountain', 'waterbody', 'isle', 'airport', 'country', 'state', 'adm1st', 'adm2nd' );

var typeSelect = null; var regionSelect = null; var editbox = null; var coordinatebox = null;

// // Insert the coordinate Form into the edit box. // function coordinatesInstall { cohel_config.restore; var i=0; var copywarn = document.getElementById('editpage-copywarn'); if (copywarn != null) { var cleaner = ""; CoordinatesHTML = ' ' CoordinatesHTML += ' '; CoordinatesHTML += ''; CoordinatesHTML += ' minimize'; CoordinatesHTML += 'Region: - '; for (i=0; i'+listRegions[i]+' '; } CoordinatesHTML += ' Type:'; for (i=0; i'+listTypes[i]+' '; } CoordinatesHTML += '     '; copywarn.innerHTML = CoordinatesHTML + copywarn.innerHTML; wpSummary = document.getElementById('wpSummary'); optionalBox = document.getElementById('CoordinateBoxOptional'); optionalBoxLabel = document.getElementById('CoordinateBoxOptionalLabel'); optionalNumber = document.getElementById('CoordinateNumber'); detailBox = document.getElementById('CoordinateDetails');

typeSelect = document.getElementById('CoordinateType'); regionSelect = document.getElementById('CoordinateRegion');

hideDetails;

editbox = document.getElementById('wpTextbox1'); coordinatebox = document.getElementById('CoordinateBox'); // suche nach boilerplate text: Koordinaten 38,18° Nord, 122,26° West var boilerplate_filter = /Koordinaten.* ([^°]+)° (Nord|Süd), ([^°]+)° (West|Ost)/; var result; if( result = boilerplate_filter.exec(editbox.value) ) {  lat = result[1].replace(/,/, "."); if( result[2] == 'Süd' ) lat*=-1; lon = result[3].replace(/,/, "."); if( result[4] == 'West' ) lon*=-1; coordinatebox.value = 'boilerplate:'+lat+','+lon; showDetails; } boilerplate_filter = / ([0-9.,]+) Einwohner/; if( result = boilerplate_filter.exec(editbox.value) ) {  optionalNumber.value = result[1].replace(/[,\.]/, ""); typeSelect.selectedIndex = 1; toggleOptional; }   if( editbox.value.indexOf('') > -1 ) {  regionSelect.selectedIndex = 3; } } }

function toggleOptional { optionalBox.style.visibility="hidden";

if(typeSelect.value == 'city') { optionalBoxLabel.innerHTML='Population:'; optionalBox.style.visibility="visible"; } if(typeSelect.value == 'mountain') { optionalBoxLabel.innerHTML='Elevation (Meters above mean sealevel):'; optionalBox.style.visibility="visible"; } }

function hideDetails { //detailBox.style.visibility="hidden"; detailBox.style.display="none"; return false; } function showDetails { //detailBox.style.visibility="visible"; detailBox.style.display="block"; }

// // Hook up installation function // addOnloadHook(coordinatesInstall);

// // Coordinate interpretation // function interpretCoordinates { var coordinate1 = coordinatebox.value; var coordinate2 = ''; var from_ll, result; var coord_filter = /http:\/\/www\.google\.([a-zA-Z]+)\/maps/; var latlon = new Array; // magic! if(coordinate1.substr(0,19)=='http://maps.google.' || coord_filter.test(coordinate1) ) { from_ll = coordinate1.substr(coordinate1.indexOf("ll=")+3); var ampers = from_ll.indexOf('&'); if(ampers==-1) {  latlon = from_ll.split(","); } else {  latlon = (from_ll.substr(0,ampers)).split(","); } } //http://tools.wikimedia.de/~magnus/geo/geohack.php?params=39_18_S_175_35_E_type:mountain_region:NZ if(coordinate1.substr(0,49)=='http://tools.wikimedia.de/~magnus/geo/geohack.php') { latlon = from_ll.split(","); }

// gefundene boilerplate daten if( coordinate1.substr(0,12) == 'boilerplate:' ) { from_ll = coordinate1.substr(12); latlon = from_ll.split(","); }

//31°46'14.44"N ; 35°14'5.88"E coord_filter = /([\d.,]+)°\s*([\d.,]+)['`]\s*([\d.,]+)"\s*([nN])\s*[;,]\s*([\d.,]+)°\s*([\d.,]+)['`]\s*([\d.,]+)"\s*([eOwWeE])/; if( result = coord_filter.exec(coordinate1 ) ) { latlon[0] = result[1]*1.0 + result[2]/60.0 + result[3]/3600.0; if( result[4]=='s' || result[4]=='S' ) latlon[0] *= -1.0; latlon[1] = result[5]*1.0 + result[6]/60.0 + result[7]/3600.0; if( result[8]=='w' || result[8]=='W' ) latlon[1] *= -1.0; }

var mark_ns = 'N'; var mark_ew = 'E'; var mark_ew_d = 'O';

lat = latlon[0]; lon = latlon[1]; if(lat<0) { lat*=-1; mark_ns='S'; } if(lon<0) { lon*=-1; mark_ew='W'; mark_ew_d='W';}

var lat_m = (( lat - Math.floor(lat) ) * 60.0).toPrecision(10); var lon_m = (( lon - Math.floor(lon) ) * 60.0).toPrecision(10);

var lat_s = ( lat_m - Math.floor(lat_m) ) * 60.0; var lon_s = ( lon_m - Math.floor(lon_m) ) * 60.0;

coordinate2 = '\n\n';

var editbox_content = editbox.value.replace(//, ""); editbox.value = editbox_content + coordinate2;

if( wpSummary.value == "" ) wpSummary.value="+Coord using User:Dschwen/Coord tool";

cohel_config.save; } //