1
- import { CheckBoxInterface } from "./" ;
2
- import { View } from "ui/core/view" ;
3
- import { Color } from "color" ;
4
- import { isAndroid , device } from "platform" ;
5
- import { Property , PropertyChangeData } from "ui/core/dependency-observable" ;
6
- import { PropertyMetadata } from "ui/core/proxy" ;
7
- import { Font } from "ui/styling/font" ;
8
- import enums = require( "ui/enums" ) ;
9
- import style = require( "ui/styling/style" ) ;
10
- import app = require( "application" ) ;
11
- declare var android : any ;
1
+ import { Color } from "tns-core-modules/color" ;
2
+ import { device } from "tns-core-modules/platform" ;
3
+ import app = require( "tns-core-modules/application" ) ;
4
+ import { CheckBoxInterface } from "." ;
5
+ import {
6
+ View ,
7
+ Property ,
8
+ CssProperty ,
9
+ Style ,
10
+ booleanConverter
11
+ } from "tns-core-modules/ui/core/view" ;
12
+ declare const android : any ;
13
+
14
+ export const checkedProperty = new Property < CheckBox , boolean > ( {
15
+ name : 'checked' ,
16
+ defaultValue : false ,
17
+ valueConverter : booleanConverter ,
18
+ valueChanged : onCheckedPropertyChanged
19
+ } ) ;
20
+
21
+ export const textProperty = new Property < CheckBox , string > ( {
22
+ name : 'text' ,
23
+ defaultValue : '' ,
24
+ valueChanged : onTextPropertyChanged
25
+ } ) ;
26
+
27
+ export const fillColorProperty = new CssProperty < Style , string > ( {
28
+ name : 'fillColor' ,
29
+ cssName : 'fill-color' ,
30
+ valueConverter : v => {
31
+ return String ( v )
32
+ }
33
+ } ) ;
34
+
35
+ export const tintColorProperty = new CssProperty < Style , string > ( {
36
+ name : 'tintColor' ,
37
+ cssName : 'tint-color' ,
38
+ defaultValue : '#0075ff' ,
39
+ valueConverter : v => {
40
+ return String ( v )
41
+ }
42
+ } ) ;
12
43
13
44
export class CheckBox extends View implements CheckBoxInterface {
14
45
private _android : any ; /// android.widget.CheckBox
15
- private _fillColor : string ;
16
46
private _checkStyle : string ;
17
47
private _checkPadding : string ;
18
48
private _checkPaddingLeft : string ;
19
49
private _checkPaddingTop : string ;
20
50
private _checkPaddingRight : string ;
21
51
private _checkPaddingBottom : string ;
22
- public static checkedProperty = new Property (
23
- "checked" ,
24
- "CheckBox" ,
25
- new PropertyMetadata ( false )
26
- ) ;
27
-
28
- public static textProperty = new Property (
29
- "text" ,
30
- "CheckBox" ,
31
- new PropertyMetadata ( false )
32
- ) ;
33
-
52
+ public checked :boolean ;
34
53
constructor ( ) {
35
54
super ( ) ;
36
55
}
@@ -39,10 +58,6 @@ export class CheckBox extends View implements CheckBoxInterface {
39
58
return this . _android ;
40
59
}
41
60
42
- get _nativeView ( ) {
43
- return this . _android ;
44
- }
45
-
46
61
get checkStyle ( ) {
47
62
return this . _checkStyle ;
48
63
}
@@ -91,49 +106,40 @@ export class CheckBox extends View implements CheckBoxInterface {
91
106
get checkPaddingBottom ( ) {
92
107
return this . _checkPaddingBottom ;
93
108
}
94
-
95
- get checked ( ) : boolean {
96
- return this . _getValue ( CheckBox . checkedProperty ) ;
109
+ [ checkedProperty . getDefault ] ( ) : boolean {
110
+ return false ;
97
111
}
98
-
99
- set checked ( value : boolean ) {
100
- this . _setValue ( CheckBox . checkedProperty , value ) ;
112
+ [ checkedProperty . setNative ] ( value : boolean ) {
113
+ this . nativeView . setChecked ( Boolean ( value ) ) ;
101
114
}
102
-
103
- get text ( ) : string {
104
- return this . _getValue ( CheckBox . textProperty ) ;
115
+ [ textProperty . getDefault ] ( ) : string {
116
+ return '' ;
105
117
}
106
-
107
- set text ( value : string ) {
108
- this . _setValue ( CheckBox . textProperty , value ) ;
118
+ [ textProperty . setNative ] ( value : string ) {
119
+ this . nativeView . setText ( value ) ;
109
120
}
110
121
111
- get fillColor ( ) : string {
112
- return this . _fillColor ;
122
+ get fillColor ( ) :string {
123
+ return ( < any > this . style ) . fillColor ;
113
124
}
114
-
115
- set fillColor ( color : string ) {
116
- this . _fillColor = color ;
117
-
125
+ set fillColor ( color :string ) {
126
+ ( < any > this . style ) . fillColor = color ;
118
127
if ( this . _android && device . sdkVersion >= "21" )
119
- this . _android . setButtonTintList ( android . content . res . ColorStateList . valueOf ( new Color ( this . _fillColor ) . android ) ) ;
128
+ this . _android . setButtonTintList ( android . content . res . ColorStateList . valueOf ( new Color ( color ) . android ) ) ;
120
129
}
121
130
122
131
//There is no difference between tint and fill on the android widget
123
132
get tintColor ( ) : string {
124
- return this . fillColor ;
133
+ return ( < any > this . style ) . fillColor ;
125
134
}
126
135
127
136
set tintColor ( color : string ) {
128
- this . fillColor = color ;
137
+ ( < any > this . style ) . fillColor = color ;
129
138
}
130
139
140
+ public createNativeView ( ) {
131
141
132
- public _createUI ( ) {
133
-
134
- // this._android = new android.widget.CheckBox(this._context, null);
135
142
this . _android = new android . support . v7 . widget . AppCompatCheckBox ( this . _context , null ) ;
136
-
137
143
if ( this . checkPaddingLeft ) {
138
144
this . _android . setPadding ( parseInt ( this . checkPaddingLeft ) , this . _android . getPaddingTop ( ) , this . _android . getPaddingRight ( ) , this . _android . getPaddingBottom ( ) ) ;
139
145
}
@@ -167,13 +173,21 @@ export class CheckBox extends View implements CheckBoxInterface {
167
173
break ;
168
174
}
169
175
}
170
- if ( this . text ) {
171
- this . _android . setText ( this . text ) ;
176
+
177
+
178
+ if ( this . style . color ) {
179
+ this . _android . setTextColor ( this . style . color . android ) ;
180
+ }
181
+
182
+ if ( ! this . style . fontSize ) {
183
+ this . style . fontSize = 15 ;
172
184
}
173
185
174
- /// works with class styling - Brad
175
- if ( ! this . fontSize ) {
176
- this . fontSize = 15 ;
186
+ this . _android . setTextSize ( this . style . fontSize ) ;
187
+
188
+ var typeface = this . style . fontInternal . getAndroidTypeface ( ) ;
189
+ if ( typeface ) {
190
+ this . _android . setTypeface ( typeface ) ;
177
191
}
178
192
179
193
if ( this . _checkStyle ) {
@@ -184,122 +198,61 @@ export class CheckBox extends View implements CheckBoxInterface {
184
198
185
199
if ( this . _android ) {
186
200
if ( this . fillColor ) {
187
- android . support . v4 . widget . CompoundButtonCompat . setButtonTintList ( this . _android , android . content . res . ColorStateList . valueOf ( new Color ( this . _fillColor ) . android ) ) ;
201
+ android . support . v4 . widget . CompoundButtonCompat . setButtonTintList ( this . _android , android . content . res . ColorStateList . valueOf ( new Color ( this . fillColor ) . android ) ) ;
188
202
}
189
203
}
190
204
191
- var that = new WeakRef ( this ) ;
205
+ return this . _android ;
206
+ }
192
207
193
- this . _android . setOnCheckedChangeListener ( new android . widget . CompoundButton . OnCheckedChangeListener ( {
194
- get owner ( ) {
208
+ public initNativeView ( ) {
209
+ var that = new WeakRef ( this ) ;
210
+ this . nativeView . setOnCheckedChangeListener ( new android . widget . CompoundButton . OnCheckedChangeListener ( {
211
+ get owner ( ) : CheckBox {
195
212
return that . get ( ) ;
196
213
} ,
197
214
198
215
onCheckedChanged : function ( sender , isChecked ) {
199
216
if ( this . owner ) {
200
- this . owner . _onPropertyChangedFromNative ( CheckBox . checkedProperty , isChecked ) ;
217
+ checkedProperty . nativeValueChange ( this . owner , isChecked ) ;
201
218
}
202
219
}
203
220
} ) ) ;
204
-
205
- }
206
-
207
- public toggle ( ) : void {
208
- this . _android . toggle ( ) ;
209
- }
210
- }
211
-
212
-
213
- function onCheckedPropertyChanged ( data : PropertyChangeData ) {
214
- var cBox = < CheckBox > data . object ;
215
- if ( ! cBox . android ) {
216
- return ;
217
221
}
218
222
219
- cBox . android . setChecked ( data . newValue ) ;
220
- }
221
-
222
- // register the setNativeValue callbacks
223
- ( < PropertyMetadata > CheckBox . checkedProperty . metadata ) . onSetNativeValue = onCheckedPropertyChanged ;
224
-
225
-
226
- function onTextPropertyChanged ( data : PropertyChangeData ) {
227
- var cBox = < CheckBox > data . object ;
228
- if ( ! cBox . android ) {
229
- return ;
223
+ public disposeNativeView ( ) {
224
+ this . nativeView . setOnCheckedChangeListener ( null ) ;
230
225
}
231
226
232
- cBox . android . setText ( data . newValue ) ;
233
- }
234
-
235
- // register the setNativeValue callbacks
236
- ( < PropertyMetadata > CheckBox . textProperty . metadata ) . onSetNativeValue = onTextPropertyChanged ;
237
-
238
-
239
- export class CheckBoxStyler implements style . Styler {
240
- private static setColorLabelProperty ( view : any , newValue : any ) {
241
- var cb = < android . widget . CheckBox > view . _nativeView ;
242
- if ( cb ) {
243
- ( < any > cb ) . setTextColor ( new Color ( newValue ) . android ) ;
244
- }
227
+ public toggle ( ) : void {
228
+ this . nativeView . toggle ( ) ;
245
229
}
246
230
247
- // font
248
- private static setFontInternalProperty ( view : any , newValue : any , nativeValue ?: any ) {
249
- var tv = < android . widget . CheckBox > view . _nativeView ;
250
- var fontValue = < Font > newValue ;
251
-
252
- var typeface = fontValue . getAndroidTypeface ( ) ;
253
- if ( typeface ) {
254
- tv . setTypeface ( typeface ) ;
255
- }
256
- else {
257
- tv . setTypeface ( nativeValue . typeface ) ;
258
- }
259
-
260
- if ( fontValue . fontSize ) {
261
- tv . setTextSize ( fontValue . fontSize ) ;
262
- }
263
- else {
264
- tv . setTextSize ( android . util . TypedValue . COMPLEX_UNIT_PX , nativeValue . size ) ;
231
+ _onCheckedPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
232
+ if ( ! this . nativeView ) {
233
+ return
265
234
}
235
+ checkedProperty . nativeValueChange ( this , newValue ) ;
266
236
}
267
-
268
- private static resetFontInternalProperty ( view : any , nativeValue : any ) {
269
- var tv : android . widget . CheckBox = < android . widget . CheckBox > view . _nativeView ;
270
- if ( tv && nativeValue ) {
271
- tv . setTypeface ( nativeValue . typeface ) ;
272
- tv . setTextSize ( android . util . TypedValue . COMPLEX_UNIT_PX , nativeValue . size ) ;
237
+ _onTextPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
238
+ if ( ! this . nativeView ) {
239
+ return
273
240
}
241
+ textProperty . nativeValueChange ( this , newValue ) ;
274
242
}
275
243
276
- private static getNativeFontInternalValue ( view : any ) : any {
277
- var tv : android . widget . TextView = < android . widget . CheckBox > view . _nativeView ;
278
- return {
279
- typeface : tv . getTypeface ( ) ,
280
- size : tv . getTextSize ( )
281
- } ;
282
- }
283
-
284
- private static resetColorProperty ( view : View , nativeValue : number ) {
285
- // Do nothing.
286
- }
287
-
288
-
289
- public static registerHandlers ( ) {
290
- style . registerHandler ( style . colorProperty , new style . StylePropertyChangedHandler (
291
- CheckBoxStyler . setColorLabelProperty ,
292
- CheckBoxStyler . resetColorProperty ) , "CheckBox" ) ;
244
+ }
293
245
294
- style . registerHandler ( style . fontInternalProperty , new style . StylePropertyChangedHandler (
295
- CheckBoxStyler . setFontInternalProperty ,
296
- CheckBoxStyler . resetFontInternalProperty ,
297
- CheckBoxStyler . getNativeFontInternalValue ) , "CheckBox" ) ;
298
246
299
- style . registerHandler ( style . backgroundColorProperty , new style . StylePropertyChangedHandler (
300
- CheckBoxStyler . setColorLabelProperty ,
301
- CheckBoxStyler . resetColorProperty ) , "CheckBox" ) ;
302
- }
247
+ function onCheckedPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
248
+ checkbox . _onCheckedPropertyChanged ( checkbox , oldValue , newValue ) ;
249
+ }
250
+ function onTextPropertyChanged ( checkbox : CheckBox , oldValue , newValue ) {
251
+ checkbox . _onTextPropertyChanged ( checkbox , oldValue , newValue ) ;
303
252
}
304
253
305
- CheckBoxStyler . registerHandlers ( ) ;
254
+
255
+ checkedProperty . register ( CheckBox ) ;
256
+ textProperty . register ( CheckBox ) ;
257
+ fillColorProperty . register ( Style ) ;
258
+ tintColorProperty . register ( Style ) ;
0 commit comments