/
symphony.timeago.js
128 lines (105 loc) · 3.03 KB
/
symphony.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
/**
* @package assets
*/
(function($, Symphony) {
/**
* Convert absolute to relative dates.
*
* @name $.symphonyTimeAgo
* @class
*
* @param {Object} options An object specifying containing the attributes specified below
* @param {String} [options.items='time'] Selector to find the absolute date
* @param {String} [options.timestamp='utc'] Attribute of `object.items` representing the timestamp of the given date
* @param {Integer} [options.max=0] Plugin will disable when the minutes exceed this value. By default this behaviour is off.
*
* @example
$('.notifier').symphonyTimeAgo();
*/
$.fn.symphonyTimeAgo = function(options) {
var objects = this,
settings = {
items: 'time',
timestamp: 'utc',
max: 0
};
$.extend(settings, options);
/*-----------------------------------------------------------------------*/
Symphony.Language.add({
'just now': false,
'a minute ago': false,
'{$minutes} minutes ago': false,
'about 1 hour ago': false,
'about {$hours} hours ago': false
});
/*-------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------*/
function parse(item) {
var timestamp = item.data('timestamp'),
datetime;
// Fetch stored timestamp
if($.isNumeric(timestamp)) {
return timestamp;
}
// Parse date
else {
datetime = item.attr(settings.timestamp);
// Defined date and time
if(datetime) {
// Datetime will be in seconds since Epoch, JS requires
// millseconds, so multiply by 1000.
timestamp = new Date(datetime * 1000);
}
// Undefined date and time
else {
timestamp = new Date().getTime();
}
// Store and return timestamp
item.data('timestamp', timestamp);
return timestamp;
}
}
function say(from, to) {
// Calculate time difference
var distance = to - from,
// Convert time to minutes
time = Math.floor(distance / 60000);
// Return relative date based on passed time
if(time < 1) {
return Symphony.Language.get('just now');
}
if(time < 2) {
return Symphony.Language.get('a minute ago');
}
if(time < 45) {
return Symphony.Language.get('{$minutes} minutes ago', {
'minutes': time
});
}
if(time < 90) {
return Symphony.Language.get('about 1 hour ago');
}
else if (!settings.max || time < settings.max) {
return Symphony.Language.get('about {$hours} hours ago', {
'hours': Math.floor(time / 60)
});
}
}
/*-------------------------------------------------------------------------
Initialisation
-------------------------------------------------------------------------*/
objects.find(settings.items).each(function timeago() {
var item = $(this),
from = parse(item),
to = new Date(),
rel = say(from, to);
// Set relative time
if (rel) {
item.text(rel);
}
});
/*-----------------------------------------------------------------------*/
return objects;
};
})(window.jQuery, window.Symphony);