diff --git a/jquery.timeago.js b/jquery.timeago.js index e3731b2..da181e0 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -41,6 +41,7 @@ refreshMillis: 60000, allowFuture: false, localeTitle: false, + cutoff: 0, strings: { prefixAgo: null, prefixFromNow: null, @@ -150,8 +151,12 @@ function refresh() { var data = prepareData(this); + var $s = $t.settings; + if (!isNaN(data.datetime)) { - $(this).text(inWords(data.datetime)); + if ( $s.cutoff == 0 || distance(data.datetime) < $s.cutoff) { + $(this).text(inWords(data.datetime)); + } } return this; } diff --git a/test/index.html b/test/index.html index cee81f9..e0e105d 100644 --- a/test/index.html +++ b/test/index.html @@ -63,6 +63,12 @@

+

Cutoff

+ +

Date that is older than cutoff: (this should be displayed)

+ +

Date that is newer than cutoff: (you shouldn't see this)

+

Errors

Bad (letters): (this should be displayed).

@@ -212,9 +218,13 @@ prepareDynamicDates(); - $("abbr.timeago").timeago(); + $("abbr.timeago").not("abbr.cutoff").timeago(); $("time.timeago").timeago(); + loadCutoffSetting(); + $("abbr.cutoff").timeago(); + unloadCutoffSetting(); + var tooltip = $("#testTooltip").data("timeago"); $("abbr.todate").each(function () { @@ -291,6 +301,20 @@ ok(tooltip.datetime, "datetime set"); }); + module("Cutoff"); + + test("should not change dates older than cutoff setting", function () { + ok(testElements("abbr.doCutoff", function (element) { + return (element.html() === "(this should be displayed)"); + }), "Cutoff 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"); + }), "Cutoff setting working fine"); + }); + module("Tooltip"); test("should set title to original text contents", function () { diff --git a/test/test_helpers.js b/test/test_helpers.js index bafb787..7cf78cf 100644 --- a/test/test_helpers.js +++ b/test/test_helpers.js @@ -22,6 +22,14 @@ function unloadNumbers() { jQuery.timeago.settings.strings.numbers = []; } +function loadCutoffSetting() { + jQuery.timeago.settings.cutoff = 7*24*60*60*1000; +} + +function unloadCutoffSetting() { + jQuery.timeago.settings.cutoff = 0; +} + function loadPigLatin() { jQuery.timeago.settings.strings = { suffixAgo: "ago-hay",