1
1
// Copyright (c) Jupyter Development Team.
2
2
// Distributed under the terms of the Modified BSD License.
3
3
4
- import { DOMWidgetView , StyleModel } from '@jupyter-widgets/base' ;
4
+ import { DOMWidgetView , StyleModel , StyleView } from '@jupyter-widgets/base' ;
5
5
6
6
import { CoreDOMWidgetModel } from './widget_core' ;
7
7
8
8
import { JUPYTER_CONTROLS_VERSION } from './version' ;
9
9
10
+ export function bold_to_weight ( value : boolean ) : string | null {
11
+ if ( value == true ) return 'bold' ;
12
+ if ( value == false ) return 'normal' ;
13
+ return null ;
14
+ }
15
+
16
+ export function italic_to_style ( value : boolean ) : string | null {
17
+ if ( value == true ) return 'italic' ;
18
+ if ( value == false ) return 'normal' ;
19
+ return null ;
20
+ }
21
+
22
+ export function underline_to_decoration ( value : boolean ) : string | null {
23
+ if ( value == true ) return 'underline' ;
24
+ if ( value == false ) return 'none' ;
25
+ return null ;
26
+ }
27
+
10
28
export class ButtonStyleModel extends StyleModel {
11
29
defaults ( ) : Backbone . ObjectHash {
12
30
return {
13
31
...super . defaults ( ) ,
14
32
_model_name : 'ButtonStyleModel' ,
15
33
_model_module : '@jupyter-widgets/controls' ,
16
- _model_module_version : JUPYTER_CONTROLS_VERSION
34
+ _model_module_version : JUPYTER_CONTROLS_VERSION ,
35
+ _view_name : 'ButtonStyleView' ,
36
+ _view_module : '@jupyter-widgets/controls' ,
37
+ _view_module_version : JUPYTER_CONTROLS_VERSION
17
38
} ;
18
39
}
19
40
20
41
public static styleProperties = {
42
+ bold : {
43
+ selector : '' ,
44
+ attribute : 'font-weight' ,
45
+ default : ''
46
+ } ,
21
47
button_color : {
22
48
selector : '' ,
23
49
attribute : 'background-color' ,
@@ -33,7 +59,7 @@ export class ButtonStyleModel extends StyleModel {
33
59
attribute : 'font-size' ,
34
60
default : ''
35
61
} ,
36
- font_style : {
62
+ italic : {
37
63
selector : '' ,
38
64
attribute : 'font-style' ,
39
65
default : ''
@@ -43,24 +69,54 @@ export class ButtonStyleModel extends StyleModel {
43
69
attribute : 'font-variant' ,
44
70
default : ''
45
71
} ,
46
- font_weight : {
47
- selector : '' ,
48
- attribute : 'font-weight' ,
49
- default : ''
50
- } ,
51
72
text_color : {
52
73
selector : '' ,
53
74
attribute : 'color' ,
54
75
default : ''
55
76
} ,
56
- text_decoration : {
77
+ underline : {
57
78
selector : '' ,
58
79
attribute : 'text-decoration' ,
59
80
default : ''
60
81
}
61
82
} ;
62
83
}
63
84
85
+ export class ButtonStyleView extends StyleView {
86
+ /**
87
+ * Handles when a trait value changes
88
+ */
89
+ handleChange ( trait : string , value : any ) : void {
90
+ // should be synchronous so that we can measure later.
91
+ const parent = this . options . parent as DOMWidgetView ;
92
+ if ( parent ) {
93
+ const ModelType = this . model . constructor as typeof StyleModel ;
94
+ const styleProperties = ModelType . styleProperties ;
95
+ const attribute = styleProperties [ trait ] . attribute ;
96
+ const selector = styleProperties [ trait ] . selector ;
97
+ const elements = selector
98
+ ? parent . el . querySelectorAll < HTMLElement > ( selector )
99
+ : [ parent . el ] ;
100
+ let transform = undefined ;
101
+ if ( trait == 'bold' ) transform = bold_to_weight ;
102
+ if ( trait == 'italic' ) transform = italic_to_style ;
103
+ if ( trait == 'underline' ) transform = underline_to_decoration ;
104
+ if ( transform !== undefined ) value = transform ( value ) ;
105
+ if ( value === null ) {
106
+ for ( let i = 0 ; i !== elements . length ; ++ i ) {
107
+ elements [ i ] . style . removeProperty ( attribute ) ;
108
+ }
109
+ } else {
110
+ for ( let i = 0 ; i !== elements . length ; ++ i ) {
111
+ elements [ i ] . style . setProperty ( attribute , value ) ;
112
+ }
113
+ }
114
+ } else {
115
+ console . warn ( 'Style not applied because a parent view does not exist' ) ;
116
+ }
117
+ }
118
+ }
119
+
64
120
export class ButtonModel extends CoreDOMWidgetModel {
65
121
defaults ( ) : Backbone . ObjectHash {
66
122
return {
0 commit comments