Skip to content

Commit c8947d7

Browse files
committed
chore(material): move dialog to TypeScript.
1 parent 4f3acdb commit c8947d7

File tree

2 files changed

+108
-112
lines changed
  • modules

2 files changed

+108
-112
lines changed

modules/angular2_material/src/components/dialog/dialog.js renamed to modules/angular2_material/src/components/dialog/dialog.ts

Lines changed: 88 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
import {DynamicComponentLoader, ElementRef, ComponentRef, onDestroy, DomRenderer} from 'angular2/angular2';
2-
import {bind, Injector} from 'angular2/di';
1+
import {
2+
Component,
3+
Directive,
4+
View,
5+
Parent,
6+
ElementRef,
7+
DynamicComponentLoader,
8+
ComponentRef,
9+
DomRenderer
10+
} from 'angular2/angular2';
11+
import {bind, Injector, Injectable, FORWARD_REF} from 'angular2/di';
12+
313
import {ObservableWrapper, Promise, PromiseWrapper} from 'angular2/src/facade/async';
414
import {isPresent, Type} from 'angular2/src/facade/lang';
515
import {DOM} from 'angular2/src/dom/dom_adapter';
616
import {MouseEvent, KeyboardEvent} from 'angular2/src/facade/browser';
717
import {KEY_ESC} from 'angular2_material/src/core/constants';
818

9-
// TODO(radokirov): Once the application is transpiled by TS instead of Traceur,
10-
// add those imports back into 'angular2/angular2';
11-
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
12-
import {Parent} from 'angular2/src/core/annotations_impl/visibility';
13-
import {View} from 'angular2/src/core/annotations_impl/view';
14-
1519
// TODO(jelbourn): Opener of dialog can control where it is rendered.
1620
// TODO(jelbourn): body scrolling is disabled while dialog is open.
1721
// TODO(jelbourn): Don't manually construct and configure a DOM element. See #1402
@@ -21,11 +25,10 @@ import {View} from 'angular2/src/core/annotations_impl/view';
2125
// TODO(jelbourn): Pre-built `alert` and `confirm` dialogs.
2226
// TODO(jelbourn): Animate dialog out of / into opening element.
2327

