]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
fixed style issues. cached options.target, cached length in a for loop, lots of style... t571
authorChristopher De Cairos <chris.decairos@gmail.com>
Mon, 11 Jul 2011 15:03:12 +0000 (11:03 -0400)
committerChristopher De Cairos <chris.decairos@gmail.com>
Mon, 11 Jul 2011 15:03:12 +0000 (11:03 -0400)
plugins/openmap/popcorn.openmap.js

index d36f4b3e39ab4f9147c2807f116758a1121b9564..4a782e4a1715ef3c54c5b16a0963c13710a4bcfb 100755 (executable)
           target: 'map',
           lat: 43.665429,
           lng: -79.403323
-        } )
+        })
    *
    */
   var newdiv,
-      i = 1,
-      _mapFired = false,
-      _mapLoaded = false;
+      i = 1;
 
   // insert openlayers api script once
-  if ( !_mapFired ) {
-    _mapFired = true;
-    Popcorn.getScript( "http://openlayers.org/api/OpenLayers.js",
-    function() {
-      _mapLoaded = true;
-    });
+  if ( !window.OpenLayers ) {
+    Popcorn.getScript( "http://openlayers.org/api/OpenLayers.js", function() {});
   }
 
   function toggle( container, display ) {
@@ -61,7 +55,7 @@
     }, 10 );
   }
 
