Ejemplo n.º 1
0
        public override ReactElement Render()
        {
            var  historyHandler = props.HistoryHandlerOverride.GetValueOrDefault(Html5HistoryRouter.Instance);
            var  currentUrl     = historyHandler.CurrentLocation;
            bool isAncestor;

            if (historyHandler.CurrentLocation.Segments.Count >= props.Url.Segments.Count)
            {
                var stringComparison = props.CaseSensitiveUrlMatching ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
                isAncestor = props.Url.Segments
                             .Zip(historyHandler.CurrentLocation.Segments.Take((int)props.Url.Segments.Count), (x, y) => new { CurrentUrlSegment = x, PropsUrlSegment = y })
                             .All(segment => segment.CurrentUrlSegment.Value.Equals(segment.PropsUrlSegment.Value, stringComparison));
            }
            else
            {
                isAncestor = false;
            }
            bool isSelected;

            if (isAncestor && (historyHandler.CurrentLocation.Segments.Count == props.Url.Segments.Count))
            {
                isSelected = true;
                isAncestor = false;                 // Don't identify a URL as "ancestor" AND "selected", "selected" should take precedent
            }
            else
            {
                isSelected = false;
            }
            var className = props.ClassName;

            if (isAncestor)
            {
                className = className.Add(props.AncestorClassName);
            }
            if (isSelected)
            {
                className = className.Add(props.SelectedClassName);
            }
            return(DOM.A(
                       new AnchorAttributes
            {
                Name = props.Name.ToNullableString(),
                Target = props.Target.ToNullableString(),
                ClassName = className.ToNullableString(),
                Href = props.Url.ToString(),
                OnClick = e =>
                {
                    if (props.OnClick.IsDefined)
                    {
                        props.OnClick.Value(e);
                    }

                    // ONLY intercept left-click and ONLY if there's no holding-Control-or-Shift-of-whatever, so that we don't mess with people trying to
                    // open in a new tab, window, etc.. Note that the button values are reversed for left-handed mice, so button 0 is always the primary
                    // click button (except on IE8 and earlier, where it's a different value, but we don't care about those browsers). For more details,
                    // see http://www.w3schools.com/jsref/event_button.asp.
                    // - Only do this if target has not been specified or if it has been explicitly set to the default "_self" value because we don't
                    //   want to get in the way of the browser doing its thing if it needs to open in a new window or similar (if the target is "_blank",
                    //   "_parent", "_top" or "{TARGET NAME}" - see https://www.w3.org/TR/html4/types.html#type-frame-target for more details)
                    var overrideBrowserBehaviour = !props.Target.IsDefined || props.Target.Value.Value.Equals("_self", StringComparison.OrdinalIgnoreCase);
                    if ((e.Button != 0) || e.AltKey || e.CtrlKey || e.MetaKey || e.ShiftKey || !overrideBrowserBehaviour)
                    {
                        return;
                    }
                    historyHandler.NavigateTo(props.Url);
                    e.PreventDefault();
                }
            },
                       props.Text
                       ));
        }