Adding some slugification

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4045366
This commit is contained in:
skewed
2010-01-13 23:01:52 +00:00
parent 9d9f4b1669
commit be81ee7436
23 changed files with 148 additions and 88 deletions

View File

@@ -0,0 +1,28 @@
using System.Text.RegularExpressions;
using System.Web.Mvc;
namespace Orchard.Core.Common.Controllers {
public class RoutableController : Controller {
[HttpPost]
public ActionResult Slugify(FormCollection formCollection, string value) {
if (!string.IsNullOrEmpty(value)) {
//todo: (heskew) improve - just doing multi-pass regex replaces for now with the simple rules of
// (1) can't begin with a '/', (2) can't have adjacent '/'s and (3) can't have these characters
var startsoffbad = new Regex(@"^[\s/]+");
var slashhappy = new Regex("/{2,}");
var dissallowed = new Regex(@"[:?#\[\]@!$&'()*+,;=\s]+");
value = value.Trim();
value = startsoffbad.Replace(value, "-");
value = slashhappy.Replace(value, "/");
value = dissallowed.Replace(value, "-");
if (value.Length > 1000)
value = value.Substring(0, 1000);
}
return Json(value);
}
}
}

View File

@@ -0,0 +1,25 @@
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.ViewModels;
namespace Orchard.Core.Common.Controllers {
public class Routable : ContentPartDriver<RoutableAspect> {
private const string TemplateName = "Parts/Common.Routable";
protected override string Prefix {
get { return "Routable"; }
}
protected override DriverResult Editor(RoutableAspect part) {
var model = new RoutableEditorViewModel {RoutableAspect = part};
return ContentPartTemplate(model, TemplateName, Prefix).Location("primary", "before.5");
}
protected override DriverResult Editor(RoutableAspect part, IUpdateModel updater) {
var model = new RoutableEditorViewModel {RoutableAspect = part};
updater.TryUpdateModel(model, Prefix, null, null);
return ContentPartTemplate(model, TemplateName, Prefix).Location("primary", "before.5");
}
}
}

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;
namespace Orchard.Core.Common {
public class Routes : IRouteProvider {
#region IRouteProvider Members
public IEnumerable<RouteDescriptor> GetRoutes() {
return new[] {
new RouteDescriptor {
Route = new Route(
"Admin/Blogs/Slugify",
new RouteValueDictionary {
{"area", "Orchard.Blogs"},
{"controller", "BlogPost"},
{"action", "Slugify"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "Orchard.Blogs"}
},
new MvcRouteHandler())
}
};
}
public void GetRoutes(ICollection<RouteDescriptor> routes) {
foreach (RouteDescriptor routeDescriptor in GetRoutes()) {
routes.Add(routeDescriptor);
}
}
#endregion
}
}

View File

@@ -0,0 +1,9 @@
jQuery.fn.extend({
slugify: function(options) {
//todo: (heskew) need messaging system
if (!options.target || !options.url) return;
jQuery.post(options.url, { value: $(this).val(), __RequestVerificationToken: $("input[name=__RequestVerificationToken]").val() }, function(data) {
options.target.val(data);
}, "json");
}
});

View File

@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.Core.Common.Models;
using Orchard.Core.Common.Models;
namespace Orchard.Core.Common.ViewModels {
public class BodyDisplayViewModel {
public BodyAspect BodyAspect { get; set; }
}
}
}

View File

@@ -16,4 +16,4 @@ namespace Orchard.Core.Common.ViewModels {
public string TextEditorTemplate { get; set; }
}
}
}

View File

@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Orchard.Core.Common.ViewModels {
public class OwnerEditorViewModel {
[Required]
public string Owner { get; set; }
}
}
}

View File

@@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations;
using Orchard.Core.Common.Models;
namespace Orchard.Core.Common.ViewModels {
public class RoutableEditorViewModel {
public RoutableAspect RoutableAspect { get; set; }
[Required]
public string Title {
get { return RoutableAspect.Record.Title; }
set { RoutableAspect.Record.Title = value; }
}
[Required]
public string Slug {
get { return RoutableAspect.Record.Slug; }
set { RoutableAspect.Record.Slug = value; }
}
}
}

View File

@@ -0,0 +1,14 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<RoutableEditorViewModel>" %>
<%@ Import Namespace="Orchard.Core.Common.ViewModels"%>
<% Html.RegisterFootScript("jquery.slugify.js"); %>
<fieldset>
<%=Html.LabelFor(m => m.Title) %>
<%=Html.EditorFor(m => m.Title) %>
</fieldset>
<fieldset class="permalink">
<label class="sub" for="Slug"><%=_Encoded("Permalink")%><br /><span>[todo: (heskew) need path to here]/</span></label>
<span><%=Html.TextBoxFor(m => m.Slug, new { @class = "text" })%></span>
</fieldset>
<% using (this.Capture("end-of-page-scripts")) { %>
<script type="text/javascript">$(function(){$("input#Routable_Title").blur(function(){$(this).slugify({target:$("input#Routable_Slug"),url:"<%="/common/routable/slugify" %>"})})})</script>
<% } %>

View File

@@ -62,8 +62,11 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Controllers\BodyDriver.cs" />
<Compile Include="Common\Controllers\RoutableDriver.cs" />
<Compile Include="Common\Controllers\RoutableController.cs" />
<Compile Include="Common\Permissions.cs" />
<Compile Include="Common\Records\CommonVersionRecord.cs" />
<Compile Include="Common\Routes.cs" />
<Compile Include="Common\Utilities\LazyField.cs" />
<Compile Include="Common\Providers\CommonAspectHandler.cs" />
<Compile Include="Common\Models\CommonAspect.cs" />
@@ -76,6 +79,7 @@
<Compile Include="Common\Records\RoutableRecord.cs" />
<Compile Include="Common\ViewModels\BodyDisplayViewModel.cs" />
<Compile Include="Common\ViewModels\BodyEditorViewModel.cs" />
<Compile Include="Common\ViewModels\RoutableEditorViewModel.cs" />
<Compile Include="Common\ViewModels\OwnerEditorViewModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scheduling\Models\ScheduledAspect.cs" />
@@ -143,7 +147,9 @@
<Content Include="Themes\Views\Admin\Index.aspx" />
</ItemGroup>
<ItemGroup>
<Content Include="Common\Scripts\jquery.slugify.js" />
<Content Include="Common\Views\DisplayTemplates\Parts\Common.Body.ascx" />
<Content Include="Common\Views\EditorTemplates\Parts\Common.Routable.ascx" />
<Content Include="Common\Views\EditorTemplates\Parts\Common.Body.ascx" />
<Content Include="Common\Views\EditorTemplates\Parts\Common.Owner.ascx" />
<Content Include="Scheduling\Package.txt" />