-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeago.js
215 lines (192 loc) · 5.44 KB
/
timeago.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* A yui 3 timeago widget
*
* @module time-ago
* @requires oop, base, dom, node
*/
/*
* a YUI 3 implementation of the jQuery timeAgo widget
* @class TimeAgo
* @namespace Y.TimeAgo
* @extends Y.Base
*/
/*
* Inspired by: http://github.com/rmm5t/jquery-timeago/
*/
YUI.add('time-ago', function(Y) {
/*
* @constructor
* @param {Object} config to be used to setup widget
*/
function TimeAgo(config) {
TimeAgo.superclass.constructor.apply(this, arguments);
}
/*
* Static Properties
*/
TimeAgo.NAME = "timeAgo";
TimeAgo.CLASS_NAME = "yui-time-ago";
TimeAgo.refreshMillis= 60000;
TimeAgo.strings = {
prefixAgo: null,
suffixAgo: "ago",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years"
};
Y.extend( TimeAgo, Y.Base );
/*
* Overwrite standard Y.Base.initializer method to setup widget
* @method initializer
* @param {Object} cfg object with setup properties
*/
TimeAgo.prototype.initializer = function(cfg) {
var elements = Y.all(cfg.contentBox);
elements.addClass(TimeAgo.CLASS_NAME);
this.elements = elements;
this.updateDateTimes();
this.update();
this._contentBox = cfg.contentBox;
this.refreshMillis = cfg.refreshMillis ? cfg.refreshMillis : TimeAgo.refreshMillis;
}
/*
* Destroy and cleanup
* @method destroy
*/
TimeAgo.prototype.destroy = function() {
delete this.elements;
this.elements = null;
}
/*
* update content of the dom elements that show time ago information
* @method update
*/
TimeAgo.prototype.update = function() {
this.elements.each(function(node) {
var timeago = this._distance(node._datetime);
node.set('innerHTML', this._inWords(timeago));
}, this, true);
}
/*
* refresh widget with whole recalculation of the dom element list and datetime values
* @method refresh
*/
TimeAgo.prototype.refresh = function() {
var timerId = this.timerId;
if(timerId)
timerId.cancel();
this.timerId = null;
this.elements = Y.all(this._contentBox);
this.updateDateTimes();
this.update();
if(timerId) {
this._recall();
}
}
/*
* update values from dom for date time and hold it internaly
* @method updateDateTimes
*/
TimeAgo.prototype.updateDateTimes = function() {
this.elements.each( function(node) {
var datetime = node.getAttribute("datetime");
node._datetime = this._parse(datetime);
}, this, true);
}
/*
* start widget automaticaly update it self and dom elements content
* @method startUpdates
*/
TimeAgo.prototype.startUpdates = function() {
if(!this.timerId)
this._recall();
}
/*
* stop widget automatical update it self and dom elements content
* @method stopUpdates
*/
TimeAgo.prototype.stopUpdates = function() {
if(this.timerId)
this.timerId.cancel();
this.timerId = null;
}
/*
* calculate human readable string for the date based on datetime
* @method _inWords
* @private
* @param {int} distance in miliseconds from current time
* @return {String} value to show as content of the dom element
*/
TimeAgo.prototype._inWords = function(distance) {
var sec = distance/1000,
min = sec/60,
hours = min/60,
days = hours/24,
years = days/365,
words,
str = TimeAgo.strings;
function substitute(string, number) {
return string.replace(/%d/i, number);
}
words = sec < 45 && substitute(str.seconds, Math.round(sec)) ||
sec < 90 && substitute(str.minute, 1) ||
min < 45 && substitute(str.minutes, Math.round(min)) ||
min < 90 && substitute(str.hour, 1) ||
hours < 24 && substitute(str.hours, Math.round(hours)) ||
hours < 48 && substitute(str.day, 1) ||
days < 30 && substitute(str.days, Math.floor(days)) ||
days < 60 && substitute(str.month, 1) ||
days < 365 && substitute(str.months, Math.floor(days / 30)) ||
years < 2 && substitute(str.year, 1) ||
substitute(str.years, Math.floor(years));
return [str.prefixAgo, words, str.suffixAgo].join(" ");
}
/*
* calculate distance from current time to given date in miliseconds
* @method _distance
* @private
* @param {int} date given to calculate distance
* @return {int} the distance value
*/
TimeAgo.prototype._distance = function(date) {
return (new Date().getTime() - date.getTime());
}
/*
* convert iso8601 to date value
* @method _parse
* @private
* @param {String} iso8601 time in string format
* @return {int} the date value
*/
TimeAgo.prototype._parse = function(iso8601) {
var s = Y.Lang.trim(iso8601);
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
return new Date(s);
}
/*
* self calling method to do refresh of the dom content every x miliseconds
* @method _recall
* @private
*/
TimeAgo.prototype._recall = function() {
var refreshMillis = this.refreshMillis;
if(refreshMillis <= 0)
return;
this.update();
this.timerId = Y.later(this.refreshMillis, this, this._recall );
}
Y.namespace('time-ago');
Y.TimeAgo = TimeAgo;
},
'1.0',
{requires:['node', 'base', 'lang']});