Skip to content

Commit 7342b12

Browse files
committed
fix: add periods, remove rank
1 parent 3673a93 commit 7342b12

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

examples/vue-cron.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
<div>
77
<span>
88
Every:
9-
<select @input="p.rankEvents.input(parseInt($event.target.value))">
10-
<option v-for="item in p.rankData.items" :key="item.value" :value="item.value">{{item.text}}</option>
9+
<select @input="p.periodEvents.input(JSON.parse($event.target.value))">
10+
<option v-for="item in p.periodData.items" :key="item.text" :value="JSON.stringify(item.value)">{{item.text}}</option>
1111
</select>
1212
</span>
1313

1414

15-
<template v-for="f in orderByRank(p.fields)">
16-
<span :key="f.id" v-if="p.rankAttrs.value >= f.rank">
15+
<template v-for="f in p.fields">
16+
<span :key="f.id">
1717
{{f.id}}:
1818
<select @input="f.events.input(getSelected($event.target))" multiple>
1919
<option v-for="item in f.items" :key="item.value" :value="item.value">{{item.text}}</option>
@@ -24,7 +24,7 @@
2424
<div>-</div>
2525

2626
<div>error:{{p.error}}</div>
27-
<div>rank:{{p.rankAttrs.value}}</div>
27+
<div>period:{{p.periodAttrs.value}}</div>
2828
<div v-for="f in p.fields" :key="'div'+f.id">{{f.id}}: {{f.attrs.value}}, {{f.cron}}, {{f.selectedStr}}</div>
2929
</div>
3030
</template>
@@ -69,10 +69,6 @@ export default {
6969
let options = select && select.options;
7070
return Array.from(options).filter((opt) => opt.selected).map((opt) => opt.value)
7171
},
72-
73-
orderByRank(fields){
74-
return fields.slice().sort((a, b) => b.rank - a.rank)
75-
}
7672
}
7773
}
7874
</script>

