MediaWiki:Wikiminiatlas.js: Difference between revisions

direct 1% of traffic to Labs
(this needs some serious jshint cleanup (unfortunately that site is down right now))
(direct 1% of traffic to Labs)
Line 6: Line 6:
jQuery(function ($) {
jQuery(function ($) {
  // send a fraction of traffic to labs
  // send a fraction of traffic to labs
  var useLabs = false; //Math.random()<0.01;
  var useLabs = Math.random()<0.01;


  var config = {
  var config = {
Line 249: Line 249:


   // insert iframe into DOM on demand (to preserve page caching)
   // insert iframe into DOM on demand (to preserve page caching)
   if( !wi.indom ) {
   if (!wi.indom) {
   $('#content,#mw_content').prepend(wi.div);
   $('#content,#mw_content').prepend(wi.div);
   wi.indom = true;
   wi.indom = true;
   }
   }


   if( wi.iframe.attr('src') !== newurl ) {
   if (wi.iframe.attr('src')!==newurl) {
   wi.iframe.attr( 'src', newurl );
   wi.iframe.attr( 'src', newurl );
   } else if( wi.div.css('display') !== 'none' ) {
   } else if( wi.div.css('display') !== 'none' ) {
Line 265: Line 265:


  function highlight(i) {
  function highlight(i) {
   if( coord_highlight >= 0 ) {
   if (coord_highlight>=0) {
   $(coord_list[coord_highlight].obj).css('background-color','').find('span:visible').css('background-color','');
   $(coord_list[coord_highlight].obj).css('background-color','').find('span:visible').css('background-color','');
   }
   }
   coord_highlight = i;
   coord_highlight = i;
   if( coord_highlight >= 0 ) {
   if (coord_highlight>=0) {
   $(coord_list[coord_highlight].obj).css('background-color','yellow').find('span:visible').css('background-color','yellow');
   $(coord_list[coord_highlight].obj).css('background-color','yellow').find('span:visible').css('background-color','yellow');
   }
   }
Line 285: Line 285:
     // make a JSON encodable copy of coord_list (no HTML objects!)
     // make a JSON encodable copy of coord_list (no HTML objects!)
     // find center and extent
     // find center and extent
     for( i = 0; i < coord_list.length; ++i ) {
     for (i=0; i<coord_list.length; ++i) {
     clist.coords[i] = {
     clist.coords[i] = {
       lat: coord_list[i].lat,
       lat: coord_list[i].lat,
Line 291: Line 291:
       title: coord_list[i].title.replace(/[\+_]/g,' ')
       title: coord_list[i].title.replace(/[\+_]/g,' ')
     };
     };
     if( coord_list[i].lat < minlat ) { minlat = coord_list[i].lat; }
     if (coord_list[i].lat<minlat) { minlat = coord_list[i].lat; }
     if( coord_list[i].lat > maxlat ) { maxlat = coord_list[i].lat; }
     if (coord_list[i].lat>maxlat) { maxlat = coord_list[i].lat; }
     geoext[i] = {
     geoext[i] = {
       x: Math.cos(coord_list[i].lon/180.0*Math.PI),
       x: Math.cos(coord_list[i].lon/180.0*Math.PI),
Line 304: Line 304:
     clist.latmin = minlat;
     clist.latmin = minlat;
     // extent in longitude
     // extent in longitude
     for( i = 0; i < geoext.length; ++i ) {
     for (i=0; i<geoext.length; ++i) {
     s = (geoext[i].x*sy-geoext[i].y*sx);
     s = (geoext[i].x*sy-geoext[i].y*sx);
     geoext[i].z = (geoext[i].x*sx+geoext[i].y*sy);
     geoext[i].z = (geoext[i].x*sx+geoext[i].y*sy);
     if( s<0 && ( ineg<0 || geoext[i].z<geoext[ineg].z ) ) { ineg=i; }
     if (s<0 && (ineg<0 || geoext[i].z<geoext[ineg].z)) { ineg=i; }
     if( s>0 && ( ipos<0 || geoext[i].z<geoext[ipos].z ) ) { ipos=i; }
     if (s>0 && (ipos<0 || geoext[i].z<geoext[ipos].z)) { ipos=i; }
     }
     }
     if( ipos>=0 && ineg>=0 ) {
     if (ipos>=0 && ineg>=0) {
     clist.lonleft  = coord_list[ipos].lon;
     clist.lonleft  = coord_list[ipos].lon;
     clist.lonright = coord_list[ineg].lon;
     clist.lonright = coord_list[ineg].lon;
     }
     }
     if( typeof JSON !== "undefined" ) {
     if (typeof JSON !== 'undefined') {
     mes.postMessage( JSON.stringify(clist), document.location.protocol + wma_domain );
     mes.postMessage( JSON.stringify(clist), document.location.protocol + wma_domain );
     if( kml !== null ) {
     if (kml!==null) {
       mes.postMessage( JSON.stringify(kml), document.location.protocol + wma_domain );
       mes.postMessage( JSON.stringify(kml), document.location.protocol + wma_domain );
     }
     }
Line 342: Line 342:
  function parseParams(url) {
  function parseParams(url) {
   var map = {}, h, i, pair = url.substr(url.indexOf('?')+1).split('&');
   var map = {}, h, i, pair = url.substr(url.indexOf('?')+1).split('&');
   for( i=0; i<pair.length; ++i ) {
   for (i=0; i<pair.length; ++i) {
   h = pair[i].split('=');
   h = pair[i].split('=');
   map[h[0]] = h[1];
   map[h[0]] = h[1];
Line 357: Line 357:


  // apply settings
  // apply settings
  if( typeof(wma_settings) === 'object' ) {
  if (typeof wma_settings==='object') {
   for( key in wma_settings ) {
   for (key in wma_settings) {
   if( typeof(wma_settings[key]) === typeof(wc[key]) ) {
   if (typeof wma_settings[key]===typeof wc[key]) { wc[key] = wma_settings[key]; }
    wc[key] = wma_settings[key];
  }
   }
   }
  }
  }


  if( wc.enabled === false ) { return; }
  if (wc.enabled===false) { return; }


  site = ( dbName==="commonswiki" ) ? "commons" : mw.config.get( 'wgPageContentLanguage' );
  site = ( dbName==="commonswiki" ) ? "commons" : mw.config.get( 'wgPageContentLanguage' );
Line 380: Line 378:


   // check for timeout (every 10 links only)
   // check for timeout (every 10 links only)
   if( key % 10 === 9 && (new Date()).getTime() > startTime + wc.timeout ) {  
   if (key%10===9 && (new Date()).getTime() > startTime+wc.timeout) {  
   return false; // break out of each
   return false; // break out of each
   }
   }


   if( !('href' in link) || !coord_filter.exec(link.href) ){ // invalid links do not contain href attribute in IE!
   if (!('href' in link) || !coord_filter.exec(link.href)) { // invalid links do not contain href attribute in IE!
   return true;
   return true;
   }
   }
   marker.lat=(1.0*RegExp.$1) + ((RegExp.$2||0)/60.0) + ((RegExp.$3||0)/3600.0);
   marker.lat=(1.0*RegExp.$1) + ((RegExp.$2||0)/60.0) + ((RegExp.$3||0)/3600.0);
   if( RegExp.$4 !== 'N' ) { marker.lat*=-1; }
   if (RegExp.$4!=='N') { marker.lat*=-1; }
   marker.lon=(1.0*RegExp.$5) + ((RegExp.$6||0)/60.0) + ((RegExp.$7||0)/3600.0);
   marker.lon=(1.0*RegExp.$5) + ((RegExp.$6||0)/60.0) + ((RegExp.$7||0)/3600.0);
   if( RegExp.$8 === 'W' ) { marker.lon*=-1; }
   if (RegExp.$8==='W') { marker.lon*=-1; }
   coord_params = RegExp.$9;
   coord_params = RegExp.$9;


Line 400: Line 398:
   if( /_type:(airport|edu|pass|landmark|railwaystation)/.test(coord_params) ) {
   if( /_type:(airport|edu|pass|landmark|railwaystation)/.test(coord_params) ) {
   zoomlevel = 8;
   zoomlevel = 8;
   } else if( /_type:(event|forest|glacier)/.test(coord_params) ) {
   } else if (/_type:(event|forest|glacier)/.test(coord_params)) {
   zoomlevel = 6;
   zoomlevel = 6;
   } else if( /_type:(adm3rd|city|mountain|isle|river|waterbody)/.test(coord_params) ) {
   } else if (/_type:(adm3rd|city|mountain|isle|river|waterbody)/.test(coord_params)) {
   zoomlevel = 4;
   zoomlevel = 4;
   }
   }


   // wma shows dim approx 4e7m at zoom 0 or 1.5e8 is the scale of zoomlevel 0
   // wma shows dim approx 4e7m at zoom 0 or 1.5e8 is the scale of zoomlevel 0
   if( /_dim:([\d.+-]+)(km|m|_|$)/.exec(coord_params) ) {
   if (/_dim:([\d.+-]+)(km|m|_|$)/.exec(coord_params)) {
   zoomlevel = Math.log( ( RegExp.$2 === "km" ? 4e4 : 4e7 ) / RegExp.$1 ) / Math.log(2);
   zoomlevel = Math.log( ( RegExp.$2 === "km" ? 4e4 : 4e7 ) / RegExp.$1 ) / Math.log(2);
   }
   }
   if( /_scale:(\d+)(_|$)/.exec(coord_params) ) {
   if (/_scale:(\d+)(_|$)/.exec(coord_params)) {
   zoomlevel = Math.log( 1.5e8/RegExp.$1 ) / Math.log(2);
   zoomlevel = Math.log( 1.5e8/RegExp.$1 ) / Math.log(2);
   }
   }


   if( wc.zoom !== -1 ) { zoomlevel = wc.zoom; }
   if (wc.zoom!==-1) { zoomlevel = wc.zoom; }
   //if( zoomlevel > 12 ) { zoomlevel = 12; }
   //if( zoomlevel > 12 ) { zoomlevel = 12; }
   if( zoomlevel < 0 ) { zoomlevel = 0; }
   if (zoomlevel<0) { zoomlevel = 0; }


   function capitalize(s) { return s.substr(0,1).toUpperCase()+s.substr(1).toLowerCase(); }
   function capitalize(s) { return s.substr(0,1).toUpperCase()+s.substr(1).toLowerCase(); }
Line 423: Line 421:


   // Test the unicode Symbol
   // Test the unicode Symbol
   if( site === 'de' && link.parentNode.id !== 'coordinates' ) {
   if (site==='de' && link.parentNode.id!=='coordinates') {
   mapbutton = $('<span>♁</span>').css('color','blue');
   mapbutton = $('<span>♁</span>').css('color','blue');
   } else {
   } else {
Line 436: Line 434:
   .css('padding', rtl ? '0px 0px 0px 3px' : '0px 3px 0px 0px' ).css('cursor', 'pointer');
   .css('padding', rtl ? '0px 0px 0px 3px' : '0px 3px 0px 0px' ).css('cursor', 'pointer');


   if( wc.alwaysTooltips || ( wc.flowTextTooltips && $(link).parents('li, table, #coordinates').length===0 ) ) {
   if (wc.alwaysTooltips || ( wc.flowTextTooltips && $(link).parents('li, table, #coordinates').length===0)) {
   // insert tooltip rather than icon to improve text readability
   // insert tooltip rather than icon to improve text readability
   mapbutton = $('<span>').append(mapbutton).append("&nbsp;WikiMiniAtlas").css('cursor','pointer');
   mapbutton = $('<span>').append(mapbutton).append("&nbsp;WikiMiniAtlas").css('cursor','pointer');
Line 454: Line 452:
   // insert icon directly
   // insert icon directly
   ws = $(link).css('whiteSpace');
   ws = $(link).css('whiteSpace');
   if( site !== 'de' || link.parentNode.id !== 'coordinates' ) {
   if (site!=='de' || link.parentNode.id!=='coordinates') {
     $(link).wrap( $('<span/>').css('whiteSpace', 'nowrap') ).css('whiteSpace', ws).before(mapbutton);
     $(link).wrap( $('<span/>').css('whiteSpace', 'nowrap') ).css('whiteSpace', ws).before(mapbutton);
   } else {
   } else {
Line 485: Line 483:
   }, showIFrame ); // zoomlevel!
   }, showIFrame ); // zoomlevel!


   if(!titlebutton ) {  
   if (!titlebutton) {  
   if( $('#coordinates').length ) {
   if ($('#coordinates').length) {
     $('#coordinates').find('img').detach();
     $('#coordinates').find('img').detach();
     $('#coordinates').append(mapbutton);
     $('#coordinates').append(mapbutton);
Line 504: Line 502:
     ,lo1 = Infinity, lo2 =- Infinity
     ,lo1 = Infinity, lo2 =- Infinity
     ,ex,ey;
     ,ex,ey;
   for( i = 0; i < l.length; ++i ) {// TODO: replace with .each
 
   for (i=0; i<l.length; ++i) {// TODO: replace with .each
   coordinates = true;
   coordinates = true;
   $.ajax({
   $.ajax({
Line 520: Line 519:


         // determine extent of way
         // determine extent of way
         if( lat<la1 ) { la1=lat; }
         if (lat<la1) { la1=lat; }
         if( lon<lo1 ) { lo1=lon; }
         if (lon<lo1) { lo1=lon; }
         if( lat>la2 ) { la2=lat; }
         if (lat>la2) { la2=lat; }
         if( lon>lo2 ) { lo2=lon; }
         if (lon>lo2) { lo2=lon; }
       }
       }
       }
       }
Line 535: Line 534:
     $(xml).find('LineString > coordinates').each(function () {
     $(xml).find('LineString > coordinates').each(function () {
       var way = processCoords( $(this).text() );
       var way = processCoords( $(this).text() );
       if( way.length > 0 ) { kml.ways.push(way); }
       if (way.length>0) { kml.ways.push(way); }
     });
     });


Line 546: Line 545:
       $(this).find('outerBoundaryIs > LinearRing > coordinates').each(function () {
       $(this).find('outerBoundaryIs > LinearRing > coordinates').each(function () {
       var way = processCoords( $(this).text() );
       var way = processCoords( $(this).text() );
       if( way.length > 0 ) {
       if (way.length>0) {
         area.outer.push(way);
         area.outer.push(way);
       }
       }
Line 553: Line 552:
       // inner boundary (holes in the polygon)
       // inner boundary (holes in the polygon)
       $(this).find('innerBoundaryIs > LinearRing > coordinates').each(function () {
       $(this).find('innerBoundaryIs > LinearRing > coordinates').each(function () {
       var way = processCoords( $(this).text() );
       var way = processCoords($(this).text());
       if( way.length > 0 ) { area.inner.push(way); }
       if (way.length>0) { area.inner.push(way); }
       });
       });


       // only add if we have an outer boundary
       // only add if we have an outer boundary
       if( area.outer.length>0 ) { kml.areas.push(area); }
       if (area.outer.length>0) { kml.areas.push(area); }
     });
     });


Line 568: Line 567:


     // already got a request message
     // already got a request message
     if( mes !== null && kml.ways.length > 0 && typeof JSON !== "undefined" ) {
     if (mes!==null && kml.ways.length>0 && typeof JSON !== 'undefined') {
       mes.postMessage( JSON.stringify(kml), document.location.protocol + wma_domain );
       mes.postMessage(JSON.stringify(kml), document.location.protocol + wma_domain);
     }
     }


Line 596: Line 595:
  // detect "All Coordinates"
  // detect "All Coordinates"
  links = $('#coordinates>span>a');
  links = $('#coordinates>span>a');
  if (links.length>0 && links[0].href.substr(0,50)==="http://www.lenz-online.de/cgi-bin/wiki/wiki-osm.pl") {
  if (links.length>0 && links[0].href.substr(0,50)==='http://www.lenz-online.de/cgi-bin/wiki/wiki-osm.pl') {
   addTitleButton( 0, 0, 1 );
   addTitleButton( 0, 0, 1 );
   coordinates = true;
   coordinates = true;
Line 602: Line 601:


  // prepare iframe to house the map
  // prepare iframe to house the map
  if ( coordinates !== null ) {
  if (coordinates!==null) {
   wi.div = $('<div/>').css( {
   wi.div = $('<div/>').css( {
   width: (wc.width+2)+'px', height: (wc.height+2)+'px',
   width: (wc.width+2)+'px', height: (wc.height+2)+'px',
Line 653: Line 652:
     .mouseleave( function(e) { if( idle ) { wi.resizebutton.fadeOut(); } } )
     .mouseleave( function(e) { if( idle ) { wi.resizebutton.fadeOut(); } } )
     .mousedown( function(e) {
     .mousedown( function(e) {
       if( idle ) {  
       if (idle) {  
         wi.resizehelper.show();
         wi.resizehelper.show();
         adjusthelper();
         adjusthelper();
Anonymous user