2015-05-18 11:57:20 -07:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {isBlank, isPresent, BaseException, stringify} from 'angular2/src/facade/lang';
|
2015-06-10 14:40:24 +02:00
|
|
|
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
|
|
|
|
2015-06-09 10:21:25 -07:00
|
|
|
import {XHR} from 'angular2/src/render/xhr';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-04-09 21:20:11 +02:00
|
|
|
import {ViewDefinition} from '../../api';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {UrlResolver} from 'angular2/src/services/url_resolver';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strategy to load component templates.
|
2015-04-13 21:00:52 -07:00
|
|
|
* TODO: Make public API once we are more confident in this approach.
|
2015-03-23 14:10:55 -07:00
|
|
|
*/
|
2015-04-02 14:40:49 -07:00
|
|
|
@Injectable()
|
2015-03-23 14:10:55 -07:00
|
|
|
export class TemplateLoader {
|
2015-06-10 14:40:24 +02:00
|
|
|
_cache: Map<string, Promise<string>> = MapWrapper.create();
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-06-10 14:40:24 +02:00
|
|
|
constructor(private _xhr: XHR, urlResolver: UrlResolver) {}
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-06-10 14:40:24 +02:00
|
|
|
load(view: ViewDefinition): Promise</*element*/ any> {
|
|
|
|
let html;
|
|
|
|
let fetchedStyles;
|
|
|
|
|
|
|
|
// Load the HTML
|
|
|
|
if (isPresent(view.template)) {
|
|
|
|
html = PromiseWrapper.resolve(view.template);
|
|
|
|
} else if (isPresent(view.templateAbsUrl)) {
|
|
|
|
html = this._loadText(view.templateAbsUrl);
|
|
|
|
} else {
|
|
|
|
throw new BaseException('View should have either the templateUrl or template property set');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the styles
|
|
|
|
if (isPresent(view.styleAbsUrls) && view.styleAbsUrls.length > 0) {
|
|
|
|
fetchedStyles = ListWrapper.map(view.styleAbsUrls, url => this._loadText(url));
|
|
|
|
} else {
|
|
|
|
fetchedStyles = [];
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
2015-06-10 14:40:24 +02:00
|
|
|
|
|
|
|
// Inline the styles and return a template element
|
|
|
|
return PromiseWrapper.all(ListWrapper.concat([html], fetchedStyles))
|
|
|
|
.then((res: List<string>) => {
|
|
|
|
let html = res[0];
|
|
|
|
let fetchedStyles = ListWrapper.slice(res, 1);
|
|
|
|
|
|
|
|
html = _createStyleTags(view.styles) + _createStyleTags(fetchedStyles) + html;
|
|
|
|
|
|
|
|
return DOM.createTemplate(html);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _loadText(url: string): Promise<string> {
|
|
|
|
var response = MapWrapper.get(this._cache, url);
|
|
|
|
|
|
|
|
if (isBlank(response)) {
|
|
|
|
// TODO(vicb): change error when TS gets fixed
|
|
|
|
// https://github.com/angular/angular/issues/2280
|
|
|
|
// throw new BaseException(`Failed to fetch url "${url}"`);
|
|
|
|
response = PromiseWrapper.catchError(
|
|
|
|
this._xhr.get(url),
|
|
|
|
_ => PromiseWrapper.reject(new BaseException(`Failed to fetch url "${url}"`), null));
|
|
|
|
|
|
|
|
MapWrapper.set(this._cache, url, response);
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
|
2015-06-10 14:40:24 +02:00
|
|
|
return response;
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 14:40:24 +02:00
|
|
|
|
|
|
|
function _createStyleTags(styles?: List<string>): string {
|
|
|
|
return isBlank(styles) ?
|
|
|
|
'' :
|
|
|
|
ListWrapper.map(styles, css => `<style type='text/css'>${css}</style>`).join('');
|
|
|
|
}
|