Line | |
---|
1 | Vue.filter("two_digits", function(value) { |
---|
2 | if (value.toString().length <= 1) { |
---|
3 | return "0" + value.toString(); |
---|
4 | } |
---|
5 | return value.toString(); |
---|
6 | }); |
---|
7 | |
---|
8 | Vue.component("countdown", { |
---|
9 | template: "#countdown-template", |
---|
10 | |
---|
11 | props:['duration'], |
---|
12 | data() { |
---|
13 | return { |
---|
14 | now: new moment().format('X') |
---|
15 | } |
---|
16 | }, |
---|
17 | computed: { |
---|
18 | normalizedDate() { |
---|
19 | let a = this.duration.split(' '); |
---|
20 | return new moment().add(a[0], a[1]).format('X'); |
---|
21 | }, |
---|
22 | |
---|
23 | diff() { |
---|
24 | return this.normalizedDate - this.now; |
---|
25 | }, |
---|
26 | |
---|
27 | seconds() { |
---|
28 | return this.diff % 60; |
---|
29 | }, |
---|
30 | |
---|
31 | minutes() { |
---|
32 | return Math.trunc(this.diff / 60) % 60; |
---|
33 | }, |
---|
34 | |
---|
35 | hours() { |
---|
36 | return Math.trunc(this.diff / 60 / 60) % 24; |
---|
37 | }, |
---|
38 | |
---|
39 | days() { |
---|
40 | return Math.trunc(this.diff / 60 / 60 / 24); |
---|
41 | }, |
---|
42 | |
---|
43 | clSeconds() { |
---|
44 | if(this.days > 0) { |
---|
45 | return { green: true } |
---|
46 | } else if(this.hours > 0) { |
---|
47 | return { yellow: true } |
---|
48 | } else if (this.minutes > 0) { |
---|
49 | return { orange: true } |
---|
50 | } else if (this.seconds > 0) { |
---|
51 | return { red: true } |
---|
52 | } |
---|
53 | }, |
---|
54 | |
---|
55 | clMinutes() { |
---|
56 | if(this.days > 0) { |
---|
57 | return { green: true } |
---|
58 | } else if(this.hours > 0) { |
---|
59 | return { yellow: true } |
---|
60 | } else if (this.minutes > 0) { |
---|
61 | return { orange: true } |
---|
62 | } |
---|
63 | }, |
---|
64 | |
---|
65 | clHours() { |
---|
66 | if(this.days > 0) { |
---|
67 | return { green: true } |
---|
68 | } else if(this.hours > 0) { |
---|
69 | return { yellow: true} |
---|
70 | } |
---|
71 | }, |
---|
72 | |
---|
73 | clDays() { |
---|
74 | if(this.days > 0) { |
---|
75 | return { green: true } |
---|
76 | } |
---|
77 | } |
---|
78 | }, |
---|
79 | methods: { |
---|
80 | moment() { |
---|
81 | return moment(); |
---|
82 | }, |
---|
83 | updateTime() { |
---|
84 | if (this.diff > 0) { |
---|
85 | this.now = new moment().format('X'); |
---|
86 | setTimeout(this.updateTime, 1000); |
---|
87 | } |
---|
88 | } |
---|
89 | }, |
---|
90 | mounted() { |
---|
91 | this.updateTime(); |
---|
92 | }, |
---|
93 | }); |
---|
94 | |
---|
95 | new Vue({ |
---|
96 | el: "#countdown" |
---|
97 | }); |
---|
Note: See
TracBrowser
for help on using the repository browser.