@*   Xafari Framework version : 17.2.3011.1336   *@

@using System.Web.UI.WebControls
@using DevExpress.ExpressApp.Model.Core
@using Xafari.BC.Settings
@using Xafari.BC.Settings.Mvc
@using Xafari.BC.Settings.Runtime
@using Xafari.Mvc.Templates
@using Xafari.Utils

@model MvcSettingsPropertyEditor
@{
	var postUrl = Url.Action("SelectSettingsGroup", "MVCxSettingsPropertyEditor");
	
	var settingsKeyCallbackPanelName = this.Model.Frame.ControlId(this.Model.FormName() + "SettingsKeyCallbackPanel");

	var settings = new TreeViewSettings { Name = this.ViewData.Frame().ControlId(this.Model.FormName() + "SettingsGroups_TreeView"), AllowSelectNode = true};
	settings.ControlStyle.Border.BorderStyle = BorderStyle.None;
	settings.ControlStyle.CssClass = "xafNavigationBarActionContainer";
	settings.SaveStateToCookies = true;
    settings.SaveStateToCookiesID = this.Model.Id + "SettingsGroups_TreeView";
	settings.EnableClientSideAPI = true;
	settings.Width = Unit.Percentage(100);

	var actualSettings = ((SettingsObject) this.Model.CurrentObject).ActualSettings;

	var rootSettingsNode = settings.Nodes.Add();
	rootSettingsNode.Name = MvcSettingsPropertyEditor.RootPath;

    rootSettingsNode.Text = SettingsAccessor.Instance.GetRootCaption();//actualSettings.Caption;
	rootSettingsNode.Image.SpriteProperties.SetImageProperties(actualSettings.ImageName);
	rootSettingsNode.DataItem = actualSettings;

	var childrenForRoot = new List<IModelBCSettingsGroup>();
	for (var i = 0; i < actualSettings.NodeCount; i++)
	{
		var settingGroupNode = actualSettings.GetNode(i) as IModelBCSettingsGroup;
		if (settingGroupNode != null)
		{
			childrenForRoot.Add(settingGroupNode);
		}
	}
	childrenForRoot.Recursive(rootSettingsNode.Nodes,
	(parentNode, settingsGroupNode) =>
	{
		var children = new List<IModelBCSettingsGroup>();
		for (var i = 0; i < settingsGroupNode.NodeCount; i++)
		{
			var settingGroupNode = settingsGroupNode.GetNode(i) as IModelBCSettingsGroup;
			if (settingGroupNode != null)
			{
				children.Add(settingGroupNode);
			}
		}
		return children;
	},
	(parentNode, settingsGroupNode) =>
	{
		var baseInterface = ((ModelNode)settingsGroupNode).NodeInfo.BaseInterface;
		var obsoleteAttributes = baseInterface.GetCustomAttributes(typeof(ObsoleteAttribute), false);
		return obsoleteAttributes.Length <= 0;
	},
	(parentNode, settingsGroupNode) =>
	{
		var node = parentNode.Add();
	    node.Text = SettingsAccessor.Instance.GetGroupCaption(settingsGroupNode);//settingsGroupNode.Caption;
		node.Name = settingsGroupNode.SettingsPath;
		node.Image.SpriteProperties.SetImageProperties(settingsGroupNode.ImageName);
		node.ToolTip = settingsGroupNode.ToolTip;
		node.DataItem = settingsGroupNode;
		return node.Nodes;
	});
	settings.PreRender += (sender, args) =>
	{
		var treeView = (ASPxTreeView)sender;
		if (this.Model.SettingsCurrentGroupPath == null)
		{
			var firstNode = treeView.Nodes[0];
			if (firstNode != null)
			{
				treeView.SelectedNode = firstNode;
				firstNode.Expanded = true;
			}
		}
		else
		{
			treeView.SelectedNode = treeView.Nodes.FindRecursive(node => node.Name == this.Model.SettingsCurrentGroupPath);
			treeView.ExpandToNode(treeView.SelectedNode);
		}
	};
	string nodeClickScript = string.Format(@"function(s, e) {{
if(window.xafari.settings.groupSelectTimeoutId)
	window.clearTimeout(window.xafari.settings.groupSelectTimeoutId);
var nodeName = e.node.name;
window.xafari.settings.groupSelectTimeoutId = window.setTimeout(function() {{ 
	window.xafari.settings.groupSelectTimeoutId = null;
	 window.top.setTimeout('window.top.xafari.startProgress()', 1);
	$.post('{0}', {{ currentSettingsGroup: nodeName, windowId: '{1}', frameId: '{2}' }}).
			success(function() {{ if ({3}) {3}.PerformCallback(); }});
}}, 500);
}}", postUrl, this.ViewData.Window().FrameId(), this.ViewData.Frame().FrameId(), settingsKeyCallbackPanelName);
	settings.ClientSideEvents.NodeClick = nodeClickScript;
}
@Html.DevExpress().TreeView(settings).GetHtml()


    