-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
99 lines (81 loc) · 2.24 KB
/
script.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
(function($) {
function Countdown(options) {
this.$timer = $(options.$timer);
this.$date = $(options.$date);
this.date = options.date;
this.initialize();
}
Countdown.prototype = {
initialize: function() {
this.update();
$("title").html(this.getDateString());
},
update: function() {
var self = this;
var val = this.getTimeLeft();
this.$timer.html(val);
this.$date.html(this.getDateString());
setTimeout($.proxy(this.update, this), 1000);
},
getMoment: function() {
return moment(this.date);
},
//=> "Today at 9:00PM"
getDateString: function() {
return this.getMoment().calendar();
},
//=> "34 minutes"
getTimeLeft: function() {
var diff = this.getMoment().diff();
if (diff > 0) return "" + sToDuration(diff / 1000);
else return "now";
}
};
function getSearch() {
var str = window.location.search || '';
return decodeURIComponent(str
.replace(/\+/g, ' ')
.replace(/\/$/, '')
.replace(/\/$/, '')
.replace(/^\?q=/, ''));
}
$(function() {
$("textarea").autoexpand();
var hash = getSearch().replace(/^in /, '+');
var date = Date.parse(hash);
if (date) {
$("[role~='countdown_page']").removeClass('hide');
new Countdown({
$timer: $("[role~='timer_value']"),
$date: $("[role~='timer_date']"),
date: date
});
} else {
$("[role~='message']").removeClass('hide');
}
});
function sToDuration(n) {
var units = [
{ secs: 86400, name: ["day", "days"] },
{ secs: 3600, name: ["hour", "hours"] },
{ secs: 60, name: ["minute", "minutes"] },
{ secs: 1, name: ["second", "seconds"] }
];
var remaining = n;
var result = [];
for (var i=0; i<units.length; ++i) {
var unit = units[i];
if (remaining >= unit.secs) {
var number = parseInt(remaining / unit.secs, 10);
remaining -= number * unit.secs;
if (number === 1) {
result.push("" + number + " " + unit.name[0]);
} else {
result.push("" + number + " " + unit.name[1]);
}
}
}
return result.join(" ");
}
window.sToDuration = sToDuration;
})(jQuery);