-  Popcorn.plugin( "openmap" , function( options ){
+  Popcorn.plugin( "openmap", function( options ){
     var newdiv,
         centerlonlat,
         projection,
         pointLayer,
         selectControl,
         popup,
-        isGeoReady;
+        isGeoReady,
+        target;
+        
+    target = document.getElementById( options.target );
 
     // create a new div within the target div
     // this is later passed on to the maps api
-    newdiv               = document.createElement( 'div' );
-    newdiv.id            = "openmapdiv" + i;
-    newdiv.style.width   = "100%";
-    newdiv.style.height  = "100%";
+    newdiv = document.createElement( "div" );
+    newdiv.id = "openmapdiv" + i;
+    newdiv.style.width = "100%";
+    newdiv.style.height = "100%";
     i++;
 
-    document.getElementById( options.target ) && document.getElementById( options.target ).appendChild( newdiv );
+    target && target.appendChild( newdiv );
 
     // callback function fires when the script is run
     isGeoReady = function() {
-      if ( !_mapLoaded ) {
-        setTimeout( function () {
+      if ( !window.OpenLayers ) {
+        setTimeout(function() {
           isGeoReady();
         }, 50);
       } else {
-        if( options.location ){
+        if ( options.location ) {
           // set a dummy center at start
           location = new OpenLayers.LonLat( 0, 0 );
           // query TinyGeocoder and re-center in callback
           centerlonlat = new OpenLayers.LonLat( options.lng, options.lat );
         }
         options.type = options.type || "ROADMAP";
-        if( options.type === "SATELLITE" ) {
+        if ( options.type === "SATELLITE" ) {
           // add NASA WorldWind / LANDSAT map
           options.map = new OpenLayers.Map( { div: newdiv, "maxResolution": 0.28125, tileSize: new OpenLayers.Size( 512, 512 ) } );
           var worldwind = new OpenLayers.Layer.WorldWind( "LANDSAT", "http://worldwind25.arc.nasa.gov/tile/tile.aspx", 2.25, 4, { T: "105" } );
           displayProjection = new OpenLayers.Projection( "EPSG:4326" );
           projection = new OpenLayers.Projection( "EPSG:4326" );
         }
-        else if( options.type === "TERRAIN" ) {
+        else if ( options.type === "TERRAIN" ) {
           // add terrain map ( USGS )
           displayProjection = new OpenLayers.Projection( "EPSG:4326" );
           projection = new OpenLayers.Projection( "EPSG:4326" );
           options.map = new OpenLayers.Map( {div: newdiv, projection: projection } );
           var relief = new OpenLayers.Layer.WMS( "USGS Terraserver", "http://terraserver-usa.org/ogcmap.ashx?", { layers: 'DRG' } ); 
           options.map.addLayer( relief );
-        }
-        else {
+        } else {
           // add OpenStreetMap layer
           projection = new OpenLayers.Projection( 'EPSG:900913' );
           displayProjection = new OpenLayers.Projection( 'EPSG:4326' );
           var osm = new OpenLayers.Layer.OSM();
           options.map.addLayer( osm );
         }
-        if( options.map ) {
+        if ( options.map ) {
           options.map.div.style.display = "none";
         }
       }
        */
       _setup: function( options ) {
       
-        var isReady = function () {
+        var isReady = function() {
           // wait until OpenLayers has been loaded, and the start function is run, before adding map
           if ( !options.map ) {
-            setTimeout( function () {
+            setTimeout(function() {
               isReady();
             }, 13 );
           } else {
 
             // reset the location and zoom just in case the user played with the map
             options.map.setCenter( centerlonlat, options.zoom );
-            if( options.markers ){
+            if ( options.markers ) {
               var layerStyle = OpenLayers.Util.extend( {} , OpenLayers.Feature.Vector.style[ 'default' ] ),
                   featureSelected = function( clickInfo ) {
                     clickedFeature = clickInfo.feature;
-                    if( !clickedFeature.attributes.text ){
+                    if ( !clickedFeature.attributes.text ) {
                       return;
                     }
                     popup = new OpenLayers.Popup.FramedCloud(
                       feature.popup = null;
                     }
                   },
-                  gcThenPlotMarker = function( myMarker ){
+                  gcThenPlotMarker = function( myMarker ) {
                     Popcorn.getJSONP(
                       "http://tinygeocoder.com/create-api.php?q=" + myMarker.location + "&callback=jsonp",
-                      function( latlng ){
+                      function( latlng ) {
                         var myPoint = new OpenLayers.Geometry.Point( latlng[1], latlng[0] ).transform( displayProjection, projection ),
                             myPointStyle = OpenLayers.Util.extend( {}, layerStyle );
-                        if( !myMarker.size || isNaN( myMarker.size ) ) {
+                        if ( !myMarker.size || isNaN( myMarker.size ) ) {
                           myMarker.size = 14;
                         }
                         myPointStyle.pointRadius = myMarker.size;
                         myPointStyle.graphicOpacity = 1;
                         myPointStyle.externalGraphic = myMarker.icon;
                         var myPointFeature = new OpenLayers.Feature.Vector( myPoint, null, myPointStyle );
-                        if( myMarker.text ) {
+                        if ( myMarker.text ) {
                           myPointFeature.attributes = { 
                             text: myMarker.text
                           };
                   };
               pointLayer = new OpenLayers.Layer.Vector( "Point Layer", { style: layerStyle } );
               options.map.addLayer( pointLayer ); 
-              for( var m = 0; m < options.markers.length; m++ ) {
+              for ( var m = 0, l = options.markers.length; m < l ; m++ ) {
                 var myMarker = options.markers[ m ];
                 if( myMarker.text ){
                   if( !selectControl ){
                     selectControl = new OpenLayers.Control.SelectFeature( pointLayer );
                     options.map.addControl( selectControl );
                     selectControl.activate();
-                    pointLayer.events.on( {
+                    pointLayer.events.on({
                       "featureselected": featureSelected,
                       "featureunselected": featureUnSelected
-                    } );
+                    });
                   }
                 }
-                if( myMarker.location ){
+                if ( myMarker.location ) {
                   var geocodeThenPlotMarker = gcThenPlotMarker;
                   geocodeThenPlotMarker(myMarker);
                 } else {
                   var myPoint = new OpenLayers.Geometry.Point( myMarker.lng, myMarker.lat ).transform( displayProjection, projection ),
                       myPointStyle = OpenLayers.Util.extend( {}, layerStyle );
-                  if( !myMarker.size || isNaN( myMarker.size ) ) {
+                  if ( !myMarker.size || isNaN( myMarker.size ) ) {
                     myMarker.size = 14;
                   }
                   myPointStyle.pointRadius = myMarker.size;
                   myPointStyle.graphicOpacity = 1;
                   myPointStyle.externalGraphic = myMarker.icon;
                   var myPointFeature = new OpenLayers.Feature.Vector( myPoint, null, myPointStyle );
-                  if( myMarker.text ) {
+                  if ( myMarker.text ) {
                     myPointFeature.attributes = { 
                       text: myMarker.text
                     };
       
       _teardown: function( options ) {
 
-        document.getElementById( options.target ) && document.getElementById( options.target ).removeChild( newdiv );
+        target && target.removeChild( newdiv );
         newdiv = map = centerlonlat = projection = displayProjection = pointLayer = selectControl = popup = null;
       }
     };
       location : { elem: 'input', type: 'text', label: 'Location'},
       markers  : { elem: 'input', type: 'text', label: 'List Markers'}
     }
-  } );
-} ) ( Popcorn );
+  });
+}) ( Popcorn );