24-
var _nextDialogId = 0;
25-
2628
/**
2729
* Service for opening modal dialogs.
2830
*/
31+
@Injectable()
2932
export class MdDialog {
3033
componentLoader: DynamicComponentLoader;
3134
domRenderer: DomRenderer;
@@ -39,13 +42,12 @@ export class MdDialog {
3942
* Opens a modal dialog.
4043
* @param type The component to open.
4144
* @param elementRef The logical location into which the component will be opened.
45+
* @param parentInjector
46+
* @param options
4247
* @returns Promise for a reference to the dialog.
4348
*/
44-
open(
45-
type: Type,
46-
elementRef: ElementRef,
47-
parentInjector: Injector,
48-
options: MdDialogConfig = null): Promise<MdDialogRef> {
49+
open(type: Type, elementRef: ElementRef, parentInjector: Injector,
50+
options: MdDialogConfig = null): Promise<MdDialogRef> {
4951
var config = isPresent(options) ? options : new MdDialogConfig();
5052

5153
// Create the dialogRef here so that it can be injected into the content component.
@@ -57,61 +59,63 @@ export class MdDialog {
5759
var backdropRefPromise = this._openBackdrop(elementRef, contentInjector);
5860

5961
// First, load the MdDialogContainer, into which the given component will be loaded.
60-
return this.componentLoader.loadIntoNewLocation(
61-
MdDialogContainer, elementRef).then(containerRef => {
62-
// TODO(tbosch): clean this up when we have custom renderers (https://github.com/angular/angular/issues/1807)
63-
// TODO(jelbourn): Don't use direct DOM access. Need abstraction to create an element
64-
// directly on the document body (also needed for web workers stuff).
65-
// Create a DOM node to serve as a physical host element for the dialog.
66-
var dialogElement = this.domRenderer.getHostElement(containerRef.hostView.render);
67-
DOM.appendChild(DOM.query('body'), dialogElement);
68-
69-
// TODO(jelbourn): Use hostProperties binding to set these once #1539 is fixed.
70-
// Configure properties on the host element.
71-
DOM.addClass(dialogElement, 'md-dialog');
72-
DOM.setAttribute(dialogElement, 'tabindex', '0');
73-
74-
// TODO(jelbourn): Do this with hostProperties (or another rendering abstraction) once ready.
75-
if (isPresent(config.width)) {
76-
DOM.setStyle(dialogElement, 'width', config.width);
77-
}
78-
if (isPresent(config.height)) {
79-
DOM.setStyle(dialogElement, 'height', config.height);
80-
}
81-
82-
dialogRef.containerRef = containerRef;
83-
84-
// Now load the given component into the MdDialogContainer.
85-
return this.componentLoader.loadNextToExistingLocation(
86-
type, containerRef.instance.contentRef, contentInjector).then(contentRef => {
87-
88-
// Wrap both component refs for the container and the content so that we can return
89-
// the `instance` of the content but the dispose method of the container back to the
90-
// opener.
91-
dialogRef.contentRef = contentRef;
92-
containerRef.instance.dialogRef = dialogRef;
93-
94-
backdropRefPromise.then(backdropRef => {
95-
dialogRef.whenClosed.then((_) => {
96-
backdropRef.dispose();
97-
});
62+
return this.componentLoader.loadIntoNewLocation(MdDialogContainer, elementRef)
63+
.then(containerRef => {
64+
// TODO(tbosch): clean this up when we have custom renderers
65+
// (https://github.com/angular/angular/issues/1807)
66+
// TODO(jelbourn): Don't use direct DOM access. Need abstraction to create an element
67+
// directly on the document body (also needed for web workers stuff).
68+
// Create a DOM node to serve as a physical host element for the dialog.
69+
var dialogElement = this.domRenderer.getHostElement(containerRef.hostView.render);
70+
DOM.appendChild(DOM.query('body'), dialogElement);
71+
72+
// TODO(jelbourn): Use hostProperties binding to set these once #1539 is fixed.
73+
// Configure properties on the host element.
74+
DOM.addClass(dialogElement, 'md-dialog');
75+
DOM.setAttribute(dialogElement, 'tabindex', '0');
76+
77+
// TODO(jelbourn): Do this with hostProperties (or another rendering abstraction) once
78+
// ready.
79+
if (isPresent(config.width)) {
80+
DOM.setStyle(dialogElement, 'width', config.width);
81+
}
82+
if (isPresent(config.height)) {
83+
DOM.setStyle(dialogElement, 'height', config.height);
84+
}
85+
86+
dialogRef.containerRef = containerRef;
87+
88+
// Now load the given component into the MdDialogContainer.
89+
return this.componentLoader.loadNextToExistingLocation(
90+
type, containerRef.instance.contentRef, contentInjector)
91+
.then(contentRef => {
92+
93+
// Wrap both component refs for the container and the content so that we can return
94+
// the `instance` of the content but the dispose method of the container back to the
95+
// opener.
96+
dialogRef.contentRef = contentRef;
97+
containerRef.instance.dialogRef = dialogRef;
98+
99+
backdropRefPromise.then(backdropRef => {
100+
dialogRef.whenClosed.then((_) => { backdropRef.dispose(); });
101+
});
102+
103+
return dialogRef;
104+
});
98105
});
99-
100-
return dialogRef;
101-
});
102-
});
103106
}
104107

105108
/** Loads the dialog backdrop (transparent overlay over the rest of the page). */
106-
_openBackdrop(elementRef:ElementRef, injector: Injector): Promise<ComponentRef> {
107-
return this.componentLoader.loadIntoNewLocation(
108-
MdBackdrop, elementRef, injector).then( (componentRef) => {
109-
// TODO(tbosch): clean this up when we have custom renderers (https://github.com/angular/angular/issues/1807)
110-
var backdropElement = this.domRenderer.getHostElement(componentRef.hostView.render);
111-
DOM.addClass(backdropElement, 'md-backdrop');
112-
DOM.appendChild(DOM.query('body'), backdropElement);
113-
return componentRef;
114-
});
109+
_openBackdrop(elementRef: ElementRef, injector: Injector): Promise<ComponentRef> {
110+
return this.componentLoader.loadIntoNewLocation(MdBackdrop, elementRef, injector)
111+
.then((componentRef) => {
112+
// TODO(tbosch): clean this up when we have custom renderers
113+
// (https://github.com/angular/angular/issues/1807)
114+
var backdropElement = this.domRenderer.getHostElement(componentRef.hostView.render);
115+
DOM.addClass(backdropElement, 'md-backdrop');
116+
DOM.appendChild(DOM.query('body'), backdropElement);
117+
return componentRef;
118+
});
115119
}
116120

117121
alert(message: string, okMessage: string): Promise {
@@ -163,8 +167,10 @@ export class MdDialogRef {
163167
return this._contentRef.instance;
164168
}
165169

166-
// The only time one could attempt to access this property before the value is set is if an access occurs during
167-
// the constructor of the very instance they are trying to get (which is much more easily accessed as `this`).
170+
// The only time one could attempt to access this property before the value is set is if an
171+
// access occurs during
172+
// the constructor of the very instance they are trying to get (which is much more easily
173+
// accessed as `this`).
168174
throw "Cannot access dialog component instance *from* that component's constructor.";
169175
}
170176

@@ -203,13 +209,11 @@ export class MdDialogConfig {
203209
*/
204210
@Component({
205211
selector: 'md-dialog-container',
206-
hostListeners: {
207-
'body:^keydown': 'documentKeypress($event)'
208-
}
212+
hostListeners: {'body:^keydown': 'documentKeypress($event)'},
209213
})
210214
@View({
211215
templateUrl: 'angular2_material/src/components/dialog/dialog.html',
212-
directives: [MdDialogContent]
216+
directives: [FORWARD_REF(() => MdDialogContent)]
213217
})
214218
class MdDialogContainer {
215219
// Ref to the dialog content. Used by the DynamicComponentLoader to load the dialog content.
@@ -234,13 +238,22 @@ class MdDialogContainer {
234238
}
235239
}
236240

241+
/**
242+
* Simple decorator used only to communicate an ElementRef to the parent MdDialogContainer as the
243+
* location
244+
* for where the dialog content will be loaded.
245+
*/
246+
@Directive({selector: 'md-dialog-content'})
247+
class MdDialogContent {
248+
constructor(@Parent() dialogContainer: MdDialogContainer, elementRef: ElementRef) {
249+
dialogContainer.contentRef = elementRef;
250+
}
251+
}
237252

238253
/** Component for the dialog "backdrop", a transparent overlay over the rest of the page. */
239254
@Component({
240255
selector: 'md-backdrop',
241-
hostListeners: {
242-
'click': 'onClick()'
243-
}
256+
hostListeners: {'click': 'onClick()'},
244257
})
245258
@View({template: ''})
246259
class MdBackdrop {
@@ -256,15 +269,3 @@ class MdBackdrop {
256269
this.dialogRef.close();
257270
}
258271
}
259-
260-
261-
/**
262-
* Simple decorator used only to communicate an ElementRef to the parent MdDialogContainer as the location
263-
* for where the dialog content will be loaded.
264-
*/
265-
@Directive({selector: 'md-dialog-content'})
266-
class MdDialogContent {
267-
constructor(@Parent() dialogContainer: MdDialogContainer, elementRef: ElementRef) {
268-
dialogContainer.contentRef = elementRef;
269-
}
270-
}

modules/examples/src/material/dialog/index.js renamed to modules/examples/src/material/dialog/index.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import {bootstrap, ElementRef, ComponentRef} from 'angular2/angular2';
2-
import {MdDialog, MdDialogRef, MdDialogConfig} from 'angular2_material/src/components/dialog/dialog'
1+
import {bootstrap, ElementRef, ComponentRef, Component, View} from 'angular2/angular2';
2+
import {
3+
MdDialog,
4+
MdDialogRef,
5+
MdDialogConfig
6+
} from 'angular2_material/src/components/dialog/dialog';
37
import {UrlResolver} from 'angular2/src/services/url_resolver';
48
import {commonDemoSetup, DemoUrlResolver} from '../demo_common';
59
import {bind, Injector} from 'angular2/di';
610
import {isPresent} from 'angular2/src/facade/lang';
711

8-
// TODO(radokirov): Once the application is transpiled by TS instead of Traceur,
9-
// add those imports back into 'angular2/angular2';
10-
import {Component, Directive} from 'angular2/src/core/annotations_impl/annotations';
11-
import {View} from 'angular2/src/core/annotations_impl/view';
12-
1312

1413
@Component({
1514
selector: 'demo-app',
16-
appInjector: [MdDialog]
15+
appInjector: [MdDialog],
1716
})
1817
@View({
1918
templateUrl: './demo_app.html',
20-
directives: []
19+
directives: [],
2120
})
2221
class DemoApp {
2322
dialog: MdDialog;
@@ -43,16 +42,16 @@ class DemoApp {
4342
return;
4443
}
4544

46-
this.dialog.open(SimpleDialogComponent,
47-
this.elementRef, this.injector, this.dialogConfig).then(ref => {
48-
this.dialogRef = ref;
49-
ref.instance.numCoconuts = 777;
45+
this.dialog.open(SimpleDialogComponent, this.elementRef, this.injector, this.dialogConfig)
46+
.then(ref => {
47+
this.dialogRef = ref;
48+
ref.instance.numCoconuts = 777;
5049

51-
ref.whenClosed.then(result => {
52-
this.dialogRef = null;
53-
this.lastResult = result;
54-
});
55-
});
50+
ref.whenClosed.then(result => {
51+
this.dialogRef = null;
52+
this.lastResult = result;
53+
});
54+
});
5655
}
5756

5857
close() {
@@ -62,15 +61,15 @@ class DemoApp {
6261

6362
@Component({
6463
selector: 'simple-dialog',
65-
properties: ['numCoconuts']
64+
properties: ['numCoconuts'],
6665
})
6766
@View({
6867
template: `
6968
<h2>This is the dialog content</h2>
7069
<p>There are {{numCoconuts}} coconuts.</p>
7170
<p>Return: <input (input)="updateValue($event)"></p>
7271
<button type="button" (click)="done()">Done</button>
73-
`
72+
`,
7473
})
7574
class SimpleDialogComponent {
7675
numCoconuts: number;
@@ -95,9 +94,5 @@ class SimpleDialogComponent {
9594

9695
export function main() {
9796
commonDemoSetup();
98-
bootstrap(DemoApp, [
99-
bind(UrlResolver).toValue(new DemoUrlResolver())
100-
]);
97+
bootstrap(DemoApp, [bind(UrlResolver).toValue(new DemoUrlResolver())]);
10198
}
102-
103-

0 commit comments

Comments
 (0)