Example #1
0
        internal static async Task <string> RenderActionAsync(ModuleAction action, ModuleAction.RenderModeEnum mode, string id,
                                                              ModuleAction.RenderEngineEnum RenderEngine = ModuleAction.RenderEngineEnum.KendoMenu, int BootstrapSmartMenuLevel = 0, bool HasSubmenu = false)
        {
            // check if we're in the right mode
            if (!await action.RendersSomethingAsync())
            {
                return(null);
            }

            await Manager.AddOnManager.AddTemplateFromUIHintAsync("ActionIcons");// this is needed because we're not used by templates

            if (!string.IsNullOrWhiteSpace(action.ConfirmationText) && (action.Style != ModuleAction.ActionStyleEnum.Post && action.Style != ModuleAction.ActionStyleEnum.Nothing))
            {
                throw new InternalError("When using ConfirmationText, the Style property must be set to Post");
            }
            if (!string.IsNullOrWhiteSpace(action.PleaseWaitText) && (action.Style != ModuleAction.ActionStyleEnum.Normal && action.Style != ModuleAction.ActionStyleEnum.Post))
            {
                throw new InternalError("When using PleaseWaitText, the Style property must be set to Normal or Post");
            }
            if (action.CookieAsDoneSignal && action.Style != ModuleAction.ActionStyleEnum.Normal)
            {
                throw new InternalError("When using CookieAsDoneSignal, the Style property must be set to Normal");
            }

            ModuleAction.ActionStyleEnum style = action.Style;
            if (style == ModuleAction.ActionStyleEnum.OuterWindow)
            {
                if (!Manager.IsInPopup)
                {
                    style = ModuleAction.ActionStyleEnum.Normal;
                }
            }

            if (style == ModuleAction.ActionStyleEnum.Popup || style == ModuleAction.ActionStyleEnum.PopupEdit)
            {
                if (Manager.IsInPopup)
                {
                    style = ModuleAction.ActionStyleEnum.NewWindow;
                }
            }

            if (style == ModuleAction.ActionStyleEnum.Popup || style == ModuleAction.ActionStyleEnum.PopupEdit || style == ModuleAction.ActionStyleEnum.ForcePopup)
            {
                await YetaWFCoreRendering.Render.AddPopupsAddOnsAsync();
            }

            bool newWindow = false, outerWindow = false;
            bool popup = false, popupEdit = false;
            bool nothing = false, post = false;

            switch (style)
            {
            default:
            case ModuleAction.ActionStyleEnum.Normal:
                break;

            case ModuleAction.ActionStyleEnum.NewWindow:
                newWindow = true;
                break;

            case ModuleAction.ActionStyleEnum.Popup:
            case ModuleAction.ActionStyleEnum.ForcePopup:
                popup = Manager.CurrentSite.AllowPopups;
                break;

            case ModuleAction.ActionStyleEnum.PopupEdit:
                popup     = Manager.CurrentSite.AllowPopups;
                popupEdit = Manager.CurrentSite.AllowPopups;
                break;

            case ModuleAction.ActionStyleEnum.OuterWindow:
                outerWindow = true;
                break;

            case ModuleAction.ActionStyleEnum.Nothing:
                nothing = true;
                break;

            case ModuleAction.ActionStyleEnum.Post:
                post = true;
                break;
            }

            YTagBuilder tag = new YTagBuilder("a");

            if (!string.IsNullOrWhiteSpace(action.Tooltip))
            {
                tag.MergeAttribute(Basics.CssTooltip, action.Tooltip);
            }
            if (!string.IsNullOrWhiteSpace(action.Name))
            {
                tag.MergeAttribute("data-name", action.Name);
            }
            if (!action.Displayed)
            {
                tag.MergeAttribute("style", "display:none");
            }
            if (HasSubmenu)
            {
                if (RenderEngine == ModuleAction.RenderEngineEnum.BootstrapSmartMenu)
                {
                    tag.AddCssClass("dropdown-toggle");
                    tag.Attributes.Add("data-toggle", "dropdown-toggle");
                }
                tag.Attributes.Add("aria-haspopup", "true");
                tag.Attributes.Add("aria-expanded", "false");
            }
            if (RenderEngine == ModuleAction.RenderEngineEnum.BootstrapSmartMenu)
            {
                tag.AddCssClass(BootstrapSmartMenuLevel <= 1 ? "nav-link" : "dropdown-item");
            }

            if (!string.IsNullOrWhiteSpace(id))
            {
                tag.Attributes.Add("id", id);
            }

            if (!string.IsNullOrWhiteSpace(action.CssClass))
            {
                tag.AddCssClass(Manager.AddOnManager.CheckInvokedCssModule(action.CssClass));
            }
            string extraClass;

            switch (mode)
            {
            default:
            case ModuleAction.RenderModeEnum.Button: extraClass = "y_act_button"; break;

            case ModuleAction.RenderModeEnum.ButtonIcon: extraClass = "y_act_buttonicon"; break;

            case ModuleAction.RenderModeEnum.ButtonOnly: extraClass = "y_act_buttononly"; break;

            case ModuleAction.RenderModeEnum.IconsOnly: extraClass = "y_act_icon"; break;

            case ModuleAction.RenderModeEnum.LinksOnly: extraClass = "y_act_link"; break;

            case ModuleAction.RenderModeEnum.NormalLinks: extraClass = "y_act_normlink"; break;

            case ModuleAction.RenderModeEnum.NormalMenu: extraClass = "y_act_normmenu"; break;
            }
            tag.AddCssClass(Manager.AddOnManager.CheckInvokedCssModule(extraClass));

            string url = action.GetCompleteUrl(OnPage: true);

            if (!string.IsNullOrWhiteSpace(url))
            {
                tag.MergeAttribute("href", Utility.UrlEncodePath(url));
                if (Manager.CurrentPage != null)
                {
                    string currUrl = Manager.CurrentPage.EvaluatedCanonicalUrl;
                    if (!string.IsNullOrWhiteSpace(currUrl) && currUrl != "/")  // this doesn't work on home page because everything matches
                    {
                        if (action.Url == currUrl)
                        {
                            tag.AddCssClass("t_currenturl");
                        }
                        if (currUrl.StartsWith(action.Url))
                        {
                            tag.AddCssClass("t_currenturlpart");
                        }
                    }
                }
            }
            else
            {
                tag.MergeAttribute("href", "javascript:void(0);");
            }

            if (!string.IsNullOrWhiteSpace(action.ConfirmationText))
            {
                if (action.Category == ModuleAction.ActionCategoryEnum.Delete)
                {
                    // confirm deletions?
                    if (UserSettings.GetProperty <bool>("ConfirmDelete"))
                    {
                        tag.MergeAttribute(Basics.CssConfirm, action.ConfirmationText);
                    }
                }
                else
                {
                    // confirm actions?
                    if (UserSettings.GetProperty <bool>("ConfirmActions"))
                    {
                        tag.MergeAttribute(Basics.CssConfirm, action.ConfirmationText);
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(action.PleaseWaitText))
            {
                tag.MergeAttribute(Basics.CssPleaseWait, action.PleaseWaitText);
            }
            if (action.CookieAsDoneSignal)
            {
                tag.Attributes.Add(Basics.CookieDoneCssAttr, "");
            }
            if (action.SaveReturnUrl)
            {
                tag.Attributes.Add(Basics.CssSaveReturnUrl, "");
                if (!action.AddToOriginList)
                {
                    tag.Attributes.Add(Basics.CssDontAddToOriginList, "");
                }
            }
            if (!string.IsNullOrWhiteSpace(action.ExtraData))
            {
                tag.Attributes.Add(Basics.CssExtraData, action.ExtraData);
            }
            if (action.NeedsModuleContext)
            {
                tag.Attributes.Add(Basics.CssAddModuleContext, "");
            }

            if (post)
            {
                tag.Attributes.Add(Basics.PostAttr, "");
            }
            if (action.DontFollow || action.CookieAsDoneSignal || post || nothing)
            {
                tag.MergeAttribute("rel", "nofollow"); // this is so bots don't follow this assuming it's a simple page (Post actions can't be retrieved with GET/HEAD anyway)
            }
            if (outerWindow)
            {
                tag.Attributes.Add(Basics.CssOuterWindow, "");
            }
            if (!nothing)
            {
                tag.AddCssClass(Manager.AddOnManager.CheckInvokedCssModule(Basics.CssActionLink));
            }
            if (newWindow)
            {
                tag.MergeAttribute("target", "_blank");
                tag.MergeAttribute("rel", "noopener noreferrer");
            }
            if (popup)
            {
                tag.AddCssClass(Manager.AddOnManager.CheckInvokedCssModule(Basics.CssPopupLink));
                if (popupEdit)
                {
                    tag.Attributes.Add(Basics.CssAttrDataSpecialEdit, "");
                }
            }
            if (mode == ModuleAction.RenderModeEnum.Button || mode == ModuleAction.RenderModeEnum.ButtonIcon || mode == ModuleAction.RenderModeEnum.ButtonOnly)
            {
                tag.Attributes.Add(Basics.CssAttrActionButton, "");
            }

            bool   hasText = false, hasImg = false;
            string innerHtml = "";

            if (mode != ModuleAction.RenderModeEnum.LinksOnly && mode != ModuleAction.RenderModeEnum.ButtonOnly && !string.IsNullOrWhiteSpace(action.ImageUrlFinal))
            {
                string text = mode == ModuleAction.RenderModeEnum.NormalMenu ? action.MenuText : action.LinkText;
                if (RenderEngine == ModuleAction.RenderEngineEnum.KendoMenu)
                {
                    innerHtml += ImageHTML.BuildKnownIcon(action.ImageUrlFinal, alt: text, cssClass: Basics.CssNoTooltip + " k-image"); // k-image is needed to align <i> and <img> correctly
                }
                else
                {
                    innerHtml += ImageHTML.BuildKnownIcon(action.ImageUrlFinal, alt: text, cssClass: Basics.CssNoTooltip);
                }
                hasImg = true;
            }
            if (mode != ModuleAction.RenderModeEnum.IconsOnly && mode != ModuleAction.RenderModeEnum.ButtonIcon)
            {
                string text = mode == ModuleAction.RenderModeEnum.NormalMenu ? action.MenuText : action.LinkText;
                if (!string.IsNullOrWhiteSpace(text))
                {
                    innerHtml += Utility.HtmlEncode(text);
                    hasText    = true;
                }
            }
            if (hasText)
            {
                if (hasImg)
                {
                    tag.AddCssClass("y_act_textimg");
                }
                else
                {
                    tag.AddCssClass("y_act_text");
                }
            }
            else
            {
                if (hasImg)
                {
                    tag.AddCssClass("y_act_img");
                }
            }
            if (HasSubmenu && RenderEngine == ModuleAction.RenderEngineEnum.BootstrapSmartMenu)
            {
                innerHtml += " <span class='caret'></span>";
            }

            tag.AddCssClass(Globals.CssModuleNoPrint);
            tag.InnerHtml = innerHtml;

            return(tag.ToString(YTagRenderMode.Normal));
        }