-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.timestamper.js
129 lines (111 loc) · 3.29 KB
/
jquery.timestamper.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
<!--
/*
* (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
*
* Available on github <http://www.github.com/codeconsortium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Plugin jQuery.TimeStamper
*
* @author Reece Fowell <reece at codeconsortium dot com>
*
* Create fuzzy timestamps that autoupdate as time progresses.
*
* Requires JQuery, make sure to have JQuery included in your JS to use this.
* JQuery needs to be loaded before this script in order for it to work.
* @link http://jquery.com/
*
* Also requires moments.js
* @link http://momentjs.com/
*/
$(document).ready(function() {
$('abbr.timestamper').timestamper({
interval: (1000 * 30)
});
});
!function($) {
//
// TIMESTAMPER PLUGIN DEFINITION
//
$.fn.timestamper = function (params) {
var timestampers = $([]);
return this.each(function () {
var $this = $(this);
var obj = new Timestamper($this, params);
setInterval($.proxy(obj.refresh, obj), obj.params.interval);
timestampers.push(obj);
});
};
//
// TIMESTAMPER PUBLIC CLASS DEFINITION
//
function Timestamper(element, params) {
this.init('timestamper', element, params)
};
//
// Default values.
//
Timestamper.prototype.defaults = {
interval: 10000,
seconds: {
inMinute: 60,
inHour: (60 * 60),
inDay: (60 * 60 * 24),
inWeek: (60 * 60 * 24 * 7),
inMonth: (60 * 60 * 24 * 7 * 4),
inYear: (60 * 60 * 24 * 365)
}
};
Timestamper.prototype.init = function (type, element, params) {
// Setup config.
this.params = this.defaults;
this.mergeDefaultParams(this.params, params);
// Save objects.
this.element = element;
// Prepare the date object from the timestamp date string.
this.ts = new moment.utc($(this.element).attr('title'), "YYYY-MM-DD HH:mm:ss TZ");
// Initial run.
this.refresh();
};
//
// Merges optional parameters with defaults.
// - Optional params will always overwrite default params.
// - Undefined params will be added to the default params.
//
Timestamper.prototype.mergeDefaultParams = function (defaults, compare) {
var self = this;
$.each(compare, function(key, val) {
if ($.isPlainObject(val)) {
if (defaults.hasOwnProperty(key)) {
self.mergeDefaultParams(defaults[key], val);
} else {
defaults[key] = val;
}
} else {
if (defaults.hasOwnProperty(key)) {
defaults[key] = val;
}
}
});
};
//
// Gets the new time message and sets the text value
// of the element to the new timestamp message.
//
Timestamper.prototype.refresh = function () {
var nw = new moment();
var self = this;
var message = function() {
if (nw.diff(self.ts, 'seconds') < 60) { return self.ts.fromNow(); }
if (nw.diff(self.ts, 'minutes') < 60) { return self.ts.fromNow(); }
if (nw.diff(self.ts, 'hours') < 24) { return self.ts.fromNow(); }
if (nw.diff(self.ts, 'days') < 7) { return self.ts.fromNow() + self.ts.format(' HH:mm'); }
if (nw.diff(self.ts, 'weeks') < 4) { return self.ts.format('YYYY-MM-DD HH:mm:ss'); }
if (nw.diff(self.ts, 'months') < 12) { return self.ts.format('YYYY-MM-DD HH:mm:ss'); }
if (nw.diff(self.ts, 'years') < 1) { return self.ts.format('YYYY-MM-DD HH:mm:ss'); }
};
$(this.element).text(message());
};
}(window.jQuery);