mirror of
https://github.com/scratchfoundation/jquery-timeago.git
synced 2025-02-17 00:20:32 -05:00
Added "allowPast" setting.
This commit is contained in:
parent
873f0f7c10
commit
9d6c2c6b64
3 changed files with 60 additions and 7 deletions
|
@ -39,6 +39,7 @@
|
|||
$.extend($.timeago, {
|
||||
settings: {
|
||||
refreshMillis: 60000,
|
||||
allowPast: true,
|
||||
allowFuture: false,
|
||||
localeTitle: false,
|
||||
cutoff: 0,
|
||||
|
@ -47,6 +48,7 @@
|
|||
prefixFromNow: null,
|
||||
suffixAgo: "ago",
|
||||
suffixFromNow: "from now",
|
||||
inPast: 'any moment now',
|
||||
seconds: "less than a minute",
|
||||
minute: "about a minute",
|
||||
minutes: "%d minutes",
|
||||
|
@ -62,7 +64,12 @@
|
|||
numbers: []
|
||||
}
|
||||
},
|
||||
|
||||
inWords: function(distanceMillis) {
|
||||
if(!this.settings.allowPast && ! this.settings.allowFuture) {
|
||||
throw 'timeago allowPast and allowFuture settings can not both be set to false.';
|
||||
}
|
||||
|
||||
var $l = this.settings.strings;
|
||||
var prefix = $l.prefixAgo;
|
||||
var suffix = $l.suffixAgo;
|
||||
|
@ -73,6 +80,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
if(!this.settings.allowPast && distanceMillis >= 0) {
|
||||
return this.settings.strings.inPast;
|
||||
}
|
||||
|
||||
var seconds = Math.abs(distanceMillis) / 1000;
|
||||
var minutes = seconds / 60;
|
||||
var hours = minutes / 60;
|
||||
|
@ -101,6 +112,7 @@
|
|||
if ($l.wordSeparator === undefined) { separator = " "; }
|
||||
return $.trim([prefix, words, suffix].join(separator));
|
||||
},
|
||||
|
||||
parse: function(iso8601) {
|
||||
var s = $.trim(iso8601);
|
||||
s = s.replace(/\.\d+/,""); // remove milliseconds
|
||||
|
|
|
@ -190,12 +190,19 @@
|
|||
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
|
||||
</ul>
|
||||
|
||||
<h2>Future</h2>
|
||||
<h2>Do Not Allow Future</h2>
|
||||
|
||||
<ul>
|
||||
<li><abbr id="testAllowFutureFalse1" class="doNotAllowFuture" title="2012-01-01T09:24:17Z">(you shouldn't see this)</abbr></li>
|
||||
</ul>
|
||||
|
||||
<h2>Do Not Allow Past</h2>
|
||||
|
||||
<ul>
|
||||
<li><abbr id="testAllowPastFalse1" class="doNotAllowPast" title="2008-01-01T09:24:17Z">(you shouldn't see this)</abbr></li>
|
||||
<li><abbr id="testAllowPastAndFutureFalse" title="2008-01-01T09:24:17Z"></abbr></li>
|
||||
</ul>
|
||||
|
||||
<h2>Disposal</h2>
|
||||
<p><abbr class="disposal disposed"></abbr></p>
|
||||
<p><abbr class="disposal notDisposed"></abbr></p>
|
||||
|
@ -253,6 +260,10 @@
|
|||
$("abbr.doNotAllowFuture").timeago();
|
||||
unloadDoNotAllowFuture();
|
||||
|
||||
loadDoNotAllowPast();
|
||||
$("abbr.doNotAllowPast").timeago();
|
||||
unloadDoNotAllowPast();
|
||||
|
||||
setupDisposal();
|
||||
|
||||
loadYoungOldYears();
|
||||
|
@ -605,19 +616,35 @@
|
|||
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
|
||||
});
|
||||
|
||||
module("Do Not Allow Future");
|
||||
module("Allow Future and Past");
|
||||
|
||||
// if allowFuture is false, then a minute into the future is altually reported as a minute in the past.
|
||||
// if allowFuture is false, then a minute into the future is actually reported as a minute in the past.
|
||||
// we did not want to alter the current behavior and break backwards compatability
|
||||
test("allow future false with a future date moves time backwards", function () {
|
||||
equal($("#testAllowFutureFalse1").html(), "2 years ago");
|
||||
});
|
||||
|
||||
module("Allow Future");
|
||||
test("allow future false with a future date moves time backwards", function () {
|
||||
equal($("#testAllowPastFalse1").html(), "in the past");
|
||||
});
|
||||
|
||||
// test("allow future false with a future date moves time backwards", function () {
|
||||
// ok($("#testAllowFutureFalse1").html().match(/years ago$/));
|
||||
// });
|
||||
test("throws if allowPast and allowFuture are both false", function () {
|
||||
// setup
|
||||
var origAllowFuture = $.timeago.settings.allowFuture;
|
||||
var origAllowPast = $.timeago.settings.allowPast;
|
||||
$.timeago.settings.allowFuture = false;
|
||||
$.timeago.settings.allowPast = false;
|
||||
|
||||
try {
|
||||
throws(function () {
|
||||
$("#testAllowPastAndFutureFalse").timeago();
|
||||
});
|
||||
} finally {
|
||||
// teardown
|
||||
$.timeago.settings.allowFuture = origAllowFuture;
|
||||
$.timeago.settings.allowPast = origAllowPast;
|
||||
}
|
||||
});
|
||||
|
||||
module("Disposal");
|
||||
|
||||
|
|
|
@ -143,6 +143,20 @@ function unloadDoNotAllowFuture() {
|
|||
disableMockedDate();
|
||||
}
|
||||
|
||||
function loadDoNotAllowPast() {
|
||||
var mockDateToUse = "2010-01-01";
|
||||
$.timeago.settings.allowFuture = true;
|
||||
$.timeago.settings.allowPast = false;
|
||||
$.timeago.settings.strings.inPast = "in the past";
|
||||
enableMockedDate(mockDateToUse);
|
||||
}
|
||||
|
||||
function unloadDoNotAllowPast() {
|
||||
$.timeago.settings.allowFuture = true;
|
||||
$.timeago.settings.allowPast = true;
|
||||
disableMockedDate();
|
||||
}
|
||||
|
||||
function enableMockedDate(dateToReturn) {
|
||||
var mockDate = dateToReturn;
|
||||
window.NativeDate = Date;
|
||||
|
|
Loading…
Reference in a new issue