Exemplo n.º 1
0
        //проверка доступности пунктов
        public RenderItem ToPreRenderNode(NavigationNode node)
        {
            bool isVisible = node != null && node.CheckAuthorization(_context);

            if (!isVisible)
            {
                return(null);
            }

            RenderItem renderItem = new RenderItem(node, _context);

            foreach (NavigationNode childNode in node.Children)
            {
                RenderItem childRenderItem = ToPreRenderNode(childNode);
                if (childRenderItem != null)
                {
                    childRenderItem.Parent = renderItem;
                    renderItem.Children.Add(childRenderItem);
                }
            }

            renderItem.Children = renderItem.Children.OrderBy(p => p.Node.Order).ToList();

            return(renderItem);
        }
Exemplo n.º 2
0
        private List <RenderItem> BuildList(RenderItem currentItem, object renderTarget)
        {
            if (currentItem == null)
            {
                return(new List <RenderItem>());
            }

            List <RenderItem> breadCrumbItems = new List <RenderItem>();

            bool isVisible = currentItem.Node.CheckRenderTarget(_context, renderTarget);

            if (isVisible)
            {
                breadCrumbItems.Add(currentItem);
                RenderItem parent = currentItem.Parent;

                while (parent != null)
                {
                    isVisible = parent.Node.CheckRenderTarget(_context, renderTarget);
                    if (!isVisible)
                    {
                        new List <RenderItem>();
                    }

                    breadCrumbItems.Insert(0, parent);
                    parent = parent.Parent;
                }
            }

            return(breadCrumbItems);
        }
Exemplo n.º 3
0
        //поиск текущего пункта
        public void MatchNodeUrl(List <RenderItem> renderItems)
        {
            foreach (RenderItem renderItem in renderItems)
            {
                bool isNodeMatched = CurrentItem == null &&
                                     renderItem.Node.MatchNodeUrl(_context);

                if (isNodeMatched)
                {
                    CurrentItem = renderItem.ItemsProvider == null
                        ? renderItem
                        : renderItem.MatchNavigationItem(_context);
                }

                if (CurrentItem == null)
                {
                    MatchNodeUrl(renderItem.Children);
                }

                if (CurrentItem != null)
                {
                    return;
                }
            }
        }
Exemplo n.º 4
0
        private List <RenderItem> PickNodeItems(List <RenderItem> breadCrumbItems)
        {
            List <RenderItem> pickedList = new List <RenderItem>();

            foreach (RenderItem item in breadCrumbItems)
            {
                if (item.ItemsProvider == null)
                {
                    item.BuildHref(_context);
                    pickedList.Add(item);
                }
                else
                {
                    RenderItem matchedItem = item.MatchNavigationItem(_context);
                    if (matchedItem == null)
                    {
                        item.IsClickable = false;
                        pickedList.Add(item);
                    }
                    else
                    {
                        pickedList.Add(matchedItem);
                    }
                }
            }

            return(pickedList);
        }
        //методы
        internal RenderItem MatchNavigationItem(CompositionContext context)
        {
            if (ItemsProvider == null)
            {
                return(null);
            }

            List <NavigationItem> items = ItemsProvider.Provide(Node, context.HttpContext);

            NavigationItem matchedItem = items.FirstOrDefault(
                item => Node.MatchItemUrl(item, context, MatchRouteValues, MatchQueryString));

            if (matchedItem == null)
            {
                return(null);
            }

            RenderItem renderItem = FromNavigationItem(matchedItem, context);

            renderItem.Parent             = Parent;
            renderItem.IsCurrentItem      = IsCurrentItem;
            renderItem.IsChildCurrentItem = IsChildCurrentItem;

            return(renderItem);
        }
        protected virtual string ToQueryString(
            RenderItem renderItem, IDictionary <string, string> queryValues)
        {
            NameValueCollection         currentQueryString = renderItem.MatchQueryString;
            Dictionary <string, string> buildQueryString   = new Dictionary <string, string>();

            foreach (string nodeKey in queryValues.Keys)
            {
                string value = queryValues[nodeKey];

                if (value == "*")
                {
                    value = currentQueryString != null
                        ? currentQueryString[nodeKey]
                        : null;
                }

                buildQueryString.Add(HttpUtility.UrlEncode(nodeKey), HttpUtility.UrlEncode(value));
            }

            string qs = string.Join("&",
                                    buildQueryString.Select(kvp =>
                                                            string.Format("{0}={1}", kvp.Key, kvp.Value)));

            return(qs);
        }
