66 */
77
88import { ElForm } from "element-plus"
9- import { computed , ComputedRef , defineComponent , h , ref , Ref , SetupContext } from "vue"
9+ import { computed , ComputedRef , defineComponent , h , reactive , ref , Ref , UnwrapRef } from "vue"
1010import '../style/el-input'
11- import pathEdit from "./path-edit"
12- import timeFormItem from "./time-limit "
13- import urlFormItem , { FormUrlProps , Protocol } from "./url "
14- import UrlPathItem from "./url-path-item "
11+ import LimitPathEdit from "./path-edit"
12+ import LimitUrlFormItem from "./url "
13+ import LimitTimeFormItem from "./time-limit "
14+ import { parseUrl } from "./common "
1515
16- // Limited time
17- const hourRef : Ref < number > = ref ( )
18- const minuteRef : Ref < number > = ref ( )
19- const secondRef : Ref < number > = ref ( )
20-
21- // Limited url
22- const protocolRef : Ref < string > = ref ( )
23- const pathItemsRef : Ref < UrlPathItem [ ] > = ref ( [ ] )
24- const urlRef : ComputedRef < string > = computed ( ( ) => pathItemsRef . value . map ( i => i . toString ( ) ) . join ( '/' ) )
16+ function computeFormInfo ( urlInfo : UrlInfo , timeRef : Ref < number > ) : FormInfo {
17+ const { url, protocol } = urlInfo
18+ const result : FormInfo = {
19+ timeLimit : timeRef . value || 0 ,
20+ condition : url ? protocol + url : ''
21+ }
22+ return result
23+ }
2524
26- const timeProps = { hourRef, minuteRef, secondRef }
27- const urlProps : FormUrlProps = { protocolRef, pathItemsRef, urlRef }
28- const pathEditProps = { pathItemsRef }
29- const render = ( ) => h ( ElForm ,
30- { labelWidth : '100px' } ,
31- ( ) => [ timeFormItem ( timeProps ) , urlFormItem ( urlProps ) , pathEdit ( pathEditProps ) ]
32- )
25+ function init ( timeRef : Ref < number > , urlInfo : UrlInfo ) {
26+ // 1 hour
27+ timeRef . value = 3600
28+ urlInfo . protocol = '*://'
29+ urlInfo . url = ''
30+ }
3331
34- function init ( ) {
35- hourRef . value = 1
36- minuteRef . value = undefined
37- secondRef . value = undefined
38- protocolRef . value = Protocol . ALL
39- pathItemsRef . value = [ ]
32+ function parseRow ( row : timer . limit . Item , timeRef : Ref < number > , urlInfo : UrlInfo ) {
33+ const { cond , time } = row
34+ timeRef . value = time || 0
35+ const { protocol , url } = parseUrl ( cond )
36+ urlInfo . url = url
37+ urlInfo . protocol = protocol
4038}
4139
42- init ( )
40+ const _default = defineComponent ( {
41+ name : "LimitForm" ,
42+ setup ( _ , ctx ) {
43+ // Limited time
44+ const timeRef : Ref < number > = ref ( )
45+ // Limited url
46+ const urlInfo : UnwrapRef < UrlInfo > = reactive ( {
47+ protocol : undefined ,
48+ url : undefined
49+ } )
50+ const editMode : Ref < Mode > = ref ( 'create' )
51+ init ( timeRef , urlInfo )
4352
44- export type FormData = {
45- /**
46- * Time / seconds
47- */
48- timeLimit : number
49- /**
50- * Protocol + url
51- */
52- url : string
53- }
53+ const formInfo : ComputedRef < FormInfo > = computed ( ( ) => computeFormInfo ( urlInfo , timeRef ) )
54+ const canEditUrl : ComputedRef < boolean > = computed ( ( ) => editMode . value === 'create' )
55+ const pathEditRef : Ref = ref ( )
5456
55- const handleGetData = ( ) => {
56- let timeLimit = 0
57- timeLimit += ( hourRef . value || 0 ) * 3600
58- timeLimit += ( minuteRef . value || 0 ) * 60
59- timeLimit += ( secondRef . value || 0 )
60- const url = urlRef . value || ''
61- const protocol = protocolRef . value
62- const result : FormData = {
63- timeLimit,
64- url : url ? protocol + url : ''
65- }
66- return result
67- }
57+ function setUrl ( newUrl : string ) {
58+ urlInfo . url = newUrl
59+ pathEditRef ?. value ?. forceUpdateUrl ?.( newUrl )
60+ }
6861
69- const exposeOption = {
70- getData : ( ) => handleGetData ( ) ,
71- clean : ( ) => init ( )
72- }
62+ ctx . expose ( {
63+ getData : ( ) => formInfo . value ,
64+ clean : ( ) => {
65+ editMode . value = 'create'
66+ init ( timeRef , urlInfo )
67+ } ,
68+ modify : ( row : timer . limit . Item ) => {
69+ editMode . value = 'modify'
70+ parseRow ( row , timeRef , urlInfo )
71+ setUrl ( urlInfo . url )
72+ } ,
73+ } )
7374
74- const _default = defineComponent ( {
75- setup : ( _ , context : SetupContext ) => context . expose ( exposeOption ) ,
76- render
75+ return ( ) => h ( ElForm ,
76+ { labelWidth : 120 } ,
77+ ( ) => {
78+ const items = [
79+ h ( LimitTimeFormItem , {
80+ modelValue : timeRef . value ,
81+ onChange : ( newVal : number ) => timeRef . value = newVal
82+ } ) ,
83+ h ( LimitUrlFormItem , {
84+ url : urlInfo . url ,
85+ protocol : urlInfo . protocol ,
86+ disabled : ! canEditUrl . value ,
87+ onUrlChange : ( newUrl : string ) => setUrl ( newUrl ) ,
88+ onProtocolChange : ( newProtocol : Protocol ) => urlInfo . protocol = newProtocol
89+ } ) ,
90+ ]
91+ canEditUrl . value && items . push (
92+ h ( LimitPathEdit , {
93+ ref : pathEditRef ,
94+ disabled : editMode . value === 'modify' ,
95+ url : urlInfo . url ,
96+ onUrlChange : ( newVal : string ) => {
97+ urlInfo . url = newVal
98+ }
99+ } )
100+ )
101+ return items
102+ }
103+ )
104+ }
77105} )
78106
79107export default _default
0 commit comments