src/core.vue

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ export default {
2424
let locale = getLocale(this.locale)
2525
2626
return [
27-
{id: 'minute', items: locale.minuteItems, rank: 0},
28-
{id: 'hour', items: locale.hourItems, rank: 1},
29-
{id: 'day', items: locale.dayItems, rank: 2},
30-
{id: 'month', items: locale.monthItems, rank: 4},
31-
{id: 'dayOfWeek', items: locale.dayOfWeekItems, rank: 3},
27+
{id: 'minute', items: locale.minuteItems},
28+
{id: 'hour', items: locale.hourItems},
29+
{id: 'day', items: locale.dayItems},
30+
{id: 'month', items: locale.monthItems},
31+
{id: 'dayOfWeek', items: locale.dayOfWeekItems},
3232
]
3333
}
3434
},
35-
ranks: {
35+
periods: {
3636
type: Array,
3737
default: () => {
3838
return [
39-
{ text: 'Minute', value: -1 },
40-
{ text: 'Hour', value: 0 },
41-
{ text: 'Day', value: 1 },
42-
{ text: 'Week', value: 2 },
43-
{ text: 'Month', value: 3 },
44-
{ text: 'Year', value: 4 },
39+
{ text: 'Minute', value: [] },
40+
{ text: 'Hour', value: ['minute'] },
41+
{ text: 'Day', value: ['hour', 'minute'] },
42+
{ text: 'Week', value: ['day', 'hour', 'minute'] },
43+
{ text: 'Month', value: ['dayOfWeek', 'day', 'hour', 'minute'] },
44+
{ text: 'Year', value: ['month', 'dayOfWeek', 'day', 'hour', 'minute'] },
4545
]
4646
}
4747
}
@@ -56,7 +56,7 @@ export default {
5656
return {
5757
selected: selected,
5858
error: '',
59-
selectedRank: this.ranks[this.ranks.length-1].value
59+
selectedPeriod: this.periods[this.periods.length-1].value
6060
}
6161
},
6262
@@ -71,7 +71,13 @@ export default {
7171
}, {})
7272
},
7373
computedFields(){
74-
return this.fields.map((f) => new Field(f.id, f.items, f.rank))
74+
return this.fields.map((f) => new Field(f.id, f.items))
75+
},
76+
filteredFields(){
77+
return this.selectedPeriod.map((fieldId) => {
78+
let i = this.fieldIndex[fieldId]
79+
return this.computedFields[i]
80+
})
7581
}
7682
},
7783
@@ -88,7 +94,7 @@ export default {
8894
},
8995
deep:true
9096
},
91-
selectedRank: {
97+
selectedPeriod: {
9298
handler: function(){
9399
this.selectedToCron(this.selected)
94100
},
@@ -102,7 +108,7 @@ export default {
102108
}
103109
104110
let fieldProps = []
105-
for(let field of this.computedFields){
111+
for(let field of this.filteredFields){
106112
let i = this.fieldIndex[field.id]
107113
let values = this.selected[field.id]
108114
@@ -129,16 +135,16 @@ export default {
129135
error: this.error,
130136
fields: fieldProps,
131137
132-
rankAttrs: {
133-
value: this.selectedRank
138+
periodAttrs: {
139+
value: this.selectedPeriod
134140
},
135-
rankEvents: {
141+
periodEvents: {
136142
input: (evt) => {
137-
this.selectedRank = evt
143+
this.selectedPeriod = evt
138144
}
139145
},
140-
rankData: {
141-
items: this.ranks
146+
periodData: {
147+
items: this.periods
142148
},
143149
})
144150
},
@@ -160,7 +166,7 @@ export default {
160166
161167
for(var i = 0; i < this.splitValue.length; i++){
162168
let field = this.computedFields[i]
163-
if(field.rank > this.selectedRank){
169+
if(!this.selectedPeriod.includes(field.id)){
164170
continue
165171
}
166172
@@ -178,7 +184,7 @@ export default {
178184
179185
let strings = []
180186
for(let field of this.computedFields){
181-
if(field.rank > this.selectedRank){
187+
if(!this.selectedPeriod.includes(field.id)){
182188
strings.push('*')
183189
continue
184190
}

src/util.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ class Field {
1515
*
1616
* @param {String} name
1717
* @param {Array} items
18-
* @param {Number} rank
1918
*/
20-
constructor(id, items, rank){
19+
constructor(id, items){
2120
this.id = id
2221
this.items = items
23-
this.rank = rank
2422

2523
this.itemMap = this.items.reduce((acc, item) => {
2624
acc[item.value] = item

test/core.test.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { shallowMount } from '@vue/test-utils'
22
import VueCron from '../src/core.vue'
3+
import 'regenerator-runtime/runtime'
34

4-
test('test VueCron', () => {
5+
test('test VueCron', async () => {
56
// render the component
67
let onInput = jest.fn()
78
let props = null
@@ -23,16 +24,18 @@ test('test VueCron', () => {
2324
}
2425
})
2526

26-
expect(props.fields[0].attrs.value).toEqual([0,15,30,45])
27-
expect(props.fields[1].attrs.value).toEqual([12])
27+
await wrapper.vm.$nextTick()
28+
29+
expect(props.fields[4].attrs.value).toEqual([0,15,30,45])
30+
expect(props.fields[3].attrs.value).toEqual([12])
2831
expect(props.fields[2].attrs.value).toEqual([])
29-
expect(props.fields[3].attrs.value).toEqual([])
30-
expect(props.fields[4].attrs.value).toEqual([])
32+
expect(props.fields[1].attrs.value).toEqual([])
33+
expect(props.fields[0].attrs.value).toEqual([])
3134

32-
props.fields[0].events.input([1,2,3,4,5])
35+
props.fields[4].events.input([1,2,3,4,5])
36+
37+
await wrapper.vm.$nextTick()
3338

34-
wrapper.vm.$nextTick(() => {
35-
expect(onInput).toHaveBeenCalled()
36-
})
39+
expect(onInput).toHaveBeenCalled()
3740

3841
})

0 commit comments

Comments
 (0)