Making DateTimeRelative an extension method

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-10-18 17:21:42 -07:00
parent ee8f511484
commit 1bda590fda
11 changed files with 63 additions and 41 deletions

View File

@@ -9,7 +9,7 @@
}
<ul class="pageStatus">
<li>@if (modifiedUtc.HasValue) {
@T("Last modified: {0}", Html.DateTimeRelative(modifiedUtc.Value, T))}&nbsp;&#124;&nbsp;
@T("Last modified: {0}", Display.DateTimeRelative(dateTime: modifiedUtc.Value))}&nbsp;&#124;&nbsp;
</li>
<li>@T("By {0}", owner.UserName)</li>
</ul>

View File

@@ -6,5 +6,5 @@
DateTime? versionPublishedUtc = publishLaterPart.As<CommonPart>() == null ? null : publishLaterPart.As<CommonPart>().VersionPublishedUtc;
}
@if (publishLaterPart.IsPublished() && versionPublishedUtc.HasValue) {
@T("Published: {0}", Html.DateTimeRelative(versionPublishedUtc.Value, T)) @T(" | ")
@T("Published: {0}", Display.DateTimeRelative(versionPublishedUtc.Value)) @T(" | ")
}

View File

@@ -26,7 +26,7 @@
@if ((((DateTime?)Model.ScheduledPublishUtc).HasValue && ((DateTime?)Model.ScheduledPublishUtc).Value > DateTime.UtcNow) || (publishLaterPart.IsPublished() && versionPublishedUtc.HasValue)) {
<li>
@if (publishLaterPart.IsPublished() && versionPublishedUtc.HasValue) {
@T("Published: {0}", Html.DateTimeRelative(versionPublishedUtc.Value, T))
@T("Published: {0}", Display.DateTimeRelative(versionPublishedUtc.Value))
}
else {
<img class="icon" src="@Href("~/Core/PublishLater/Content/Admin/images/scheduled.gif")" alt="@T("Scheduled")" title="@T("The page is scheduled for publishing")" /><text> @T("Scheduled") </text>

View File

@@ -6,5 +6,5 @@
DateTime? versionPublishedUtc = publishLaterPart.As<CommonPart>() == null ? null : publishLaterPart.As<CommonPart>().VersionPublishedUtc;
}
@if (publishLaterPart.IsPublished() && versionPublishedUtc.HasValue) {
@T("Published: {0}", Html.DateTimeRelative(versionPublishedUtc.Value, T))
@T("Published: {0}", Display.DateTimeRelative(dateTime: versionPublishedUtc.Value))
}

View File

@@ -3,19 +3,19 @@
<ul class="comments">
@foreach (var comment in Model) {
<li>
<article class="comment">
<header>
<h4>
<span class="who">@Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })
</span>
<span class="when">said <time datetime="@comment.Record.CommentDateUtc.GetValueOrDefault()">@Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault(), T).ToString(), "#")</time>
</span>
</h4>
</header>
<p class="text">@comment.Record.CommentText</p>
</article>
</li>
<li>
<article class="comment">
<header>
<h4>
<span class="who">@Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })
</span>
<span class="when">said <time datetime="@comment.Record.CommentDateUtc.GetValueOrDefault()">@Html.Link(Display.DateTimeRelative(dateTime: comment.Record.CommentDateUtc.GetValueOrDefault()).ToString(), "#")</time>
</span>
</h4>
</header>
<p class="text">@comment.Record.CommentText</p>
</article>
</li>
}
</ul>

View File

@@ -20,7 +20,7 @@
<p>@T("The search index contains the following fields: {0}.", string.Join(T(", ").Text, Model.IndexEntry.Fields))</p>
}
<p>@T("The search index was last updated {0}.", Html.DateTimeRelative(Model.IndexEntry.LastUpdateUtc.Value, T))</p>
<p>@T("The search index was last updated {0}.", Display.DateTimeRelative(dateTime: Model.IndexEntry.LastUpdateUtc.Value))</p>
}
<p>@T("Update the search index now: ")<button type="submit" title="@T("Update the search index.")" class="primaryAction">@T("Update")</button></p>
@Html.AntiForgeryTokenOrchard()

View File

