-
Notifications
You must be signed in to change notification settings - Fork 6
/
cleanTime.vue
75 lines (71 loc) · 2.5 KB
/
cleanTime.vue
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
<template>
<span class="vue-crono-time">
{{renderedTime}}
</span>
</template>
<script>
const template = require('es6-template-strings');
export default{
name: 'cleanTime',
data(){
return { renderedTime: undefined };
},
props: {
time: Date,
round: { type: Number, default: 5 },
dayAgo: { type: Boolean, default: true },
dateFn: { type: String, default: 'toLocaleDateString' },
locale: { type: String, default: 'en' },
localeMap: {
type: Object, default(){
return {
en: {
minute: '${time} minute ago',
minutes: '${time} minutes ago',
hour: '${time} hour ago',
hours: '${time} hours ago',
day: '${time} day ago'
}
};
}
}
},
methods: {
refreshTime(){
const minutesAgo = (new Date() - this.time) / 1000 / 60;
const hoursAgo = minutesAgo / 60;
const roundedMinutes = Math.ceil(minutesAgo / this.round) * this.round;
let time;
if(roundedMinutes == 0 || roundedMinutes === 1){
time = template(this.localeMap[this.locale].minute, { time: 1 });
}
else if (roundedMinutes <= 55){
time = template(this.localeMap[this.locale].minutes, { time: roundedMinutes });
}
else if (hoursAgo < 8){
const floorHours = Math.floor(hoursAgo);
if (floorHours === 0 || floorHours === 1){
time = template(this.localeMap[this.locale].hour, { time: floorHours });
}
else{
time = template(this.localeMap[this.locale].hours, { time: floorHours });
}
}
else if(hoursAgo >= 24 && hoursAgo <= 36){
time = template(this.localeMap[this.locale].day, { time: 1 });
}
else {
time = this.time[this.dateFn]();
}
this.renderedTime = time;
}
},
mounted(){
this.refreshTime();
},
cron: {
time: 60000,
method: 'refreshTime'
}
};
</script>