User:Dschwen/wikiminiatlas.js

// // Script to embed interactive maps into pages that have coordinate templates // also check my commons page commons:User:Dschwen for more tools

// // defaults // var wikiminiatlas_coordinate_region = ''; var wikiminiatlas_width = 500; var wikiminiatlas_height = 300; var wikiminiatlas_imgbase = 'http://up2p77.gwdg.de/~daniel/MiniWikiAtlas/tiles/';

// // globals // var wikiminiatlas_widget = null; var wikiminiatlas_map = 0; var wikiminiatlas_nx; var wikiminiatlas_ny; var wikiminiatlas_tile; var wikiminiatlas_old_onmouseup; var wikiminiatlas_old_onmousemove; var wikiminiatlas_dragging = null; var wikiminiatlas_gx = 0; var wikiminiatlas_gy = 0; var wikiminiatlas_zoom = 1; var wikiminiatlas_zoomsize = [ 3, 6 ,12 ,24 ,48 ]; var wikiminiatlas_marker = null; var wikiminiatlas_marker_lat; var wikiminiatlas_marker_lon;

// // Insert the map Widget into the page. // function wikiminiatlasInstall { var coordinates = document.getElementById('coordinates'); if (coordinates != null && wikiminiatlas_widget == null) {

//document.getElementById('debugbox').innerHTML='';

var coord_index = (coordinates.innerHTML).indexOf('params='); if(coord_index>-1) {  var coord_params = (coordinates.innerHTML).substr(coord_index+7); //document.getElementById('debugbox').innerHTML+=coord_params+' ';

// en.wp coordinates var coord_filter = /([\d+-.]+)_([NS])_([\d+-.]+)_([EW])_/; if(coord_filter.test(coord_params)) {   coord_filter.exec(coord_params); wikiminiatlas_marker_lat=RegExp.$1; if(RegExp.$2=='S') wikiminiatlas_marker_lat*=-1; wikiminiatlas_marker_lon=RegExp.$3; if(RegExp.$4=='W') wikiminiatlas_marker_lon*=-1; }

// de.wp coordinates coord_filter = /([\d+-.]+)_([\d+-.]+)_([\d+-.]+)_([NS])_([\d+-.]+)_([\d+-.]+)_([\d+-.]+)_([EW])_/; if(coord_filter.test(coord_params)) {   coord_filter.exec(coord_params); wikiminiatlas_marker_lat=(1.0*RegExp.$1) + (RegExp.$2/60.0) + (RegExp.$3/3600.0); if(RegExp.$4=='S') wikiminiatlas_marker_lat*=-1; wikiminiatlas_marker_lon=(1.0*RegExp.$5) + (RegExp.$6/60.0) + (RegExp.$6/3600.0); if(RegExp.$8=='W') wikiminiatlas_marker_lon*=-1; //document.getElementById('debugbox').innerHTML=wikiminiatlas_marker_lat+','+wikiminiatlas_marker_lon; }  // de.wp coordinates coord_filter = /([\d+-.]+)_([\d+-.]+)_([NS])_([\d+-.]+)_([\d+-.]+)_([EW])_/; if(coord_filter.test(coord_params)) {   coord_filter.exec(coord_params); wikiminiatlas_marker_lat=(1.0*RegExp.$1) + (RegExp.$2/60.0); if(RegExp.$3=='S') wikiminiatlas_marker_lat*=-1; wikiminiatlas_marker_lon=(1.0*RegExp.$4) + (RegExp.$5/60.0); if(RegExp.$6=='W') wikiminiatlas_marker_lon*=-1; //document.getElementById('debugbox').innerHTML=wikiminiatlas_marker_lat+','+wikiminiatlas_marker_lon; } }  var region_index = (coordinates.innerHTML).indexOf('region:'); if(region_index>-1) {  wikiminiatlas_coordinate_region = (coordinates.innerHTML).substr(region_index+7); wikiminiatlas_coordinate_region = wikiminiatlas_coordinate_region.substr(0,wikiminiatlas_coordinate_region.indexOf('"')); }  WikiMiniAtlasHTML =  ' Atlas';  WikiMiniAtlasHTML += ''; //  WikiMiniAtlasHTML += '+ ' //  WikiMiniAtlasHTML += '- ' WikiMiniAtlasHTML += '' WikiMiniAtlasHTML += '' WikiMiniAtlasHTML += '' WikiMiniAtlasHTML += '' WikiMiniAtlasHTML += 'MiniWikiAtlas' WikiMiniAtlasHTML += ' '; WikiMiniAtlasHTML += ' ';

coordinates.innerHTML = coordinates.innerHTML + WikiMiniAtlasHTML ; wikiminiatlas_widget = document.getElementById('wikiminiatlas_widget');

wikiminiatlas_old_onmouseup = document.onmouseup; wikiminiatlas_old_onmousemove = document.onmousemove;

initializeWikiMiniAtlasMap; wmaMoveToTarget; } }

