Merge pull request #169 from telerik/master
Added allowPast settings & tests
This commit is contained in:
commit
fc90536dac
4 changed files with 118 additions and 0 deletions
|
@ -116,6 +116,15 @@ jQuery.timeago(jQuery("abbr#some_id")); //=> "<span id="prog_element"></spa
|
|||
<pre>
|
||||
jQuery.timeago.settings.allowFuture = true;</pre>
|
||||
|
||||
<p class="how">
|
||||
To disable timestamps in the past, use the <tt>allowPast</tt> setting.
|
||||
This setting is set to true by default. When set to false, if the time is in the past then instead of displaying a message like "5 minutes ago" a static message will be displayed.
|
||||
The staic message displayed can be configured with the <tt>strings.inPast</tt> setting:
|
||||
</p>
|
||||
<pre>
|
||||
jQuery.timeago.settings.strings.inPast = "time has elapsed";
|
||||
jQuery.timeago.settings.allowPast = false;</pre>
|
||||
|
||||
<h3>Excusez-moi?</h3>
|
||||
<p>
|
||||
Yes, timeago has locale/i18n/language support. Here are some <a href="https://github.com/rmm5t/jquery-timeago/tree/master/locales">configuration examples</a>. Please submit a <a href="https://github.com/rmm5t/jquery-timeago">GitHub pull request</a> for corrections or additional languages.
|
||||
|
|
|
@ -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,6 +190,19 @@
|
|||
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
|
||||
</ul>
|
||||
|
||||
<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>
|
||||
|
@ -243,6 +256,14 @@
|
|||
$("abbr.tonumbers").each(toWords);
|
||||
unloadNumbers();
|
||||
|
||||
loadDoNotAllowFuture();
|
||||
$("abbr.doNotAllowFuture").timeago();
|
||||
unloadDoNotAllowFuture();
|
||||
|
||||
loadDoNotAllowPast();
|
||||
$("abbr.doNotAllowPast").timeago();
|
||||
unloadDoNotAllowPast();
|
||||
|
||||
setupDisposal();
|
||||
|
||||
loadYoungOldYears();
|
||||
|
@ -595,6 +616,36 @@
|
|||
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
|
||||
});
|
||||
|
||||
module("Allow Future and 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");
|
||||
});
|
||||
|
||||
test("allow future false with a future date moves time backwards", function () {
|
||||
equal($("#testAllowPastFalse1").html(), "in the past");
|
||||
});
|
||||
|
||||
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");
|
||||
|
||||
asyncTest("disposal", function() {
|
||||
|
|
|
@ -131,3 +131,49 @@ function loadYoungOldYears() {
|
|||
years: function(value) { return (value < 21) ? "%d young years" : "%d old years"; }
|
||||
});
|
||||
}
|
||||
|
||||
function loadDoNotAllowFuture() {
|
||||
var mockDateToUse = "2010-01-01";
|
||||
$.timeago.settings.allowFuture = false;
|
||||
enableMockedDate(mockDateToUse);
|
||||
}
|
||||
|
||||
function unloadDoNotAllowFuture() {
|
||||
$.timeago.settings.allowFuture = true;
|
||||
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;
|
||||
window.Date = function () {
|
||||
if(arguments.length === 0) {
|
||||
return new window.NativeDate(mockDate);
|
||||
} else if(arguments.length === 1) {
|
||||
return new window.NativeDate(arguments[0]);
|
||||
} else if(arguments.length === 7) {
|
||||
return new window.NativeDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
|
||||
} else {
|
||||
throw "Mocking Date with this number of parameters is not implemented.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function disableMockedDate() {
|
||||
window.Date = window.NativeDate;
|
||||
delete window.NativeDate;
|
||||
}
|
Reference in a new issue