t20-common-lib 0.10.1 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "t20-common-lib",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "T20",
5
5
  "private": false,
6
6
  "main": "dist/index.js",
@@ -14,22 +14,24 @@
14
14
  "
15
15
  :type="type"
16
16
  :value-format="valueFormat"
17
- :placeholder="$l('选择日期')"
18
- :start-placeholder="$l('开始日期')"
19
- :end-placeholder="$l('结束日期')"
17
+ :placeholder="$lc('选择日期')"
18
+ :start-placeholder="$lc('开始日期')"
19
+ :end-placeholder="$lc('结束日期')"
20
20
  :picker-options="pickerOptionsAs"
21
21
  :clearable="clearable"
22
22
  v-bind="$attrs"
23
23
  v-on="listeners"
24
+ @focus="onFocus"
24
25
  />
25
26
  </template>
26
27
 
27
28
  <script>
29
+ import { $lc } from '../../../src/utils/i18n/index'
28
30
  import dayjs from 'dayjs'
29
31
 
30
32
  const getShortcutDefinitions = () => ({
31
33
  'today': {
32
- text: this.$l('今天'),
34
+ text: $lc('今天'),
33
35
  onClick(picker) {
34
36
  const start = new Date()
35
37
  const end = new Date()
@@ -37,7 +39,7 @@ const getShortcutDefinitions = () => ({
37
39
  }
38
40
  },
39
41
  'recent-week': {
40
- text: this.$l('最近一周'),
42
+ text: $lc('最近一周'),
41
43
  onClick(picker) {
42
44
  const end = new Date()
43
45
  const start = new Date()
@@ -46,7 +48,7 @@ const getShortcutDefinitions = () => ({
46
48
  }
47
49
  },
48
50
  'recent-month': {
49
- text: this.$l('最近一月'),
51
+ text: $lc('最近一月'),
50
52
  onClick(picker) {
51
53
  const start = new Date()
52
54
  const end = new Date()
@@ -57,7 +59,7 @@ const getShortcutDefinitions = () => ({
57
59
  }
58
60
  },
59
61
  'recent-3-months': {
60
- text: this.$l('最近三月'),
62
+ text: $lc('最近三月'),
61
63
  onClick(picker) {
62
64
  const start = new Date()
63
65
  const end = new Date()
@@ -68,7 +70,7 @@ const getShortcutDefinitions = () => ({
68
70
  }
69
71
  },
70
72
  'recent-6-months': {
71
- text: this.$l('最近六月'),
73
+ text: $lc('最近六月'),
72
74
  onClick(picker) {
73
75
  const start = new Date()
74
76
  const end = new Date()
@@ -79,7 +81,7 @@ const getShortcutDefinitions = () => ({
79
81
  }
80
82
  },
81
83
  'recent-year': {
82
- text: this.$l('最近一年'),
84
+ text: $lc('最近一年'),
83
85
  onClick(picker) {
84
86
  const start = new Date()
85
87
  const end = new Date()
@@ -90,7 +92,7 @@ const getShortcutDefinitions = () => ({
90
92
  }
91
93
  },
92
94
  'recent-3-years': {
93
- text: this.$l('最近三年'),
95
+ text: $lc('最近三年'),
94
96
  onClick(picker) {
95
97
  const start = new Date()
96
98
  const end = new Date()
@@ -101,7 +103,7 @@ const getShortcutDefinitions = () => ({
101
103
  }
102
104
  },
103
105
  'start-from-today': {
104
- text: this.$l('今天开始'),
106
+ text: $lc('今天开始'),
105
107
  onClick(picker) {
106
108
  const start = dayjs().format('YYYY-MM-DD')
107
109
  const end = dayjs().add(99, 'year').format('YYYY-MM-DD')
@@ -109,7 +111,7 @@ const getShortcutDefinitions = () => ({
109
111
  }
110
112
  },
111
113
  'end-today': {
112
- text: this.$l('今天截止'),
114
+ text: $lc('今天截止'),
113
115
  onClick(picker) {
114
116
  const end = dayjs().format('YYYY-MM-DD')
115
117
  const start = dayjs().subtract(100, 'year').format('YYYY-MM-DD')
@@ -117,7 +119,7 @@ const getShortcutDefinitions = () => ({
117
119
  }
118
120
  },
119
121
  'current-month': {
120
- text: this.$l('当月'),
122
+ text: $lc('当月'),
121
123
  onClick(picker) {
122
124
  const start = dayjs().startOf('month').toDate()
123
125
  const end = dayjs().endOf('month').toDate()
@@ -125,7 +127,7 @@ const getShortcutDefinitions = () => ({
125
127
  }
126
128
  },
127
129
  'current-year': {
128
- text: this.$l('当年'),
130
+ text: $lc('当年'),
129
131
  onClick(picker) {
130
132
  const start = dayjs().startOf('year').toDate()
131
133
  const end = dayjs().endOf('year').toDate()
@@ -217,6 +219,7 @@ export default {
217
219
  },
218
220
  data() {
219
221
  let shortcuts = undefined
222
+ let shortcutKeys = []
220
223
  const allShortcuts = getShortcutDefinitions()
221
224
 
222
225
  let config = this.shortcutConfig
@@ -229,13 +232,15 @@ export default {
229
232
  if (config) {
230
233
  shortcuts = config.map(item => {
231
234
  if (typeof item === 'string') {
235
+ shortcutKeys.push(item)
232
236
  return allShortcuts[item]
233
237
  }
238
+ shortcutKeys.push(null)
234
239
  return item
235
240
  }).filter(Boolean)
236
241
  }
237
242
 
238
- this.pickerOptionsAs = Object.assign(
243
+ const pickerOptionsAs = Object.assign(
239
244
  {
240
245
  disabledDate: this.minNow ? disabledDate_1 : this.maxNow ? disabledDate_2 : undefined,
241
246
  shortcuts
@@ -243,11 +248,18 @@ export default {
243
248
  this.pickerOptions
244
249
  )
245
250
 
246
- this.listeners = Object.assign({}, this.$listeners, {
251
+ const listeners = Object.assign({}, this.$listeners, {
247
252
  input: () => {},
248
253
  change: () => {}
249
254
  })
250
- return {}
255
+ return {
256
+ pickerOptionsAs,
257
+ listeners,
258
+ shortcutKeys
259
+ }
260
+ },
261
+ mounted() {
262
+ this.applyDefaultShortcut()
251
263
  },
252
264
  computed: {
253
265
  valueC: {
@@ -301,6 +313,78 @@ export default {
301
313
  }
302
314
  },
303
315
  methods: {
316
+ applyDefaultShortcut() {
317
+ const defaultKey = localStorage.getItem('t20-datepicker-default-shortcut')
318
+ if (defaultKey && !this.value) {
319
+ const allShortcuts = getShortcutDefinitions()
320
+ const shortcut = allShortcuts[defaultKey]
321
+ if (shortcut) {
322
+ const picker = {
323
+ $emit: (event, val) => {
324
+ if (event === 'pick') {
325
+ this.valueC = val
326
+ }
327
+ }
328
+ }
329
+ shortcut.onClick(picker)
330
+ }
331
+ }
332
+ },
333
+ onFocus() {
334
+ setTimeout(() => {
335
+ this.injectDefaultButtons()
336
+ }, 100)
337
+ },
338
+ injectDefaultButtons() {
339
+ const picker = this.$refs['date-picker'] && this.$refs['date-picker'].picker
340
+ if (!picker || !picker.$el) return
341
+
342
+ const sidebar = picker.$el.querySelector('.el-picker-panel__sidebar')
343
+ if (!sidebar) return
344
+
345
+ const shortcuts = sidebar.querySelectorAll('.el-picker-panel__shortcut')
346
+ const defaultKey = localStorage.getItem('t20-datepicker-default-shortcut')
347
+
348
+ shortcuts.forEach((el, index) => {
349
+ const key = this.shortcutKeys[index]
350
+ if (!key) return
351
+
352
+ if (el.querySelector('.shortcut-default-icon')) return
353
+
354
+ const icon = document.createElement('i')
355
+ const isDefault = defaultKey === key
356
+ icon.className = isDefault ? 'el-icon-star-on shortcut-default-icon' : 'el-icon-star-off shortcut-default-icon'
357
+ icon.style.marginLeft = '10px'
358
+ icon.style.cursor = 'pointer'
359
+ icon.style.color = isDefault ? '#E6A23C' : '#C0C4CC'
360
+ icon.title = '设为默认'
361
+
362
+ icon.onclick = (e) => {
363
+ e.stopPropagation()
364
+ this.setDefaultShortcut(key, index)
365
+ }
366
+
367
+ el.appendChild(icon)
368
+ })
369
+ },
370
+ setDefaultShortcut(key, index) {
371
+ localStorage.setItem('t20-datepicker-default-shortcut', key)
372
+ this.$message.success('已设置为默认快捷选项')
373
+
374
+ const picker = this.$refs['date-picker'].picker
375
+ if (picker && picker.$el) {
376
+ const icons = picker.$el.querySelectorAll('.shortcut-default-icon')
377
+ icons.forEach((icon, i) => {
378
+ if (i === index) {
379
+ icon.className = 'el-icon-star-on shortcut-default-icon'
380
+ icon.style.color = '#E6A23C'
381
+ } else {
382
+ icon.className = 'el-icon-star-off shortcut-default-icon'
383
+ icon.style.color = '#C0C4CC'
384
+ }
385
+ })
386
+ }
387
+ }
304
388
  }
305
389
  }
306
390
  </script>