// // Hook up instalation function // $(wikiminiatlasInstall);

// // Coordinate interpretation // function toggleWikiMiniAtlas { if(wikiminiatlas_widget.style.visibility != "visible") {  wikiminiatlas_widget.style.visibility="visible"; } else {  wikiminiatlas_widget.style.visibility="hidden"; }

return false; }

function initializeWikiMiniAtlasMap { if(wikiminiatlas_map == 0) { wikiminiatlas_map = document.getElementById('wikiminiatlas_map'); wikiminiatlas_map.onmousedown = mouseDownWikiMiniAtlasMap; document.onmousemove = mouseMoveWikiMiniAtlasMap; document.onmouseup = mouseUpWikiMiniAtlasMap;

wikiminiatlas_nx = Math.floor(wikiminiatlas_width/128)+2; wikiminiatlas_ny = Math.floor(wikiminiatlas_height/128)+2; wikiminiatlas_tile = new Array(wikiminiatlas_nx*wikiminiatlas_ny);

var n = 0; var thistile;

for(var j = 0; j < wikiminiatlas_ny; j++) for(var i = 0; i < wikiminiatlas_nx; i++) {   //wikiminiatlas_map.innerHTML += ''; //wikiminiatlas_map.innerHTML += ''+n+' '; wikiminiatlas_map.innerHTML += ' '; //wikiminiatlas_tile[n] = new Image(128,128);

thistile = document.getElementById('wmatile'+n); thistile.onmousedown = mouseDownWikiMiniAtlasMap; //thistile.onmouseup = mouseUpWikiMiniAtlasMap;

n++; } wikiminiatlas_map.innerHTML += ' '; wikiminiatlas_marker = document.getElementById('wmamarker'); } }

// // Set new map Position (to wikiminiatlas_gx, wikiminiatlas_gy) // function moveWikiMiniAtlasMapTo { if(wikiminiatlas_gy<0) wikiminiatlas_gy=0; if(wikiminiatlas_gx<0) wikiminiatlas_gx+=Math.floor(wikiminiatlas_zoomsize[wikiminiatlas_zoom]*256);

var lx = Math.floor(wikiminiatlas_gx/128) % wikiminiatlas_nx; var ly = Math.floor(wikiminiatlas_gy/128) % wikiminiatlas_ny; var fx = wikiminiatlas_gx % 128; var fy = wikiminiatlas_gy % 128; var n; var thistile; var tileurl;

//document.getElementById('debugbox').innerHTML='';

