Make sure that only the menu item with the largest weight is active.
    If a child of a menu item is active, the parent should be active too.
    :param menu:
    :return:
176445
def menu_clean(menu_config):
    
    max_weight = -1
    for _, value in list(menu_config.items()):
        if value["submenu"]:
            for _, v in list(value["submenu"].items()):
                if v["active"]:
                    # parent inherits the weight of the axctive child
                    value["active"] = True
                    value["active_weight"] = v["active_weight"]

        if value["active"]:
            max_weight = max(value["active_weight"], max_weight)

    if max_weight > 0:
        # one of the items is active: make items with lesser weight inactive
        for _, value in list(menu_config.items()):
            if value["active"] and value["active_weight"] < max_weight:
                value["active"] = False
    return menu_config
332548
public function validateMenu (Menu $menu)
    {
        foreach ($menu->getItems() as $item) {
            if (! $this->validateItem($item)) {
                return false;
            }
        }

        return true;
    }
728228
def menu(menu_config=None, **kwargs):
    
    request = kwargs.pop("request", None)
    user = kwargs.pop("user", None)
    url_full_name = ":".join(
        [request.resolver_match.namespace, request.resolver_match.url_name]
    )

    if not menu_config:
        menu_config = settings.ARCTIC_MENU

    menu_dict = OrderedDict()
    for menu_entry in menu_config:
        if type(menu_entry) in (list, tuple):

            # check permission based on named_url
            path = None
            if menu_entry[1]:
                if not view_from_url(menu_entry[1]).has_permission(user):
                    continue

                path = reverse(menu_entry[1])
            # icons and collapse are optional
            icon = None
            if (len(menu_entry) >= 3) and (
                not type(menu_entry[2]) in (list, tuple)
            ):
                icon = menu_entry[2]
            active_weight = len(path) if path else 0
            menu_dict[menu_entry[0]] = {
                "url": menu_entry[1],
                "icon": icon,
                "submenu": None,
                "active": is_active(menu_entry, url_full_name),
                "active_weight": active_weight,
            }

            # check if the last item in a menu entry is a submenu
            submenu = _get_submenu(menu_entry)
            if submenu:
                menu_dict[menu_entry[0]]["submenu"] = menu(
                    submenu, user=user, request=request
                )
    return menu_clean(menu_dict)
1164740
def persistent_menu(menu):
    
    if len(menu) > 3:
        raise Invalid('menu should not exceed 3 call to actions')

    if any(len(item['call_to_actions']) > 5 for item in menu if item['type'] == 'nested'):
        raise Invalid('call_to_actions is limited to 5 for sub-levels')

    for item in menu:
        if len(item['title']) > 30:
            raise Invalid('menu item title should not exceed 30 characters')

        if item['type'] == 'postback' and len(item['payload']) > 1000:
            raise Invalid('menu item payload should not exceed 1000 characters')
487549
public void addMenuItem(SubMenuItem parent, MenuItem item) {
        SubMenuItem subMenu = (parent != null) ? parent : ROOT;

        synchronized (subMenuItems) {
            ArrayList<MenuItem> subMenuChildren = subMenuItems.computeIfAbsent(subMenu, sm -> new ArrayList<>());
            subMenuChildren.add(item);

            if (item.hasChildren()) {
                subMenuItems.put(item, new ArrayList<>());
            }
        }
    }
1337882
def parent(self, index):
        
        if not index.isValid():
            return QtCore.QModelIndex()

        childItem = index.internalPointer()
        # the only place where the parent item is queried
        parentItem = childItem.getParent()

        if parentItem == self.root:
            return QtCore.QModelIndex()

        return self.createIndex(parentItem.row(), 0, parentItem)
292281
private void enableMenu(final JMenu menu) {
    menu.setEnabled(true);
    for (final Component c : menu.getMenuComponents()) {
      if (c instanceof JMenu) {
        enableMenu((JMenu) c);
      } else if (c instanceof JMenuItem) {
        ((JMenuItem) c).setEnabled(true);
      }
    }
  }
84290
def mark_as_inactive(self, kid):
        
        k = self.get_key_with_kid(kid)
        k.inactive_since = time.time()
89763
def delete_menu(self, menu):
        
        if menu.parent is None:
            del self.menus[menu.name()]
        menu._delete()
