Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Jun 28, 2013 @ 16:41
    Warren Buckley
    0

    Custom Tree - Can't figure out how to add child nodes

    Hello All,
    I am workign with Tim Geyssens on a new package called Optimus. A bundle & transfomer package.

    In this package we have a custom tree for SASS & LESS dynamic CSS files. I have these items listing out in the tree perfectly fine, howver if I am trying to a child node/s to one of those items I cannot seem to be able to do it.

    Can anyone please give me some guidance on where I am going wrong.

    public override void RenderJS(ref StringBuilder Javascript)
    {
        Javascript.Append(
        @"
            function openDyanmicCSSFileEditor(fileName, compiled) {
                UmbClientMgr.contentFrame('../App_Plugins/Optimus/Pages/FileEditor.aspx?file='+ fileName + '&path=/css/&compiled=' + compiled);
            }");
    }
    
    public override void Render(ref XmlTree tree)
    {
        if (String.IsNullOrEmpty(this.NodeKey))
        {
            //Create Top Level SASS or LESS nodes
            TopLevelCSSNodes(ref tree);
        }
        else
        {
            //A SASS or LESS file has been selected with child static CSS file
            CreateChildNodes(ref tree);
        }
    }
    
    
    protected void TopLevelCSSNodes(ref XmlTree tree)
    {
        string orgPath  = string.Empty;
        string path     = string.Empty;
        string FilePath = "/css/";
        path            = IOHelper.MapPath(FilePath);
    
    
        DirectoryInfo dirInfo       = new DirectoryInfo(path);
        DirectoryInfo[] dirInfos    = dirInfo.GetDirectories();
    
        var args = new TreeEventArgs(tree);
        OnBeforeTreeRender(dirInfo, args);
    
        //Loop through files
        var fileInfo = dirInfo.GetFilesByExtensions(new Translation.Core().GetPossibleExtensions(Enums.TranslatorType.StyleSheet).ToArray());
    
        foreach (FileInfo file in fileInfo)
        {
            if ((file.Attributes & FileAttributes.Hidden) == 0)
            {
                XmlTreeNode xFileNode   = XmlTreeNode.Create(this);
                xFileNode.NodeID        = orgPath + file.Name;
                xFileNode.Text          = file.Name;
                xFileNode.OpenIcon      = "doc.gif";
                xFileNode.Menu          = new List<IAction> { ActionDelete.Instance };
                xFileNode.NodeType      = "initstylesheetsNew";
                xFileNode.Icon          = new Optimus.Translation.Core().GetTranslatorTreeIconPath(file.Name);
    
                //Check for compiled version of file
                var fileName        = file.FullName.TrimStart('/');
                var staticFileName  = fileName.Replace(".scss"".css").Replace(".sass"".css").Replace(".less"".css");
    
                //Check if compileFileName exists
                if (System.IO.File.Exists(staticFileName))
                {
                    //Add a child node to the current node to display the static CSS file
                    xFileNode.HasChildren   = true;
                    var functionToCall      = "javascript:openDyanmicCSSFileEditor('" + orgPath + staticFileName + "', true')";
                    var nodeSourceURL       = TreeUrlGenerator.GetServiceUrl(-1, "stylesheetsNew"falsefalse"settings", orgPath + staticFileName, functionToCall);
                    xFileNode.Source        = nodeSourceURL;
                }
    
                //CSS Action link...
                //Only run/set an action if it's empty (as in not been set above as static/compiled file)
                if (string.IsNullOrEmpty(xFileNode.Action))
                {
                    if (orgPath != string.Empty)
                    {
                        xFileNode.Action = "javascript:openDyanmicCSSFileEditor('" + orgPath + file.Name + "', false');";
                    }
                    else
                    {
                        xFileNode.Action = "javascript:openDyanmicCSSFileEditor('" + file.Name + "', 'false');";
                    }
                }
    
                //OnRenderFileNode(ref xFileNode);
                OnBeforeNodeRender(ref tree, ref xFileNode, EventArgs.Empty);
    
                if (xFileNode != null)
                {
                    tree.Add(xFileNode);
                    OnAfterNodeRender(ref tree, ref xFileNode, EventArgs.Empty);
                }
            }
        }
    
        //After TREE Rendering
        OnAfterTreeRender(dirInfo, args);
    
    }
    
    protected void CreateChildNodes(ref XmlTree tree)
    {
        if (!string.IsNullOrEmpty(this.NodeKey))
        {
            string orgPath  = string.Empty;
            string path     = string.Empty;
            string FilePath = "/css/";
            path            = IOHelper.MapPath(FilePath);
    
    
            XmlTreeNode xFileNode   = XmlTreeNode.Create(this);
            xFileNode.NodeID        = orgPath + this.NodeKey;
            xFileNode.Text = "BADGER";
            xFileNode.OpenIcon      = "doc.gif";
            xFileNode.Menu          = null;
            xFileNode.NodeType      = "initstylesheetsNew-Compiled";
            xFileNode.Icon          = new Optimus.Translation.Core().GetTranslatorTreeIconPath(this.NodeKey);
            xFileNode.Action        = "javascript:openDyanmicCSSFileEditor('" + orgPath + this.NodeKey + "', true');";
    
    
            OnBeforeNodeRender(ref tree, ref xFileNode, EventArgs.Empty);
    
            //Y U NO Add Child Node?!
            if (xFileNode != null)
            {
                tree.Add(xFileNode);
                OnAfterNodeRender(ref tree, ref xFileNode, EventArgs.Empty);
            }
        }
    }
    
    protected override void CreateRootNode(ref XmlTreeNode rootNode)
    {
        rootNode.Icon       = ".sprTreeFolder";
        rootNode.OpenIcon   = ".sprTreeFolder_o";
        rootNode.NodeID     = "init";
        rootNode.NodeType   = rootNode.NodeID + TreeAlias;
        rootNode.Menu       = new List<IAction> { ActionNew.Instance, ActionRefresh.Instance };
    }

    Thanks,
    Warren :)

  • Comment author was deleted

    Jun 28, 2013 @ 16:59

    Is it possible to add children to tree items you have to use a tree service?  Just speculating.

    The only trees I've made are one level deep.  When I added a MNTP type deal into one of my projects, I had to use a tree service to get children.

  • Warren Buckley 2106 posts 4836 karma points MVP 7x admin c-trib
    Jun 28, 2013 @ 17:11
    Warren Buckley
    0

    Hi Kevin,
    Yes I am using the TreeServiceURL call to generate the URL for this. See this bit of code for that:

    var nodeSourceURL       =TreeUrlGenerator.GetServiceUrl(-1,"stylesheetsNew",false,false,"settings", orgPath + staticFileName, functionToCall);
    xFileNode.Source        = nodeSourceURL; 

    The tree knows it has to render out child nodes and the Render() event re-fires when the tree is expanded for the new levels and then runs the stub CreateChildNodes() which is simailar to TopLevelCSSNodes() and uses the Tree.Add(xFileNode) call but in the case for rendering out the child level it does not work, which I find highly odd.

    Warren 

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jun 28, 2013 @ 19:27
    Anders Bjerner
    0

    I think you're pretty close to nailing it. It seems that your main problem is, that you never modify orgPath, but still use the value for the NodeID.

    If you you try to build your IDs like this, I believe you're close at nailing it:

    -- LESS (id: "less")
      -- Some folder (id: "less/Some folder")
        -- Nested folder (id: "less/Some folder/Nested folder")
          -- epic.less (id: "less/Some folder/Nested folder/epic.less")

    Then in the render method, you can check for these IDs:

    if (NodeKey == "less") {
        // the main less node
    } else if (NodeKey.StartsWith("less/") && NodeKey.EndsWith(".less")) {
        // do something for a less file
    } else if (NodeKey.StartsWith("less/")) {
       // must be a folder then
    }
  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jun 28, 2013 @ 21:20
    Anders Bjerner
    0

    And here is the some code:

    public class ExampleTree : BaseTree {
    
        public ExampleTree(string application) : base(application) {
            // Do nothing
        }
    
        protected override void CreateRootNode(ref XmlTreeNode rootNode) {
    
            rootNode.Icon = FolderIcon;
            rootNode.OpenIcon = FolderIconOpen;
            rootNode.NodeType = "init" + TreeAlias;
            rootNode.NodeID = "init" + TreeAlias;
            rootNode.HasChildren = true;
            rootNode.Action = "";
    
        }
    
        public static HttpServerUtility Server {
            get { return HttpContext.Current.Server; }
        }
    
        public static string LessPath {
            get { return HttpContext.Current.Server.MapPath("~/less"); }
        }
    
        public static DirectoryInfo LessDir {
            get { return new DirectoryInfo(LessPath); }
        }
    
        public static string SassPath {
            get { return HttpContext.Current.Server.MapPath("~/sass"); }
        }
    
        public static DirectoryInfo SassDir {
            get { return new DirectoryInfo(LessPath); }
        }
    
        public static string GetDatPath(string path) {
            return path.Substring(Server.MapPath("~").Length);
        }
    
        public override void Render(ref XmlTree tree) {
    
            if (String.IsNullOrEmpty(NodeKey)) {
                RenderRootNode(tree);
            } else if (NodeKey == "less") {
                RenderLessDirectory(LessDir, tree);
            } else if (NodeKey.StartsWith("dir:less:")) {
                string[] info = NodeKey.Split(':');
                string path = info.Last();
                if (info.Length != 3 || path.Contains("..")) return;
                RenderLessDirectory(new DirectoryInfo(Server.MapPath("~/" + path)), tree);
            } else if (NodeKey.StartsWith("file:less:")) {
                RenderLessFile(tree);
            }
    
        }
    
        private void RenderRootNode(XmlTree tree) {
    
            XmlTreeNode lessNode = XmlTreeNode.Create(this);
            lessNode.NodeID = "less";
            lessNode.Text = "LESS";
            lessNode.HasChildren = LessDir.Exists && LessDir.GetDirectories().Length > 0;
            lessNode.Source = lessNode.HasChildren ? GetTreeServiceUrl(lessNode.NodeID) : "";
            lessNode.Icon = FolderIcon;
            lessNode.Menu.Add(ActionRefresh.Instance);
            tree.Add(lessNode);
    
            XmlTreeNode sassNode = XmlTreeNode.Create(this);
            sassNode.NodeID = "sass";
            sassNode.Text = "SASS";
            sassNode.HasChildren = SassDir.Exists && SassDir.GetDirectories().Length > 0;
            sassNode.Source = sassNode.HasChildren ? GetTreeServiceUrl(sassNode.NodeID) : "";
            sassNode.Icon = FolderIcon;
            lessNode.Menu.Add(ActionRefresh.Instance);
            tree.Add(sassNode);
    
        }
    
        private void RenderLessDirectory(DirectoryInfo dir, XmlTree tree) {
    
            if (!dir.Exists) return;
    
            foreach (var child in dir.GetDirectories()) {
                XmlTreeNode node = XmlTreeNode.Create(this);
                node.NodeID = "dir:less:" + GetDatPath(child.FullName);
                node.Text = child.Name + " (" + node.NodeID + ")";
                node.HasChildren = child.GetDirectories().Length > 0 || child.GetFiles("*.less").Length > 0;
                node.Source = node.HasChildren ? GetTreeServiceUrl(node.NodeID) : "";
                node.Icon = FolderIcon;
                node.Menu.Add(ActionRefresh.Instance);
                tree.Add(node);
            }
    
            foreach (var file in dir.GetFiles("*.less")) {
                FileInfo compiled = new FileInfo(file.FullName.Substring(0, file.FullName.Length - 5) + ".css");
                FileInfo minified = new FileInfo(file.FullName.Substring(0, file.FullName.Length - 5) + ".min.css");
                XmlTreeNode node = XmlTreeNode.Create(this);
                node.NodeID = "file:less:" + GetDatPath(file.FullName);
                node.Text = file.Name + " (" + node.NodeID + ")";
                node.HasChildren = compiled.Exists || minified.Exists;
                node.Source = node.HasChildren ? GetTreeServiceUrl(node.NodeID) : "";
                node.Icon = "developerScript.gif";
                node.Menu.Add(ActionRefresh.Instance);
                tree.Add(node);
            }
    
        }
    
        private void RenderLessFile(XmlTree tree) {
    
            string[] info = NodeKey.Split(':');
            string path = info.Last();
            if (info.Length != 3 || path.Contains("..") || !path.ToLower().EndsWith(".less")) return;
    
            FileInfo file = new FileInfo(Server.MapPath("~/" + path));
            if (!file.Exists) return;
    
            FileInfo compiled = new FileInfo(file.FullName.Substring(0, file.FullName.Length - 5) + ".css");
            FileInfo minified = new FileInfo(file.FullName.Substring(0, file.FullName.Length - 5) + ".min.css");
    
            if (compiled.Exists) {
                XmlTreeNode node = XmlTreeNode.Create(this);
                node.NodeID = "file:compiled";
                node.Text = compiled.Name + " (" + node.NodeID + ")";
                node.HasChildren = false;
                node.Icon = "developerScript.gif";
                tree.Add(node);
            }
    
            if (minified.Exists) {
                XmlTreeNode node = XmlTreeNode.Create(this);
                node.NodeID = "file:minified";
                node.Text = minified.Name + " (" + node.NodeID + ")";
                node.HasChildren = false;
                node.Icon = "developerScript.gif";
                tree.Add(node);
            }
    
        }
    
        public override void RenderJS(ref StringBuilder js) {
            // while(true) { alert('Yay!'); }
        }
    
    }
  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Jul 01, 2013 @ 10:24
    Morten Christensen
    0

    I don't think I've seen TreeUrlGenerator.GetServiceUrl used before :) But assuming you're class is inheriting from BaseTree it's much easier to do as Anders suggested above:

    node.HasChildren= child.GetDirectories().Length>0|| child.GetFiles("*.less").Length>0;
    node
    .Source= node.HasChildren?GetTreeServiceUrl(node.NodeID):"";

     

    - Morten

Please Sign in or register to post replies

Write your reply to:

Draft