mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
Require() now returns RequireSettings which has a fluent api. Dropped more complex overloads of Require in favor of it.
--HG-- branch : dev
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
@model Orchard.Core.PublishLater.ViewModels.PublishLaterViewModel
|
||||
@{
|
||||
Script.RequireFoot("jQueryUtils_TimePicker");
|
||||
Script.RequireFoot("jQueryUI_DatePicker");
|
||||
Script.Require("jQueryUtils_TimePicker");
|
||||
Script.Require("jQueryUI_DatePicker");
|
||||
Style.Require("PublishLater_DatePicker");
|
||||
Style.Require("jQueryUtils_TimePicker");
|
||||
Style.Require("jQueryUI_DatePicker");
|
||||
|
@@ -69,11 +69,6 @@ namespace Orchard.Core.Shapes {
|
||||
return tagBuilder;
|
||||
}
|
||||
|
||||
//[Shape]
|
||||
//public HtmlString Resource(ResourceRequiredContext Resource, RequireSettings DefaultSettings, string AppPath) {
|
||||
// return new HtmlString(Resource.GetTagBuilder(DefaultSettings, AppPath).ToString());
|
||||
//}
|
||||
|
||||
[Shape]
|
||||
public void HeadScripts(HtmlHelper Html, IResourceManager ResourceManager) {
|
||||
WriteResources(Html, ResourceManager, "script", ResourceLocation.Head, null);
|
||||
|
@@ -3,8 +3,8 @@
|
||||
<%
|
||||
// todo: use Style.Require and Script.Require when this is converted to use a base Orchard view type.
|
||||
var rm = Html.Resolve<IResourceManager>();
|
||||
rm.Require(new RequireSettings { ResourceType = "stylesheet", ResourceName = "Switchable" });
|
||||
rm.Require(new RequireSettings { ResourceType = "script", ResourceName = "Switchable", Location = ResourceLocation.Foot });
|
||||
rm.Require("stylesheet", "Switchable");
|
||||
rm.Require("script", "Switchable");
|
||||
var cssClass = string.Format("{0} switchable", Model);
|
||||
|
||||
%>
|
||||
|
@@ -4,8 +4,8 @@
|
||||
Style.Require("jQueryUtils_TimePicker");
|
||||
Style.Require("jQueryUI_DatePicker");
|
||||
Style.Require("ArchiveLater_DatePicker");
|
||||
Script.RequireFoot("jQueryUI_DatePicker");
|
||||
Script.RequireFoot("jQueryUtils_TimePicker");
|
||||
Script.Require("jQueryUI_DatePicker");
|
||||
Script.Require("jQueryUtils_TimePicker");
|
||||
}
|
||||
|
||||
<fieldset>
|
||||
|
@@ -3,7 +3,7 @@
|
||||
@{
|
||||
BlogPostArchiveViewModel model = Model.Archives;
|
||||
Style.Require("BlogsArchives");
|
||||
Script.RequireFoot("BlogsArchives");
|
||||
Script.Require("BlogsArchives");
|
||||
}
|
||||
<div class="archives">
|
||||
<h3>@T("Archives")</h3>
|
||||
|
@@ -4,7 +4,7 @@
|
||||
<%@ Import Namespace="Orchard.UI.Resources" %>
|
||||
<%
|
||||
Style.Require("Green_Archives");
|
||||
Script.Require(new RequireSettings {ResourceName = "archives.js", Location = ResourceLocation.Foot});
|
||||
Script.Require("BlogsArchives");
|
||||
%>
|
||||
<div class="archives">
|
||||
<h4 class="collapsible"><%: T("Archives") %></h4><%
|
||||
|
@@ -10,7 +10,7 @@ namespace Orchard.UI.Resources {
|
||||
ResourceManifest DynamicResources { get; }
|
||||
ResourceDefinition FindResource(RequireSettings settings);
|
||||
void NotRequired(string resourceType, string resourceName);
|
||||
void Require(RequireSettings settings);
|
||||
RequireSettings Require(string resourceType, string resourceName);
|
||||
void RegisterLink(LinkEntry link);
|
||||
void SetMeta(MetaEntry meta);
|
||||
void AppendMeta(MetaEntry meta, string contentSeparator);
|
||||
|
@@ -12,21 +12,73 @@ namespace Orchard.UI.Resources {
|
||||
public ResourceLocation Location { get; set; }
|
||||
public Action<ResourceDefinition> InlineDefinition { get; set; }
|
||||
|
||||
public RequireSettings AtHead() {
|
||||
return AtLocation(ResourceLocation.Head);
|
||||
}
|
||||
|
||||
public RequireSettings AtFoot() {
|
||||
return AtLocation(ResourceLocation.Foot);
|
||||
}
|
||||
|
||||
public RequireSettings AtLocation(ResourceLocation location) {
|
||||
// if head is specified it takes precedence since it's safer than foot
|
||||
Location = (ResourceLocation)Math.Max((int)Location, (int)location);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings UseCulture(string cultureName) {
|
||||
if (!String.IsNullOrEmpty(cultureName)) {
|
||||
Culture = cultureName;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings UseDebugMode() {
|
||||
return UseDebugMode(true);
|
||||
}
|
||||
|
||||
public RequireSettings UseDebugMode(bool debugMode) {
|
||||
DebugMode |= debugMode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings UseCdn() {
|
||||
return UseCdn(true);
|
||||
}
|
||||
|
||||
public RequireSettings UseCdn(bool cdn) {
|
||||
CdnMode |= cdn;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings WithMinimumVersion(string minimumVersion) {
|
||||
MinimumVersion = String.IsNullOrEmpty(MinimumVersion)
|
||||
? minimumVersion
|
||||
: (MinimumVersion.CompareTo(minimumVersion) > 0 ? minimumVersion : MinimumVersion);
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings WithBasePath(string basePath) {
|
||||
BasePath = basePath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings Define(Action<ResourceDefinition> resourceDefinition) {
|
||||
InlineDefinition = resourceDefinition ?? InlineDefinition;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RequireSettings Combine(RequireSettings other) {
|
||||
return new RequireSettings {
|
||||
return (new RequireSettings {
|
||||
Name = Name,
|
||||
Type = Type,
|
||||
InlineDefinition = other.InlineDefinition ?? InlineDefinition,
|
||||
BasePath = String.IsNullOrEmpty(other.BasePath) ? BasePath : other.BasePath,
|
||||
CdnMode = CdnMode || other.CdnMode,
|
||||
DebugMode = DebugMode || other.DebugMode,
|
||||
Culture = String.IsNullOrEmpty(other.Culture) ? Culture : other.Culture,
|
||||
MinimumVersion = String.IsNullOrEmpty(MinimumVersion)
|
||||
? other.MinimumVersion
|
||||
: MinimumVersion.CompareTo(other.MinimumVersion) > 0 ? other.MinimumVersion : MinimumVersion,
|
||||
// if head is specified it takes precedence since it's safer than foot
|
||||
Location = (ResourceLocation) Math.Max((int)Location, (int)other.Location),
|
||||
};
|
||||
Type = Type
|
||||
}).AtLocation(other.Location)
|
||||
.WithBasePath(other.BasePath)
|
||||
.UseCdn(other.CdnMode)
|
||||
.UseDebugMode(other.DebugMode)
|
||||
.UseCulture(other.Culture)
|
||||
.WithMinimumVersion(other.MinimumVersion)
|
||||
.Define(other.InlineDefinition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Orchard.UI.Resources {
|
||||
public class ResourceManager : IResourceManager {
|
||||
@@ -26,17 +27,30 @@ namespace Orchard.UI.Resources {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void Require(RequireSettings settings) {
|
||||
RequireSettings existingSettings;
|
||||
var key = new Tuple<string, string>(settings.Type, settings.Name);
|
||||
if (_required.TryGetValue(key, out existingSettings)) {
|
||||
settings = settings.Combine(existingSettings);
|
||||
public virtual RequireSettings Require(string resourceType, [NotNull] string resourceName) {
|
||||
if (resourceType == null) {
|
||||
throw new ArgumentNullException("resourceType");
|
||||
}
|
||||
_builtResources[settings.Type] = null;
|
||||
_required[key] = settings;
|
||||
if (resourceName == null) {
|
||||
throw new ArgumentNullException("resourceName");
|
||||
}
|
||||
RequireSettings settings;
|
||||
var key = new Tuple<string, string>(resourceType, resourceName);
|
||||
if (!_required.TryGetValue(key, out settings)) {
|
||||
settings = new RequireSettings {Type = resourceType, Name = resourceName};
|
||||
_required[key] = settings;
|
||||
}
|
||||
_builtResources[resourceType] = null;
|
||||
return settings;
|
||||
}
|
||||
|
||||
public virtual void NotRequired(string resourceType, string resourceName) {
|
||||
if (resourceType == null) {
|
||||
throw new ArgumentNullException("resourceType");
|
||||
}
|
||||
if (resourceName == null) {
|
||||
throw new ArgumentNullException("resourceName");
|
||||
}
|
||||
var key = new Tuple<string, string>(resourceType, resourceName);
|
||||
_builtResources[resourceType] = null;
|
||||
_required.Remove(key);
|
||||
|
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.UI;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Orchard.UI.Resources {
|
||||
public class ResourceRegister {
|
||||
@@ -17,70 +18,23 @@ namespace Orchard.UI.Resources {
|
||||
_resourceType = resourceType;
|
||||
}
|
||||
|
||||
public void Require(string resourceName) {
|
||||
Require(resourceName, null, null);
|
||||
public RequireSettings Require(string resourceName) {
|
||||
return Require(resourceName, (string)null);
|
||||
}
|
||||
|
||||
public void Require(string resourceName, string minimumVersion) {
|
||||
Require(resourceName, minimumVersion, null);
|
||||
}
|
||||
|
||||
public void Require(string resourceName, Action<ResourceDefinition> inlineDefinition) {
|
||||
Require(resourceName, null, inlineDefinition);
|
||||
}
|
||||
|
||||
public void Require(string resourceName, string minimumVersion, Action<ResourceDefinition> inlineDefinition) {
|
||||
Require(resourceName, new RequireSettings {
|
||||
MinimumVersion = minimumVersion,
|
||||
InlineDefinition = inlineDefinition,
|
||||
});
|
||||
}
|
||||
|
||||
public void Require(RequireSettings settings) {
|
||||
Require(settings.Name, settings);
|
||||
}
|
||||
|
||||
protected void Require(string resourceName, RequireSettings settings) {
|
||||
if (settings == null) {
|
||||
throw new ArgumentNullException("settings");
|
||||
}
|
||||
settings.Type = String.IsNullOrEmpty(settings.Type) ? _resourceType : settings.Type;
|
||||
settings.Name = String.IsNullOrEmpty(settings.Name) ? resourceName : settings.Name;
|
||||
public virtual RequireSettings Require(string resourceName, string minimumVersion) {
|
||||
var settings = _resourceManager.Require(_resourceType, resourceName)
|
||||
.WithMinimumVersion(minimumVersion);
|
||||
if (_templateContainer != null) {
|
||||
settings.BasePath = ResourceDefinition.GetBasePathFromViewPath(_resourceType, _templateContainer.AppRelativeVirtualPath);
|
||||
settings.WithBasePath(ResourceDefinition.GetBasePathFromViewPath(_resourceType, _templateContainer.AppRelativeVirtualPath));
|
||||
}
|
||||
_resourceManager.Require(settings);
|
||||
return settings;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ScriptRegister : ResourceRegister {
|
||||
public ScriptRegister(IViewDataContainer container, IResourceManager resourceManager) : base(container, resourceManager, "script") {
|
||||
}
|
||||
|
||||
public void RequireFoot(string scriptName) {
|
||||
RequireFoot(scriptName, null, null);
|
||||
}
|
||||
|
||||
public void RequireFoot(string scriptName, string minimumVersion) {
|
||||
RequireFoot(scriptName, minimumVersion, null);
|
||||
}
|
||||
|
||||
public void RequireFoot(string scriptName, Action<ResourceDefinition> inlineDefinition) {
|
||||
RequireFoot(scriptName, null, inlineDefinition);
|
||||
}
|
||||
|
||||
public void RequireFoot(string scriptName, string minimumVersion, Action<ResourceDefinition> inlineDefinition) {
|
||||
Require(scriptName, new RequireSettings {
|
||||
MinimumVersion = minimumVersion,
|
||||
InlineDefinition = inlineDefinition,
|
||||
Location = ResourceLocation.Foot
|
||||
});
|
||||
}
|
||||
|
||||
public void RequireFoot(RequireSettings settings) {
|
||||
settings.Location = ResourceLocation.Foot;
|
||||
Require(settings.Name, settings);
|
||||
}
|
||||
// todo: Head/Tail registration
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user