timeago_js 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/assets/javascripts/timeago/locales/ar.js +26 -26
- data/assets/javascripts/timeago/locales/be.js +51 -51
- data/assets/javascripts/timeago/locales/bg.js +17 -17
- data/assets/javascripts/timeago/locales/ca.js +17 -17
- data/assets/javascripts/timeago/locales/da.js +17 -17
- data/assets/javascripts/timeago/locales/de.js +17 -17
- data/assets/javascripts/timeago/locales/el.js +17 -17
- data/assets/javascripts/timeago/locales/en.js +17 -17
- data/assets/javascripts/timeago/locales/en_short.js +17 -17
- data/assets/javascripts/timeago/locales/es.js +17 -17
- data/assets/javascripts/timeago/locales/eu.js +17 -17
- data/assets/javascripts/timeago/locales/fa.js +18 -0
- data/assets/javascripts/timeago/locales/fi.js +17 -17
- data/assets/javascripts/timeago/locales/fr.js +17 -17
- data/assets/javascripts/timeago/locales/he.js +17 -17
- data/assets/javascripts/timeago/locales/hu.js +17 -17
- data/assets/javascripts/timeago/locales/in_ID.js +17 -17
- data/assets/javascripts/timeago/locales/it.js +17 -17
- data/assets/javascripts/timeago/locales/ja.js +17 -17
- data/assets/javascripts/timeago/locales/ko.js +17 -17
- data/assets/javascripts/timeago/locales/ml.js +17 -17
- data/assets/javascripts/timeago/locales/my.js +18 -0
- data/assets/javascripts/timeago/locales/nl.js +17 -17
- data/assets/javascripts/timeago/locales/pl.js +34 -34
- data/assets/javascripts/timeago/locales/pt_BR.js +17 -17
- data/assets/javascripts/timeago/locales/ro.js +28 -28
- data/assets/javascripts/timeago/locales/ru.js +51 -51
- data/assets/javascripts/timeago/locales/sv.js +17 -17
- data/assets/javascripts/timeago/locales/ta.js +17 -17
- data/assets/javascripts/timeago/locales/th.js +17 -17
- data/assets/javascripts/timeago/locales/tr.js +17 -17
- data/assets/javascripts/timeago/locales/uk.js +40 -40
- data/assets/javascripts/timeago/locales/vi.js +17 -17
- data/assets/javascripts/timeago/locales/zh_CN.js +17 -17
- data/assets/javascripts/timeago/locales/zh_TW.js +17 -17
- data/assets/javascripts/timeago.js +261 -258
- data/assets/javascripts/timeago.locales.js +1 -1
- data/lib/timeago_js/version.rb +1 -1
- metadata +4 -2
@@ -1,258 +1,261 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) 2016 hustcc
|
3
|
-
* License: MIT
|
4
|
-
* Version: v3.0.
|
5
|
-
* https://github.com/hustcc/timeago.js
|
6
|
-
**/
|
7
|
-
/* jshint expr: true */
|
8
|
-
!function (root, factory) {
|
9
|
-
if (typeof module === 'object' && module.exports) {
|
10
|
-
module.exports = factory(root); // nodejs support
|
11
|
-
module.exports['default'] = module.exports; // es6 support
|
12
|
-
}
|
13
|
-
else
|
14
|
-
root.timeago = factory(root);
|
15
|
-
}(typeof window !== 'undefined' ? window : this,
|
16
|
-
function () {
|
17
|
-
var indexMapEn = 'second_minute_hour_day_week_month_year'.split('_'),
|
18
|
-
indexMapZh = '秒_分钟_小时_天_周_月_年'.split('_'),
|
19
|
-
// build-in locales: en & zh_CN
|
20
|
-
locales = {
|
21
|
-
'en': function(number, index) {
|
22
|
-
if (index === 0) return ['just now', 'right now'];
|
23
|
-
var unit = indexMapEn[parseInt(index / 2)];
|
24
|
-
if (number > 1) unit += 's';
|
25
|
-
return [number + ' ' + unit + ' ago', 'in ' + number + ' ' + unit];
|
26
|
-
},
|
27
|
-
'zh_CN': function(number, index) {
|
28
|
-
if (index === 0) return ['刚刚', '片刻后'];
|
29
|
-
var unit = indexMapZh[parseInt(index / 2)];
|
30
|
-
return [number + unit + '前', number + unit + '后'];
|
31
|
-
}
|
32
|
-
},
|
33
|
-
// second, minute, hour, day, week, month, year(365 days)
|
34
|
-
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
|
35
|
-
SEC_ARRAY_LEN = 6,
|
36
|
-
ATTR_DATETIME = 'datetime',
|
37
|
-
ATTR_DATA_TID = 'data-tid',
|
38
|
-
timers = {}; // real-time render timers
|
39
|
-
|
40
|
-
// format Date / string / timestamp to Date instance.
|
41
|
-
function toDate(input) {
|
42
|
-
if (input instanceof Date) return input;
|
43
|
-
if (!isNaN(input)) return new Date(toInt(input));
|
44
|
-
if (/^\d+$/.test(input)) return new Date(toInt(input));
|
45
|
-
input = (input || '').trim().replace(/\.\d+/, '') // remove milliseconds
|
46
|
-
.replace(/-/, '/').replace(/-/, '/')
|
47
|
-
.replace(/(\d)T(\d)/, '$1 $2').replace(/Z/, ' UTC') // 2017-2-5T3:57:52Z -> 2017-2-5 3:57:52UTC
|
48
|
-
.replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2'); // -04:00 -> -0400
|
49
|
-
return new Date(input);
|
50
|
-
}
|
51
|
-
// change f into int, remove decimal. Just for code compression
|
52
|
-
function toInt(f) {
|
53
|
-
return parseInt(f);
|
54
|
-
}
|
55
|
-
// format the diff second to *** time ago, with setting locale
|
56
|
-
function formatDiff(diff, locale, defaultLocale) {
|
57
|
-
// if locale is not exist, use defaultLocale.
|
58
|
-
// if defaultLocale is not exist, use build-in `en`.
|
59
|
-
// be sure of no error when locale is not exist.
|
60
|
-
locale = locales[locale] ? locale : (locales[defaultLocale] ? defaultLocale : 'en');
|
61
|
-
// if (! locales[locale]) locale = defaultLocale;
|
62
|
-
var i = 0,
|
63
|
-
agoin = diff < 0 ? 1 : 0, // timein or timeago
|
64
|
-
total_sec = diff = Math.abs(diff);
|
65
|
-
|
66
|
-
for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
|
67
|
-
diff /= SEC_ARRAY[i];
|
68
|
-
}
|
69
|
-
diff = toInt(diff);
|
70
|
-
i *= 2;
|
71
|
-
|
72
|
-
if (diff > (i === 0 ? 9 : 1)) i += 1;
|
73
|
-
return locales[locale](diff, i, total_sec)[agoin].replace('%s', diff);
|
74
|
-
}
|
75
|
-
// calculate the diff second between date to be formated an now date.
|
76
|
-
function diffSec(date, nowDate) {
|
77
|
-
nowDate = nowDate ? toDate(nowDate) : new Date();
|
78
|
-
return (nowDate - toDate(date)) / 1000;
|
79
|
-
}
|
80
|
-
/**
|
81
|
-
* nextInterval: calculate the next interval time.
|
82
|
-
* - diff: the diff sec between now and date to be formated.
|
83
|
-
*
|
84
|
-
* What's the meaning?
|
85
|
-
* diff = 61 then return 59
|
86
|
-
* diff = 3601 (an hour + 1 second), then return 3599
|
87
|
-
* make the interval with high performace.
|
88
|
-
**/
|
89
|
-
function nextInterval(diff) {
|
90
|
-
var rst = 1, i = 0, d = Math.abs(diff);
|
91
|
-
for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
|
92
|
-
diff /= SEC_ARRAY[i];
|
93
|
-
rst *= SEC_ARRAY[i];
|
94
|
-
}
|
95
|
-
// return leftSec(d, rst);
|
96
|
-
d = d % rst;
|
97
|
-
d = d ? rst - d : rst;
|
98
|
-
return Math.ceil(d);
|
99
|
-
}
|
100
|
-
// get the datetime attribute,
|
101
|
-
function getDateAttr(node) {
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
function getAttr(node, name) {
|
106
|
-
if(node.getAttribute) return node.getAttribute(name); // native
|
107
|
-
if(node.attr) return node.attr(name); // jquery
|
108
|
-
}
|
109
|
-
|
110
|
-
|
111
|
-
if(node.
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
*
|
121
|
-
*
|
122
|
-
*
|
123
|
-
*
|
124
|
-
*
|
125
|
-
* var
|
126
|
-
* var timeago = timeagoLib(
|
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
|
-
* timeago.
|
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
|
-
* var
|
204
|
-
* var timeago = timeagoFactory(
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
*
|
214
|
-
*
|
215
|
-
*
|
216
|
-
*
|
217
|
-
*
|
218
|
-
*
|
219
|
-
*
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
*
|
230
|
-
*
|
231
|
-
*
|
232
|
-
*
|
233
|
-
* timeagoFactory.
|
234
|
-
*
|
235
|
-
*
|
236
|
-
*
|
237
|
-
*
|
238
|
-
*
|
239
|
-
* timeago.
|
240
|
-
* timeagoFactory
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
}
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
}
|
1
|
+
/**
|
2
|
+
* Copyright (c) 2016 hustcc
|
3
|
+
* License: MIT
|
4
|
+
* Version: v3.0.2
|
5
|
+
* https://github.com/hustcc/timeago.js
|
6
|
+
**/
|
7
|
+
/* jshint expr: true */
|
8
|
+
!function (root, factory) {
|
9
|
+
if (typeof module === 'object' && module.exports) {
|
10
|
+
module.exports = factory(root); // nodejs support
|
11
|
+
module.exports['default'] = module.exports; // es6 support
|
12
|
+
}
|
13
|
+
else
|
14
|
+
root.timeago = factory(root);
|
15
|
+
}(typeof window !== 'undefined' ? window : this,
|
16
|
+
function () {
|
17
|
+
var indexMapEn = 'second_minute_hour_day_week_month_year'.split('_'),
|
18
|
+
indexMapZh = '秒_分钟_小时_天_周_月_年'.split('_'),
|
19
|
+
// build-in locales: en & zh_CN
|
20
|
+
locales = {
|
21
|
+
'en': function(number, index) {
|
22
|
+
if (index === 0) return ['just now', 'right now'];
|
23
|
+
var unit = indexMapEn[parseInt(index / 2)];
|
24
|
+
if (number > 1) unit += 's';
|
25
|
+
return [number + ' ' + unit + ' ago', 'in ' + number + ' ' + unit];
|
26
|
+
},
|
27
|
+
'zh_CN': function(number, index) {
|
28
|
+
if (index === 0) return ['刚刚', '片刻后'];
|
29
|
+
var unit = indexMapZh[parseInt(index / 2)];
|
30
|
+
return [number + unit + '前', number + unit + '后'];
|
31
|
+
}
|
32
|
+
},
|
33
|
+
// second, minute, hour, day, week, month, year(365 days)
|
34
|
+
SEC_ARRAY = [60, 60, 24, 7, 365/7/12, 12],
|
35
|
+
SEC_ARRAY_LEN = 6,
|
36
|
+
// ATTR_DATETIME = 'datetime',
|
37
|
+
ATTR_DATA_TID = 'data-tid',
|
38
|
+
timers = {}; // real-time render timers
|
39
|
+
|
40
|
+
// format Date / string / timestamp to Date instance.
|
41
|
+
function toDate(input) {
|
42
|
+
if (input instanceof Date) return input;
|
43
|
+
if (!isNaN(input)) return new Date(toInt(input));
|
44
|
+
if (/^\d+$/.test(input)) return new Date(toInt(input));
|
45
|
+
input = (input || '').trim().replace(/\.\d+/, '') // remove milliseconds
|
46
|
+
.replace(/-/, '/').replace(/-/, '/')
|
47
|
+
.replace(/(\d)T(\d)/, '$1 $2').replace(/Z/, ' UTC') // 2017-2-5T3:57:52Z -> 2017-2-5 3:57:52UTC
|
48
|
+
.replace(/([\+\-]\d\d)\:?(\d\d)/, ' $1$2'); // -04:00 -> -0400
|
49
|
+
return new Date(input);
|
50
|
+
}
|
51
|
+
// change f into int, remove decimal. Just for code compression
|
52
|
+
function toInt(f) {
|
53
|
+
return parseInt(f);
|
54
|
+
}
|
55
|
+
// format the diff second to *** time ago, with setting locale
|
56
|
+
function formatDiff(diff, locale, defaultLocale) {
|
57
|
+
// if locale is not exist, use defaultLocale.
|
58
|
+
// if defaultLocale is not exist, use build-in `en`.
|
59
|
+
// be sure of no error when locale is not exist.
|
60
|
+
locale = locales[locale] ? locale : (locales[defaultLocale] ? defaultLocale : 'en');
|
61
|
+
// if (! locales[locale]) locale = defaultLocale;
|
62
|
+
var i = 0,
|
63
|
+
agoin = diff < 0 ? 1 : 0, // timein or timeago
|
64
|
+
total_sec = diff = Math.abs(diff);
|
65
|
+
|
66
|
+
for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
|
67
|
+
diff /= SEC_ARRAY[i];
|
68
|
+
}
|
69
|
+
diff = toInt(diff);
|
70
|
+
i *= 2;
|
71
|
+
|
72
|
+
if (diff > (i === 0 ? 9 : 1)) i += 1;
|
73
|
+
return locales[locale](diff, i, total_sec)[agoin].replace('%s', diff);
|
74
|
+
}
|
75
|
+
// calculate the diff second between date to be formated an now date.
|
76
|
+
function diffSec(date, nowDate) {
|
77
|
+
nowDate = nowDate ? toDate(nowDate) : new Date();
|
78
|
+
return (nowDate - toDate(date)) / 1000;
|
79
|
+
}
|
80
|
+
/**
|
81
|
+
* nextInterval: calculate the next interval time.
|
82
|
+
* - diff: the diff sec between now and date to be formated.
|
83
|
+
*
|
84
|
+
* What's the meaning?
|
85
|
+
* diff = 61 then return 59
|
86
|
+
* diff = 3601 (an hour + 1 second), then return 3599
|
87
|
+
* make the interval with high performace.
|
88
|
+
**/
|
89
|
+
function nextInterval(diff) {
|
90
|
+
var rst = 1, i = 0, d = Math.abs(diff);
|
91
|
+
for (; diff >= SEC_ARRAY[i] && i < SEC_ARRAY_LEN; i++) {
|
92
|
+
diff /= SEC_ARRAY[i];
|
93
|
+
rst *= SEC_ARRAY[i];
|
94
|
+
}
|
95
|
+
// return leftSec(d, rst);
|
96
|
+
d = d % rst;
|
97
|
+
d = d ? rst - d : rst;
|
98
|
+
return Math.ceil(d);
|
99
|
+
}
|
100
|
+
// get the datetime attribute, `data-timeagp` / `datetime` are supported.
|
101
|
+
function getDateAttr(node) {
|
102
|
+
return getAttr(node, 'data-timeago') || getAttr(node, 'datetime');
|
103
|
+
}
|
104
|
+
// get the node attribute, native DOM and jquery supported.
|
105
|
+
function getAttr(node, name) {
|
106
|
+
if(node.getAttribute) return node.getAttribute(name); // native
|
107
|
+
if(node.attr) return node.attr(name); // jquery
|
108
|
+
}
|
109
|
+
// set the node attribute, native DOM and jquery supported.
|
110
|
+
function setTidAttr(node, val) {
|
111
|
+
if(node.setAttribute) return node.setAttribute(ATTR_DATA_TID, val); // native
|
112
|
+
if(node.attr) return node.attr(ATTR_DATA_TID, val); // jquery
|
113
|
+
}
|
114
|
+
// get the timer id of node.
|
115
|
+
// remove the function, can save some bytes.
|
116
|
+
// function getTidFromNode(node) {
|
117
|
+
// return getAttr(node, ATTR_DATA_TID);
|
118
|
+
// }
|
119
|
+
/**
|
120
|
+
* timeago: the function to get `timeago` instance.
|
121
|
+
* - nowDate: the relative date, default is new Date().
|
122
|
+
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
|
123
|
+
*
|
124
|
+
* How to use it?
|
125
|
+
* var timeagoLib = require('timeago.js');
|
126
|
+
* var timeago = timeagoLib(); // all use default.
|
127
|
+
* var timeago = timeagoLib('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
|
128
|
+
* var timeago = timeagoLib(null, 'zh_CN'); // set default locale is `zh_CN`.
|
129
|
+
* var timeago = timeagoLib('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
|
130
|
+
**/
|
131
|
+
function Timeago(nowDate, defaultLocale) {
|
132
|
+
this.nowDate = nowDate;
|
133
|
+
// if do not set the defaultLocale, set it with `en`
|
134
|
+
this.defaultLocale = defaultLocale || 'en'; // use default build-in locale
|
135
|
+
// for dev test
|
136
|
+
// this.nextInterval = nextInterval;
|
137
|
+
}
|
138
|
+
// what the timer will do
|
139
|
+
Timeago.prototype.doRender = function(node, date, locale) {
|
140
|
+
var diff = diffSec(date, this.nowDate),
|
141
|
+
self = this,
|
142
|
+
tid;
|
143
|
+
// delete previously assigned timeout's id to node
|
144
|
+
node.innerHTML = formatDiff(diff, locale, this.defaultLocale);
|
145
|
+
// waiting %s seconds, do the next render
|
146
|
+
timers[tid = setTimeout(function() {
|
147
|
+
self.doRender(node, date, locale);
|
148
|
+
delete timers[tid];
|
149
|
+
}, Math.min(nextInterval(diff) * 1000, 0x7FFFFFFF))] = 0; // there is no need to save node in object.
|
150
|
+
// set attribute date-tid
|
151
|
+
setTidAttr(node, tid);
|
152
|
+
};
|
153
|
+
/**
|
154
|
+
* format: format the date to *** time ago, with setting or default locale
|
155
|
+
* - date: the date / string / timestamp to be formated
|
156
|
+
* - locale: the formated string's locale name, e.g. en / zh_CN
|
157
|
+
*
|
158
|
+
* How to use it?
|
159
|
+
* var timeago = require('timeago.js')();
|
160
|
+
* timeago.format(new Date(), 'pl'); // Date instance
|
161
|
+
* timeago.format('2016-09-10', 'fr'); // formated date string
|
162
|
+
* timeago.format(1473473400269); // timestamp with ms
|
163
|
+
**/
|
164
|
+
Timeago.prototype.format = function(date, locale) {
|
165
|
+
return formatDiff(diffSec(date, this.nowDate), locale, this.defaultLocale);
|
166
|
+
};
|
167
|
+
/**
|
168
|
+
* render: render the DOM real-time.
|
169
|
+
* - nodes: which nodes will be rendered.
|
170
|
+
* - locale: the locale name used to format date.
|
171
|
+
*
|
172
|
+
* How to use it?
|
173
|
+
* var timeago = require('timeago.js')();
|
174
|
+
* // 1. javascript selector
|
175
|
+
* timeago.render(document.querySelectorAll('.need_to_be_rendered'));
|
176
|
+
* // 2. use jQuery selector
|
177
|
+
* timeago.render($('.need_to_be_rendered'), 'pl');
|
178
|
+
*
|
179
|
+
* Notice: please be sure the dom has attribute `datetime`.
|
180
|
+
**/
|
181
|
+
Timeago.prototype.render = function(nodes, locale) {
|
182
|
+
if (nodes.length === undefined) nodes = [nodes];
|
183
|
+
for (var i = 0, len = nodes.length; i < len; i++) {
|
184
|
+
this.doRender(nodes[i], getDateAttr(nodes[i]), locale); // render item
|
185
|
+
}
|
186
|
+
};
|
187
|
+
/**
|
188
|
+
* setLocale: set the default locale name.
|
189
|
+
*
|
190
|
+
* How to use it?
|
191
|
+
* var timeago = require('timeago.js')();
|
192
|
+
* timeago.setLocale('fr');
|
193
|
+
**/
|
194
|
+
Timeago.prototype.setLocale = function(locale) {
|
195
|
+
this.defaultLocale = locale;
|
196
|
+
};
|
197
|
+
/**
|
198
|
+
* timeago: the function to get `timeago` instance.
|
199
|
+
* - nowDate: the relative date, default is new Date().
|
200
|
+
* - defaultLocale: the default locale, default is en. if your set it, then the `locale` parameter of format is not needed of you.
|
201
|
+
*
|
202
|
+
* How to use it?
|
203
|
+
* var timeagoFactory = require('timeago.js');
|
204
|
+
* var timeago = timeagoFactory(); // all use default.
|
205
|
+
* var timeago = timeagoFactory('2016-09-10'); // the relative date is 2016-09-10, so the 2016-09-11 will be 1 day ago.
|
206
|
+
* var timeago = timeagoFactory(null, 'zh_CN'); // set default locale is `zh_CN`.
|
207
|
+
* var timeago = timeagoFactory('2016-09-10', 'zh_CN'); // the relative date is 2016-09-10, and locale is zh_CN, so the 2016-09-11 will be 1天前.
|
208
|
+
**/
|
209
|
+
function timeagoFactory(nowDate, defaultLocale) {
|
210
|
+
return new Timeago(nowDate, defaultLocale);
|
211
|
+
}
|
212
|
+
/**
|
213
|
+
* register: register a new language locale
|
214
|
+
* - locale: locale name, e.g. en / zh_CN, notice the standard.
|
215
|
+
* - localeFunc: the locale process function
|
216
|
+
*
|
217
|
+
* How to use it?
|
218
|
+
* var timeagoFactory = require('timeago.js');
|
219
|
+
*
|
220
|
+
* timeagoFactory.register('the locale name', the_locale_func);
|
221
|
+
* // or
|
222
|
+
* timeagoFactory.register('pl', require('timeago.js/locales/pl'));
|
223
|
+
**/
|
224
|
+
timeagoFactory.register = function(locale, localeFunc) {
|
225
|
+
locales[locale] = localeFunc;
|
226
|
+
};
|
227
|
+
|
228
|
+
/**
|
229
|
+
* cancel: cancels one or all the timers which are doing real-time render.
|
230
|
+
*
|
231
|
+
* How to use it?
|
232
|
+
* For canceling all the timers:
|
233
|
+
* var timeagoFactory = require('timeago.js');
|
234
|
+
* var timeago = timeagoFactory();
|
235
|
+
* timeago.render(document.querySelectorAll('.need_to_be_rendered'));
|
236
|
+
* timeagoFactory.cancel(); // will stop all the timers, stop render in real time.
|
237
|
+
*
|
238
|
+
* For canceling single timer on specific node:
|
239
|
+
* var timeagoFactory = require('timeago.js');
|
240
|
+
* var timeago = timeagoFactory();
|
241
|
+
* var nodes = document.querySelectorAll('.need_to_be_rendered');
|
242
|
+
* timeago.render(nodes);
|
243
|
+
* timeagoFactory.cancel(nodes[0]); // will clear a timer attached to the first node, stop render in real time.
|
244
|
+
**/
|
245
|
+
timeagoFactory.cancel = function(node) {
|
246
|
+
var tid;
|
247
|
+
// assigning in if statement to save space
|
248
|
+
if (node) {
|
249
|
+
tid = getAttr(node, ATTR_DATA_TID); // get the timer of DOM node(native / jq).
|
250
|
+
if (tid) {
|
251
|
+
clearTimeout(tid);
|
252
|
+
delete timers[tid];
|
253
|
+
}
|
254
|
+
} else {
|
255
|
+
for (tid in timers) clearTimeout(tid);
|
256
|
+
timers = {};
|
257
|
+
}
|
258
|
+
};
|
259
|
+
|
260
|
+
return timeagoFactory;
|
261
|
+
});
|