Example #1
0
        public static Element FindLinkContainer(Element Link, List <Element> LinkGroup, Element CommonParent)
        {
            var target = Link;

            var DirectParent = target;

            while (DirectParent != null && !DirectParent.isEqualNode(CommonParent))
            {
                foreach (var item in LinkGroup)
                {
                    if (!item.isEqualNode(Link))
                    {
                        if (DomService.ContainsOrEqualElement(DirectParent, item))
                        {
                            break;
                        }
                    }
                }

                target       = DirectParent;
                DirectParent = target.parentElement;
            }

            return(target);
        }
Example #2
0
        public static Menu CreateMenu(Document doc, MenuViewModel MenuViewModel)
        {
            Menu    menu             = new Menu();
            Element LinkElement      = null;
            Element ItemContainer    = null;
            Element ContainerElement = null;

            int MenuTemplateStart = int.MaxValue;
            int MenuTemplaetEnd   = 0;

            if (!string.IsNullOrEmpty(MenuViewModel.LinkElement) && !string.IsNullOrEmpty(MenuViewModel.ContainerElement))
            {
                var linkelement = DomService.GetElementByKoobooId(doc, MenuViewModel.LinkElement);
                if (linkelement != null)
                {
                    LinkElement = linkelement as Element;
                }

                var container = DomService.GetElementByKoobooId(doc, MenuViewModel.ContainerElement);
                if (container == null && linkelement != null)
                {
                    container = linkelement;
                }
                if (container != null)
                {
                    ContainerElement  = container as Element;
                    MenuTemplateStart = ContainerElement.location.openTokenStartIndex;
                    MenuTemplaetEnd   = ContainerElement.location.endTokenEndIndex;
                }
            }

            string SubMenuItemsTemplate     = null;
            string SubMenuItemOrginalString = null;
            int    SubMenuStart             = int.MaxValue;
            int    SubMenuEnd = 0;

            if (MenuViewModel.children.Count > 0)
            {
                List <Menu> SubMenus = new List <Menu>();

                foreach (var item in MenuViewModel.children)
                {
                    Menu submenu = CreateMenu(doc, item);
                    if (submenu != null)
                    {
                        SubMenus.Add(submenu);
                        if (submenu.tempdata.StartIndex < SubMenuStart)
                        {
                            SubMenuStart = submenu.tempdata.StartIndex;
                        }
                        if (submenu.tempdata.EndIndex > SubMenuEnd)
                        {
                            SubMenuEnd = submenu.tempdata.EndIndex;
                        }
                    }
                }

                ItemContainer = _GetItemContainer(doc, MenuViewModel);

                string SubMenuString = doc.HtmlSource.Substring(SubMenuStart, SubMenuEnd - SubMenuStart + 1);

                if (ItemContainer != null)
                {
                    // 1, sub item within the link element...
                    if (ContainerElement == null || DomService.ContainsOrEqualElement(ContainerElement, ItemContainer))
                    {
                        SubMenuItemOrginalString = ItemContainer.OuterHtml;
                        SubMenuItemsTemplate     = SubMenuItemOrginalString.Replace(SubMenuString, MenuHelper.MarkSubItems);
                    }
                    else if (ContainerElement.isEqualNode(ItemContainer))
                    {
                        SubMenuItemOrginalString = SubMenuString;
                        SubMenuItemsTemplate     = MenuHelper.MarkSubItems;
                    }

                    else
                    {
                        var  distance = DomService.GetTreeDistance(ContainerElement, ItemContainer);
                        bool sibling  = false;
                        if (distance == 1)
                        {
                            sibling = true;
                        }
                        else if (distance < 5)
                        {
                            var nodes = DomService.GetNodesInBetween(doc, ContainerElement, ItemContainer);
                            if (nodes == null || nodes.Count == 0 || IsPossibleSeperator(nodes))
                            {
                                sibling = true;
                            }
                        }

                        if (sibling)
                        {
                            if (MenuTemplateStart > ItemContainer.location.openTokenStartIndex)
                            {
                                MenuTemplateStart = ItemContainer.location.openTokenStartIndex;
                            }

                            if (MenuTemplaetEnd < ItemContainer.location.endTokenEndIndex)
                            {
                                MenuTemplaetEnd = ItemContainer.location.endTokenEndIndex;
                            }

                            SubMenuItemOrginalString = ItemContainer.OuterHtml;
                            SubMenuItemsTemplate     = SubMenuItemOrginalString.Replace(SubMenuString, MenuHelper.MarkSubItems);
                        }

                        else
                        {
                            //menu.RenderSubMenuSeperated = true;
                            SubMenuItemOrginalString = ItemContainer.OuterHtml;
                            SubMenuItemsTemplate     = ItemContainer.OuterHtml.Replace(SubMenuString, MenuHelper.MarkSubItems);
                        }
                    }
                }

                if (SubMenus.Count > 0)
                {
                    menu.children.AddRange(SubMenus);
                }
            }

            if (MenuTemplateStart > 0 && MenuTemplaetEnd > 0)
            {
                string menutemplate = doc.HtmlSource.Substring(MenuTemplateStart, MenuTemplaetEnd - MenuTemplateStart + 1);

                if (!string.IsNullOrEmpty(SubMenuItemOrginalString))
                {
                    menutemplate = menutemplate.Replace(SubMenuItemOrginalString, MenuHelper.MarkSubItems);
                }

                string OriginalLink = LinkElement.OuterHtml;
                string NewLInk      = DomService.ReplaceLink(LinkElement, MenuHelper.MarkHref, MenuHelper.MarkAnchorText);
                menutemplate  = menutemplate.Replace(OriginalLink, NewLInk);
                menu.Template = menutemplate;
                menu.Name     = MenuViewModel.text;
                menu.Url      = MenuViewModel.href;
            }

            menu.SubItemContainer = SubMenuItemsTemplate;

            menu.tempdata.StartIndex = MenuTemplateStart;
            menu.tempdata.EndIndex   = MenuTemplaetEnd;

            if (menu.tempdata.StartIndex == 0 || menu.tempdata.EndIndex == 0)
            {
                if (ItemContainer != null)
                {
                    menu.tempdata.StartIndex = ItemContainer.location.openTokenStartIndex;
                    menu.tempdata.EndIndex   = ItemContainer.location.endTokenEndIndex;
                }
            }
            return(menu);
        }