1
- import { NativescriptPopupCommon } from './common' ;
1
+ import { Builder , Utils , View , Frame , StackLayout , Color , Device , Screen , knownFolders , path } from '@nativescript/core' ;
2
+ import { PopupOptions } from './common' ;
2
3
3
- export class NativescriptPopup extends NativescriptPopupCommon { }
4
+ export class Popup {
5
+ private _popup : android . widget . PopupWindow ;
6
+ private _options : PopupOptions ;
7
+ private resolveData ;
8
+ resolve ;
9
+ reject ;
10
+ private _view ;
11
+
12
+ constructor ( options ?: PopupOptions ) {
13
+ this . _options = new PopupOptions ( ) ;
14
+ this . _popup = new android . widget . PopupWindow (
15
+ Utils . android . getApplicationContext ( )
16
+ ) ;
17
+ this . _popup . setOnDismissListener ( new android . widget . PopupWindow . OnDismissListener ( {
18
+ onDismiss : ( ) => {
19
+ if ( this . resolve ) {
20
+ this . resolve ( this . resolveData ) ;
21
+ }
22
+ this . resolve = null ;
23
+ this . reject = null ;
24
+ if ( this . _view ) {
25
+ Frame . topmost ( ) . _removeView ( this . _view ) ;
26
+ }
27
+ }
28
+ } ) ) ;
29
+ if ( options ) {
30
+ Object . keys ( options ) . forEach ( key => {
31
+ this . _options [ key ] = options [ key ] ;
32
+ } ) ;
33
+ }
34
+ }
35
+
36
+ public showPopup ( source : any , view : any ) : Promise < boolean > {
37
+ return new Promise ( ( resolve , reject ) => {
38
+ this . _popup . setOutsideTouchable ( this . _options . outsideTouchble ) ;
39
+ this . reject = reject ;
40
+ this . resolve = resolve ;
41
+ // check the view argument
42
+ if ( view instanceof android . view . View ) {
43
+ this . _popup . setContentView ( view ) ;
44
+ } else if ( view instanceof View ) {
45
+ Frame . topmost ( ) . _addView ( view ) ;
46
+ this . _stylePopup ( ) ;
47
+ this . _view = view ;
48
+ this . _popup . setContentView ( view . android ) ;
49
+ } else if ( typeof view === 'string' || view instanceof String ) {
50
+ // this is a template so use the builder to load the template
51
+ this . _stylePopup ( ) ;
52
+ const stack = new StackLayout ( ) ;
53
+ Frame . topmost ( ) . _addView ( stack ) ;
54
+ stack . removeChildren ( ) ; // ensure nothing in the stack
55
+ let filePath ;
56
+ let component ;
57
+ if ( view . startsWith ( '~' ) ) {
58
+ view = view . replace ( '~' , '' ) ;
59
+ filePath = knownFolders . currentApp ( ) . path ;
60
+ console . log ( path . join ( filePath , view ) ) ;
61
+ component = Builder . load ( path . join ( filePath , view ) ) ;
62
+ console . log ( component ) ;
63
+ } else {
64
+ component = Builder . load ( < any > view ) ;
65
+ }
66
+ stack . addChild ( component ) ;
67
+ this . _view = stack ;
68
+ this . _popup . setContentView ( stack . android ) ;
69
+ }
70
+
71
+ // check the source argument
72
+ if ( source instanceof android . view . View ) {
73
+ this . _popup . showAsDropDown ( source ) ;
74
+ } else if ( source instanceof View ) {
75
+ this . _popup . showAsDropDown ( source . android ) ;
76
+ }
77
+ } ) ;
78
+ }
79
+
80
+ public hidePopup ( data ?: any ) {
81
+ this . resolveData = data ;
82
+ this . _popup . dismiss ( ) ;
83
+ }
84
+
85
+ private _stylePopup ( ) {
86
+ let height ;
87
+ let width ;
88
+ switch ( this . _options . unit ) {
89
+ case 'px' :
90
+ if ( this . _options . height && ! this . _options . width ) {
91
+ height = this . _options . height ;
92
+ width =
93
+ this . _options . height *
94
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
95
+ } else if ( this . _options . width && ! this . _options . height ) {
96
+ height =
97
+ this . _options . width *
98
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
99
+ width = this . _options . width ;
100
+ } else {
101
+ width = this . _options . width ;
102
+ height = this . _options . height ;
103
+ }
104
+ break ;
105
+ case '%' :
106
+ if ( this . _options . height && ! this . _options . width ) {
107
+ height = Screen . mainScreen . heightDIPs * ( this . _options . height / 100 ) ;
108
+ width =
109
+ height *
110
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
111
+ } else if ( this . _options . width && ! this . _options . height ) {
112
+ width = Screen . mainScreen . widthDIPs * ( this . _options . width / 100 ) ;
113
+ height =
114
+ width *
115
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
116
+ } else {
117
+ width = Screen . mainScreen . widthDIPs * ( this . _options . width / 100 ) ;
118
+ height = Screen . mainScreen . heightDIPs * ( this . _options . height / 100 ) ;
119
+ }
120
+ break ;
121
+ default :
122
+ if ( this . _options . height && ! this . _options . width ) {
123
+ height = this . _options . height ;
124
+ width =
125
+ this . _options . height *
126
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
127
+ } else if ( this . _options . width && ! this . _options . height ) {
128
+ height =
129
+ this . _options . width *
130
+ ( Screen . mainScreen . widthPixels / Screen . mainScreen . heightPixels ) ;
131
+ width = this . _options . width ;
132
+ } else {
133
+ width = this . _options . width ? this . _options . width : 400 ;
134
+ height = this . _options . height ? this . _options . height : 320 ;
135
+ }
136
+ break ;
137
+ }
138
+
139
+ const shape = new android . graphics . drawable . GradientDrawable ( ) ;
140
+ shape . setShape ( android . graphics . drawable . GradientDrawable . RECTANGLE ) ;
141
+
142
+ if ( this . _options && this . _options . borderRadius ) {
143
+ shape . setCornerRadius ( this . _options . borderRadius ) ;
144
+ }
145
+ if ( this . _options && this . _options . backgroundColor ) {
146
+ shape . setColor ( new Color ( this . _options . backgroundColor ) . android ) ;
147
+ }
148
+ ( this . _popup as any ) . setBackgroundDrawable ( shape ) ;
149
+ if ( parseInt ( Device . sdkVersion , 10 ) >= 21 ) {
150
+ if ( this . _options . elevation ) {
151
+ ( this . _popup as any ) . setElevation ( this . _options . elevation ) ;
152
+ }
153
+ }
154
+ this . _popup . setWidth ( Utils . layout . toDevicePixels ( width ) ) ;
155
+ this . _popup . setHeight ( Utils . layout . toDevicePixels ( height ) ) ;
156
+ }
157
+ }
0 commit comments