1+ /**
2+ * Copyright (c) 2022 Hengyang Zhang
3+ *
4+ * This software is released under the MIT License.
5+ * https://opensource.org/licenses/MIT
6+ */
7+ import { Ref , PropType , ComputedRef , watch } from "vue"
8+
9+ import { ElOption , ElSelect , ElTimePicker } from "element-plus"
10+ import { defineComponent , ref , h , computed } from "vue"
11+ import { t } from "@app/locale"
12+
13+ function computeSecondToDate ( secondOfDate : number ) : Date {
14+ const now = new Date ( )
15+ const hour = Math . floor ( secondOfDate / 3600 )
16+ const minute = Math . floor ( ( secondOfDate - hour * 3600 ) / 60 )
17+ const second = Math . floor ( secondOfDate % 60 )
18+ now . setHours ( hour )
19+ now . setMinutes ( minute )
20+ now . setSeconds ( second )
21+ now . setMilliseconds ( 0 )
22+ return now
23+ }
24+
25+ function computeDateToSecond ( date : Date ) {
26+ const hour = date . getHours ( )
27+ const minute = date . getMinutes ( )
28+ const second = date . getSeconds ( )
29+ return hour * 3600 + minute * 60 + second
30+ }
31+
32+ const _default = defineComponent ( {
33+ name : "DarkModeInput" ,
34+ props : {
35+ modelValue : String as PropType < Timer . AppearanceOptionDarkMode > ,
36+ startSecond : Number ,
37+ endSecond : Number
38+ } ,
39+ emits : [ "change" ] ,
40+ setup ( props , ctx ) {
41+ const darkMode : Ref < Timer . AppearanceOptionDarkMode > = ref ( props . modelValue )
42+ // @ts -ignore
43+ const start : Ref < Date > = ref ( computeSecondToDate ( props . startSecond ) )
44+ // @ts -ignore
45+ const end : Ref < Date > = ref ( computeSecondToDate ( props . endSecond ) )
46+ watch ( ( ) => props . modelValue , newVal => darkMode . value = newVal )
47+ watch ( ( ) => props . startSecond , newVal => start . value = computeSecondToDate ( newVal ) )
48+ watch ( ( ) => props . endSecond , newVal => end . value = computeSecondToDate ( newVal ) )
49+ const startSecond : ComputedRef < number > = computed ( ( ) => computeDateToSecond ( start . value ) )
50+ const endSecond : ComputedRef < number > = computed ( ( ) => computeDateToSecond ( end . value ) )
51+
52+ const handleChange = ( ) => ctx . emit ( "change" , darkMode . value , [ startSecond . value , endSecond . value ] )
53+
54+ return ( ) => {
55+ const result = [ h ( ElSelect , {
56+ modelValue : darkMode . value ,
57+ size : 'small' ,
58+ style : { width : '120px' , marginLeft : '10px' } ,
59+ onChange : async ( newVal : string ) => {
60+ const before = darkMode . value
61+ darkMode . value = newVal as Timer . AppearanceOptionDarkMode
62+ handleChange ( )
63+ }
64+ } , {
65+ default : ( ) => [ "on" , "off" , "timed" ] . map (
66+ value => h ( ElOption , { value, label : t ( msg => msg . option . appearance . darkMode . options [ value ] ) } )
67+ )
68+ } ) ]
69+ if ( darkMode . value === "timed" ) {
70+ result . push (
71+ h ( ElTimePicker , {
72+ modelValue : start . value ,
73+ size : "small" ,
74+ style : { marginLeft : '10px' } ,
75+ "onUpdate:modelValue" : ( newVal ) => {
76+ start . value = newVal
77+ handleChange ( )
78+ } ,
79+ clearable : false
80+ } ) ,
81+ h ( 'a' , '-' ) ,
82+ h ( ElTimePicker , {
83+ modelValue : end . value ,
84+ size : "small" ,
85+ "onUpdate:modelValue" : ( newVal ) => {
86+ end . value = newVal
87+ handleChange ( )
88+ } ,
89+ clearable : false
90+ } )
91+ )
92+ }
93+ return result
94+ }
95+ }
96+ } )
97+
98+ export default _default
0 commit comments