feat(url_resolver): support package: urls (fixes #2991)

This commit is contained in:
Yegor
2015-07-23 00:00:14 -07:00
committed by yjbanov
parent 771c0170d9
commit 408618b836
19 changed files with 61 additions and 75 deletions

View File

@ -0,0 +1,23 @@
library angular2_examples.material.demo_common;
import 'package:angular2/src/dom/browser_adapter.dart';
import 'package:angular2/src/services/url_resolver.dart';
void commonDemoSetup() {
BrowserDomAdapter.makeCurrent();
}
class DemoUrlResolver extends UrlResolver {
@override
String resolve(String baseUrl, String url) {
const MATERIAL_PKG = 'package:angular2_material/';
// We run a proxy server in front of pub serve that prepends "example" to
// paths
if (url.startsWith(MATERIAL_PKG)) {
return '/examples/packages/angular2_material/' + url.substring(MATERIAL_PKG.length);
}
return super.resolve(baseUrl, url);
}
}

View File

@ -18,67 +18,16 @@ export function commonDemoSetup(): void {
@Injectable()
export class DemoUrlResolver extends UrlResolver {
static a;
isInPubServe: boolean;
constructor() {
super();
if (isBlank(DemoUrlResolver.a)) {
DemoUrlResolver.a = DOM.createElement('a');
}
this.isInPubServe = _isInPubServe();
}
resolve(baseUrl: string, url: string): string {
if (isBlank(baseUrl)) {
DOM.resolveAndSetHref(DemoUrlResolver.a, url, null);
return DOM.getHref(DemoUrlResolver.a);
}
if (isBlank(url) || url == '') {
return baseUrl;
}
if (url[0] == '/') {
return url;
}
var m = RegExpWrapper.firstMatch(_schemeRe, url);
if (isPresent(m[1])) {
return url;
}
if (StringWrapper.startsWith(url, './')) {
return `${baseUrl}/${url}`;
}
// Whether the `examples/` dir is being directly served (as the server root).
// For cases when this is not true AND we're in pub-serve, `examples/` needs to be
// prepended to the URL.
var isDirectlyServingExamplesDir = !StringWrapper.contains(baseUrl, 'examples/');
if (this.isInPubServe && isDirectlyServingExamplesDir) {
return `/packages/${url}`;
} else if (this.isInPubServe) {
return `/examples/packages/${url}`;
} else {
return `/${url}`;
// The standard UrlResolver looks for "package:" templateUrls in
// node_modules, however in our repo we host material widgets at the root.
if (url.startsWith('package:angular2_material/')) {
return '/' + url.substring(8);
}
}
}
var _schemeRe = /^([^:/?#]+:)?/g;
// TODO: remove this hack when http://dartbug.com/23128 is fixed
function _isInPubServe(): boolean {
try {
int.parse('123');
print('>> Running in Dart');
return true;
} catch (_) {
print('>> Running in JS');
return false;
return super.resolve(baseUrl, url);
}
}

View File

@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/input/input.css";</style>
<style>@import "package:angular2_material/src/components/input/input.css";</style>
<style>
body {