| 1 |
(function ($, window, undefined) { |
| 2 |
$.geo._serviceTypes.tiled = (function () { |
| 3 |
var rTmplString = new RegExp( "<.*>|{{.*}}" ); |
| 4 |
|
| 5 |
return { |
| 6 |
_serviceId: 0, |
| 7 |
_tmplGeoSrcId: "", |
| 8 |
|
| 9 |
|
| 10 |
create: function (map, serviceContainer, service ) { |
| 11 |
this._serviceId = map._serviceIdSeed++; |
| 12 |
this._tmplGeoSrcId = "geoSrc" + this._serviceId; |
| 13 |
|
| 14 |
var serviceState = $.data(service, "geoServiceState"); |
| 15 |
|
| 16 |
if ( !serviceState ) { |
| 17 |
serviceState = { |
| 18 |
loadCount: 0, |
| 19 |
reloadTiles: false |
| 20 |
}; |
| 21 |
|
| 22 |
var scHtml = '<div data-geo-service="tiled" style="-webkit-transform:translateZ(0); position:absolute; left:0; top:0; width:8px; height:8px; margin:0; padding:0;"></div>'; |
| 23 |
|
| 24 |
serviceContainer.append( window.toStaticHTML( scHtml ) ); |
| 25 |
|
| 26 |
serviceState.serviceContainer = serviceContainer.children( ":last" ); |
| 27 |
|
| 28 |
$.data(service, "geoServiceState", serviceState); |
| 29 |
} |
| 30 |
|
| 31 |
return serviceState.serviceContainer; |
| 32 |
}, |
| 33 |
|
| 34 |
destroy: function (map, serviceContainer, service) { |
| 35 |
var serviceState = $.data(service, "geoServiceState"); |
| 36 |
|
| 37 |
serviceState.serviceContainer.remove(); |
| 38 |
|
| 39 |
$.removeData(service, "geoServiceState"); |
| 40 |
}, |
| 41 |
|
| 42 |
interactiveTransform: function ( map, service, center, pixelSize ) { |
| 43 |
|
| 44 |
var serviceState = $.data( service, "geoServiceState" ), |
| 45 |
tilingScheme = map.options[ "tilingScheme" ]; |
| 46 |
|
| 47 |
if ( serviceState ) { |
| 48 |
this._cancelUnloaded( map, service ); |
| 49 |
|
| 50 |
serviceState.serviceContainer.children( ).each( function ( ) { |
| 51 |
var $scaleContainer = $(this), |
| 52 |
scalePixelSize = $scaleContainer.data("pixelSize"), |
| 53 |
scaleRatio = scalePixelSize / pixelSize; |
| 54 |
|
| 55 |
if ( scalePixelSize > 0 ) { |
| 56 |
scaleRatio = Math.round(scaleRatio * 1000) / 1000; |
| 57 |
|
| 58 |
var oldMapCoord = $scaleContainer.data("scaleOrigin"), |
| 59 |
newPixelPoint = map._toPixel(oldMapCoord, center, pixelSize); |
| 60 |
|
| 61 |
$scaleContainer.css( { |
| 62 |
left: Math.round(newPixelPoint[0]) + "px", |
| 63 |
top: Math.round(newPixelPoint[1]) + "px", |
| 64 |
width: tilingScheme.tileWidth * scaleRatio, |
| 65 |
height: tilingScheme.tileHeight * scaleRatio |
| 66 |
} ); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
} |
| 76 |
}); |
| 77 |
} |
| 78 |
}, |
| 79 |
|
| 80 |
refresh: function (map, service, force) { |
| 81 |
|
| 82 |
var serviceState = $.data( service, "geoServiceState" ); |
| 83 |
|
| 84 |
this._cancelUnloaded(map, service); |
| 85 |
|
| 86 |
if ( serviceState && force ) { |
| 87 |
|
| 88 |
serviceState.reloadTiles = true; |
| 89 |
} |
| 90 |
|
| 91 |
if ( serviceState && service && service.style.visibility === "visible" && !( serviceState.serviceContainer.is( ":hidden" ) ) ) { |
| 92 |
var bbox = map._getBbox(), |
| 93 |
pixelSize = map._pixelSize, |
| 94 |
|
| 95 |
serviceObj = this, |
| 96 |
$serviceContainer = serviceState.serviceContainer, |
| 97 |
|
| 98 |
image = map.options[ "axisLayout" ] === "image", |
| 99 |
ySign = image ? +1 : -1, |
| 100 |
|
| 101 |
tilingScheme = map.options["tilingScheme"], |
| 102 |
tileWidth = tilingScheme.tileWidth, |
| 103 |
tileHeight = tilingScheme.tileHeight, |
| 104 |
|
| 105 |
tileX = Math.floor((bbox[0] - tilingScheme.origin[0]) / (pixelSize * tileWidth)), |
| 106 |
tileY = Math.max( Math.floor( ( image ? bbox[1] - tilingScheme.origin[1] : tilingScheme.origin[1] - bbox[ 3 ] ) / (pixelSize * tileHeight) ), 0 ), |
| 107 |
tileX2 = Math.ceil((bbox[2] - tilingScheme.origin[0]) / (pixelSize * tileWidth)), |
| 108 |
tileY2 = Math.ceil( ( image ? bbox[3] - tilingScheme.origin[1] : tilingScheme.origin[1] - bbox[ 1 ] ) / (pixelSize * tileHeight) ), |
| 109 |
|
| 110 |
bboxMax = map._getBboxMax(), |
| 111 |
pixelSizeAtZero = map._getPixelSize(0), |
| 112 |
ratio = pixelSizeAtZero / pixelSize, |
| 113 |
fullXAtScale = Math.floor((bboxMax[0] - tilingScheme.origin[0]) / (pixelSizeAtZero * tileWidth)) * ratio, |
| 114 |
fullYAtScale = Math.floor((tilingScheme.origin[1] + ySign * bboxMax[3]) / (pixelSizeAtZero * tileHeight)) * ratio, |
| 115 |
|
| 116 |
fullXMinX = tilingScheme.origin[0] + (fullXAtScale * tileWidth) * pixelSize, |
| 117 |
fullYMinOrMaxY = tilingScheme.origin[1] + ySign * (fullYAtScale * tileHeight) * pixelSize, |
| 118 |
|
| 119 |
serviceLeft = Math.round((fullXMinX - bbox[0]) / pixelSize), |
| 120 |
serviceTop = Math.round( ( image ? fullYMinOrMaxY - bbox[1] : bbox[3] - fullYMinOrMaxY ) / pixelSize), |
| 121 |
|
| 122 |
scaleContainers = $serviceContainer.children().show(), |
| 123 |
scaleContainer = scaleContainers.filter("[data-pixel-size='" + pixelSize + "']").appendTo($serviceContainer), |
| 124 |
|
| 125 |
opacity = service.style.opacity, |
| 126 |
|
| 127 |
x, y, |
| 128 |
|
| 129 |
loadImageDeferredDone = function( url ) { |
| 130 |
|
| 131 |
|
| 132 |
serviceObj._loadImage( this.img, url, pixelSize, map, serviceState, opacity ); |
| 133 |
}, |
| 134 |
|
| 135 |
loadImageDeferredFail = function( ) { |
| 136 |
delete this.img; |
| 137 |
serviceState.loadCount--; |
| 138 |
map._requestComplete(); |
| 139 |
}; |
| 140 |
|
| 141 |
if (serviceState.reloadTiles) { |
| 142 |
scaleContainers.find("img").attr("data-dirty", "true"); |
| 143 |
} |
| 144 |
|
| 145 |
if (!scaleContainer.length) { |
| 146 |
$serviceContainer.append( window.toStaticHTML( "<div style='-webkit-transform:translateZ(0);position:absolute; left:" + serviceLeft % tileWidth + "px; top:" + serviceTop % tileHeight + "px; width:" + tileWidth + "px; height:" + tileHeight + "px; margin:0; padding:0;' data-pixel-size='" + pixelSize + "'></div>" ) ); |
| 147 |
scaleContainer = $serviceContainer.children(":last").data("scaleOrigin", map._toMap( [ (serviceLeft % tileWidth), (serviceTop % tileHeight) ] ) ); |
| 148 |
} else { |
| 149 |
scaleContainer.css({ |
| 150 |
left: (serviceLeft % tileWidth) + "px", |
| 151 |
top: (serviceTop % tileHeight) + "px" |
| 152 |
}).data("scaleOrigin", map._toMap( [ (serviceLeft % tileWidth), (serviceTop % tileHeight) ] ) ); |
| 153 |
|
| 154 |
scaleContainer.children().each( function () { |
| 155 |
var $img = $(this), |
| 156 |
tile = $img.attr("data-tile").split(","); |
| 157 |
|
| 158 |
$img.css({ |
| 159 |
left: Math.round(((parseInt(tile[0], 10) - fullXAtScale) * 100) + (serviceLeft - (serviceLeft % tileWidth)) / tileWidth * 100) + "%", |
| 160 |
top: Math.round(((parseInt(tile[1], 10) - fullYAtScale) * 100) + (serviceTop - (serviceTop % tileHeight)) / tileHeight * 100) + "%" |
| 161 |
}); |
| 162 |
|
| 163 |
if (opacity < 1) { |
| 164 |
$img.fadeTo(0, opacity); |
| 165 |
} |
| 166 |
} ); |
| 167 |
} |
| 168 |
|
| 169 |
for (x = tileX; x < tileX2; x++) { |
| 170 |
for (y = tileY; y < tileY2; y++) { |
| 171 |
var tileStr = "" + x + "," + y, |
| 172 |
$img = scaleContainer.children("[data-tile='" + tileStr + "']").removeAttr("data-dirty"); |
| 173 |
|
| 174 |
if ($img.length === 0 || serviceState.reloadTiles) { |
| 175 |
var bottomLeft = [ |
| 176 |
tilingScheme.origin[0] + (x * tileWidth) * pixelSize, |
| 177 |
tilingScheme.origin[1] + ySign * (y * tileHeight) * pixelSize |
| 178 |
], |
| 179 |
|
| 180 |
topRight = [ |
| 181 |
tilingScheme.origin[0] + ((x + 1) * tileWidth - 1) * pixelSize, |
| 182 |
tilingScheme.origin[1] + ySign * ((y + 1) * tileHeight - 1) * pixelSize |
| 183 |
], |
| 184 |
|
| 185 |
tileBbox = [bottomLeft[0], bottomLeft[1], topRight[0], topRight[1]], |
| 186 |
|
| 187 |
urlArgs = { |
| 188 |
bbox: tileBbox, |
| 189 |
width: tileWidth, |
| 190 |
height: tileHeight, |
| 191 |
zoom: map._getZoom(), |
| 192 |
tile: { |
| 193 |
row: y, |
| 194 |
column: x |
| 195 |
}, |
| 196 |
index: Math.abs(y + x) |
| 197 |
}, |
| 198 |
isFunc = $.isFunction( service[ 'src' ] ), |
| 199 |
imageUrl; |
| 200 |
|
| 201 |
if ( isFunc ) { |
| 202 |
imageUrl = service[ 'src' ]( urlArgs ); |
| 203 |
} else { |
| 204 |
if ( rTmplString.test( service[ 'src' ] ) ) { |
| 205 |
$.templates( this._tmplGeoSrcId, service[ 'src' ] ); |
| 206 |
imageUrl = $.render[ this._tmplGeoSrcId ]( urlArgs ); |
| 207 |
} else { |
| 208 |
imageUrl = service[ 'src' ]; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
serviceState.loadCount++; |
| 213 |
map._requestQueued(); |
| 214 |
|
| 215 |
if (serviceState.reloadTiles && $img.length > 0) { |
| 216 |
$img.attr("src", imageUrl); |
| 217 |
} else { |
| 218 |
var imgMarkup = "<img style='-webkit-transform:translateZ(0);position:absolute; " + |
| 219 |
"left:" + (((x - fullXAtScale) * 100) + (serviceLeft - (serviceLeft % tileWidth)) / tileWidth * 100) + "%; " + |
| 220 |
"top:" + (((y - fullYAtScale) * 100) + (serviceTop - (serviceTop % tileHeight)) / tileHeight * 100) + "%; "; |
| 221 |
|
| 222 |
imgMarkup += "width: 100%; height: 100%;"; |
| 223 |
|
| 224 |
imgMarkup += "margin:0; padding:0; -khtml-user-select:none; -moz-user-select:none; -webkit-user-select:none; user-select:none; display:none;' unselectable='on' data-tile='" + tileStr + "' />"; |
| 225 |
|
| 226 |
scaleContainer.append( window.toStaticHTML( imgMarkup ) ); |
| 227 |
$img = scaleContainer.children(":last"); |
| 228 |
} |
| 229 |
|
| 230 |
if ( typeof imageUrl === "string" ) { |
| 231 |
serviceObj._loadImage( $img, imageUrl, pixelSize, map, serviceState, opacity ); |
| 232 |
} else if ( imageUrl && $.isFunction( imageUrl.promise ) ) { |
| 233 |
|
| 234 |
$.extend( imageUrl.promise( ), { img: $img } ); |
| 235 |
imageUrl.done( loadImageDeferredDone ).fail( loadImageDeferredFail ); |
| 236 |
} else { |
| 237 |
$img.remove( ); |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
scaleContainers.find("[data-dirty]").remove(); |
| 244 |
serviceState.reloadTiles = false; |
| 245 |
} |
| 246 |
}, |
| 247 |
|
| 248 |
resize: function ( ) { |
| 249 |
}, |
| 250 |
|
| 251 |
opacity: function ( map, service ) { |
| 252 |
var serviceState = $.data( service, "geoServiceState" ); |
| 253 |
serviceState.serviceContainer.find( "img" ).stop( true ).fadeTo( "fast", service.style.opacity ); |
| 254 |
}, |
| 255 |
|
| 256 |
toggle: function ( map, service ) { |
| 257 |
var serviceState = $.data( service, "geoServiceState" ); |
| 258 |
serviceState.serviceContainer.css( "display", service.style.visibility === "visible" ? "block" : "none" ); |
| 259 |
}, |
| 260 |
|
| 261 |
_cancelUnloaded: function (map, service) { |
| 262 |
var serviceState = $.data( service, "geoServiceState" ); |
| 263 |
|
| 264 |
if (serviceState && serviceState.loadCount > 0) { |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
serviceState.serviceContainer.find("img:hidden").remove(); |
| 276 |
while (serviceState.loadCount > 0) { |
| 277 |
serviceState.loadCount--; |
| 278 |
map._requestComplete(); |
| 279 |
} |
| 280 |
} |
| 281 |
}, |
| 282 |
|
| 283 |
_loadImage: function ( $img, url, pixelSize, map, serviceState, opacity ) { |
| 284 |
var serviceContainer = serviceState.serviceContainer; |
| 285 |
|
| 286 |
$img.on( 'load', function (e) { |
| 287 |
if (opacity < 1) { |
| 288 |
$(e.target).fadeTo(0, opacity); |
| 289 |
} else { |
| 290 |
$(e.target).show(); |
| 291 |
} |
| 292 |
|
| 293 |
serviceState.loadCount--; |
| 294 |
map._requestComplete(); |
| 295 |
|
| 296 |
if (serviceState.loadCount <= 0) { |
| 297 |
serviceContainer.children(":not([data-pixel-size='" + pixelSize + "'])").remove(); |
| 298 |
serviceState.loadCount = 0; |
| 299 |
} |
| 300 |
}).on( 'error', function (e) { |
| 301 |
$(e.target).remove(); |
| 302 |
serviceState.loadCount--; |
| 303 |
map._requestComplete(); |
| 304 |
|
| 305 |
if (serviceState.loadCount <= 0) { |
| 306 |
serviceContainer.children(":not([data-pixel-size='" + pixelSize + "'])").remove(); |
| 307 |
serviceState.loadCount = 0; |
| 308 |
} |
| 309 |
}).attr("src", url); |
| 310 |
} |
| 311 |
}; |
| 312 |
}()); |
| 313 |
}(jQuery, window)); |