@@ -7,7 +7,7 @@ foreach (var comment in Model) { %>
<p><%: comment.Record.CommentText %></p>
</div>
<div class="commentauthor">
<span class="who"><%: Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault(), T).Text, "#")%></span>
<span class="who"><%: Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Display.DateTimeRelative(dateTime: comment.Record.CommentDateUtc.GetValueOrDefault()).Text, "#")%></span>
</div>
</li><%
} %>

View File

@@ -7,7 +7,7 @@ foreach (var comment in Model) { %>
<p><%: comment.Record.CommentText %></p>
</div>
<div class="commentauthor">
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault(), T).Text, "#")%></span>
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Display.DateTimeRelative(dateTime: comment.Record.CommentDateUtc.GetValueOrDefault()).Text, "#")%></span>
</div>
</li><%
} %>

View File

@@ -206,27 +206,6 @@ namespace Orchard.Mvc.Html {
#region Format Date/Time
public static LocalizedString DateTimeRelative(this HtmlHelper htmlHelper, DateTime? value, LocalizedString defaultIfNull, Localizer T) {
return value.HasValue ? htmlHelper.DateTimeRelative(value.Value, T) : defaultIfNull;
}
public static LocalizedString DateTimeRelative(this HtmlHelper htmlHelper, DateTime value, Localizer T) {
var time = htmlHelper.Resolve<IClock>().UtcNow - value;
if (time.TotalDays > 7)
return htmlHelper.DateTime(value, T("'on' MMM d yyyy 'at' h:mm tt"));
if (time.TotalHours > 24)
return T.Plural("1 day ago", "{0} days ago", time.Days);
if (time.TotalMinutes > 60)
return T.Plural("1 hour ago", "{0} hours ago", time.Hours);
if (time.TotalSeconds > 60)
return T.Plural("1 minute ago", "{0} minutes ago", time.Minutes);
if (time.TotalSeconds > 10)
return T.Plural("1 second ago", "{0} seconds ago", time.Seconds); //aware that the singular won't be used
return T("a moment ago");
}
public static LocalizedString DateTime(this HtmlHelper htmlHelper, DateTime? value, LocalizedString defaultIfNull) {
return value.HasValue ? htmlHelper.DateTime(value.Value) : defaultIfNull;
}

42
src/Orchard/Mvc/Shapes.cs Normal file
View File

@@ -0,0 +1,42 @@
using System;
using System.Web.Mvc;
using Orchard.DisplayManagement;
using Orchard.Localization;
using Orchard.Mvc.Html;
using Orchard.Services;
namespace Orchard.Mvc {
public class Shapes {
private readonly IClock _clock;
public Shapes(IClock clock) {
_clock = clock;
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
[Shape]
public LocalizedString DateTimeRelative(HtmlHelper Html, DateTime? dateTime, LocalizedString defaultIfNull) {
return dateTime.HasValue ? DateTimeRelative(Html, dateTime.Value) : defaultIfNull;
}
[Shape]
public LocalizedString DateTimeRelative(HtmlHelper Html, DateTime dateTime) {
var time = _clock.UtcNow - dateTime;
if (time.TotalDays > 7)
return Html.DateTime(dateTime, T("'on' MMM d yyyy 'at' h:mm tt"));
if (time.TotalHours > 24)
return T.Plural("1 day ago", "{0} days ago", time.Days);
if (time.TotalMinutes > 60)
return T.Plural("1 hour ago", "{0} hours ago", time.Hours);
if (time.TotalSeconds > 60)
return T.Plural("1 minute ago", "{0} minutes ago", time.Minutes);
if (time.TotalSeconds > 10)
return T.Plural("1 second ago", "{0} seconds ago", time.Seconds); //aware that the singular won't be used
return T("a moment ago");
}
}
}

View File

@@ -176,6 +176,7 @@
<Compile Include="Environment\Extensions\Loaders\RawThemeExtensionLoader.cs" />
<Compile Include="Localization\Services\DefaultLocalizedStringManager.cs" />
<Compile Include="Localization\Services\ILocalizedStringManager.cs" />
<Compile Include="Mvc\Shapes.cs" />
<Compile Include="Mvc\IOrchardViewPage.cs" />
<Compile Include="Mvc\Spooling\HtmlStringWriter.cs" />
<Compile Include="Scripting\IScriptingManager.cs" />