refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {ɵglobal as global} from '@angular/core';
|
||||
|
||||
/**
|
||||
* An implementation of ResourceLoader that uses a template cache to avoid doing an actual
|
||||
* ResourceLoader.
|
||||
*
|
||||
* The template cache needs to be built and loaded into window.$templateCache
|
||||
* via a separate mechanism.
|
||||
*/
|
||||
export class CachedResourceLoader extends ResourceLoader {
|
||||
private _cache: {[url: string]: string};
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this._cache = (<any>global).$templateCache;
|
||||
if (this._cache == null) {
|
||||
throw new Error('CachedResourceLoader: Template cache was not found in $templateCache.');
|
||||
}
|
||||
}
|
||||
|
||||
get(url: string): Promise<string> {
|
||||
if (this._cache.hasOwnProperty(url)) {
|
||||
return Promise.resolve(this._cache[url]);
|
||||
} else {
|
||||
return <Promise<any>>Promise.reject(
|
||||
'CachedResourceLoader: Did not find cached template for ' + url);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
import {ResourceLoader} from '@angular/compiler';
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class ResourceLoaderImpl extends ResourceLoader {
|
||||
get(url: string): Promise<string> {
|
||||
let resolve: (result: any) => void;
|
||||
let reject: (error: any) => void;
|
||||
const promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('GET', url, true);
|
||||
xhr.responseType = 'text';
|
||||
|
||||
xhr.onload = function() {
|
||||
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
|
||||
// response/responseType properties were introduced in ResourceLoader Level2 spec (supported
|
||||
// by IE10)
|
||||
const response = xhr.response || xhr.responseText;
|
||||
|
||||
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
||||
let status = xhr.status === 1223 ? 204 : xhr.status;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
if (200 <= status && status <= 300) {
|
||||
resolve(response);
|
||||
} else {
|
||||
reject(`Failed to load ${url}`);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = function() { reject(`Failed to load ${url}`); };
|
||||
|
||||
xhr.send();
|
||||
return promise;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user