Exemplo n.º 7
0
        //методы
        public List <RenderItem> Compose(RenderItem currentItem, object renderTarget)
        {
            if (!_cache.ContainsKey(renderTarget))
            {
                List <RenderItem> breadCrumbs = BuildList(currentItem, renderTarget);
                _cache[renderTarget] = PickNodeItems(breadCrumbs);
            }

            return(_cache[renderTarget]);
        }
Exemplo n.º 8
0
        public void SetCurrentChain()
        {
            if (CurrentItem != null)
            {
                CurrentItem.IsCurrentItem = true;
                RenderItem parent = CurrentItem;

                while (parent != null)
                {
                    parent.IsChildCurrentItem = true;
                    parent = parent.Parent;
                }
            }
        }
        private void CreateRenderItems()
        {
            if (HttpContext.Current == null)
            {
                throw new NullReferenceException("HttpContext is not ready.");
            }

            _context.HttpContext = HttpContext.Current;
            _context.UrlHelper   = new UrlHelper(HttpContext.Current.Request.RequestContext);

            RenderItem rootItem = _preRenderComposer.ToPreRenderNode(_context.Settings.Root);

            _preRenderComposer.PreRenderNodes = _preRenderComposer.MoveStartingNode(rootItem);
        }
Exemplo n.º 10
0
        public RenderItem Ancestor(string nodeKey)
        {
            RenderItem item = this;

            do
            {
                if (item.Node.NodeKey == nodeKey)
                {
                    return(item);
                }
                item = item.Parent;
            }while (item != null);

            return(null);
        }
Exemplo n.º 11
0
        public List <RenderItem> MoveStartingNode(RenderItem rootNode)
        {
            List <RenderItem> rootList = rootNode == null
                ? new List <RenderItem>()
                : rootNode.Children;

            if (_context.Settings.ShowStartingNode && rootNode != null)
            {
                rootNode.Children = new List <RenderItem>();
                rootList.Insert(0, rootNode);
                _context.RootNode = rootNode;
            }

            return(rootList.OrderBy(p => p.Node.Order).ToList());
        }
        private List <RenderItem> ToPreRenderNodes(RenderItem renderItem, RenderItem parent)
        {
            List <RenderItem>     extractedList = new List <RenderItem>();
            List <NavigationItem> items         = renderItem.ItemsProvider.Provide(renderItem.Node, _context.HttpContext);

            foreach (NavigationItem item in items)
            {
                RenderItem extractedItem = renderItem.FromNavigationItem(item, _context);
                extractedItem.Children = ExtractProvidedItems(renderItem.Children, extractedItem);
                extractedItem.Parent   = parent;
                extractedList.Add(extractedItem);
            }

            return(extractedList);
        }
Exemplo n.º 13
0
        //поиск по ключу
        public RenderItem FindByKey(List <RenderItem> renderItems, string nodeKey)
        {
            foreach (RenderItem renderItem in renderItems)
            {
                if (renderItem.Node.NodeKey == nodeKey)
                {
                    return(renderItem);
                }

                RenderItem matchedItem = FindByKey(renderItem.Children, nodeKey);
                if (matchedItem != null)
                {
                    return(matchedItem);
                }
            }

            return(null);
        }