831177
function buildSubmenu(level) {
	let menu = [];

	/* Move up to second level */
	let current = level;
	while(current.parent && current.parent.parent) {
		current = current.parent;
	}

	if(current.content === '') {
		/* Add children if toplevel is empty */
		menu.push(...current.children);
	} else {
		/* Add toplevel if not */
		menu.push(current);
	}

	/* Return an empty menu if we have only a single entry without children or are toplevel*/
	return (!current.parent || (menu.length === 1 && menu[0].children.length === 0)) ? [] : menu;
}
504671
function () {
            var self = this, cur = self, last;
            do {
                // 沿着 menu，menuitem 链
                last = cur;
                cur = cur.get('parent');
            } while (cur && (cur.isMenuItem || cur.isMenu));
            return last;
        }
868829
function() {
        var options = this.options;

        if (options.calculate) {
            this.calculate();
        }

        if (!this.active) {
            return;
        }

        var eHeight = this.elementHeight,
            eTop = this.elementTop,
            pHeight = this.parentHeight,
            pTop = this.parentTop,
            cssTop = this.initialTop,
            scrollTop = $(window).scrollTop(),
            pos = {},
            x = options.xOffset,
            y = 0;

        // Scroll is above the parent, remove pin inline styles
        if (scrollTop < pTop || scrollTop === 0) {
            if (this.pinned) {
                this.unpin();
            }

            return;
        }

        // Don't extend out the bottom
        var elementMaxPos = scrollTop + eHeight,
            parentMaxHeight = pHeight + pTop;

        // Swap positioning of the fixed menu once it reaches the parent borders
        if (options.fixed) {
            if (elementMaxPos >= parentMaxHeight) {
                y = 'auto';

                pos.position = 'absolute';
                pos.bottom = 0;

            } else if (scrollTop >= eTop) {
                y = options.yOffset;

                pos.position = 'fixed';
                pos.bottom = 'auto';
            }

        // Stop positioning absolute menu once it exits the parent
        } else {
            pos.position = 'absolute';

            if (elementMaxPos >= parentMaxHeight) {
                y += (pHeight - eHeight);

            } else {
                y += (scrollTop - pTop) + options.yOffset;
            }

            // Don't go lower than default top
            if (cssTop && y < cssTop) {
                y = cssTop;
            }
        }

        pos[options.location] = x;
        pos.top = y;

        this.element
            .css(pos)
            .addClass('is-pinned');

        this.pinned = true;

        this.fireEvent('pinned');
    }
851801
function () {
		var c = this.findCollapsibleItem();
		if (c) {
			//apply movedClass is needed
			if(this.movedClass && this.movedClass.length > 0 && !c.hasClass(this.movedClass)) {
				c.addClass(this.movedClass);
			}
			// passing null to add child to the front of the control list
			this.$.menu.addChild(c, null);
			var p = this.$.menu.hasNode();
			if (p && c.hasNode()) {
				c.insertNodeInParent(p);
			}
			return true;
		}
	}
696876
def set_menu(self, menu):
        ''''''
        self.menu = menu
        self.in_queue.put(MPImageMenu(menu))
969042
def prime_menu_items(self, menu_items):
        
        for item in menu_items:
            item = self._prime_menu_item(item)
            if item is not None:
                yield item
1154360
public function isActive($menuItem)
    {
        // If the `is_active` option is a callable object, return the result of this callable object with the current
        // item passed as the only argument
        if (is_callable(array_get($menuItem, 'is_active'))) {
            return (bool)call_user_func($menuItem['is_active'], $menuItem);
        }

        // If the `url_def` option is defined, use the `hieu-le/active` helper functions to calculate the result from
        // the keys and values of this array.
        if ($urlDef = array_get($menuItem, 'url_def')) {
            $result = true;
            foreach ($urlDef as $method => $value) {
                if (!is_array($value)) {
                    $result = $result && call_user_func("if_" . $method, [$value]);
                } else {
                    foreach ($value as $k => $v) {
                        $result = $result && call_user_func("if_" . $method, $k, $v);
                    }
                }
            }

            return $result;
        }

        // Default: not active
        return false;
    }
494398
public function menuActive()
    {
        $idParents = self::getIdParents();
        
        if (in_array( $this->id, $idParents ))
        {
            return true;
        }
        return false;
    }