1 /*************************************** 2 * Copyright 2011, 2012 GlobWeb contributors. 3 * 4 * This file is part of GlobWeb. 5 * 6 * GlobWeb is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * GlobWeb is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>. 18 ***************************************/ 19 20 define(['./Tile'], function(Tile) { 21 22 /**************************************************************************************************************/ 23 24 /** @constructor 25 TileRequest constructor 26 */ 27 var TileRequest = function(tileManager) 28 { 29 this.tile = null; 30 this.imageLoaded = false; 31 this.elevationLoaded = true; 32 this.tileManager = tileManager; 33 34 var that = this; 35 36 this.image = new Image(); 37 this.image.crossOrigin = ''; 38 this.image.onload = function() { that.handleLoadedImage(); }; 39 this.image.onerror = function() { that.handleErrorImage(); }; 40 this.image.onabort = function() { that.handleAbort(); }; 41 42 this.xhr = new XMLHttpRequest(); 43 this.xhr.onreadystatechange = function(e) 44 { 45 if ( that.xhr.readyState == 4 ) 46 { 47 if ( that.xhr.status == 200 ) 48 { 49 that.handleLoadedElevation(); 50 } 51 else 52 { 53 that.handleErrorElevation(); 54 } 55 } 56 }; 57 } 58 59 /**************************************************************************************************************/ 60 61 /** 62 Handle when image is loaded 63 */ 64 TileRequest.prototype.handleLoadedImage = function() 65 { 66 this.imageLoaded = true; 67 if ( this.elevationLoaded ) 68 { 69 this.tileManager.completedRequests.push(this); 70 this.tileManager.renderContext.requestFrame(); 71 } 72 } 73 74 /**************************************************************************************************************/ 75 76 /** 77 Handle when loading image failed 78 */ 79 TileRequest.prototype.handleErrorImage = function() 80 { 81 console.log( "Error while loading " + this.image.src ); 82 this.tile.state = Tile.State.ERROR; 83 this.tileManager.availableRequests.push(this); 84 } 85 86 /**************************************************************************************************************/ 87 88 /** 89 Abort request 90 */ 91 TileRequest.prototype.handleAbort = function() 92 { 93 this.tile.state = Tile.State.NONE; 94 this.tileManager.availableRequests.push(this); 95 } 96 97 /**************************************************************************************************************/ 98 99 /** 100 Handle when elevation is loaded 101 */ 102 TileRequest.prototype.handleLoadedElevation = function() 103 { 104 this.elevations = this.xhr.responseText; 105 106 this.elevationLoaded = true; 107 108 if ( this.imageLoaded ) 109 { 110 this.tileManager.completedRequests.push(this); 111 this.tileManager.renderContext.requestFrame(); 112 } 113 } 114 115 /**************************************************************************************************************/ 116 117 /** 118 Handle when loading elevation failed 119 */ 120 TileRequest.prototype.handleErrorElevation = function() 121 { 122 this.elevations = null; 123 this.elevationLoaded = true; 124 125 if ( this.imageLoaded ) 126 { 127 this.tileManager.completedRequests.push(this); 128 this.tileManager.renderContext.requestFrame(); 129 } 130 } 131 132 /**************************************************************************************************************/ 133 134 /** 135 Launch the HTTP request for a tile 136 */ 137 TileRequest.prototype.launch = function(tile) 138 { 139 this.tile = tile; 140 141 // Request the elevation if needed 142 if ( this.tileManager.elevationProvider ) 143 { 144 this.elevationLoaded = false; 145 this.xhr.open("GET", this.tileManager.elevationProvider.getUrl(tile) ); 146 this.xhr.send(); 147 } 148 else 149 { 150 this.elevationLoaded = true; 151 } 152 this.imageLoaded = false; 153 this.image.src = this.tileManager.imageryProvider.getUrl(tile); 154 } 155 156 /**************************************************************************************************************/ 157 158 return TileRequest; 159 160 }); 161