10000 Store the datetime as a jQuery data object instead of re-parsing on e… · rmm5t/jquery-timeago@ae84945 · GitHub
[go: up one dir, main page]

Skip to content

Commit ae84945

Browse files
committed
Store the datetime as a jQuery data object instead of re-parsing on every refresh
1 parent a5c1424 commit ae84945

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<script src="test/test_helpers.js" type="text/javascript"></script>
1111
<script type="text/javascript">
1212
jQuery(document).ready(function($) {
13+
prepareDynamicDates();
1314
$('abbr.timeago').timeago();
1415

1516
$("#prog_date").text(jQuery.timeago(new Date()));
@@ -54,8 +55,9 @@ <h3>What?</h3>
5455
timestamps (e.g. "4 minutes ago" or "about 1 day ago"). <a href="jquery.timeago.js">Download</a>, view
5556
the examples, and enjoy.
5657
</p>
58+
<noscript><p class="example"><strong>Turn on javascript, loser!</strong></p></noscript>
5759
<p class="example">
58-
You opened this page <abbr class="loaded timeago">sometime before now <span class="help">(turn on javascript, loser)</span></abbr>. <span class="help">(This will update every minute. Wait for it.)</span>
60+
You opened this page <abbr class="loaded timeago">when you opened the page</abbr>. <span class="help">(This will update every minute. Wait for it.)</span>
5961
</p>
6062
<p class="example">
6163
This page was last modified <abbr class="modified timeago">sometime before now [browser might not support document.lastModified]</abbr>.

jquery.timeago.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* timeago: a jQuery plugin, version: 0.7.2 (2009-07-30)
3-
* @requires jQuery v1.2 or later
3+
* @requires jQuery v1.2.3 or later
44
*
55
* Timeago is a jQuery plugin that makes it easy to support automatically
66
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
@@ -85,8 +85,8 @@
8585
return new Date(s);
8686
},
8787
datetime: function(elem) {
88-
var that = $(elem);
89-
return $t.parse(that.is('time') ? that.attr('datetime') : that.attr('title'));
88+
var iso8601 = $(elem).is('time') ? $(elem).attr('datetime') : $(elem).attr('title');
89+
return $t.parse(iso8601);
9090
}
9191
});
9292

@@ -102,13 +102,23 @@
102102
};
103103

104104
function refresh() {
105-
var date = $t.datetime(this);
106-
if (!isNaN(date)) {
107-
$(this).text(inWords(date));
105+
var data = prepareData(this);
106+
if (!isNaN(data.datetime)) {
107+
$(this).text(inWords(data.datetime));
108108
}
109109
return this;
110110
}
111111

112+
function prepareData(element) {
113+
element = $(element);
114+
if (element.data("timeago") === undefined) {
115+
element.data("timeago", { datetime: $t.datetime(element) });
116+
var text = $.trim(element.text());
117+
if (text.length > 0) element.attr("title", text);
118+
}
119+
return element.data("timeago");
120+
}
121+
112122
function inWords(date) {
113123
return $t.inWords(distance(date));
114124
}

test/index.html

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ <h2>Other formats</h2>
6868

6969
<p>Date only (abbr element): <abbr class="timeago" title="2008-02-26">(you shouldn't see this)</abbr>.</p>
7070
<p>Date only (time element): <time class="timeago" datetime="2008-02-26">(you shouldn't see this)</time>.</p>
71+
<p>Date only (friendly tooltip): <abbr id="testTooltip" class="timeago" title="2008-02-26">February 26th</abbr>.</p>
72+
<p>Date only (default tooltip): <abbr id="defaultTooltip" class="timeago" title="2008-02-26"> </abbr>.</p>
7173

7274
<h2>Errors</h2>
7375

@@ -190,12 +192,13 @@ <h2>Settings</h2>
190192

191193
$.timeago.settings.allowFuture = true;
192194

193-
$('abbr.loaded').attr('title', iso8601(new Date()));
194-
$('abbr.modified').attr('title', iso8601(new Date(document.lastModified)));
195+
prepareDynamicDates();
195196

196197
$('abbr.timeago').timeago();
197198
$('time.timeago').timeago();
198199

200+
var tooltip = $("#testTooltip").data("timeago");
201+
199202
$('abbr.todate').each(function () {
200203
var date = $.timeago.parse(this.title);
201204
$(this).text(date.toUTCString());
@@ -250,6 +253,23 @@ <h2>Settings</h2>
250253
}), 'All long term dates correctly parsed');
251254
});
252255

256+
module('Data object');
257+
258+
test("should set timeago data object", function () {
259+
ok(tooltip, "data set");
260+
ok(tooltip.datetime, "datetime set");
261+
});
262+
263+
module('Tooltip');
264+
265+
test("should set title to original text contents", function () {
266+
ok($("#testTooltip").attr("title") == "February 26th", "correctly set");
267+
});
268+
269+
test("should leave title alone if no text text content", function () {
270+
ok($("#defaultTooltip").attr("title") == "2008-02-26", "correctly set");
271+
});
272+
253273
module('Parsing');
254274

255275
// Note, different browsers behave slightly different

test/test_helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ var iso8601 = function (date) {
1010
+ ":" + zeropad(date.getUTCSeconds()) + "Z";
1111
};
1212

13-
jQuery(document).ready(function($) {
13+
function prepareDynamicDates() {
1414
$('abbr.loaded').attr("title", iso8601(new Date()));
1515
$('abbr.modified').attr("title", iso8601(new Date(document.lastModified)));
16-
});
16+
}
1717

1818
function loadPigLatin() {
1919
jQuery.timeago.settings.strings = {

0 commit comments

Comments
 (0)
0