Ok, getting pretty close to making those legacy tree dialogs work.

This commit is contained in:
Shannon
2013-09-04 18:10:57 +10:00
parent be9c3b8996
commit b105bd305b
6 changed files with 73 additions and 199 deletions

View File

@@ -38,5 +38,5 @@
<add silent="false" initialize="true" sortOrder="2" alias="yourTasks" application="translation" title="Tasks created by you" iconClosed=".sprTreeFolder" iconOpen=".sprTreeFolder_o" type="umbraco.loadYourTasks, umbraco" action="" />
<!-- Custom -->
<!--<add application="myApplication" alias="myTree" title="Me Tree" type="MyNamespace.myTree, MyAssembly"
iconClosed=".sprTreeFolder" iconOpen=".sprTreeFolder_o" sortOrder="10" />-->
iconClosed=".sprTreeFolder" iconOpen=".sprTreeFolder_o" sortOrder="10" />-->
</trees>

View File

@@ -43,7 +43,7 @@ namespace Umbraco.Web.Trees
var rootId = Constants.System.Root.ToString(CultureInfo.InvariantCulture);
//find all tree definitions that have the current application alias
var appTrees = ApplicationContext.Current.Services.ApplicationTreeService.GetApplicationTrees(application).Where(x => x.Initialize).ToArray();
var appTrees = ApplicationContext.Current.Services.ApplicationTreeService.GetApplicationTrees(application, true).ToArray();
if (appTrees.Count() == 1)
{
return GetRootForSingleAppTree(

View File

@@ -7,8 +7,11 @@ using System.Web;
using System.Web.Http.Routing;
using Umbraco.Core;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Core.Services;
using Umbraco.Web.Trees.Menu;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.BusinessLogic.Actions;
using umbraco.cms.helpers;
using umbraco.cms.presentation.Trees;
@@ -17,6 +20,13 @@ using umbraco.interfaces;
namespace Umbraco.Web.Trees
{
/// <summary>
/// This attribute is used purely to maintain some compatibility with legacy webform tree pickers
/// </summary>
/// <remarks>
/// This allows us to attribute new trees with their legacy counterparts and when a legacy tree is loaded this will indicate
/// on the new tree which legacy tree to load (it won't actually render using the new tree)
/// </remarks>
[AttributeUsage(AttributeTargets.Class)]
internal sealed class LegacyBaseTreeAttribute : Attribute
{
@@ -32,80 +42,59 @@ namespace Umbraco.Web.Trees
BaseTreeType = baseTreeType;
}
}
internal class LegacyBaseTreeWrapper : BaseTree
{
private readonly string _treeAlias;
private readonly TreeNodeCollection _children;
private readonly TreeNode _root;
public LegacyBaseTreeWrapper(string treeAlias, string application, TreeNode root, TreeNodeCollection children = null)
: base(application)
{
_treeAlias = treeAlias;
_root = root;
_children = children;
}
public override void RenderJS(ref StringBuilder javascript)
{
}
public override void Render(ref XmlTree tree)
{
foreach (var c in _children)
{
var node = XmlTreeNode.Create(this);
LegacyTreeDataConverter.ConvertToLegacyNode(node, c, _treeAlias);
node.Source = IsDialog == false ? GetTreeServiceUrl(int.Parse(node.NodeID)) : GetTreeDialogUrl(int.Parse(node.NodeID));
tree.Add(node);
}
}
protected override void CreateRootNode(ref XmlTreeNode rootNode)
{
rootNode.NodeID = _root.NodeId;
rootNode.Icon = _root.IconIsClass ? _root.Icon.EnsureStartsWith('.') : _root.IconFilePath;
rootNode.HasChildren = _root.HasChildren;
rootNode.NodeID = _root.NodeId;
rootNode.Text = _root.Title;
rootNode.NodeType = _root.NodeType;
rootNode.OpenIcon = _root.IconIsClass ? _root.Icon.EnsureStartsWith('.') : _root.IconFilePath;
}
public override string TreeAlias
{
get { return _treeAlias; }
}
}
/// <summary>
/// Converts the legacy tree data to the new format
/// </summary>
internal class LegacyTreeDataConverter
{
internal static FormDataCollection ConvertFromLegacyTreeParams(TreeRequestParams treeParams)
/// <summary>
/// This is used by any legacy services that require rendering a BaseTree, if a new controller tree is detected it will try to invoke it's legacy predecessor.
/// </summary>
/// <param name="appTreeService"></param>
/// <param name="treeType"></param>
/// <returns></returns>
internal static BaseTree GetLegacyTreeForLegacyServices(ApplicationTreeService appTreeService, string treeType)
{
return new FormDataCollection(new Dictionary<string, string>
BaseTree tree;
//first get the app tree definition so we can then figure out if we need to load by legacy or new
//now we'll look up that tree
var appTree = appTreeService.GetByAlias(treeType);
if (appTree == null)
throw new InvalidOperationException("No tree found with alias " + treeType);
var controllerAttempt = appTree.TryGetControllerTree();
if (controllerAttempt.Success)
{
var legacyAtt = controllerAttempt.Result.GetCustomAttribute<LegacyBaseTreeAttribute>(false);
if (legacyAtt == null)
{
{TreeQueryStringParameters.Application, treeParams.Application},
{TreeQueryStringParameters.DialogMode, treeParams.IsDialog.ToString()},
});
}
LogHelper.Warn<LegacyTreeDataConverter>("Cannot render tree: " + treeType + ". Cannot render a " + typeof(TreeApiController) + " tree type with the legacy web services unless attributed with " + typeof(LegacyBaseTreeAttribute));
return null;
}
internal static void ConvertToLegacyNode(XmlTreeNode legacy, TreeNode node, string treeType)
{
legacy.Action = node.AdditionalData.ContainsKey("legacyDialogAction") ? node.AdditionalData["legacyDialogAction"].ToString() : "";
legacy.HasChildren = node.HasChildren;
legacy.Icon = node.IconIsClass ? node.Icon.EnsureStartsWith('.') : node.IconFilePath;
legacy.NodeID = node.NodeId;
legacy.NodeType = node.NodeType;
legacy.OpenIcon = node.IconIsClass ? node.Icon.EnsureStartsWith('.') : node.IconFilePath;
legacy.Text = node.Title;
legacy.TreeType = treeType;
var treeDef = new TreeDefinition(
legacyAtt.BaseTreeType,
new ApplicationTree(false, true, appTree.SortOrder, appTree.ApplicationAlias, appTree.Alias, appTree.Title, appTree.IconClosed, appTree.IconOpened, "", legacyAtt.BaseTreeType.GetFullNameWithAssembly(), ""),
new Application(treeType, treeType, "", 0));
tree = treeDef.CreateInstance();
tree.TreeAlias = appTree.Alias;
}
else
{
//get the tree that we need to render
var treeDef = TreeDefinitionCollection.Instance.FindTree(treeType);
if (treeDef == null)
{
return null;
}
tree = treeDef.CreateInstance();
}
return tree;
}
/// <summary>

View File

@@ -8,11 +8,13 @@ using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Web.Mvc;
using Umbraco.Web.Trees.Menu;
using umbraco;
using umbraco.BusinessLogic.Actions;
using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.Trees
{
[LegacyBaseTree(typeof(loadMedia))]
[Tree(Constants.Applications.Media, Constants.Trees.Media, "Media")]
[PluginController("UmbracoTrees")]
public class MediaTreeController : ContentTreeControllerBase

View File

@@ -69,80 +69,8 @@ namespace umbraco.presentation.webservices
}
else
{
BaseTree tree = null;
var xTree = new XmlTree();
var tree = LegacyTreeDataConverter.GetLegacyTreeForLegacyServices(Services.ApplicationTreeService, treeType);
//first get the app tree definition so we can then figure out if we need to load by legacy or new
//now we'll look up that tree
var appTree = Services.ApplicationTreeService.GetByAlias(treeType);
if (appTree == null)
throw new InvalidOperationException("No tree found with alias " + treeType);
var controllerAttempt = appTree.TryGetControllerTree();
if (controllerAttempt.Success)
{
var legacyAtt = controllerAttempt.Result.GetCustomAttribute<LegacyBaseTreeAttribute>(false);
if (legacyAtt == null)
{
throw new InvalidOperationException("Cannot render a " + typeof (TreeApiController) + " tree type with the legacy web services unless attributed with " + typeof (LegacyBaseTreeAttribute));
}
var treeDef = new TreeDefinition(
legacyAtt.BaseTreeType,
new ApplicationTree(false, true, appTree.SortOrder, appTree.ApplicationAlias, appTree.Alias, appTree.Title, appTree.IconClosed, appTree.IconOpened, "", legacyAtt.BaseTreeType.GetFullNameWithAssembly(), ""),
new Application(treeType, treeType, "", 0));
tree = treeDef.CreateInstance();
tree.TreeAlias = appTree.Alias;
//var queryStrings = new FormDataCollection(new Dictionary<string, string>
// {
// {TreeQueryStringParameters.Application, app},
// {TreeQueryStringParameters.DialogMode, isDialog.ToString()}
// });
//var context = WebApiHelper.CreateContext(new HttpMethod("GET"), Context.Request.Url, new HttpContextWrapper(Context));
//var rootAttempt = appTree.TryGetRootNodeFromControllerTree(
// queryStrings,
// context);
//if (rootAttempt.Success)
//{
// tree = new LegacyBaseTreeWrapper(treeType, app, rootAttempt.Result);
//}
}
else
{
//get the tree that we need to render
var treeDef = TreeDefinitionCollection.Instance.FindTree(treeType);
//if (treeDef == null)
//{
// // Load all LEGACY Trees by attribute and add them to the XML config
// var legacyTreeTypes = PluginManager.Current.ResolveAttributedTrees();
// var found = legacyTreeTypes
// .Select(x => new { att = x.GetCustomAttribute<businesslogic.TreeAttribute>(false), type = x })
// .FirstOrDefault(x => x.att.Alias == treeType);
// if (found == null)
// {
// throw new InvalidOperationException("The " + GetType() + " service can only return data for legacy tree types");
// }
// treeDef = new TreeDefinition(
// found.type,
// new ApplicationTree(found.att.Silent, found.att.Initialize, (byte)found.att.SortOrder, found.att.ApplicationAlias, found.att.Alias, found.att.Title, found.att.IconClosed, found.att.IconOpen, "", found.type.GetFullNameWithAssembly(), found.att.Action),
// new Application(treeType, treeType, "", 0));
// tree = treeDef.CreateInstance();
//}
//else
//{
// tree = treeDef.CreateInstance();
//}
tree = treeDef.CreateInstance();
}
tree.ShowContextMenu = showContextMenu;
tree.IsDialog = isDialog;
tree.DialogMode = dialogMode;
@@ -152,6 +80,7 @@ namespace umbraco.presentation.webservices
//tree.StartNodeID =
//now render it's start node
var xTree = new XmlTree();
xTree.Add(tree.RootNode);
returnVal.Add("json", xTree.ToString());

View File

@@ -81,14 +81,15 @@ namespace umbraco.presentation.webservices
/// <param name="context"></param>
private void LoadAppTrees(TreeRequestParams treeParams, HttpContext context)
{
//find all tree definitions that have the current application alias
List<TreeDefinition> treeDefs = TreeDefinitionCollection.Instance.FindActiveTrees(treeParams.Application);
foreach (TreeDefinition treeDef in treeDefs)
var appTrees = Services.ApplicationTreeService.GetApplicationTrees(treeParams.Application, true);
foreach (var appTree in appTrees)
{
BaseTree bTree = treeDef.CreateInstance();
bTree.SetTreeParameters(treeParams);
_xTree.Add(bTree.RootNode);
var tree = LegacyTreeDataConverter.GetLegacyTreeForLegacyServices(Services.ApplicationTreeService, appTree.Alias);
if (tree != null)
{
tree.SetTreeParameters(treeParams);
_xTree.Add(tree.RootNode);
}
}
}
@@ -99,64 +100,17 @@ namespace umbraco.presentation.webservices
/// <param name="httpContext"></param>
private void LoadTree(TreeRequestParams treeParams, HttpContext httpContext)
{
var appTree = Services.ApplicationTreeService.GetByAlias(treeParams.TreeType);
if (appTree == null)
throw new InvalidOperationException("No tree found with alias " + treeParams.TreeType);
var controllerAttempt = appTree.TryGetControllerTree();
if (controllerAttempt.Success)
var tree = LegacyTreeDataConverter.GetLegacyTreeForLegacyServices(Services.ApplicationTreeService, treeParams.TreeType);
if (tree != null)
{
var legacyAtt = controllerAttempt.Result.GetCustomAttribute<LegacyBaseTreeAttribute>(false);
if (legacyAtt == null)
{
throw new InvalidOperationException("Cannot render a " + typeof(TreeApiController) + " tree type with the legacy web services unless attributed with " + typeof(LegacyBaseTreeAttribute));
}
var treeDef = new TreeDefinition(
legacyAtt.BaseTreeType,
new ApplicationTree(false, true, appTree.SortOrder, appTree.ApplicationAlias, appTree.Alias, appTree.Title, appTree.IconClosed, appTree.IconOpened, "", legacyAtt.BaseTreeType.GetFullNameWithAssembly(), ""),
new Application(treeParams.TreeType, treeParams.TreeType, "", 0));
var tree = treeDef.CreateInstance();
tree.TreeAlias = appTree.Alias;
tree.SetTreeParameters(treeParams);
tree.Render(ref _xTree);
//var context = WebApiHelper.CreateContext(new HttpMethod("GET"), httpContext.Request.Url, new HttpContextWrapper(httpContext));
//var rootAttempt = appTree.TryGetRootNodeFromControllerTree(
// LegacyTreeDataConverter.ConvertFromLegacyTreeParams(treeParams),
// context);
//var nodesAttempt = appTree.TryLoadFromControllerTree(
// treeParams.StartNodeID.ToInvariantString(),
// LegacyTreeDataConverter.ConvertFromLegacyTreeParams(treeParams),
// context);
//if (rootAttempt.Success && nodesAttempt.Success)
//{
// var tree = new LegacyBaseTreeWrapper(treeParams.TreeType, treeParams.Application, rootAttempt.Result, nodesAttempt.Result);
// tree.SetTreeParameters(treeParams);
// tree.Render(ref _xTree);
//}
}
else
{
var treeDef = TreeDefinitionCollection.Instance.FindTree(treeParams.TreeType);
if (treeDef != null)
{
var bTree = treeDef.CreateInstance();
bTree.SetTreeParameters(treeParams);
bTree.Render(ref _xTree);
}
else
LoadNullTree(treeParams);
LoadNullTree(treeParams);
}
}
/// <summary>