| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  | import {ResourceLoader} from '@angular/compiler'; | 
					
						
							| 
									
										
										
										
											2016-07-11 17:19:18 -07:00
										 |  |  | import {Injectable} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-11 17:19:18 -07:00
										 |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  | export class ResourceLoaderImpl extends ResourceLoader { | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |   get(url: string): Promise<string> { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let resolve: (result: any) => void; | 
					
						
							|  |  |  |     let reject: (error: any) => void; | 
					
						
							| 
									
										
										
										
											2017-08-02 16:30:50 -07:00
										 |  |  |     const promise = new Promise<string>((res, rej) => { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       resolve = res; | 
					
						
							|  |  |  |       reject = rej; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const xhr = new XMLHttpRequest(); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |     xhr.open('GET', url, true); | 
					
						
							|  |  |  |     xhr.responseType = 'text'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     xhr.onload = function() { | 
					
						
							| 
									
										
										
										
											2015-06-16 23:15:26 -07:00
										 |  |  |       // responseText is the old-school way of retrieving response (supported by IE8 & 9)
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |       // response/responseType properties were introduced in ResourceLoader Level2 spec (supported
 | 
					
						
							|  |  |  |       // by IE10)
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       const response = xhr.response || xhr.responseText; | 
					
						
							| 
									
										
										
										
											2015-06-16 23:15:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       let status = xhr.status === 1223 ? 204 : xhr.status; | 
					
						
							| 
									
										
										
										
											2015-06-16 23:15:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // fix status code when it is 0 (0 status is undocumented).
 | 
					
						
							|  |  |  |       // Occurs when accessing file resources or on Android 4.1 stock browser
 | 
					
						
							|  |  |  |       // while retrieving files from application cache.
 | 
					
						
							|  |  |  |       if (status === 0) { | 
					
						
							|  |  |  |         status = response ? 200 : 0; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |       if (200 <= status && status <= 300) { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |         resolve(response); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |         reject(`Failed to load ${url}`); | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |     xhr.onerror = function() { reject(`Failed to load ${url}`); }; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     xhr.send(); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |     return promise; | 
					
						
							| 
									
										
										
										
											2015-01-30 09:43:21 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | } |