feat(url_resolver): support package: urls (fixes #2991)
This commit is contained in:
23
modules/examples/src/material/demo_common.dart
Normal file
23
modules/examples/src/material/demo_common.dart
Normal 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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Reference in New Issue
Block a user