-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
date.view.ts
126 lines (90 loc) · 2.74 KB
/
date.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
namespace $.$$ {
/**
* Date presenter and picker.
* @see https://mol.hyoo.ru/#!section=demos/demo=mol_date_demo
*/
export class $mol_date extends $.$mol_date {
trigger_content() {
return [ this.value() || this.Icon() ]
}
input_mask( val : string ) {
return val.length > 8 ? '____-__-__ __:__' : '____-__-__ '
}
override input_content() {
return [
this.Today(),
this.Input(),
... this.value() ? [ this.Clear() ] : [],
]
}
@ $mol_mem
value( val? : string ) {
const moment = this.value_moment()
if( val === undefined ) return moment?.toString( 'YYYY-MM-DD hh:mm' ) ?? ''
const moment2 = $mol_try( ()=> val && new $mol_time_moment( val ).merge({ offset: new $mol_time_moment().offset }) ) || null
if( moment2 instanceof Error ) return val
this.value_moment( moment2! )
return val
}
@ $mol_mem
value_moment( next? : $mol_time_moment ) {
const stamp = this.value_number()
if( next === undefined ) {
return isNaN( stamp ) ? null! : new $mol_time_moment( stamp )
}
this.value_number( next?.valueOf() ?? NaN )
return next
}
@ $mol_mem
value_number( next? : number ): number {
const value = this.value()
if( next === undefined ) {
if (!value) return NaN
const moment = $mol_try( ()=> new $mol_time_moment( value ) )
if( moment instanceof Error ) return NaN
return moment!.valueOf() ?? NaN
}
const moment = $mol_try( ()=> new $mol_time_moment( next ) )
this.value(moment.toString(value.length > 12 ? 'YYYY-MM-DD hh:mm': 'YYYY-MM-DD' ))
return next
}
@ $mol_mem
value_moment_today() {
return this.value()
? new $mol_time_moment().mask( this.value() )
: new $mol_time_moment()
}
override clear() {
this.value( '' )
this.Input().focused( true )
this.Input().selection( [ 0 , 0 ] )
}
@ $mol_mem
month_moment( next? : $mol_time_moment ) {
if( next ) return next
let moment = $mol_try( ()=> new $mol_time_moment( this.value_changed().replace( /\D+$/, '' ) ).mask( '0000-00' ) )
if( moment instanceof Error || !moment.year ) return new $mol_time_moment().mask( '0000-00' )
if( moment.month === undefined ) {
moment = moment.merge( { month: 0 } )
}
return moment
}
day_selected( day : string ) {
return this.value_moment()?.toString( 'YYYY-MM-DD' ) === day
}
day_click( day : string ) {
const moment = new $mol_time_moment( day )
this.value_moment( this.value_moment()?.merge( moment ) ?? moment )
this.showed( false )
}
prev() {
this.month_moment( this.month_moment().shift( { month : -1 } ) )
}
next() {
this.month_moment( this.month_moment().shift( { month : +1 } ) )
}
override today_click() {
this.value_moment( this.value_moment_today() )
}
}
}