forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (102 loc) · 2.76 KB
/
index.js
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
import moment from 'moment';
const defaultDisabledTime = {
disabledHours() {
return [];
},
disabledMinutes() {
return [];
},
disabledSeconds() {
return [];
},
};
export function getTodayTime(value) {
const today = moment();
today.locale(value.locale()).utcOffset(value.utcOffset());
return today;
}
export function getTitleString(value) {
return value.format('LL');
}
export function getTodayTimeStr(value) {
const today = getTodayTime(value);
return getTitleString(today);
}
export function getMonthName(month) {
const locale = month.locale();
const localeData = month.localeData();
return localeData[locale === 'zh-cn' ? 'months' : 'monthsShort'](month);
}
export function syncTime(from, to) {
if (!moment.isMoment(from) || !moment.isMoment(to)) return;
to.hour(from.hour());
to.minute(from.minute());
to.second(from.second());
to.millisecond(from.millisecond());
}
export function getTimeConfig(value, disabledTime) {
let disabledTimeConfig = disabledTime ? disabledTime(value) : {};
disabledTimeConfig = {
...defaultDisabledTime,
...disabledTimeConfig,
};
return disabledTimeConfig;
}
export function isTimeValidByConfig(value, disabledTimeConfig) {
let invalidTime = false;
if (value) {
const hour = value.hour();
const minutes = value.minute();
const seconds = value.second();
const disabledHours = disabledTimeConfig.disabledHours();
if (disabledHours.indexOf(hour) === -1) {
const disabledMinutes = disabledTimeConfig.disabledMinutes(hour);
if (disabledMinutes.indexOf(minutes) === -1) {
const disabledSeconds = disabledTimeConfig.disabledSeconds(hour, minutes);
invalidTime = disabledSeconds.indexOf(seconds) !== -1;
} else {
invalidTime = true;
}
} else {
invalidTime = true;
}
}
return !invalidTime;
}
export function isTimeValid(value, disabledTime) {
const disabledTimeConfig = getTimeConfig(value, disabledTime);
return isTimeValidByConfig(value, disabledTimeConfig);
}
export function isAllowedDate(value, disabledDate, disabledTime) {
if (disabledDate) {
if (disabledDate(value)) {
return false;
}
}
if (disabledTime) {
if (!isTimeValid(value, disabledTime)) {
return false;
}
}
return true;
}
export function formatDate(value, format) {
const isFunction = function(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
};
if (!value) {
return '';
}
if (Array.isArray(format)) {
format = format[0];
}
if (isFunction(format)) {
const result = format(value);
if (typeof result === 'string') {
return result;
} else {
throw new Error('The function of format does not return a string');
}
}
return value.format(format);
}