forked from blocvox/jquery-friendlyTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.friendlyTime.js
161 lines (132 loc) · 5.71 KB
/
jQuery.friendlyTime.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
(function(jQuery, Date, clearInterval, setInterval, define) {
"use strict";
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals.
factory(jQuery);
}
})(function($) {
var
all$Els = [],
interval,
TIME_FORMATS = [
[1, 'Just Now', 'Just Now'],
[2, '1 Second Ago', '1 Second From Now'],
[60, 'Seconds', 1], // 60
[120, '1 Minute Ago', '1 Minute From Now'], // 60*2
[3600, 'Minutes', 60], // 60*60, 60
[7200, '1 Hour Ago', '1 Hour From Now'], // 60*60*2
[86400, 'Hours', 3600], // 60*60*24, 60*60
[172800, 'Yesterday', 'Tomorrow'], // 60*60*24*2
[604800, 'Days', 86400], // 60*60*24*7, 60*60*24
[1209600, 'Last Week', 'Next Week'], // 60*60*24*7*4*2
[2628000, 'Weeks', 604800], // 30.416 days
[5256000, 'Last Month', 'Next Month'], // 60.832 days
[31557600, 'Months', 2628000], // 365.25 days
[63115200, 'Last Year', 'Next Year']
]
;
$.fn.friendlyTime = function(opts) {
if (!this.length) {
// Invoked with no matches.
return;
}
if (!opts) opts = {};
if (opts.stopUpdates) {
this.each(stopUpdates);
return;
}
// We don't want to use a jQuery object, because we need to group together unrelated items, and
// a jQuery object has extra crap in it that implies particular relations (i.e. selector property).
var new$Els =
$.makeArray(this.
filter(function() { return !$(this).data('friendlyTime'); }).
data('friendlyTime', opts)
).
map(function(el) { return $(el); })
;
if (!opts.skipInit) friendlyTime(new$Els);
// Restore all elements, adding new ones.
all$Els.push.apply(all$Els, new$Els);
if (!interval) interval = setInterval(friendlyTime.bind(this, all$Els), $.fn.friendlyTime.DURATION);
return this;
};
$.fn.friendlyTime.DURATION = 60000;
return $;
function friendlyTime($els) {
$els.forEach(function($el) {
var utcDate = $el.data().utcDate;
// Do this in this private Function instead of directly in $.friendlyTime so
// that the initial load is as fast as possible.
if (!utcDate) {
// This comes from HTML data attribute.
var utcString = $el.data().time;
utcDate = fromUniversalString(utcString);
$el.data('utcDate', utcDate);
if (!$el.attr('title')) $el.attr('title', utcDate.toUTCString());
}
var opts = $el.data().friendlyTime;
// If friendlyTime isn't told to stop updating a detached element, the element
// will remain in our update set, but it's data() will be wiped (except for
// html data attrs) by jQuery when it was detached.
if (typeof opts === "undefined") {
// Detached element. Stop tracking it for updates.
stopUpdates.call(this);
return;
}
$el.text(toRelativeString(utcDate, opts.suppressFuture, opts.nowWindow));
});
}
function stopUpdates(){
var $el, i, thiz = this;
for (i = 0; i < all$Els.length; i++) {
$el = all$Els[i];
if ($el[0] !== thiz) continue;
// If the element was already detached, the next line is a NOOP (courtesy of jQuery
// detach logic).
$el.removeData('friendlyTime');
all$Els.splice(i, 1);
break;
}
// stopUpdate element not found.
}
function toRelativeString(time, suppressFuture, nowWindow) {
var
seconds = (new Date() - time) / 1000,
token = ' Ago',
direction = 1,
i = 0,
format = TIME_FORMATS[i++]
;
if (!suppressFuture && seconds < 0) {
seconds = Math.abs(seconds);
token = ' From Now';
direction = 2;
}
if (typeof nowWindow != "undefined" && seconds < nowWindow) {
return format[1];
}
while (format) {
if (seconds < format[0]) {
if (typeof format[2] == 'string') return format[direction];
else return Math.floor(seconds / format[2]) + ' ' + format[1] + token;
}
format = TIME_FORMATS[i++];
}
// Should never get here.
return Math.floor(seconds / 31557600) + ' Years Ago';
}
// Should be ISO8601 string, with "Z" time zone descriptor.
function fromUniversalString(dateStr) {
/*
var time = dateStr.replace(/Z/g, "");
if (time.substr(time.length - 4, 1) == ".") time = time.substr(0, time.length - 4);
if (parseNeedsSlashes) time = time.replace("-","/");
*/
return new Date(dateStr);
}
});
})(this.jQuery, Date, clearInterval, setInterval, this.define);