for(var j = 0; j < wikiminiatlas_ny; j++) for(var i = 0; i < wikiminiatlas_nx; i++) {  n = ((i+lx) % wikiminiatlas_nx) + ((j+ly) % wikiminiatlas_ny)*wikiminiatlas_nx;

thistile = document.getElementById('wmatile'+n); thistile.style.left = (i*128-fx) + 'px'; thistile.style.top = (j*128-fy) + 'px';

//thistile.innerHTML = (Math.floor(wikiminiatlas_gx/128)+i)+','+(Math.floor(wikiminiatlas_gy/128)+j); tileurl='url("'+wmaGetTileURL((Math.floor(wikiminiatlas_gy/128)+j),(Math.floor(wikiminiatlas_gx/128)+i),wikiminiatlas_zoom)+'")';

if(wikiminiatlas_tile[n]!=tileurl) {   wikiminiatlas_tile[n] = tileurl; thistile.style.backgroundImage=tileurl;

//thistile.innerHTML=tileurl; }

//document.getElementById('debugbox').innerHTML+=wmaGetTileURL((Math.floor(wikiminiatlas_gy/128)+j),(Math.floor(wikiminiatlas_gx/128)+i),1)+' ';  var newcoords = wmaLatLonToXY(wikiminiatlas_marker_lat,wikiminiatlas_marker_lon); var newx = (newcoords.x-wikiminiatlas_gx); if(newx<-100) newx+=(wikiminiatlas_zoomsize[wikiminiatlas_zoom]*256); wikiminiatlas_marker.style.left = (newx-6)+'px'; wikiminiatlas_marker.style.top = (newcoords.y-wikiminiatlas_gy-6)+'px'; } }

// // Mouse down handler (start map-drag) // function mouseDownWikiMiniAtlasMap(ev) { ev = ev || window.event; wikiminiatlas_dragging = wmaMouseCoords(ev); }

// // Mouse up handler (finish map-drag) // function mouseUpWikiMiniAtlasMap { wikiminiatlas_dragging = null; if( wikiminiatlas_old_onmouseup != null ) wikiminiatlas_old_onmouseup; }

// Mouse move handler function mouseMoveWikiMiniAtlasMap(ev) { if( wikiminiatlas_dragging != null ) { var newev = ev || window.event; var newcoords = wmaMouseCoords(newev);

wikiminiatlas_gx -= ( newcoords.x - wikiminiatlas_dragging.x ); wikiminiatlas_gy -= ( newcoords.y - wikiminiatlas_dragging.y ); wikiminiatlas_dragging = newcoords;

moveWikiMiniAtlasMapTo; }

if( wikiminiatlas_old_onmousemove != null ) wikiminiatlas_old_onmousemove(ev); }

function wmaMouseCoords(ev) { if(ev.pageX || ev.pageY) { return {x:ev.pageX, y:ev.pageY}; } return { x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, y:ev.clientY + document.body.scrollTop - document.body.clientTop }; }

function wmaGetTileURL(y,x,z) { return wikiminiatlas_imgbase+'zoom'+(z+1)+'/tile_'+(wikiminiatlas_zoomsize[z]-y-1)+'_'+(x % (wikiminiatlas_zoomsize[z]*2) )+'.png'; }

function wmaZoomIn { if(wikiminiatlas_zoom<4) { var mapcenter = wmaXYToLatLon(wikiminiatlas_gx+wikiminiatlas_width/2,wikiminiatlas_gy+wikiminiatlas_height/2); wikiminiatlas_zoom++; var newcoords = wmaLatLonToXY(mapcenter.lat,mapcenter.lon); wikiminiatlas_gx = newcoords.x-wikiminiatlas_width/2; wikiminiatlas_gy = newcoords.y-wikiminiatlas_height/2; moveWikiMiniAtlasMapTo; } }

function wmaZoomOut { if(wikiminiatlas_zoom>0) { var mapcenter = wmaXYToLatLon(wikiminiatlas_gx+wikiminiatlas_width/2,wikiminiatlas_gy+wikiminiatlas_height/2); wikiminiatlas_zoom--; var newcoords = wmaLatLonToXY(mapcenter.lat,mapcenter.lon); wikiminiatlas_gx = newcoords.x-wikiminiatlas_width/2; wikiminiatlas_gy = newcoords.y-wikiminiatlas_height/2; moveWikiMiniAtlasMapTo; } }

function wmaMoveToTarget { var newcoords = wmaLatLonToXY(wikiminiatlas_marker_lat,wikiminiatlas_marker_lon); wikiminiatlas_gx = newcoords.x-wikiminiatlas_width/2; wikiminiatlas_gy = newcoords.y-wikiminiatlas_height/2; moveWikiMiniAtlasMapTo; }

function wmaLatLonToXY(lat,lon) { var newx = Math.floor((lon/360.0)*wikiminiatlas_zoomsize[wikiminiatlas_zoom]*256); if( newx < 0 ) newx+=wikiminiatlas_zoomsize[wikiminiatlas_zoom]*256; return { y:Math.floor((0.5-lat/180.0)*wikiminiatlas_zoomsize[wikiminiatlas_zoom]*128), x:newx }; }

function wmaXYToLatLon(x,y) { return { lat:180.0*(0.5-y/(wikiminiatlas_zoomsize[wikiminiatlas_zoom]*128)), lon:360.0*(x/(wikiminiatlas_zoomsize[wikiminiatlas_zoom]*256)) }; }

//