forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number.view.ts
63 lines (48 loc) · 1.52 KB
/
number.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
namespace $.$$ {
/**
* Component for entering, incrementing and decrementing numeric values.
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_number_demo
*/
export class $mol_number extends $.$mol_number {
value_limited( next? : any ) : number {
if ( next === undefined ) return this.value()
if ( next === '' ) return this.value( Number.NaN )
const min = this.value_min()
const max = this.value_max()
const val = Number( next )
if( val < min ) return this.value( min )
if( val > max ) return this.value( max )
return this.value( val )
}
override event_dec( next? : Event ) {
this.value_limited( ( this.value_limited() || 0 ) - this.precision_change() )
}
override event_inc( next? : Event ) {
this.value_limited( ( this.value_limited() || 0 ) + this.precision_change() )
}
override value_string( next? : string ) {
const next_num = this.value_limited( next )
const precisionView = this.precision_view()
if( next_num === 0 ) return '0'
if( !next_num ) return ''
if( precisionView >= 1 ) {
return ( next_num / precisionView ).toFixed()
} else {
const fixedNumber = Math.log10( 1 / precisionView )
return next_num.toFixed( Math.ceil( fixedNumber ) )
}
}
@ $mol_mem
override dec_enabled() : boolean {
return this.enabled() && (
!( ( this.value() || 0 ) <= this.value_min() )
)
}
@ $mol_mem
override inc_enabled() : boolean {
return this.enabled() && (
!( ( this.value() || 0 ) >= this.value_max() )
)
}
}
}