-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
select.view.ts
91 lines (72 loc) · 1.97 KB
/
select.view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
namespace $.$$ {
/**
* Allow user to select value from various options and displays current value.
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_select_demo_colors
*/
export class $mol_select extends $.$mol_select {
@ $mol_mem
filter_pattern( next? : string ) {
this.focused()
return next || ''
}
open() {
this.showed( true )
}
@ $mol_mem
options() {
return Object.keys( this.dictionary() ) as readonly string[]
}
@ $mol_mem
options_filtered() {
let options = this.options()
options = options.filter( $mol_match_text( this.filter_pattern() , ( id : string )=> [ this.option_label( id ) ] ) )
const index = options.indexOf( this.value() )
if( index >= 0 ) options = [ ... options.slice( 0 , index ) , ... options.slice( index + 1 ) ]
return options
}
option_label( id : string ) {
const value = this.dictionary()[ id ]
return (value == null ? id : value) || this.option_label_default()
}
option_rows() {
return this.options_filtered().map( ( option : string ) => this.Option_row( option ) )
}
@ $mol_mem
option_focused( component? : $mol_view ) {
if( component == null ) {
for( let comp of this.nav_components() ) {
if( comp && comp.focused() ) return comp
}
return null
}
if( this.showed() ) {
component.focused( true )
}
return component
}
event_select( id : string , event? : MouseEvent ) {
this.value( id )
this.showed( false )
event?.preventDefault()
}
nav_components() {
if( this.options().length > 1 && this.Filter() ) {
return [ this.Filter() , ... this.option_rows() ]
} else {
return this.option_rows()
}
}
trigger_content() {
return [
... this.option_content( this.value() ),
this.Trigger_icon(),
] as readonly $mol_view_content[]
}
menu_content() {
return [
... this.option_rows(),
... ( this.options_filtered().length === 0 ) ? [ this.No_options() ] : []
]
}
}
}