Created
May 29, 2013 10:36
-
-
Save lars-erik/5669405 to your computer and use it in GitHub Desktop.
UCommerce Datatypes Category picker with stores
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" AutoEventWireup="true" Inherits="This.Web.usercontrols.Administrative.UCommerceDataTypesCategory" %> | |
<asp:UpdateProgress ID="UpdateProgress1" | |
runat="server" | |
AssociatedUpdatePanelID="CategoryPanel"> | |
<ProgressTemplate> | |
<img src="/umbraco/images/throbber.gif" alt="Loading"/> | |
</ProgressTemplate> | |
</asp:UpdateProgress> | |
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="CategoryPanel"> | |
<ContentTemplate> | |
<table cellspacing="20"> | |
<tr> | |
<td style="vertical-align: top; width: 30px"> | |
<asp:Image ID="selectedCategoryIcon" runat="server" /> | |
</td> | |
<td style="vertical-align: top; width: auto; padding-top: 7px"> | |
<asp:Label ID="selectedCategoryLabel" runat="server" ForeColor="Black" /> | |
</td> | |
<td style="vertical-align: top; padding-top: 6px; width: 20px"> | |
<asp:ImageButton runat="server" ID="clearSelection" OnClick="clearSelection_Clicked" /> | |
</td> | |
</tr> | |
<tr> | |
<td colspan="3"> | |
<asp:TreeView id="treeCategory" runat="server" | |
OnSelectedNodeChanged="treeCategory_SelectedNodeChanged" | |
OnTreeNodeExpanded="treeCategory_NodeExpanded" | |
RootNodeStyle-ForeColor="DarkGray" | |
RootNodeStyle-Font-Bold="True" | |
NodeStyle-ForeColor="Black" | |
NodeStyle-HorizontalPadding="5" | |
SelectedNodeStyle-ForeColor="Black" | |
SelectedNodeStyle-BorderStyle="Solid" | |
SelectedNodeStyle-BorderWidth="1" | |
SelectedNodeStyle-BorderColor="LightSkyBlue" | |
SelectedNodeStyle-BackColor="LightCyan" | |
ExpandDepth="0" | |
AutoPostBack="True"> | |
<LevelStyles> | |
<asp:TreeNodeStyle ForeColor="DarkGray" Font-Bold="True"/> | |
<asp:TreeNodeStyle ForeColor="DarkGray" Font-Bold="True"/> | |
</LevelStyles> | |
<Nodes> | |
</Nodes> | |
</asp:TreeView> | |
</td> | |
</tr> | |
</table> | |
</ContentTemplate> | |
</asp:UpdatePanel> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using UCommerce.EntitiesV2; | |
using umbraco.editorControls.userControlGrapper; | |
namespace This.Web.usercontrols.Administrative | |
{ | |
public class UCommerceDataTypesCategory : UserControl, IUsercontrolDataEditor | |
{ | |
protected UpdateProgress UpdateProgress1; | |
protected UpdatePanel CategoryPanel; | |
protected Image selectedCategoryIcon; | |
protected Label selectedCategoryLabel; | |
protected ImageButton clearSelection; | |
protected TreeView treeCategory; | |
private string _umbracoValue; | |
private string selectedItemKey; | |
public object value | |
{ | |
get | |
{ | |
if (treeCategory.SelectedNode != null) | |
return treeCategory.SelectedNode.Value; | |
return _umbracoValue; | |
} | |
set | |
{ | |
if (value != null) | |
_umbracoValue = value.ToString(); | |
else | |
_umbracoValue = ""; | |
} | |
} | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
selectedItemKey = "ucommerce_category_control_" + UniqueID; | |
if (!IsPostBack) | |
{ | |
clearSelection.ImageUrl = "/umbraco/images/close.png"; | |
foreach (var store in ProductCatalogGroup.All()) | |
{ | |
var child = new TreeNode(store.Name, "S" + Convert.ToString(store.ProductCatalogGroupId)); | |
child.ImageUrl = "/umbraco/images/umbraco/ucommerce-icon.png"; | |
if (store.ProductCatalogs.Any()) | |
child.ChildNodes.Add(new TreeNode("", "NULL_NODE")); | |
treeCategory.Nodes.Add(child); | |
} | |
Session[selectedItemKey] = _umbracoValue; | |
} | |
else | |
_umbracoValue = (string)Session[selectedItemKey]; | |
if (string.IsNullOrEmpty(_umbracoValue)) | |
{ | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/error.png"; | |
selectedCategoryLabel.Text = ""; | |
clearSelection.Visible = false; | |
} | |
else if (treeCategory.SelectedNode != null) | |
{ | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/success.png"; | |
selectedCategoryLabel.Text = treeCategory.SelectedNode.Text; | |
clearSelection.Visible = true; | |
} | |
else | |
{ | |
var categoryId = int.Parse(this._umbracoValue); | |
var category = Category.Find(c => c.CategoryId == categoryId).SingleOrDefault(); | |
if (category != null) | |
{ | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/success.png"; | |
selectedCategoryLabel.Text = category.Name; | |
clearSelection.Visible = true; | |
ExpandToCategory(category); | |
} | |
else | |
{ | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/error.png"; | |
selectedCategoryLabel.Text = ""; | |
clearSelection.Visible = false; | |
} | |
} | |
} | |
private void ExpandToCategory(Category category) | |
{ | |
var storeNode = treeCategory.Nodes.Cast<TreeNode>().SingleOrDefault(n => n.Value == "S" + Convert.ToString(category.ProductCatalog.ProductCatalogGroup.ProductCatalogGroupId)); | |
if (storeNode == null) | |
return; | |
storeNode.Expand(); | |
var catalogNode = storeNode.ChildNodes.Cast<TreeNode>().SingleOrDefault(n => n.Value == "G" + Convert.ToString(category.ProductCatalog.ProductCatalogId)); | |
if (catalogNode == null) | |
return; | |
catalogNode.Expand(); | |
ExpandParents(category, catalogNode); | |
} | |
private TreeNode ExpandParents(Category category, TreeNode catalogNode) | |
{ | |
TreeNode parent = catalogNode; | |
if (category.ParentCategory != null) | |
parent = ExpandParents(category.ParentCategory, catalogNode); | |
var categoryNode = parent.ChildNodes.Cast<TreeNode>().SingleOrDefault(n => n.Value == Convert.ToString(category.CategoryId)); | |
if (categoryNode != null) | |
categoryNode.Expand(); | |
return categoryNode; | |
} | |
protected void treeCategory_SelectedNodeChanged(object sender, EventArgs e) | |
{ | |
if (treeCategory.SelectedNode.Depth > 1) | |
{ | |
_umbracoValue = treeCategory.SelectedNode.Value; | |
Session[selectedItemKey] = _umbracoValue; | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/success.png"; | |
selectedCategoryLabel.Text = this.treeCategory.SelectedNode.Text; | |
clearSelection.Visible = true; | |
} | |
else if (Session[selectedItemKey] != null) | |
{ | |
_umbracoValue = Session[selectedItemKey].ToString(); | |
foreach (TreeNode treeNode in treeCategory.Nodes) | |
{ | |
if (treeNode.Value == _umbracoValue) | |
{ | |
treeNode.Select(); | |
break; | |
} | |
if (treeNode.ChildNodes.Count > 0) | |
CheckSubNodesForSelection(treeNode, _umbracoValue); | |
} | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/success.png"; | |
selectedCategoryLabel.Text = treeCategory.SelectedNode.Text; | |
clearSelection.Visible = true; | |
} | |
else | |
{ | |
_umbracoValue = ""; | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/error.png"; | |
selectedCategoryLabel.Text = ""; | |
clearSelection.Visible = false; | |
} | |
} | |
protected void treeCategory_NodeExpanded(object sender, TreeNodeEventArgs e) | |
{ | |
if (e.Node.ChildNodes.Count <= 0 || !(e.Node.ChildNodes[0].Value == "NULL_NODE")) | |
return; | |
e.Node.ChildNodes.RemoveAt(0); | |
if (e.Node.Depth == 0) | |
{ | |
var storeId = int.Parse(e.Node.Value.Substring(1)); | |
var store = ProductCatalogGroup.Get(storeId); | |
foreach (var catalog in store.ProductCatalogs) | |
{ | |
var child = new TreeNode(catalog.Name, "G" + Convert.ToString(catalog.ProductCatalogId)); | |
child.ImageUrl = "/umbraco/images/umbraco/folder.gif"; | |
e.Node.ChildNodes.Add(child); | |
if (catalog.Categories.Any()) | |
child.ChildNodes.Add(new TreeNode("", "NULL_NODE")); | |
} | |
} | |
else if (e.Node.Depth == 1) | |
{ | |
var catalogId = int.Parse(e.Node.Value.Substring(1)); | |
var productCatalog = ProductCatalog.Get(catalogId); | |
if (productCatalog != null) | |
{ | |
foreach (var category in productCatalog.Categories.Where(cat => cat.ParentCategory == null)) | |
{ | |
TreeNode child = new TreeNode(category.Name, Convert.ToString(category.CategoryId.ToString())); | |
child.ImageUrl = "/umbraco/images/umbraco/folder.gif"; | |
e.Node.ChildNodes.Add(child); | |
if (category.Categories.Count > 0) | |
child.ChildNodes.Add(new TreeNode("", "NULL_NODE")); | |
if (child.Value == _umbracoValue) | |
child.Select(); | |
} | |
} | |
} | |
if (e.Node.Depth > 1) | |
{ | |
var categoryId = int.Parse(e.Node.Value); | |
var category = Category.Get(categoryId); | |
if (category != null) | |
{ | |
foreach (var subCategory in category.Categories) | |
{ | |
TreeNode child = new TreeNode(subCategory.Name, subCategory.CategoryId.ToString()); | |
child.ImageUrl = "/umbraco/images/umbraco/folder.gif"; | |
e.Node.ChildNodes.Add(child); | |
if (subCategory.Categories.Count > 0) | |
child.ChildNodes.Add(new TreeNode("", "NULL_NODE")); | |
if (child.Value == _umbracoValue) | |
child.Select(); | |
} | |
} | |
} | |
} | |
private void CheckSubNodesForSelection(TreeNode treeNode, string valueToSelect) | |
{ | |
foreach (TreeNode childNode in treeNode.ChildNodes) | |
{ | |
if (childNode.Value == valueToSelect) | |
{ | |
childNode.Select(); | |
break; | |
} | |
if (childNode.ChildNodes.Count > 0) | |
CheckSubNodesForSelection(childNode, valueToSelect); | |
} | |
} | |
protected void clearSelection_Clicked(object sender, EventArgs e) | |
{ | |
_umbracoValue = ""; | |
Session[selectedItemKey] = (object)null; | |
treeCategory.CollapseAll(); | |
selectedCategoryIcon.ImageUrl = "/umbraco/images/speechBubble/error.png"; | |
selectedCategoryLabel.Text = ""; | |
clearSelection.Visible = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment