From efdbae1c1f7c11f355a8935fa9f0d5a836f8a96d Mon Sep 17 00:00:00 2001 From: Martin Solli Date: Wed, 27 Oct 2010 14:29:24 +0200 Subject: [PATCH] Cutoff setting for leaving older dates alone. The cutoff setting, if set to something other than 0, ensures that dates older than this number of milliseconds is not touched by timeago. --- jquery.timeago.js | 7 ++++++- test/index.html | 26 +++++++++++++++++++++++++- test/test_helpers.js | 8 ++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) 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",