mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Added Script.Head and Script.Foot for capturing literal script (w/o Capture lambda)
--HG-- branch : dev
This commit is contained in:
@@ -7,8 +7,9 @@ Model.Content.Zones.AddRenderPartial("primary:before", "CultureSelection", Model
|
|||||||
@Html.EditorForItem(m=>m.Content)
|
@Html.EditorForItem(m=>m.Content)
|
||||||
}
|
}
|
||||||
|
|
||||||
@using(Html.Capture(script => WorkContext.Page.Tail.Add(script) ){
|
@using(Script.Foot()){
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
//<![CDATA[
|
||||||
(function ($) {
|
(function ($) {
|
||||||
// grab the slug input
|
// grab the slug input
|
||||||
var slug = $("#Routable_Slug");
|
var slug = $("#Routable_Slug");
|
||||||
@@ -27,5 +28,6 @@ Model.Content.Zones.AddRenderPartial("primary:before", "CultureSelection", Model
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
})(jQuery);
|
})(jQuery);
|
||||||
|
//]]>
|
||||||
</script>
|
</script>
|
||||||
}
|
}
|
@@ -12,8 +12,9 @@
|
|||||||
@Html.EditorFor(m => m.PromoteToHomePage)
|
@Html.EditorFor(m => m.PromoteToHomePage)
|
||||||
<label for="@ViewData.TemplateInfo.GetFullHtmlFieldId("PromoteToHomePage")" class="forcheckbox">@T("Set as home page")</label>
|
<label for="@ViewData.TemplateInfo.GetFullHtmlFieldId("PromoteToHomePage")" class="forcheckbox">@T("Set as home page")</label>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
@using(Capture(script => WorkContext.Page.Tail.Add(script))){
|
@using(Script.Foot()){
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
//<![CDATA[
|
||||||
$(function(){
|
$(function(){
|
||||||
//pull slug input from tab order
|
//pull slug input from tab order
|
||||||
$("#@Html.FieldIdFor(m=>m.Slug)").attr("tabindex",-1);
|
$("#@Html.FieldIdFor(m=>m.Slug)").attr("tabindex",-1);
|
||||||
@@ -29,5 +30,6 @@
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
</script>
|
//]]>
|
||||||
|
</script>
|
||||||
}
|
}
|
@@ -89,15 +89,13 @@ namespace Orchard.Core.Shapes {
|
|||||||
[Shape]
|
[Shape]
|
||||||
public void HeadScripts(HtmlHelper Html, IResourceManager ResourceManager) {
|
public void HeadScripts(HtmlHelper Html, IResourceManager ResourceManager) {
|
||||||
WriteResources(Html, ResourceManager, "script", ResourceLocation.Head, null);
|
WriteResources(Html, ResourceManager, "script", ResourceLocation.Head, null);
|
||||||
|
WriteLiteralScripts(Html, ResourceManager.GetRegisteredHeadScripts());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Shape]
|
[Shape]
|
||||||
public void FootScripts(HtmlHelper Html, IResourceManager ResourceManager) {
|
public void FootScripts(HtmlHelper Html, IResourceManager ResourceManager) {
|
||||||
WriteResources(Html, ResourceManager, "script", null, ResourceLocation.Head);
|
WriteResources(Html, ResourceManager, "script", null, ResourceLocation.Head);
|
||||||
TextWriter captured;
|
WriteLiteralScripts(Html, ResourceManager.GetRegisteredFootScripts());
|
||||||
if (LayoutViewContext.From(Html.ViewContext).Contents.TryGetValue("end-of-page-scripts", out captured)) {
|
|
||||||
Html.ViewContext.Writer.Write(captured);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Shape]
|
[Shape]
|
||||||
@@ -119,6 +117,16 @@ namespace Orchard.Core.Shapes {
|
|||||||
WriteResources(Html, ResourceManager, "stylesheet", null, null);
|
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) {
|
private static void WriteResources(HtmlHelper html, IResourceManager rm, string resourceType, ResourceLocation? includeLocation, ResourceLocation? excludeLocation) {
|
||||||
var defaultSettings = new RequireSettings {
|
var defaultSettings = new RequireSettings {
|
||||||
DebugMode = html.ViewContext.HttpContext.IsDebuggingEnabled,
|
DebugMode = html.ViewContext.HttpContext.IsDebuggingEnabled,
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
@model Orchard.Core.Common.ViewModels.BodyEditorViewModel
|
@model Orchard.Core.Common.ViewModels.BodyEditorViewModel
|
||||||
@{
|
@{
|
||||||
Script.Require("TinyMce");
|
Script.Require("TinyMce");
|
||||||
using(Capture(script => WorkContext.Page.Tail.Add(script))) {
|
}
|
||||||
<script type="text/javascript">
|
@using(Script.Foot()) {
|
||||||
|
<script type="text/javascript">
|
||||||
|
//<![CDATA[
|
||||||
tinyMCE.init({
|
tinyMCE.init({
|
||||||
theme: "advanced",
|
theme: "advanced",
|
||||||
mode: "specific_textareas",
|
mode: "specific_textareas",
|
||||||
@@ -18,8 +20,7 @@
|
|||||||
addmedia_path: "@Model.AddMediaPath",
|
addmedia_path: "@Model.AddMediaPath",
|
||||||
request_verification_token: "@Html.AntiForgeryTokenValueOrchard()"
|
request_verification_token: "@Html.AntiForgeryTokenValueOrchard()"
|
||||||
});
|
});
|
||||||
</script>
|
//]]>
|
||||||
}
|
</script>
|
||||||
}
|
}
|
||||||
|
|
||||||
@Html.TextArea("Text", Model.Text, 25, 80, new { @class = "html" })
|
@Html.TextArea("Text", Model.Text, 25, 80, new { @class = "html" })
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
|
using System.Web.Mvc;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Microsoft.WebPages;
|
using Microsoft.WebPages;
|
||||||
using Orchard.DisplayManagement;
|
using Orchard.DisplayManagement;
|
||||||
@@ -33,7 +34,7 @@ namespace Orchard.Mvc.ViewEngines.Razor {
|
|||||||
public ScriptRegister Script {
|
public ScriptRegister Script {
|
||||||
get {
|
get {
|
||||||
return _scriptRegister ??
|
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);
|
_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> {
|
public abstract class WebViewPage : WebViewPage<dynamic> {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,7 @@ namespace Orchard.Mvc {
|
|||||||
public ScriptRegister Script {
|
public ScriptRegister Script {
|
||||||
get {
|
get {
|
||||||
return _scriptRegister ??
|
return _scriptRegister ??
|
||||||
(_scriptRegister = new ScriptRegister(Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
|
(_scriptRegister = new ViewPageScriptRegister(Writer, Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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> {
|
public class ViewPage : ViewPage<dynamic> {
|
||||||
|
@@ -33,7 +33,7 @@ namespace Orchard.Mvc {
|
|||||||
public ScriptRegister Script {
|
public ScriptRegister Script {
|
||||||
get {
|
get {
|
||||||
return _scriptRegister ??
|
return _scriptRegister ??
|
||||||
(_scriptRegister = new ScriptRegister(Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
|
(_scriptRegister = new ViewPage.ViewPageScriptRegister(Writer, Html.ViewDataContainer, Html.Resolve<IResourceManager>()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Orchard.UI.Resources {
|
namespace Orchard.UI.Resources {
|
||||||
public interface IResourceManager : IDependency {
|
public interface IResourceManager : IDependency {
|
||||||
@@ -6,11 +7,15 @@ namespace Orchard.UI.Resources {
|
|||||||
IList<ResourceRequiredContext> BuildRequiredResources(string resourceType);
|
IList<ResourceRequiredContext> BuildRequiredResources(string resourceType);
|
||||||
IList<LinkEntry> GetRegisteredLinks();
|
IList<LinkEntry> GetRegisteredLinks();
|
||||||
IList<MetaEntry> GetRegisteredMetas();
|
IList<MetaEntry> GetRegisteredMetas();
|
||||||
|
IList<String> GetRegisteredHeadScripts();
|
||||||
|
IList<String> GetRegisteredFootScripts();
|
||||||
IEnumerable<IResourceManifest> ResourceProviders { get; }
|
IEnumerable<IResourceManifest> ResourceProviders { get; }
|
||||||
ResourceManifest DynamicResources { get; }
|
ResourceManifest DynamicResources { get; }
|
||||||
ResourceDefinition FindResource(RequireSettings settings);
|
ResourceDefinition FindResource(RequireSettings settings);
|
||||||
void NotRequired(string resourceType, string resourceName);
|
void NotRequired(string resourceType, string resourceName);
|
||||||
RequireSettings Require(string resourceType, string resourceName);
|
RequireSettings Require(string resourceType, string resourceName);
|
||||||
|
void RegisterHeadScript(string script);
|
||||||
|
void RegisterFootScript(string script);
|
||||||
void RegisterLink(LinkEntry link);
|
void RegisterLink(LinkEntry link);
|
||||||
void SetMeta(MetaEntry meta);
|
void SetMeta(MetaEntry meta);
|
||||||
void AppendMeta(MetaEntry meta, string contentSeparator);
|
void AppendMeta(MetaEntry meta, string contentSeparator);
|
||||||
|
@@ -13,6 +13,8 @@ namespace Orchard.UI.Resources {
|
|||||||
private readonly List<LinkEntry> _links = new List<LinkEntry>();
|
private readonly List<LinkEntry> _links = new List<LinkEntry>();
|
||||||
private readonly Dictionary<string, MetaEntry> _metas = new Dictionary<string, MetaEntry>();
|
private readonly Dictionary<string, MetaEntry> _metas = new Dictionary<string, MetaEntry>();
|
||||||
private readonly Dictionary<string, IList<ResourceRequiredContext>> _builtResources = new Dictionary<string, IList<ResourceRequiredContext>>();
|
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) {
|
public ResourceManager(IEnumerable<IResourceManifest> resourceProviders) {
|
||||||
ResourceProviders = resourceProviders;
|
ResourceProviders = resourceProviders;
|
||||||
@@ -44,6 +46,20 @@ namespace Orchard.UI.Resources {
|
|||||||
return settings;
|
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) {
|
public virtual void NotRequired(string resourceType, string resourceName) {
|
||||||
if (resourceType == null) {
|
if (resourceType == null) {
|
||||||
throw new ArgumentNullException("resourceType");
|
throw new ArgumentNullException("resourceType");
|
||||||
@@ -100,6 +116,14 @@ namespace Orchard.UI.Resources {
|
|||||||
return _metas.Values.ToList().AsReadOnly();
|
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) {
|
public virtual IList<ResourceRequiredContext> BuildRequiredResources(string resourceType) {
|
||||||
IList<ResourceRequiredContext> requiredResources;
|
IList<ResourceRequiredContext> requiredResources;
|
||||||
if (_builtResources.TryGetValue(resourceType, out requiredResources) && requiredResources != null) {
|
if (_builtResources.TryGetValue(resourceType, out requiredResources) && requiredResources != null) {
|
||||||
|
@@ -9,32 +9,35 @@ using JetBrains.Annotations;
|
|||||||
namespace Orchard.UI.Resources {
|
namespace Orchard.UI.Resources {
|
||||||
public class ResourceRegister {
|
public class ResourceRegister {
|
||||||
private readonly TemplateControl _templateContainer;
|
private readonly TemplateControl _templateContainer;
|
||||||
private readonly string _resourceType;
|
|
||||||
private readonly IResourceManager _resourceManager;
|
|
||||||
|
|
||||||
public ResourceRegister(IViewDataContainer container, IResourceManager resourceManager, string resourceType) {
|
public ResourceRegister(IViewDataContainer container, IResourceManager resourceManager, string resourceType) {
|
||||||
_templateContainer = container as TemplateControl;
|
_templateContainer = container as TemplateControl;
|
||||||
_resourceManager = resourceManager;
|
ResourceManager = resourceManager;
|
||||||
_resourceType = resourceType;
|
ResourceType = resourceType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected IResourceManager ResourceManager { get; private set; }
|
||||||
|
protected string ResourceType { get; private set; }
|
||||||
|
|
||||||
public RequireSettings Require(string resourceName) {
|
public RequireSettings Require(string resourceName) {
|
||||||
return Require(resourceName, (string)null);
|
return Require(resourceName, (string)null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual RequireSettings Require(string resourceName, string minimumVersion) {
|
public virtual RequireSettings Require(string resourceName, string minimumVersion) {
|
||||||
var settings = _resourceManager.Require(_resourceType, resourceName)
|
var settings = ResourceManager.Require(ResourceType, resourceName)
|
||||||
.WithMinimumVersion(minimumVersion);
|
.WithMinimumVersion(minimumVersion);
|
||||||
if (_templateContainer != null) {
|
if (_templateContainer != null) {
|
||||||
settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(_resourceType, _templateContainer.AppRelativeVirtualPath));
|
settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(ResourceType, _templateContainer.AppRelativeVirtualPath));
|
||||||
}
|
}
|
||||||
return settings;
|
return settings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ScriptRegister : ResourceRegister {
|
public abstract class ScriptRegister : ResourceRegister {
|
||||||
public ScriptRegister(IViewDataContainer container, IResourceManager resourceManager) : base(container, resourceManager, "script") {
|
protected ScriptRegister(IViewDataContainer container, IResourceManager resourceManager) : base(container, resourceManager, "script") {
|
||||||
}
|
}
|
||||||
// todo: Head/Tail registration
|
|
||||||
|
public abstract IDisposable Head();
|
||||||
|
public abstract IDisposable Foot();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,12 +4,12 @@ ResourceManager Notes from Bertrand
|
|||||||
x No default parameters
|
x No default parameters
|
||||||
x Refactor to take over resource manager name
|
x Refactor to take over resource manager name
|
||||||
Better interface pattern for defining resources
|
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.
|
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.
|
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.
|
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.
|
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:
|
Raw notes from Dave during meeting:
|
||||||
====================================
|
====================================
|
||||||
@@ -25,13 +25,13 @@ Raw notes from Dave during meeting:
|
|||||||
|
|
||||||
// x no defualt params
|
// x no defualt params
|
||||||
// better interface/pattern for defining resources
|
// 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 Script.Require returns RequireSettings and has fluent api?
|
||||||
// x (no) Add Script.RequireHead <-- or no due to fluent api
|
// x (no) Add Script.RequireHead <-- or no due to fluent api
|
||||||
// x Rename Localization resource manager
|
// x Rename Localization resource manager
|
||||||
// Require w/ app relative url means inline definitin of url only, resolved url is resource name
|
// 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
|
// 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.
|
// 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?
|
// Integrate MS ajax min to build to minify our own js and css files?
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user