Exemplo n.º 14
0
        public RenderItem(RenderItem another)
        {
            Node          = another.Node;
            Children      = new List <RenderItem>();
            ItemsProvider = another.ItemsProvider;

            Title       = another.Title;
            Description = another.Description;
            IsClickable = another.IsClickable;

            MatchQueryString = another.MatchQueryString;
            MatchRouteValues = another.MatchRouteValues;

            Href = another.Href;

            Parent             = another.Parent;
            IsCurrentItem      = another.IsCurrentItem;
            IsChildCurrentItem = another.IsChildCurrentItem;
        }
        //методы
        public virtual string BuildUrl(RenderItem renderItem, CompositionContext context)
        {
            IDictionary <string, object> routeValues = renderItem.Item == null
                ? renderItem.Node.RouteValues
                : Combine(renderItem.Node.RouteValues, renderItem.Item.RouteValues);

            string url = routeValues == null
                ? context.UrlHelper.Action(renderItem.Node.Action, renderItem.Node.Controller)
                : context.UrlHelper.Action(renderItem.Node.Action, renderItem.Node.Controller
                                           , ToRouteValueDictionary(renderItem, routeValues));

            IDictionary <string, string> queryValues = renderItem.Item == null
                ? renderItem.Node.QueryStringValues
                : Combine(renderItem.Node.QueryStringValues, renderItem.Item.QueryStringValues);

            return(queryValues == null
                ? url
                : string.Format("{0}?{1}", url, ToQueryString(renderItem, queryValues)));
        }
        //добавление динамических пунктов
        public List <RenderItem> ExtractProvidedItems(List <RenderItem> renderItems, RenderItem parent)
        {
            List <RenderItem> extractedList = new List <RenderItem>();

            foreach (RenderItem renderItem in renderItems)
            {
                if (renderItem.ItemsProvider == null)
                {
                    extractedList.Add(renderItem);
                }
                else
                {
                    List <RenderItem> extractedItems = ToPreRenderNodes(renderItem, parent);
                    extractedList.AddRange(extractedItems);
                }
            }

            return(extractedList);
        }
        //фильтр видимости
        public List <RenderItem> FilterByRenderTarget(
            List <RenderItem> renderItems, object renderTarget, RenderItem parent)
        {
            List <RenderItem> filtered = new List <RenderItem>();

            foreach (RenderItem renderItem in renderItems)
            {
                bool isVisible = renderItem.Node.CheckRenderTarget(_context, renderTarget);
                if (isVisible)
                {
                    RenderItem filteredItem = new RenderItem(renderItem);  //пересоздание т.к. изменяются Children
                    filteredItem.Children = FilterByRenderTarget(renderItem.Children, renderTarget, filteredItem);
                    filteredItem.Parent   = parent;
                    filtered.Add(filteredItem);
                }
            }

            return(filtered);
        }
Exemplo n.º 18
0
        internal RenderItem FromNavigationItem(NavigationItem item, CompositionContext context)
        {
            var navigationItem = new RenderItem()
            {
                Node     = Node,
                Children = new List <RenderItem>(),
                Item     = item,

                Title       = item.GetTitle() ?? Node.GetTitle(),
                Description = item.GetDescription() ?? Node.GetDescription(),
                IsClickable = Node.IsClickable,

                MatchQueryString = new NameValueCollection(MatchQueryString),
                MatchRouteValues = new RouteValueDictionary(MatchRouteValues)
            };

            navigationItem.BuildHref(context);

            return(navigationItem);
        }
        protected virtual RouteValueDictionary ToRouteValueDictionary(
            RenderItem renderItem, IDictionary <string, object> routeValues)
        {
            RouteValueDictionary currentRvd = renderItem.MatchRouteValues;
            RouteValueDictionary buildRvd   = new RouteValueDictionary();

            foreach (string nodeKey in routeValues.Keys.ToList())
            {
                object value = routeValues[nodeKey];

                if (value?.ToString() == "*")
                {
                    bool currentUrlHasKey = currentRvd != null && currentRvd.ContainsKey(nodeKey) && currentRvd[nodeKey] != null;
                    value = currentUrlHasKey
                        ? currentRvd[nodeKey]
                        : null;
                }

                buildRvd.Add(nodeKey, value);
            }

            return(buildRvd);
        }
Exemplo n.º 20
0
        protected void WriteItem(XmlTextWriter writer, RenderItem renderItem, string host)
        {
            SeoDescription description = renderItem.Node.Seo;

            if (description != null)
            {
                float priority = (int)description.Priority / 10f;

                writer.WriteStartElement("url");
                writer.WriteElementString("loc", "http://" + host + renderItem.Href);
                writer.WriteElementString("changefreq", description.UpdateFrequency.ToString());
                writer.WriteElementString("priority", priority.ToString());
                writer.WriteEndElement();

                if (renderItem.Children != null)
                {
                    foreach (RenderItem subItem in renderItem.Children)
                    {
                        WriteItem(writer, subItem, host);
                    }
                }
            }
        }