1
1
import * as pd from 'polymer-ts-decorators' ;
2
2
import { IDebugConfigElementBehavior , IDebugConfig } from '../lib/debug-engine' ;
3
- import { createElement } from '../lib/debug-workbench' ;
4
- import { Disposable } from 'event-kit' ;
5
- import addDisposableListener from '../lib/disposable-dom-event-listener' ;
3
+ import * as debugWorkbench from '../lib/debug-workbench' ;
4
+ import { Disposable , Emitter } from 'event-kit' ;
6
5
7
6
interface ILocalDOM {
8
7
dialog : PolymerElements . PaperDialog ;
@@ -12,38 +11,75 @@ function $(element: any): ILocalDOM {
12
11
return element . $ ;
13
12
}
14
13
14
+ const OPENED_EVENT = 'opened' ;
15
+ const CLOSED_EVENT = 'closed' ;
16
+
15
17
/**
16
18
* Base behavior of the DebugConfigurationElement.
17
19
*/
18
20
@pd . is ( 'debug-configuration' )
19
21
export default class DebugConfigurationElement implements IDebugConfigElementBehavior {
22
+ private debugConfig : IDebugConfig ;
23
+ private emitter : Emitter ;
24
+
20
25
static create ( debugConfig : IDebugConfig ) : Promise < IDebugConfigurationElement > {
21
- return createElement ( ( < any > DebugConfigurationElement . prototype ) . is , debugConfig ) ;
26
+ return debugWorkbench . createElement ( ( < any > DebugConfigurationElement . prototype ) . is , debugConfig ) ;
22
27
}
23
28
24
- /** Add a listener to be called when the dialog is opened. */
25
- onOpened ( callback : EventListener ) : Disposable {
26
- return addDisposableListener ( $ ( this ) . dialog , 'iron-overlay-opened' , callback ) ;
29
+ created ( ) : void {
30
+ this . emitter = new Emitter ( ) ;
27
31
}
28
32
29
- /** Add a listener to be called when the dialog is closed. */
30
- onClosed ( callback : EventListener ) : Disposable {
31
- return addDisposableListener ( $ ( this ) . dialog , 'iron-overlay-closed' , callback ) ;
33
+ destroy ( ) : void {
34
+ if ( this . emitter ) {
35
+ this . emitter . dispose ( ) ;
36
+ this . emitter = null ;
37
+ }
32
38
}
33
39
34
- open ( ) : void {
35
- const dialog = $ ( this ) . dialog ;
36
- if ( dialog ) {
37
- dialog . open ( ) ;
40
+ /** Called after ready() with arguments passed to the element constructor function. */
41
+ factoryImpl ( debugConfig : IDebugConfig ) : void {
42
+ this . debugConfig = debugConfig ;
43
+ }
44
+
45
+ @pd . listener ( 'dialog.iron-overlay-opened' )
46
+ private onIronOverlayOpened ( e : CustomEvent ) : void {
47
+ if ( Polymer . dom ( e ) . localTarget === $ ( this ) . dialog ) {
48
+ this . emitter . emit ( OPENED_EVENT ) ;
49
+ } else {
50
+ e . stopPropagation ( ) ;
38
51
}
39
52
}
40
53
41
- close ( ) : void {
42
- const dialog = $ ( this ) . dialog ;
43
- if ( dialog ) {
44
- dialog . close ( ) ;
54
+ @pd . listener ( 'dialog.iron-overlay-closed' )
55
+ private onIronOverlayClosed ( e : PolymerElements . IronOverlayClosedEvent ) : void {
56
+ if ( Polymer . dom ( e ) . localTarget === $ ( this ) . dialog ) {
57
+ if ( e . detail . confirmed ) {
58
+ debugWorkbench . debugConfigs . save ( this . debugConfig ) ;
59
+ }
60
+ this . emitter . emit ( CLOSED_EVENT , e . detail ) ;
61
+ } else {
62
+ e . stopPropagation ( ) ;
45
63
}
46
64
}
65
+
66
+ /** Add a function to be called when the dialog is opened. */
67
+ onOpened ( callback : ( ) => void ) : Disposable {
68
+ return this . emitter . on ( OPENED_EVENT , callback ) ;
69
+ }
70
+
71
+ /** Add a function to be called when the dialog is closed. */
72
+ onClosed ( callback : ( closingReason : PolymerElements . IClosingReason ) => void ) : Disposable {
73
+ return this . emitter . on ( CLOSED_EVENT , callback ) ;
74
+ }
75
+
76
+ open ( ) : void {
77
+ $ ( this ) . dialog . open ( ) ;
78
+ }
79
+
80
+ close ( ) : void {
81
+ $ ( this ) . dialog . close ( ) ;
82
+ }
47
83
}
48
84
49
85
export interface IDebugConfigurationElement extends DebugConfigurationElement , HTMLElement {
0 commit comments