cqf
2022-07-12 b75a4660d54caa29048f99215a9e6281ea884098
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
 
<template>
    <div class="el-quarter-picker">
        <el-popover
            v-model="visible"
            :disabled="!canPopover"
            :tabindex="null"
            placement="bottom-start"
            transition="el-zoom-in-top"
            trigger="click">
 
            <div class="el-date-picker">
                <div class="el-picker-panel__body">
                    <div class="el-date-picker__header el-date-picker__header--bordered" style="margin:0px; line-height:30px">
                        <button
                            type="button"
                            @click="clickLast"
                            aria-label="前一年"
                            class="el-picker-panel__icon-btn el-date-picker__prev-btn el-icon-d-arrow-left"></button>
                        <span role="button" class="el-date-picker__header-label" @click="clickYear">{{ title }}</span>
                        <button
                            type="button"
                            @click="clickNext"
                            aria-label="后一年"
                            class="el-picker-panel__icon-btn el-date-picker__next-btn el-icon-d-arrow-right"></button>
                    </div>
                    <div class="el-picker-panel__content" style="margin:0px; width:100%">
                        <table class="el-month-table" style="">
                            <tbody>
                            <tr v-for="line in lineCount" :key="line">
                                <td v-for="index in (line * 4 <= viewList.length ? 4 : viewList.length - (line - 1) * 4)" :key="index" :class="{ today: viewList[(line - 1) * 4 + index - 1].current, current: viewList[(line - 1) * 4 + index - 1].active }">
                                    <div><a class="cell" @click="clickItem(viewList[(line - 1) * 4 + index - 1])">{{ viewList[(line - 1) * 4 + index - 1].label }}</a></div>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
 
            <el-input
                slot="reference"
                @change="changeText"
                @mouseenter.native="mouseEnter"
                @mouseleave.native="mouseLeave"
                :placeholder="placeholder"
                v-model="text"
                :size="size"
                :readonly="!canEdit"
                :disabled="disabled">
                <i slot="prefix" class="el-input__icon el-icon-date"></i>
                <i slot="suffix" class="el-input__icon el-icon-circle-close" v-show="showClear" style="cursor:pointer" @click.stop="clear"></i>
            </el-input>
        </el-popover>
    </div>
</template>
 
