Exemplo n.º 1
0
        /// <summary>
        /// Render the sitemenu element that on the top of the web page.
        /// </summary>
        /// <param name="htmlAttributes">The html attributes for output element.</param>
        /// <returns></returns>
        public static HelperResult SiteMenu(object htmlAttributes = null)
        {
            var _web      = AppModel.Get().Webs["home"];
            var locale    = CurrentPage.Culture;
            var curPage   = AppModel.Get().CurrentPage;
            var currentID = curPage != null ? curPage.ID : 0;

            return(new HelperResult(w =>
            {
                using (var writer = new HtmlTextWriter(w))
                {
                    var pages = _web.Pages.Where(p => p.ShowInMenu && p.ParentID == 0 && p.Locale.Equals(locale, StringComparison.OrdinalIgnoreCase)).OrderBy(p => p.Pos).ToList();
                    writer.WriteBeginTag("ul");
                    if (htmlAttributes != null)
                    {
                        var attrs = ObjectHelper.ConvertObjectToDictionary(htmlAttributes);
                        foreach (var attr in attrs)
                        {
                            var key = attr.Key;
                            if (key.StartsWith("data_"))
                            {
                                key = key.Replace("_", "-");
                            }

                            writer.WriteAttribute(key, attr.Value != null ? attr.Value.ToString() : "");
                        }
                    }
                    writer.Write(HtmlTextWriter.TagRightChar);

                    foreach (var p in pages)
                    {
                        if (!IsAccessibleToUser(p))
                        {
                            continue;
                        }

                        #region render node
                        var path = string.Format("~/{0}/{1}/{2}.html", p.Web.Name, p.Locale, p.Slug);
                        writer.WriteBeginTag("li");

                        if (curPage != null)
                        {
                            var parentPath = p.Path + "/";
                            if (!string.IsNullOrEmpty(curPage.Path) && (curPage.Path.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase) || p.ID == curPage.ID))
                            {
                                writer.WriteAttribute("class", "d-state-active");
                            }
                        }

                        writer.Write(HtmlTextWriter.TagRightChar);
                        writer.WriteBeginTag("a");
                        writer.WriteAttribute("data-id", p.ID.ToString());

                        if (p.NoFollow)
                        {
                            writer.WriteAttribute("rel", "nofollow");
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(Request.Browser.Browser) && Request.Browser.Browser.Equals("chrome", StringComparison.OrdinalIgnoreCase))
                            {
                                writer.WriteAttribute("rel", "preload");
                            }
                            else
                            {
                                writer.WriteAttribute("rel", "prefetch");
                            }
                        }

                        if (!string.IsNullOrEmpty(p.LinkTo))
                        {
                            writer.WriteAttribute("href", p.LinkTo);
                        }
                        else
                        {
                            writer.WriteAttribute("href", Href(path));
                        }

                        writer.Write(HtmlTextWriter.TagRightChar);

                        writer.WriteEncodedText(GE.GetContent(p.Title));
                        writer.WriteEndTag("a");
                        writer.WriteEndTag("li");
                        #endregion
                    }

                    writer.WriteEndTag("ul");
                }
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Render HTML ui element for web page structure.
        /// </summary>
        /// <param name="web">The parent web instance.</param>
        /// <param name="withAttributes"></param>
        /// <param name="showimg">Whether show image on the left of the node item.</param>
        /// <param name="ismenu">Show as menu</param>
        /// <param name="issitemap">Show as sitemap</param>
        /// <param name="htmlAttributes">The html attributes for output element.</param>
        /// <param name="dataType">The menu item filter:1:all pages. 2:root pages (the top level pages.) 3:show children pages</param>
        /// <returns></returns>
        public static HelperResult GetHtml(Web web = null, bool withAttributes = false, bool showimg = false, bool ismenu = false, bool issitemap = false, object htmlAttributes = null, int dataType = 1)
        {
            var _web        = new WebDecorator(web == null ? AppModel.Get().Context.Web : web, AppModel.Get().DataContext);
            var locale      = CurrentPage.Culture;// _web.DefaultLocale;
            var currentPage = AppModel.Get().CurrentPage;
            var currentID   = currentPage != null ? currentPage.ID : 0;

            return(new HelperResult(w =>
            {
                using (var writer = new HtmlTextWriter(w))
                {
                    var pages = _web.CachingPages.Where(p => p.Locale.Equals(locale, StringComparison.OrdinalIgnoreCase));
                    //IEnumerable<WebPage> pages = null;

                    if (ismenu)
                    {
                        pages = pages.Where(p => p.ShowInMenu);
                    }

                    if (issitemap)
                    {
                        pages = pages.Where(p => p.ShowInSitemap);
                    }

                    pages = pages.OrderBy(p => p.Pos).ToList();

                    var topPages = dataType == 3 ? pages.Where(p => p.ParentID == currentPage.ID) : pages.Where(p => p.ParentID == 0);

                    if (dataType == 4 && currentPage != null)
                    {
                        if (currentPage.ParentID == 0)
                        {
                            topPages = pages.Where(p => p.ParentID == currentPage.ID);
                        }
                        else
                        {
                            topPages = pages.Where(p => !string.IsNullOrEmpty(p.Path) && p.Path.Equals(currentPage.Path, StringComparison.OrdinalIgnoreCase));
                        }
                    }

                    if (topPages.Count() == 0)
                    {
                        return;
                    }

                    writer.WriteBeginTag("ul");
                    if (htmlAttributes != null)
                    {
                        var attrs = ObjectHelper.ConvertObjectToDictionary(htmlAttributes);
                        foreach (var attr in attrs)
                        {
                            var key = attr.Key;
                            if (key.StartsWith("data_"))
                            {
                                key = key.Replace("_", "-");
                            }

                            writer.WriteAttribute(key, attr.Value != null ? attr.Value.ToString() : "");
                        }
                    }
                    writer.Write(HtmlTextWriter.TagRightChar);

                    if (dataType == 3)
                    {
                        if (currentPage == null)
                        {
                            writer.WriteEndTag("ul");
                            return;
                        }
                        //pages = _web.Pages.Where(p => p.ParentID == currentPage.ID);
                    }

                    foreach (var p in topPages)
                    {
                        if (ismenu || issitemap)
                        {
                            if (!IsAccessibleToUser(p))
                            {
                                continue;
                            }
                        }

                        RenderNode(writer, p, pages, withAttributes, showimg, currentPage, dataType);
                    }

                    writer.WriteEndTag("ul");
                }
            }));
        }