Skip to content

Optionally set a cutoff message. #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jquery.timeago.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
allowFuture: false,
localeTitle: false,
cutoff: 0,
cutoffMessage: false,
strings: {
prefixAgo: null,
prefixFromNow: null,
Expand Down Expand Up @@ -170,6 +171,9 @@
if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) {
$(this).text(inWords(data.datetime));
}
else if ($s.cutoffMessage && distance(data.datetime) >= $s.cutoff) {
$(this).text($s.cutoffMessage.concat(" ", inWords(new Date(new Date().getTime() - $s.cutoff))));
}
}
return this;
}
Expand Down
12 changes: 12 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ <h2>Other formats</h2>
<h2>Cutoff</h2>

<p>Date that is older than cutoff: <abbr class="timeago cutoff doCutoff" title="1978-12-18">(this should be displayed)</abbr></p>

<p>Date that is older than cutoff with message: <abbr class="timeago cutoff withCutoffMessage doCutoffWithCutoffMessage" title="1978-12-18">(you shouldn't see this)</abbr></p>

<p>Date that is newer than cutoff: <abbr class="timeago loaded cutoff dontCutoff">(you shouldn't see this)</abbr></p>

Expand Down Expand Up @@ -228,8 +230,12 @@ <h2>Disposal</h2>

loadCutoffSetting();
$("abbr.cutoff").timeago();
loadCutoffMessageSetting();
$("abbr.cutoff.withCutoffMessage").timeago();
unloadCutoffMessageSetting();
unloadCutoffSetting();


var tooltip = $("#testTooltip").data("timeago");

$("abbr.todate").each(function () {
Expand Down Expand Up @@ -316,6 +322,12 @@ <h2>Disposal</h2>
}), "Cutoff setting working fine");
});

test("should give cutoff message for dates older than cutoff setting", function() {
ok(testElements("abbr.doCutoffWithCutoffMessage", function (element) {
return (element.html() === "over 7 days ago");
}), "Cutoff message setting working fine");
});

test("should change dates newer than cutoff setting", function () {
ok(testElements("abbr.dontCutoff", function (element) {
return (element.html() === "less than a minute ago");
Expand Down
8 changes: 8 additions & 0 deletions test/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ function unloadCutoffSetting() {
jQuery.timeago.settings.cutoff = 0;
}

function loadCutoffMessageSetting() {
jQuery.timeago.settings.cutoffMessage = "over";
}

function unloadCutoffMessageSetting() {
jQuery.timeago.settings.cutoffMessage = false;
}

function setupDisposal() {
jQuery.timeago.settings.refreshMillis = 50;
$('abbr.disposal').attr("title", iso8601(new Date())).timeago();
Expand Down