<commit_before>﻿// Copyright Naked Objects Group Ltd, 45 Station Road, Henley on Thames, UK, RG9 1AT
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using NakedObjects.Facade;
using NakedObjects.Facade.Utility;
using NakedObjects.Facade.Utility.Restricted;
using NakedObjects.Resources;

namespace NakedObjects.Web.Mvc.Html {
    public static class MenuExtensions {
        /// <summary>
        ///     Create menu from actions of domainObject
        /// </summary>
        public static MvcHtmlString ObjectMenu(this HtmlHelper html, object domainObject) {
            return html.ObjectMenu(domainObject, false);
        }

        /// <summary>
        ///     Create menu from actions of domainObject
        /// </summary>
        public static MvcHtmlString ObjectMenuOnTransient(this HtmlHelper html, object domainObject) {
            return html.ObjectMenu(domainObject, true);
        }

        private static MvcHtmlString ObjectMenu(this HtmlHelper html, object domainObject, bool isEdit) {
            var nakedObject = html.Facade().GetObject(domainObject);
            var objectMenu = nakedObject.Specification.Menu;
            return html.MenuAsHtml(objectMenu, nakedObject, isEdit, true);
        }

        /// <summary>
        /// Create main menus for all menus in ViewData
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString MainMenus(this HtmlHelper html) {
            var mainMenus = html.Facade().GetMainMenus();
            return RenderMainMenus(html, mainMenus);
        }

        private static IMenuFacade GetMenu(HtmlHelper html, object service) {
            return html.Facade().GetObject(service).Specification.Menu;
        }

        public static MvcHtmlString MainMenu(this HtmlHelper html, object service) {
            var menu = GetMenu(html, service);
            return html.MenuAsHtml(menu, null, false, false);
        }

        private static MvcHtmlString RenderMainMenus(this HtmlHelper html, IEnumerable<IMenuFacade> menus) {
            var tag = new TagBuilder("div");
            tag.AddCssClass(IdConstants.ServicesContainerName);
            foreach (var menu in menus) {
                tag.InnerHtml += html.MenuAsHtml(menu, null, false, false);
            }
            return MvcHtmlString.Create(tag.ToString());
        }

        private static MvcHtmlString MenuAsHtml(this HtmlHelper html, IMenuFacade menu, IObjectFacade nakedObject, bool isEdit, bool defaultToEmptyMenu) {
            var descriptors = new List<ElementDescriptor>();
            foreach (IMenuItemFacade item in menu.MenuItems) {
                var descriptor = MenuItemAsElementDescriptor(html, item, nakedObject, isEdit);
                if (IsDuplicateAndIsVisibleActions(html, item, menu.MenuItems, nakedObject)) {
                    //Test that both items are in fact visible
                    //The Id is set just to preseve backwards compatiblity
                    string id = menu.Id;
                    if (id.EndsWith("Actions")) {
                        id = id.Split('-').First() + "-DuplicateAction";
                    }
                    descriptor = new ElementDescriptor {
                        TagType = "div",
                        Value = item.Name,
                        Attributes = new RouteValueDictionary(new {<commit_msg>Cherry-picked minor change to menu helpers from 7.0<commit_after>﻿// Copyright Naked Objects Group Ltd, 45 Station Road, Henley on Thames, UK, RG9 1AT
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using NakedObjects.Facade;
using NakedObjects.Facade.Utility;
using NakedObjects.Facade.Utility.Restricted;
using NakedObjects.Resources;

namespace NakedObjects.Web.Mvc.Html {
    public static class MenuExtensions {
        /// <summary>
        ///     Create menu from actions of domainObject
        /// </summary>
        public static MvcHtmlString ObjectMenu(this HtmlHelper html, object domainObject) {
            return html.ObjectMenu(domainObject, false);
        }

        /// <summary>
        ///     Create menu from actions of domainObject
        /// </summary>
        public static MvcHtmlString ObjectMenuOnTransient(this HtmlHelper html, object domainObject) {
            return html.ObjectMenu(domainObject, true);
        }

        private static MvcHtmlString ObjectMenu(this HtmlHelper html, object domainObject, bool isEdit) {
            var nakedObject = html.Facade().GetObject(domainObject);
            var objectMenu = nakedObject.Specification.Menu;
            return html.MenuAsHtml(objectMenu, nakedObject, isEdit, true);
        }

        /// <summary>
        /// Create main menus for all menus in ViewData
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString MainMenus(this HtmlHelper html) {
            var mainMenus = html.Facade().GetMainMenus();
            return RenderMainMenus(html, mainMenus);
        }

        private static IMenuFacade GetMenu(HtmlHelper html, object service) {
            return html.Facade().GetObject(service).Specification.Menu;
        }

        public static MvcHtmlString MainMenu(this HtmlHelper html, object service) {
            var menu = GetMenu(html, service);
            return html.MenuAsHtml(menu, null, false, false);
        }

        private static MvcHtmlString RenderMainMenus(this HtmlHelper html, IEnumerable<IMenuFacade> menus) {
            var tag = new TagBuilder("div");
            tag.AddCssClass(IdConstants.ServicesContainerName);
            AddMainMenusIntoTag(html, menus, tag);
            return MvcHtmlString.Create(tag.ToString());
        }

        public static void AddMainMenusIntoTag(this HtmlHelper html, IEnumerable<IMenuImmutable> menus, TagBuilder tag) {
            foreach (IMenuImmutable menu in menus) {
                tag.InnerHtml += html.MenuAsHtml(menu, null, false, false);
            }
        }

        private static MvcHtmlString MenuAsHtml(this HtmlHelper html, IMenuFacade menu, IObjectFacade nakedObject, bool isEdit, bool defaultToEmptyMenu) {
            var descriptors = new List<ElementDescriptor>();
            foreach (IMenuItemFacade item in menu.MenuItems) {
                var descriptor = MenuItemAsElementDescriptor(html, item, nakedObject, isEdit);
                if (IsDuplicateAndIsVisibleActions(html, item, menu.MenuItems, nakedObject)) {
                    //Test that both items are in fact visible
                    //The Id is set just to preseve backwards compatiblity
                    string id = menu.Id;
                    if (id.EndsWith("Actions")) {
                        id = id.Split('-').First() + "-DuplicateAction";
                    }
                    descriptor = new ElementDescriptor {
                        TagType = "div",
                        Value = item.Name,
                        Attributes = new RouteValueDictionary(new {
{'fraction_non_alphanumeric': 0.08490680959922037, 'fraction_numerical': 0.0021927153124619322, 'mean_word_length': 2.3813838550247115, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2390575', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2027, 'n_words': 662}