Added Script.Head and Script.Foot for capturing literal script (w/o Capture lambda)

--HG--
branch : dev
This commit is contained in:
Dave Reed
2010-09-21 10:39:04 -07:00
parent 8b0d80f0bf
commit 45cba60180
11 changed files with 138 additions and 60 deletions

View File

@@ -7,8 +7,9 @@ Model.Content.Zones.AddRenderPartial("primary:before", "CultureSelection", Model
@Html.EditorForItem(m=>m.Content)
}
@using(Html.Capture(script => WorkContext.Page.Tail.Add(script) ){
@using(Script.Foot()){
<script type="text/javascript">
//<![CDATA[
(function ($) {
// grab the slug input
var slug = $("#Routable_Slug");
@@ -27,5 +28,6 @@ Model.Content.Zones.AddRenderPartial("primary:before", "CultureSelection", Model
});
}
})(jQuery);
//]]>
</script>
}

View File

@@ -12,22 +12,24 @@
@Html.EditorFor(m => m.PromoteToHomePage)
<label for="@ViewData.TemplateInfo.GetFullHtmlFieldId("PromoteToHomePage")" class="forcheckbox">@T("Set as home page")</label>
</fieldset>
@using(Capture(script => WorkContext.Page.Tail.Add(script))){
<script type="text/javascript">
$(function(){
//pull slug input from tab order
$("#@Html.FieldIdFor(m=>m.Slug)").attr("tabindex",-1);
$("#@Html.FieldIdFor(m=>m.Title)").blur(function(){
var slug = $("#@Html.FieldIdFor(m=>m.Slug)");
if (slug.val()) { return true; }
$(this).slugify({
target:slug,
contentType:"@Model.ContentType",
id:"@Model.Id",
@if (Model.ContainerId != null) {<text>containerId:@Model.ContainerId,</text>}
url:"@Url.Action("Slugify","Item",new RouteValueDictionary{{"Area","Routable"}})"
})
@using(Script.Foot()){
<script type="text/javascript">
//<![CDATA[
$(function(){
//pull slug input from tab order
$("#@Html.FieldIdFor(m=>m.Slug)").attr("tabindex",-1);
$("#@Html.FieldIdFor(m=>m.Title)").blur(function(){
var slug = $("#@Html.FieldIdFor(m=>m.Slug)");
if (slug.val()) { return true; }
$(this).slugify({
target:slug,
contentType:"@Model.ContentType",
id:"@Model.Id",
@if (Model.ContainerId != null) {<text>containerId:@Model.ContainerId,</text>}
url:"@Url.Action("Slugify","Item",new RouteValueDictionary{{"Area","Routable"}})"
})
})
</script>
})
//]]>
</script>
}

View File

@@ -89,15 +89,13 @@ namespace Orchard.Core.Shapes {
[Shape]
public void HeadScripts(HtmlHelper Html, IResourceManager ResourceManager) {
WriteResources(Html, ResourceManager, "script", ResourceLocation.Head, null);
WriteLiteralScripts(Html, ResourceManager.GetRegisteredHeadScripts());
}
[Shape]
public void FootScripts(HtmlHelper Html, IResourceManager ResourceManager) {
WriteResources(Html, ResourceManager, "script", null, ResourceLocation.Head);
TextWriter captured;
if (LayoutViewContext.From(Html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured)) {
Html.ViewContext.Writer.Write(captured);
}
WriteLiteralScripts(Html, ResourceManager.GetRegisteredFootScripts());
}
[Shape]
@@ -119,6 +117,16 @@ namespace Orchard.Core.Shapes {
WriteResources(Html, ResourceManager, "stylesheet", null, null);
}
private static void WriteLiteralScripts(HtmlHelper html, IEnumerable<string> scripts) {
if (scripts == null) {
return;
}
var writer = html.ViewContext.Writer;
foreach (string script in scripts) {
writer.WriteLine(script);
}
}
private static void WriteResources(HtmlHelper html, IResourceManager rm, string resourceType, ResourceLocation? includeLocation, ResourceLocation? excludeLocation) {
var defaultSettings = new RequireSettings {
DebugMode = html.ViewContext.HttpContext.IsDebuggingEnabled,

View File

@@ -1,25 +1,26 @@
@model Orchard.Core.Common.ViewModels.BodyEditorViewModel
@{
Script.Require("TinyMce");
using(Capture(script => WorkContext.Page.Tail.Add(script))) {
<script type="text/javascript">
tinyMCE.init({
theme: "advanced",
mode: "specific_textareas",
editor_selector: "html",
plugins: "fullscreen,autoresize,searchreplace,addmedia",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_buttons1: "search,replace,|,cut,copy,paste,|,undo,redo,|,image,addmedia,|,link,unlink,charmap,emoticon,codeblock,|,bold,italic,|,numlist,bullist,formatselect,|,code,fullscreen",
theme_advanced_buttons2: "",
theme_advanced_buttons3: "",
convert_urls: false,
addmedia_action: "@Url.Action("AddFromClient", "Admin", new {area = "Orchard.Media"})",
addmedia_path: "@Model.AddMediaPath",
request_verification_token: "@Html.AntiForgeryTokenValueOrchard()"
});
</script>
}
}
@using(Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
tinyMCE.init({
theme: "advanced",
mode: "specific_textareas",
editor_selector: "html",
plugins: "fullscreen,autoresize,searchreplace,addmedia",
theme_advanced_toolbar_location: "top",
theme_advanced_toolbar_align: "left",
theme_advanced_buttons1: "search,replace,|,cut,copy,paste,|,undo,redo,|,image,addmedia,|,link,unlink,charmap,emoticon,codeblock,|,bold,italic,|,numlist,bullist,formatselect,|,code,fullscreen",
theme_advanced_buttons2: "",
theme_advanced_buttons3: "",
convert_urls: false,
addmedia_action: "@Url.Action("AddFromClient", "Admin", new {area = "Orchard.Media"})",
addmedia_path: "@Model.AddMediaPath",
request_verification_token: "@Html.AntiForgeryTokenValueOrchard()"
});
//]]>
</script>
}
@Html.TextArea("Text", Model.Text, 25, 80, new { @class = "html" })

View File

@@ -1,5 +1,6 @@
using System;
using System.Web;
using System.Web.Mvc;
using Autofac;
using Microsoft.WebPages;
using Orchard.DisplayManagement;
@@ -33,7 +34,7 @@ namespace Orchard.Mvc.ViewEngines.Razor {
public ScriptRegister Script {
get {
return _scriptRegister ??
(_scriptRegister = new ScriptRegister(Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
(_scriptRegister = new WebViewScriptRegister(this, Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
}
}
@@ -106,9 +107,25 @@ namespace Orchard.Mvc.ViewEngines.Razor {
_callback(writer);
}
}
class WebViewScriptRegister : ScriptRegister {
private readonly WebPageBase _viewPage;
public WebViewScriptRegister(WebPageBase viewPage, IViewDataContainer container, IResourceManager resourceManager)
: base(container, resourceManager) {
_viewPage = viewPage;
}
public override IDisposable Head() {
return new CaptureScope(_viewPage, s => ResourceManager.RegisterHeadScript(s.ToString()));
}
public override IDisposable Foot() {
return new CaptureScope(_viewPage, s => ResourceManager.RegisterFootScript(s.ToString()));
}
}
}
public abstract class WebViewPage : WebViewPage<dynamic> {
}
}

View File

@@ -26,7 +26,7 @@ namespace Orchard.Mvc {
public ScriptRegister Script {
get {
return _scriptRegister ??
(_scriptRegister = new ScriptRegister(Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
(_scriptRegister = new ViewPageScriptRegister(Writer, Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
}
}
@@ -44,7 +44,7 @@ namespace Orchard.Mvc {
}
}
public override void InitHelpers() {
public override void InitHelpers() {
base.InitHelpers();
_workContext = ViewContext.GetWorkContext();
@@ -113,6 +113,22 @@ namespace Orchard.Mvc {
}
}
internal class ViewPageScriptRegister : ScriptRegister {
private readonly HtmlTextWriter _context;
public ViewPageScriptRegister(HtmlTextWriter context, IViewDataContainer container, IResourceManager resourceManager)
: base(container, resourceManager) {
_context = context;
}
public override IDisposable Head() {
return new CaptureScope(_context, s => ResourceManager.RegisterHeadScript(s.ToString()));
}
public override IDisposable Foot() {
return new CaptureScope(_context, s => ResourceManager.RegisterFootScript(s.ToString()));
}
}
}
public class ViewPage : ViewPage<dynamic> {

View File

@@ -33,7 +33,7 @@ namespace Orchard.Mvc {
public ScriptRegister Script {
get {
return _scriptRegister ??
(_scriptRegister = new ScriptRegister(Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
(_scriptRegister = new ViewPage.ViewPageScriptRegister(Writer, Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
namespace Orchard.UI.Resources {
public interface IResourceManager : IDependency {
@@ -6,11 +7,15 @@ namespace Orchard.UI.Resources {
IList<ResourceRequiredContext> BuildRequiredResources(string resourceType);
IList<LinkEntry> GetRegisteredLinks();
IList<MetaEntry> GetRegisteredMetas();
IList<String> GetRegisteredHeadScripts();
IList<String> GetRegisteredFootScripts();
IEnumerable<IResourceManifest> ResourceProviders { get; }
ResourceManifest DynamicResources { get; }
ResourceDefinition FindResource(RequireSettings settings);
void NotRequired(string resourceType, string resourceName);
RequireSettings Require(string resourceType, string resourceName);
void RegisterHeadScript(string script);
void RegisterFootScript(string script);
void RegisterLink(LinkEntry link);
void SetMeta(MetaEntry meta);
void AppendMeta(MetaEntry meta, string contentSeparator);

View File

@@ -13,6 +13,8 @@ namespace Orchard.UI.Resources {
private readonly List<LinkEntry> _links = new List<LinkEntry>();
private readonly Dictionary<string, MetaEntry> _metas = new Dictionary<string, MetaEntry>();
private readonly Dictionary<string, IList<ResourceRequiredContext>> _builtResources = new Dictionary<string, IList<ResourceRequiredContext>>();
private List<String> _headScripts;
private List<String> _footScripts;
public ResourceManager(IEnumerable<IResourceManifest> resourceProviders) {
ResourceProviders = resourceProviders;
@@ -44,6 +46,20 @@ namespace Orchard.UI.Resources {
return settings;
}
public virtual void RegisterHeadScript(string script) {
if (_headScripts == null) {
_headScripts = new List<string>();
}
_headScripts.Add(script);
}
public virtual void RegisterFootScript(string script) {
if (_footScripts == null) {
_footScripts = new List<string>();
}
_footScripts.Add(script);
}
public virtual void NotRequired(string resourceType, string resourceName) {
if (resourceType == null) {
throw new ArgumentNullException("resourceType");
@@ -100,6 +116,14 @@ namespace Orchard.UI.Resources {
return _metas.Values.ToList().AsReadOnly();
}
public virtual IList<String> GetRegisteredHeadScripts() {
return _headScripts == null ? null : _headScripts.AsReadOnly();
}
public virtual IList<String> GetRegisteredFootScripts() {
return _footScripts == null ? null : _footScripts.AsReadOnly();
}
public virtual IList<ResourceRequiredContext> BuildRequiredResources(string resourceType) {
IList<ResourceRequiredContext> requiredResources;
if (_builtResources.TryGetValue(resourceType, out requiredResources) && requiredResources != null) {

View File

@@ -9,32 +9,35 @@ using JetBrains.Annotations;
namespace Orchard.UI.Resources {
public class ResourceRegister {
private readonly TemplateControl _templateContainer;
private readonly string _resourceType;
private readonly IResourceManager _resourceManager;
public ResourceRegister(IViewDataContainer container, IResourceManager resourceManager, string resourceType) {
_templateContainer = container as TemplateControl;
_resourceManager = resourceManager;
_resourceType = resourceType;
ResourceManager = resourceManager;
ResourceType = resourceType;
}
protected IResourceManager ResourceManager { get; private set; }
protected string ResourceType { get; private set; }
public RequireSettings Require(string resourceName) {
return Require(resourceName, (string)null);
}
public virtual RequireSettings Require(string resourceName, string minimumVersion) {
var settings = _resourceManager.Require(_resourceType, resourceName)
var settings = ResourceManager.Require(ResourceType, resourceName)
.WithMinimumVersion(minimumVersion);
if (_templateContainer != null) {
settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(_resourceType, _templateContainer.AppRelativeVirtualPath));
settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(ResourceType, _templateContainer.AppRelativeVirtualPath));
}
return settings;
}
}
public class ScriptRegister : ResourceRegister {
public ScriptRegister(IViewDataContainer container, IResourceManager resourceManager) : base(container, resourceManager, "script") {
public abstract class ScriptRegister : ResourceRegister {
protected ScriptRegister(IViewDataContainer container, IResourceManager resourceManager) : base(container, resourceManager, "script") {
}
// todo: Head/Tail registration
public abstract IDisposable Head();
public abstract IDisposable Foot();
}
}

View File

@@ -4,12 +4,12 @@ ResourceManager Notes from Bertrand
x No default parameters
x Refactor to take over resource manager name
Better interface pattern for defining resources
Script.Load API that is a view engine specific helper and that directs rendering within the using (using Idisposable pattern) to a buffer that can be rendered later.
x Script.Load API that is a view engine specific helper and that directs rendering within the using (using Idisposable pattern) to a buffer that can be rendered later.
x Script.Require returns RequireSettings and has fluent api to add stuff later.
When using the shortcut registration+require Script.Require("../script/foo.js") we resolve the url to an app-relative path and use that as the id.
If you want ot define a debug version in that case, just do a proper registration.
To decide if the app is in debug mode, we look at a setting on the require, then at site setting mode and as a last resort on the compilation mode of the app.
Merge asap, then debug, then refactor.
x Merge asap, then debug, then refactor.
Raw notes from Dave during meeting:
====================================
@@ -25,13 +25,13 @@ Raw notes from Dave during meeting:
// x no defualt params
// better interface/pattern for defining resources
// Script.Load (view-engine specific helper required)
// x Script.Load (view-engine specific helper required)
// x Script.Require returns RequireSettings and has fluent api?
// x (no) Add Script.RequireHead <-- or no due to fluent api
// x Rename Localization resource manager
// Require w/ app relative url means inline definitin of url only, resolved url is resource name
// Site setting for debug mode true false or from web.config setting
// Moved all copies of jquery and jquery related scripts to a single location
// x Moved all copies of jquery and jquery related scripts to a single location
// Get minified versions of any external scripts that we don't currently have.
// Integrate MS ajax min to build to minify our own js and css files?