| 1 |
(function ($, window, undefined) { |
| 2 |
$.geo._serviceTypes.shingled = (function () { |
| 3 |
var rTmplString = new RegExp( "<.*>|{{.*}}" ); |
| 4 |
|
| 5 |
return { |
| 6 |
_serviceId: 0, |
| 7 |
_tmplGeoSrcId: "", |
| 8 |
|
| 9 |
create: function ( map, serviceContainer, service ) { |
| 10 |
this._serviceId = map._serviceIdSeed++; |
| 11 |
this._tmplGeoSrcId = "geoSrc" + this._serviceId; |
| 12 |
|
| 13 |
var serviceState = $.data(service, "geoServiceState"); |
| 14 |
|
| 15 |
if ( !serviceState ) { |
| 16 |
serviceState = { |
| 17 |
loadCount: 0 |
| 18 |
}; |
| 19 |
|
| 20 |
var scHtml = '<div data-geo-service="shingled" style="-webkit-transform:translateZ(0);position:absolute; left:0; top:0; width:16px; height:16px; margin:0; padding:0;"></div>'; |
| 21 |
|
| 22 |
serviceContainer.append( window.toStaticHTML( scHtml ) ); |
| 23 |
|
| 24 |
serviceState.serviceContainer = serviceContainer.children(":last"); |
| 25 |
$.data(service, "geoServiceState", serviceState); |
| 26 |
} |
| 27 |
|
| 28 |
return serviceState.serviceContainer; |
| 29 |
}, |
| 30 |
|
| 31 |
destroy: function (map, serviceContainer, service) { |
| 32 |
var serviceState = $.data(service, "geoServiceState"); |
| 33 |
|
| 34 |
serviceState.serviceContainer.remove(); |
| 35 |
|
| 36 |
$.removeData(service, "geoServiceState"); |
| 37 |
}, |
| 38 |
|
| 39 |
interactiveTransform: function ( map, service, center, pixelSize ) { |
| 40 |
var serviceState = $.data( service, "geoServiceState" ), |
| 41 |
|
| 42 |
contentBounds = map._getContentBounds(), |
| 43 |
mapWidth = contentBounds[ "width" ], |
| 44 |
mapHeight = contentBounds[ "height" ]; |
| 45 |
|
| 46 |
if ( serviceState ) { |
| 47 |
this._cancelUnloaded( map, service ); |
| 48 |
|
| 49 |
serviceState.serviceContainer.children( ).each( function ( ) { |
| 50 |
var $scaleContainer = $(this), |
| 51 |
scalePixelSize = $scaleContainer.data( "pixelSize" ), |
| 52 |
scaleRatio = scalePixelSize / pixelSize; |
| 53 |
|
| 54 |
if ( scalePixelSize > 0 ) { |
| 55 |
scaleRatio = Math.round(scaleRatio * 1000) / 1000; |
| 56 |
|
| 57 |
var oldMapOrigin = $scaleContainer.data( "origin" ), |
| 58 |
newPixelPoint = map._toPixel( oldMapOrigin, center, pixelSize ); |
| 59 |
|
| 60 |
$scaleContainer.css( { |
| 61 |
left: Math.round( newPixelPoint[ 0 ] ), |
| 62 |
top: Math.round( newPixelPoint[ 1 ] ), |
| 63 |
width: mapWidth * scaleRatio, |
| 64 |
height: mapHeight * scaleRatio |
| 65 |
} ); |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
}, |
| 70 |
|
| 71 |
refresh: function (map, service, force) { |
| 72 |
var serviceState = $.data(service, "geoServiceState"); |
| 73 |
|
| 74 |
this._cancelUnloaded(map, service); |
| 75 |
|
| 76 |
if ( serviceState && service && service.style.visibility === "visible" && !( serviceState.serviceContainer.is( ":hidden" ) ) ) { |
| 77 |
|
| 78 |
var bbox = map._getBbox(), |
| 79 |
pixelSize = map._pixelSize, |
| 80 |
|
| 81 |
serviceObj = this, |
| 82 |
serviceContainer = serviceState.serviceContainer, |
| 83 |
|
| 84 |
contentBounds = map._getContentBounds(), |
| 85 |
mapWidth = contentBounds["width"], |
| 86 |
mapHeight = contentBounds["height"], |
| 87 |
|
| 88 |
scaleContainer = serviceContainer.children('[data-pixel-size="' + pixelSize + '"]'), |
| 89 |
|
| 90 |
opacity = service.style.opacity, |
| 91 |
|
| 92 |
$img; |
| 93 |
|
| 94 |
if (opacity < 1 || force || service.shinglesMax === 1) { |
| 95 |
serviceContainer.find("img").attr("data-keep-alive", "0"); |
| 96 |
} else if ( service.shinglesMax > 1 ) { |
| 97 |
serviceContainer.find("img").slice( 0, -( service.shinglesMax - 1 ) ).attr("data-keep-alive", "0"); |
| 98 |
} |
| 99 |
|
| 100 |
if ( !scaleContainer.length ) { |
| 101 |
serviceContainer.append( window.toStaticHTML( '<div style="-webkit-transform:translateZ(0);position:absolute; left:0px; top: 0px; width:' + mapWidth + 'px; height:' + mapHeight + 'px; margin:0; padding:0;" data-pixel-size="' + pixelSize + '" data-origin="[' + map._toMap( [ 0, 0 ] ) + ']"></div>' ) ); |
| 102 |
scaleContainer = serviceContainer.children(":last"); |
| 103 |
} |
| 104 |
|
| 105 |
var urlArgs = { |
| 106 |
bbox: bbox, |
| 107 |
width: mapWidth, |
| 108 |
height: mapHeight, |
| 109 |
zoom: map._getZoom(), |
| 110 |
tile: null, |
| 111 |
index: 0 |
| 112 |
}, |
| 113 |
isFunc = $.isFunction( service[ 'src' ] ), |
| 114 |
imageUrl, |
| 115 |
imagePos = scaleContainer.position( ); |
| 116 |
|
| 117 |
imagePos.left = - ( imagePos.left ); |
| 118 |
imagePos.top = - ( imagePos.top ); |
| 119 |
|
| 120 |
if ( isFunc ) { |
| 121 |
imageUrl = service[ 'src' ]( urlArgs ); |
| 122 |
} else { |
| 123 |
if ( rTmplString.test( service[ 'src' ] ) ) { |
| 124 |
$.templates( this._tmplGeoSrcId, service[ 'src' ] ); |
| 125 |
imageUrl = $.render[ this._tmplGeoSrcId ]( urlArgs ); |
| 126 |
} else { |
| 127 |
imageUrl = service[ 'src' ]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
serviceState.loadCount++; |
| 132 |
map._requestQueued(); |
| 133 |
|
| 134 |
scaleContainer.append( window.toStaticHTML( '<img style="-webkit-transform:translateZ(0);position:absolute; left:' + ( imagePos.left / scaleContainer.width( ) * 100 ) + '%; top:' + ( imagePos.top / scaleContainer.height( ) * 100 ) + '%; width:100%; height:100%; margin:0; padding:0; -khtml-user-select:none; -moz-user-select:none; -webkit-user-select:none; user-select:none; display:none;" unselectable="on" />' ) ); |
| 135 |
$img = scaleContainer.children(":last").data("center", map._center); |
| 136 |
|
| 137 |
if ( typeof imageUrl === "string" ) { |
| 138 |
serviceObj._loadImage( $img, imageUrl, pixelSize, map, serviceState, opacity ); |
| 139 |
} else if ( imageUrl ) { |
| 140 |
|
| 141 |
imageUrl.done( function( url ) { |
| 142 |
serviceObj._loadImage( $img, url, pixelSize, map, serviceState, opacity ); |
| 143 |
} ).fail( function( ) { |
| 144 |
$img.remove( ); |
| 145 |
serviceState.loadCount--; |
| 146 |
map._requestComplete(); |
| 147 |
} ); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
}, |
| 152 |
|
| 153 |
resize: function ( map, service ) { |
| 154 |
var serviceState = $.data(service, "geoServiceState"); |
| 155 |
|
| 156 |
if ( serviceState && service && service.style.visibility === "visible" ) { |
| 157 |
this._cancelUnloaded(map, service); |
| 158 |
|
| 159 |
var serviceContainer = serviceState.serviceContainer, |
| 160 |
|
| 161 |
contentBounds = map._getContentBounds(), |
| 162 |
mapWidth = contentBounds["width"], |
| 163 |
mapHeight = contentBounds["height"], |
| 164 |
|
| 165 |
scaleContainers = serviceContainer.children(); |
| 166 |
|
| 167 |
scaleContainers.attr("data-pixel-size", "0"); |
| 168 |
|
| 169 |
scaleContainers.each( function ( ) { |
| 170 |
var $scaleContainer = $(this), |
| 171 |
position = $scaleContainer.position( ); |
| 172 |
|
| 173 |
$scaleContainer.css( { |
| 174 |
left: position.left + ( mapWidth - $scaleContainer.width( ) ) / 2, |
| 175 |
top: position.top + ( mapHeight - $scaleContainer.height( ) ) / 2 |
| 176 |
} ); |
| 177 |
|
| 178 |
} ); |
| 179 |
} |
| 180 |
}, |
| 181 |
|
| 182 |
opacity: function ( map, service ) { |
| 183 |
var serviceState = $.data( service, "geoServiceState" ); |
| 184 |
serviceState.serviceContainer.find( "img" ).stop( true ).fadeTo( "fast", service.style.opacity ); |
| 185 |
}, |
| 186 |
|
| 187 |
toggle: function (map, service) { |
| 188 |
var serviceState = $.data(service, "geoServiceState"); |
| 189 |
serviceState.serviceContainer.css("display", service.style.visibility === "visible" ? "block" : "none"); |
| 190 |
}, |
| 191 |
|
| 192 |
_cancelUnloaded: function (map, service) { |
| 193 |
var serviceState = $.data(service, "geoServiceState"); |
| 194 |
|
| 195 |
if (serviceState && serviceState.loadCount > 0) { |
| 196 |
serviceState.serviceContainer.find("img:hidden").remove(); |
| 197 |
while (serviceState.loadCount > 0) { |
| 198 |
serviceState.loadCount--; |
| 199 |
map._requestComplete(); |
| 200 |
} |
| 201 |
} |
| 202 |
}, |
| 203 |
|
| 204 |
_loadImage: function ( $img, url, pixelSize, map, serviceState, opacity ) { |
| 205 |
var serviceContainer = serviceState.serviceContainer; |
| 206 |
|
| 207 |
$img.on( 'load', function (e) { |
| 208 |
if ( !$.contains(document.body, e.target.jquery ? e.target[0] : e.target) ) { |
| 209 |
|
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
if (opacity < 1) { |
| 214 |
$(e.target).fadeTo(0, opacity); |
| 215 |
} else { |
| 216 |
$(e.target).show(); |
| 217 |
} |
| 218 |
|
| 219 |
serviceState.loadCount--; |
| 220 |
map._requestComplete(); |
| 221 |
|
| 222 |
if (serviceState.loadCount <= 0) { |
| 223 |
|
| 224 |
serviceContainer.children(':not([data-pixel-size="' + pixelSize + '"])').remove(); |
| 225 |
|
| 226 |
serviceContainer.find( "img[data-keep-alive]" ).remove( ); |
| 227 |
|
| 228 |
serviceState.loadCount = 0; |
| 229 |
} |
| 230 |
}).on( 'error', function (e) { |
| 231 |
if ( !$.contains(document.body, e.target.jquery ? e.target[0] : e.target) ) { |
| 232 |
|
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
$(e.target).remove(); |
| 237 |
serviceState.loadCount--; |
| 238 |
map._requestComplete(); |
| 239 |
|
| 240 |
if (serviceState.loadCount <= 0) { |
| 241 |
serviceContainer.children(":not([data-pixel-size='" + pixelSize + "'])").remove(); |
| 242 |
serviceState.loadCount = 0; |
| 243 |
} |
| 244 |
}).attr("src", url); |
| 245 |
} |
| 246 |
}; |
| 247 |
}()); |
| 248 |
}(jQuery, window)); |