Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/angular2/src/services/url_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class UrlResolver {
*/
String resolve(String baseUrl, String url) {
Uri uri = Uri.parse(url);

if (uri.scheme == 'package') {
return '/packages/${uri.path}';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yjbanov @sigmundch Bit late to the party on this, but I would argue that we should not add the leading slash here. It should be reading from the packages directory next to the entry point, not the root of the server. Imagine running a server with two completely separate angular apps in two different subfolders for instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know this was possible in pub serve. I'll change it to be relative to AppRootUrl (which I believe is our entry point URL path) and see what happens.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pub serve can only serve one "package", so it shouldn't be an issue there. Where it would an issue is when running a normal server in a folder which contains two different angular apps in different folders, which were built using pub build but template inlining was turned off. Also in development mode if you wanted to run a simple server in a parent directory of a few angular apps (in dartium).

}

if (uri.isAbsolute) return uri.toString();

Uri baseUri = Uri.parse(baseUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<style>@import "angular2_material/src/components/button/button.css";</style>
<style>@import "package:angular2_material/src/components/button/button.css";</style>
<span class="md-button-wrapper"><ng-content></ng-content></span>
4 changes: 2 additions & 2 deletions modules/angular2_material/src/components/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {isPresent} from 'angular2/src/facade/lang';


@Component({selector: '[md-button]:not([href])'})
@View({templateUrl: 'angular2_material/src/components/button/button.html'})
@View({templateUrl: 'package:angular2_material/src/components/button/button.html'})
export class MdButton {
// TODO(jelbourn): Ink ripples.
}
Expand All @@ -15,7 +15,7 @@ export class MdButton {
host: {'(click)': 'onClick($event)', '[tabIndex]': 'tabIndex'},
lifecycle: [LifecycleEvent.onChange]
})
@View({templateUrl: 'angular2_material/src/components/button/button.html'})
@View({templateUrl: 'package:angular2_material/src/components/button/button.html'})
export class MdAnchor {
tabIndex: number;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/checkbox/checkbox.css";</style>
<style>@import "package:angular2_material/src/components/checkbox/checkbox.css";</style>

<div (^click)="toggle($event)">
<div class="md-checkbox-container">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import {NumberWrapper} from 'angular2/src/facade/lang';
'[attr.aria-disabled]': 'disabled'
}
})
@View({templateUrl: 'angular2_material/src/components/checkbox/checkbox.html', directives: []})
@View({
templateUrl: 'package:angular2_material/src/components/checkbox/checkbox.html',
directives: []
})
export class MdCheckbox {
/** Whether this checkbox is checked. */
checked: boolean;
Expand Down
2 changes: 1 addition & 1 deletion modules/angular2_material/src/components/dialog/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export class MdDialogConfig {
host: {'(body:^keydown)': 'documentKeypress($event)'},
})
@View({
templateUrl: 'angular2_material/src/components/dialog/dialog.html',
templateUrl: 'package:angular2_material/src/components/dialog/dialog.html',
directives: [forwardRef(() => MdDialogContent)]
})
class MdDialogContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {Math} from 'angular2/src/facade/math';
properties: ['cols', 'rowHeight', 'gutterSize'],
lifecycle: [LifecycleEvent.onAllChangesDone]
})
@View({templateUrl: 'angular2_material/src/components/grid_list/grid_list.html'})
@View({templateUrl: 'package:angular2_material/src/components/grid_list/grid_list.html'})
export class MdGridList {
/** List of tiles that are being rendered. */
tiles: List<MdGridTile>;
Expand Down Expand Up @@ -223,7 +223,7 @@ export class MdGridList {
},
lifecycle: [LifecycleEvent.onDestroy, LifecycleEvent.onChange]
})
@View({templateUrl: 'angular2_material/src/components/grid_list/grid_tile.html'})
@View({templateUrl: 'package:angular2_material/src/components/grid_list/grid_tile.html'})
export class MdGridTile {
gridList: MdGridList;
_rowspan: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/grid_list/grid-list.css";</style>
<style>@import "package:angular2_material/src/components/grid_list/grid-list.css";</style>

<figure>
<ng-content></ng-content>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {Component, View} from 'angular2/angular2';

@Component({selector: 'md-progress-circular'})
@View({templateUrl: 'angular2_material/src/components/progress-circular/progress_circular.html'})
@View({
templateUrl: 'package:angular2_material/src/components/progress-circular/progress_circular.html'
})
export class MdProgressCircular {
constructor() {}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/progress-linear/progress_linear.css";</style>
<style>@import "package:angular2_material/src/components/progress-linear/progress_linear.css";</style>

<div class="md-progress-linear-container md-ready">
<div class="md-progress-linear-dashed"></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {Math} from 'angular2/src/facade/math';
}
})
@View({
templateUrl: 'angular2_material/src/components/progress-linear/progress_linear.html',
templateUrl: 'package:angular2_material/src/components/progress-linear/progress_linear.html',
directives: []
})
export class MdProgressLinear {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/radio/radio-button.css";</style>
<style>@import "package:angular2_material/src/components/radio/radio-button.css";</style>

<!-- TODO(jelbourn): render the radio on either side of the content -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _uniqueIdCounter: number = 0;
'[attr.aria-activedescendant]': 'activedescendant'
}
})
@View({templateUrl: 'angular2_material/src/components/radio/radio_group.html'})
@View({templateUrl: 'package:angular2_material/src/components/radio/radio_group.html'})
export class MdRadioGroup {
/** The selected value for the radio group. The value comes from the options. */
value: any;
Expand Down Expand Up @@ -190,7 +190,10 @@ export class MdRadioGroup {
'[attr.aria-disabled]': 'disabled'
}
})
@View({templateUrl: 'angular2_material/src/components/radio/radio_button.html', directives: []})
@View({
templateUrl: 'package:angular2_material/src/components/radio/radio_button.html',
directives: []
})
export class MdRadioButton {
/** Whether this radio is checked. */
checked: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<style>@import "angular2_material/src/components/radio/radio-group.css";</style>
<style>@import "package:angular2_material/src/components/radio/radio-group.css";</style>
<ng-content></ng-content>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<style>@import "angular2_material/src/components/switcher/switch.css";</style>
<style>@import "package:angular2_material/src/components/switcher/switch.css";</style>

<div (^click)="toggle($event)">
<div class="md-switch-container">
Expand Down
3 changes: 2 additions & 1 deletion modules/angular2_material/src/components/switcher/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {NumberWrapper} from 'angular2/src/facade/lang';
'[attr.role]': '"checkbox"'
}
})
@View({templateUrl: 'angular2_material/src/components/switcher/switch.html', directives: []})
@View(
{templateUrl: 'package:angular2_material/src/components/switcher/switch.html', directives: []})
export class MdSwitch {
/** Whether this switch is checked. */
checked: boolean;
Expand Down
23 changes: 23 additions & 0 deletions modules/examples/src/material/demo_common.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
61 changes: 5 additions & 56 deletions modules/examples/src/material/demo_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
// The standard UrlResolver looks for "package:" templateUrls in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you make the UrlResolver configurable via a token, we don't need a custom UrlResolver for the demos at all!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving UrlResolver untouched, so this still makes sense.

// node_modules, however in our repo we host material widgets at the root.
if (url.startsWith('package:angular2_material/')) {
return '/' + url.substring(8);
}

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}`;
}
}
}

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);
}
}
2 changes: 1 addition & 1 deletion modules/examples/src/material/input/demo_app.html
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down