<script>
export default {
    name: 'ElQuarterPicker',
    props: {
        placeholder: {
            type: String,
            default: ''
        },
        size: {
            type: String,
            default: ''
        },
        readonly: {
            type: Boolean,
            default: false
        },
        clearable: {
            type: Boolean,
            default: true
        },
        editable: {
            type: Boolean,
            default: true
        },
        disabled: {
            type: Boolean,
            default: false
        },
        format: {
            type: String,
            default: 'yyyy年第Q季度'
        },
        valueFormat: {
            type: String,
            default: 'yyyy年第q季度'
        },
        value: {
            type: String,
            default: ''
        }
    },
    model: {
        prop: 'value',
        event: 'change'
    },
    watch: {
        value(val) {
            // console.log('change-------', val)
            this.changeValue(val)
        },
        readonly(val) {
            this.canEdit = !val && this.editable
            this.canPopover = !this.disabled && !val
        },
        editable(val) {
            this.canEdit = !this.readonly && val
        },
        disabled(val) {
            this.canPopover = !val && !this.readonly
        }
    },
    data() {
        return {
            visible: false,
            showClear: false, // 控制清空按钮展示
            canEdit: true, // 是否可编辑
            canPopover: true, // 选择器弹出是否可用
            text: '', // 文本框值
            viewType: 1, // 视图类型,1季度,2年度
            viewYear: 0, // 当前年份
            viewList: [], // 数据列表
            lineCount: 0, // 数据行数
            title: '', // 选择器标题
            data: [0, 0] // 当前选择年度-季度
        }
    },
    mounted() {
        // console.log('mounted--------', this.value)
        this.changeValue(this.value)
 
        // 设置文本框是否可编辑
        this.canEdit = !this.readonly && this.editable
        this.canPopover = !this.disabled && !this.readonly
 
        // 监听按键(上下左右键可以切换季度)
        document.onkeydown = (event) => {
            if (this.visible) {
                const data = [this.data[0], this.data[1]]
                if (data[0] < 1 || data[1] < 1) {
                    // 以当前季度为标准
                    const curDate = new Date()
                    data[0] = curDate.getFullYear()
                    data[1] = parseInt(curDate.getMonth() / 3) + 1
                }
                if (event.code === 'ArrowLeft') {
                    // 上一个季度
                    if (data[1] === 1) {
                        data[0] = data[0] - 1
                        data[1] = 4
                    } else {
                        data[1] = data[1] - 1
                    }
                } else if (event.code === 'ArrowRight') {
                    // 下一个季度
                    if (data[1] === 4) {
                        data[0] = data[0] + 1
                        data[1] = 1
                    } else {
                        data[1] = data[1] + 1
                    }
                } else if (event.code === 'ArrowUp') {
                    // 上一年季度
                    data[0] = data[0] - 1
                } else if (event.code === 'ArrowDown') {
                    // 下一年季度
                    data[0] = data[0] + 1
                } else {
                    return
                }
 
                // 超过年限的不处理
                if (data[0] < 1000 || data[0] > 9999) {
                    return
                }
                this.data = data
                this.viewType = 1
                this.viewYear = data[0]
                this.$emit('change', this.formatTo(data, this.valueFormat))
            }
        }
    },
    destroyed() {
        document.onkeydown = null
    },
    methods: {
        // 季度文本变更
        changeText() {
            if (this.checkFormat(this.format, this.text)) {
                // 设置值
                this.formatFrom(this.text, this.format)
                this.$emit('change', this.formatTo(this.data, this.valueFormat))
            } else {
                // 输入了无效的格式,还原回原来的值
                if (this.data[0] < 1 || this.data[1] < 1) {
                    this.text = ''
                } else {
                    this.text = this.formatTo(this.data, this.format)
                }
            }
            this.visible = false
        },
        // 鼠标进入
        mouseEnter() {
            if (!this.disabled && !this.readonly && this.clearable && this.text !== '') {
                this.showClear = true
            }
        },
        // 鼠标离开
        mouseLeave() {
            if (!this.disabled && this.clearable) {
                this.showClear = false
            }
        },
        // 清除季度
        clear() {
            this.showClear = false
            this.visible = false
            this.$emit('change', '')
        },
        // 季度值变更
        changeValue(val) {
            this.viewType = 1
            if (val) {
                // 反向格式化
                this.formatFrom(val, this.valueFormat)
                this.text = this.formatTo(this.data, this.format)
                this.viewYear = this.data[0]
            } else {
                this.text = ''
                this.data = [0, 0]
                this.viewYear = new Date().getFullYear()
            }
            this.initView()
        },
        // 初始化视图数据
        initView() {
            const list = []
            const curDate = new Date()
            const curYear = curDate.getFullYear()
            const curQuarter = parseInt(curDate.getMonth() / 3) + 1
            if (this.viewType === 1) {
                let index = 0
                for (const i of '一二三四') {
                    index++
                    const item = { label: '第' + i + '季度', year: this.viewYear, quarter: index, current: false, active: false }
                    if (this.viewYear === curYear && index === curQuarter) {
                        item.current = true
                    } else if (this.viewYear === this.data[0] && index === this.data[1]) {
                        item.active = true
                    }
                    list.push(item)
                }
                this.title = this.viewYear + ' 年'
            } else {
                const start = parseInt(this.viewYear / 10) * 10
                this.viewYear = start
                for (let i = 0; i < 10; i++) {
                    const year = start + i
                    const item = { label: year + '', year: year, current: false, active: false }
                    if (year === curYear) {
                        item.current = true
                    } else if (year === this.data[0]) {
                        item.active = true
                    }
                    list.push(item)
                }
                this.title = start + ' 年 - ' + (start + 9) + ' 年'
            }
            this.viewList = list
            this.lineCount = parseInt(list.length / 4)
            if (list.length % 4 > 0) {
                this.lineCount++
            }
        },
        // 校验季度格式是否正确
        checkFormat(pattern, val) {
            // 格式转成正则表达式
            let text = ''
            for (const char of pattern) {
                const dict = '\\^$.+?*[]{}!'
                if (dict.indexOf(char) === -1) {
                    text += char
                } else {
                    text += '\\' + char
                }
            }
            text = text.replace('yyyy', '[1-9]\\d{3}')
            text = text.replace('qq', '0[1-4]')
            text = text.replace('q', '[1-4]')
            text = text.replace('Q', '[一二三四]')
            text = '^' + text + '$'
            const patt = new RegExp(text)
            return patt.test(val)
        },
        // 格式化季度到指定格式
        formatTo(data, pattern) {
            let text = pattern.replace('yyyy', '' + data[0])
            text = text.replace('qq', '0' + data[1])
            text = text.replace('q', '' + data[1])
            text = text.replace('Q', '一二三四'.substr(data[1] - 1, 1))
            return text
        },
        // 以指定格式解析季度
        formatFrom(str, pattern) {
            const year = this.findText(str, pattern, 'yyyy')
            const quarter = this.findText(str, pattern, ['qq', 'q', 'Q'])
            this.data = [year, quarter]
        },
        // 查找文本数值
        findText(str, pattern, find) {
            if (find instanceof Array) {
                for (const f of find) {
                    const val = this.findText(str, pattern, f)
                    if (val !== -1) {
                        return val
                    }
                }
                return -1
            }
            const index = pattern.indexOf(find)
            if (index === -1) {
                return index
            }
            const val = str.substr(index, find.length)
            if (find === 'Q') {
                return '一二三四'.indexOf(val) + 1
            } else {
                return parseInt(val)
            }
        },
        // 年份点击
        clickYear() {
            if (this.viewType !== 1) {
                return
            }
            // 切换年度选择器
            this.viewType = 2
            this.initView()
        },
        // 季度选择
        clickItem(item) {
            // console.log('select--------', item)
            if (this.viewType === 1) {
                // 选择季度
                this.$emit('change', this.formatTo([item.year, item.quarter], this.valueFormat))
                this.visible = false
            } else {
                // 选择年度
                this.viewType = 1
                this.viewYear = item.year
                this.initView()
            }
        },
        // 上一年
        clickLast() {
            if (this.viewYear > 1000) {
                if (this.viewType === 1) {
                    this.viewYear--
                    this.initView()
                } else {
                    this.viewYear = this.viewYear - 10
                    this.initView()
                }
            }
        },
        // 下一年
        clickNext() {
            if (this.viewYear < 9999) {
                if (this.viewType === 1) {
                    this.viewYear++
                    this.initView()
                } else {
                    this.viewYear = this.viewYear + 10
                    this.initView()
                }
            }
        }
    }
}
</script>
 
<style>
.el-quarter-picker {
    width: 220px;
    display: inline-block;
}
</style>