protected TreeNode treeElem_OnNodeCreated(DataRow itemData, TreeNode defaultNode)
    {
        if (itemData != null)
        {
            UIElementInfo uiElement = new UIElementInfo(itemData);

            if (!UIContextHelper.CheckElementAvailabilityInUI(uiElement))
            {
                return(null);
            }

            // Check permissions
            if (MembershipContext.AuthenticatedUser.IsAuthorizedPerUIElement(uiElement.ElementResourceID, uiElement.ElementName, ModuleAvailabilityForSiteRequired))
            {
                defaultNode = RaiseOnNodeCreated(uiElement, defaultNode);

                String url = UIContextHelper.GetElementUrl(uiElement, UIContext);
                if (uiElement.ElementType != UIElementTypeEnum.PageTemplate)
                {
                    url = URLHelper.RemoveParameterFromUrl(url, "displaytitle");
                }

                // If url is empty -> don't show
                if (defaultNode == null || String.IsNullOrEmpty(url))
                {
                    return(null);
                }

                // Prepare node info
                string caption     = UIElementInfoProvider.GetElementCaption(uiElement);
                string targetFrame = !String.IsNullOrEmpty(defaultNode.Target) ? defaultNode.Target : TargetFrame;
                string codeName    = uiElement.ElementName.Replace(".", String.Empty).ToLowerCSafe();

                UIElementInfo sel = UIContextHelper.CheckSelectedElement(uiElement, UIContext);
                if (sel != null)
                {
                    SelectedNode = uiElement.ElementName;

                    String selectionSuffix = ValidationHelper.GetString(UIContext["selectionSuffix"], String.Empty);
                    String spUrl           = UIContextHelper.GetElementUrl(sel, UIContext);
                    spUrl = URLHelper.AppendQuery(spUrl, selectionSuffix);

                    // Append object ID
                    url = URLHelper.UpdateParameterInUrl(url, "objectid", UIContext.ObjectID.ToString());

                    StartingPage = URLHelper.UpdateParameterInUrl(spUrl, "displaytitle", "true");
                }

                // Set node
                defaultNode.Target      = targetFrame;
                defaultNode.NavigateUrl = GetUrl(url);
                defaultNode.Text        = defaultNode.Text.Replace(NODE_CODENAME_MACRO, codeName);

                totalNodes++;

                // Set first child node to be selected if root cant be selected
                if (!EnableRootSelect && string.IsNullOrEmpty(SelectedNode))
                {
                    // Is a page node (with page url)
                    if (url != ApplicationUrlHelper.COLLAPSIBLE_EMPTY_PARENT_ELEMENT_URL)
                    {
                        SelectedNode = uiElement.ElementName;
                    }
                    else
                    {
                        // Try to display a child element
                        if (uiElement.ElementChildCount > 0)
                        {
                            defaultNode.Expanded = true;
                        }
                    }
                }

                // If url is '@' don't redirect, only collapse children
                if (uiElement.ElementTargetURL == ApplicationUrlHelper.COLLAPSIBLE_EMPTY_PARENT_ELEMENT_URL)
                {
                    // Onclick simulates click on '+' or '-' button
                    const string onClick = "onClick=\"var js = $cmsj(this).parents('tr').find('a').attr('href');eval(js);return false; \"";
                    defaultNode.Text = "<span id=\"node_" + uiElement.ElementName + "\" class=\"ContentTreeItem \"" + onClick + " ><span class=\"Name\">" + ResHelper.LocalizeString(caption) + "</span></span>";
                }

                return(defaultNode);
            }
        }

        return(null);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Generates panel with buttons loaded from given UI Element.
    /// </summary>
    /// <param name="uiElementId">ID of the UI Element</param>
    protected Panel GetButtons(int uiElementId)
    {
        const int bigButtonMinimalWidth   = 40;
        const int smallButtonMinimalWidth = 66;

        Panel pnlButtons = null;

        // Load the buttons manually from UI Element
        DataSet ds = UIElementInfoProvider.GetChildUIElements(uiElementId);

        // When no child found
        if (DataHelper.DataSourceIsEmpty(ds))
        {
            // Try to use group element as button
            ds = UIElementInfoProvider.GetUIElements("ElementID = " + uiElementId, null);

            if (!DataHelper.DataSourceIsEmpty(ds))
            {
                DataRow dr  = ds.Tables[0].Rows[0];
                string  url = ValidationHelper.GetString(dr["ElementTargetURL"], "");

                // Use group element as button only if it has URL specified
                if (string.IsNullOrEmpty(url))
                {
                    ds = null;
                }
            }
        }

        if (!DataHelper.DataSourceIsEmpty(ds))
        {
            // Filter the dataset according to UI Profile
            FilterElements(ds);

            int small = 0;
            int count = ds.Tables[0].Rows.Count;

            // No buttons, render nothing
            if (count == 0)
            {
                return(null);
            }

            // Prepare the panel
            pnlButtons          = new Panel();
            pnlButtons.CssClass = "ActionButtons";

            // Prepare the table
            Table    tabGroup    = new Table();
            TableRow tabGroupRow = new TableRow();

            tabGroup.CellPadding        = 0;
            tabGroup.CellSpacing        = 0;
            tabGroup.EnableViewState    = false;
            tabGroupRow.EnableViewState = false;
            tabGroup.Rows.Add(tabGroupRow);

            List <Panel> panels = new List <Panel>();

            for (int i = 0; i < count; i++)
            {
                // Get current and next button
                UIElementInfo uiElement = new UIElementInfo(ds.Tables[0].Rows[i]);

                UIElementInfo sel = UIContextHelper.CheckSelectedElement(uiElement, UIContext);
                if (sel != null)
                {
                    String selectionSuffix = ValidationHelper.GetString(UIContext["selectionSuffix"], String.Empty);
                    StartingPage  = UIContextHelper.GetElementUrl(sel, UIContext) + selectionSuffix;
                    HighlightItem = uiElement.ElementName;
                }

                // Raise button creating event
                if (OnButtonCreating != null)
                {
                    OnButtonCreating(this, new UniMenuArgs {
                        UIElement = uiElement
                    });
                }

                UIElementInfo uiElementNext = null;
                if (i < count - 1)
                {
                    uiElementNext = new UIElementInfo(ds.Tables[0].Rows[i + 1]);
                }

                // Set the first button
                if (mFirstUIElement == null)
                {
                    mFirstUIElement = uiElement;
                }

                // Get the sizes of current and next button. Button is large when it is the only in the group
                bool isSmall     = (uiElement.ElementSize == UIElementSizeEnum.Regular) && (count > 1);
                bool isResized   = (uiElement.ElementSize == UIElementSizeEnum.Regular) && (!isSmall);
                bool isNextSmall = (uiElementNext != null) && (uiElementNext.ElementSize == UIElementSizeEnum.Regular);

                // Set the CSS class according to the button size
                string cssClass    = (isSmall ? "SmallButton" : "BigButton");
                string elementName = uiElement.ElementName;

                // Display only caption - do not substitute with Display name when empty
                string elementCaption = ResHelper.LocalizeString(uiElement.ElementCaption);

                // Create main button panel
                CMSPanel pnlButton = new CMSPanel()
                {
                    ID      = "pnlButton" + elementName,
                    ShortID = "b" + elementName
                };

                pnlButton.Attributes.Add("name", elementName);
                pnlButton.CssClass = cssClass;

                // Remember the first button
                if (firstPanel == null)
                {
                    firstPanel = pnlButton;
                }

                // Remember the selected button
                if ((preselectedPanel == null) && elementName.EqualsCSafe(HighlightItem, true))
                {
                    preselectedPanel = pnlButton;

                    // Set the selected button
                    if (mHighlightedUIElement == null)
                    {
                        mHighlightedUIElement = uiElement;
                    }
                }

                // URL or behavior
                string url = uiElement.ElementTargetURL;

                if (!string.IsNullOrEmpty(url) && url.StartsWithCSafe("javascript:", true))
                {
                    pnlButton.Attributes["onclick"] = url.Substring("javascript:".Length);
                }
                else
                {
                    url = UIContextHelper.GetElementUrl(uiElement, UIContext);

                    if (url != String.Empty)
                    {
                        string buttonSelection = (RememberSelectedItem ? "SelectButton(this);" : "");

                        // Ensure hash code if required
                        url = MacroResolver.Resolve(URLHelper.EnsureHashToQueryParameters(url));

                        if (!String.IsNullOrEmpty(TargetFrameset))
                        {
                            if (uiElement.ElementType == UIElementTypeEnum.PageTemplate)
                            {
                                url = URLHelper.UpdateParameterInUrl(url, "displaytitle", "false");
                            }

                            String target = UseIFrame ? String.Format("frames['{0}']", TargetFrameset) : String.Format("parent.frames['{0}']", TargetFrameset);
                            pnlButton.Attributes["onclick"] = String.Format("{0}{1}.location.href = '{2}';", buttonSelection, target, UrlResolver.ResolveUrl(url));
                        }
                        else
                        {
                            pnlButton.Attributes["onclick"] = String.Format("{0}self.location.href = '{1}';", buttonSelection, UrlResolver.ResolveUrl(url));
                        }
                    }
                }

                // Tooltip
                if (!string.IsNullOrEmpty(uiElement.ElementDescription))
                {
                    pnlButton.ToolTip = ResHelper.LocalizeString(uiElement.ElementDescription);
                }
                else
                {
                    pnlButton.ToolTip = elementCaption;
                }
                pnlButton.EnableViewState = false;

                // Ensure correct grouping of small buttons
                if (isSmall && (small == 0))
                {
                    small++;

                    Panel pnlSmallGroup = new Panel()
                    {
                        ID = "pnlGroupSmall" + uiElement.ElementName
                    };
                    if (IsRTL)
                    {
                        pnlSmallGroup.Style.Add("float", "right");
                        pnlSmallGroup.Style.Add("text-align", "right");
                    }
                    else
                    {
                        pnlSmallGroup.Style.Add("float", "left");
                        pnlSmallGroup.Style.Add("text-align", "left");
                    }

                    pnlSmallGroup.EnableViewState = false;
                    pnlSmallGroup.Controls.Add(pnlButton);
                    panels.Add(pnlSmallGroup);
                }

                // Generate button link
                HyperLink buttonLink = new HyperLink()
                {
                    ID = "lnkButton" + uiElement.ElementName,
                    EnableViewState = false
                };

                // Generate button image
                Image buttonImage = new Image()
                {
                    ID              = "imgButton" + uiElement.ElementName,
                    ImageAlign      = (isSmall ? ImageAlign.AbsMiddle : ImageAlign.Top),
                    AlternateText   = elementCaption,
                    EnableViewState = false
                };

                // Use icon path
                if (!string.IsNullOrEmpty(uiElement.ElementIconPath))
                {
                    string iconPath = GetImagePath(uiElement.ElementIconPath);

                    // Check if element size was changed
                    if (isResized)
                    {
                        // Try to get larger icon
                        string largeIconPath = iconPath.Replace("list.png", "module.png");
                        if (FileHelper.FileExists(largeIconPath))
                        {
                            iconPath = largeIconPath;
                        }
                    }

                    buttonImage.ImageUrl = GetImageUrl(iconPath);
                    buttonLink.Controls.Add(buttonImage);
                }
                // Use Icon class
                else if (!string.IsNullOrEmpty(uiElement.ElementIconClass))
                {
                    var icon = new CMSIcon
                    {
                        ID = string.Format("ico_{0}_{1}", identifier, i),
                        EnableViewState = false,
                        ToolTip         = pnlButton.ToolTip,
                        CssClass        = "cms-icon-80 " + uiElement.ElementIconClass
                    };
                    buttonLink.Controls.Add(icon);
                }
                // Load default module icon if ElementIconPath is not specified
                else
                {
                    buttonImage.ImageUrl = GetImageUrl("CMSModules/module.png");
                    buttonLink.Controls.Add(buttonImage);
                }


                // Generate caption text
                Literal captionLiteral = new Literal()
                {
                    ID              = "ltlCaption" + uiElement.ElementName,
                    Text            = (isSmall ? "\n" : "<br />") + elementCaption,
                    EnableViewState = false
                };
                buttonLink.Controls.Add(captionLiteral);


                //Generate button table (IE7 issue)
                Table     tabButton     = new Table();
                TableRow  tabRow        = new TableRow();
                TableCell tabCellLeft   = new TableCell();
                TableCell tabCellMiddle = new TableCell();
                TableCell tabCellRight  = new TableCell();

                tabButton.CellPadding = 0;
                tabButton.CellSpacing = 0;

                tabButton.EnableViewState     = false;
                tabRow.EnableViewState        = false;
                tabCellLeft.EnableViewState   = false;
                tabCellMiddle.EnableViewState = false;
                tabCellRight.EnableViewState  = false;

                tabButton.Rows.Add(tabRow);
                tabRow.Cells.Add(tabCellLeft);
                tabRow.Cells.Add(tabCellMiddle);
                tabRow.Cells.Add(tabCellRight);

                // Generate left border
                Panel pnlLeft = new Panel()
                {
                    ID              = "pnlLeft" + uiElement.ElementName,
                    CssClass        = "Left" + cssClass,
                    EnableViewState = false
                };

                // Generate middle part of button
                Panel pnlMiddle = new Panel()
                {
                    ID       = "pnlMiddle" + uiElement.ElementName,
                    CssClass = "Middle" + cssClass
                };
                pnlMiddle.Controls.Add(buttonLink);
                Panel pnlMiddleTmp = new Panel()
                {
                    EnableViewState = false
                };
                if (isSmall)
                {
                    pnlMiddle.Style.Add("min-width", smallButtonMinimalWidth + "px");
                    // IE7 issue with min-width
                    pnlMiddleTmp.Style.Add("width", smallButtonMinimalWidth + "px");
                    pnlMiddle.Controls.Add(pnlMiddleTmp);
                }
                else
                {
                    pnlMiddle.Style.Add("min-width", bigButtonMinimalWidth + "px");
                    // IE7 issue with min-width
                    pnlMiddleTmp.Style.Add("width", bigButtonMinimalWidth + "px");
                    pnlMiddle.Controls.Add(pnlMiddleTmp);
                }
                pnlMiddle.EnableViewState = false;

                // Generate right border
                Panel pnlRight = new Panel()
                {
                    ID              = "pnlRight" + uiElement.ElementName,
                    CssClass        = "Right" + cssClass,
                    EnableViewState = false
                };

                // Add inner controls
                tabCellLeft.Controls.Add(pnlLeft);
                tabCellMiddle.Controls.Add(pnlMiddle);
                tabCellRight.Controls.Add(pnlRight);

                pnlButton.Controls.Add(tabButton);

                // If there were two small buttons in a row end the grouping div
                if ((small == 2) || (isSmall && !isNextSmall))
                {
                    small = 0;

                    // Add the button to the small buttons grouping panel
                    panels[panels.Count - 1].Controls.Add(pnlButton);
                }
                else
                {
                    if (small == 0)
                    {
                        // Add the generated button into collection
                        panels.Add(pnlButton);
                    }
                }
                if (small == 1)
                {
                    small++;
                }

                // Raise button created event
                if (OnButtonCreated != null)
                {
                    OnButtonCreated(this, new UniMenuArgs {
                        UIElement = uiElement, TargetUrl = url, ButtonControl = pnlButton, ImageControl = buttonImage
                    });
                }
            }

            // Add all panels to control
            foreach (Panel panel in panels)
            {
                TableCell tabGroupCell = new TableCell()
                {
                    VerticalAlign   = VerticalAlign.Top,
                    EnableViewState = false
                };

                tabGroupCell.Controls.Add(panel);
                tabGroupRow.Cells.Add(tabGroupCell);
            }

            pnlButtons.Controls.Add(tabGroup);
        }

        return(pnlButtons);
    }