001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.causeway.viewer.commons.applib.services.menu.model;
020
021import java.util.Locale;
022
023import org.springframework.lang.Nullable;
024
025import org.apache.causeway.applib.annotation.DomainServiceLayout;
026import org.apache.causeway.commons.collections.Can;
027import org.apache.causeway.viewer.commons.applib.services.menu.MenuVisitor;
028
029import lombok.val;
030
031public record NavbarSection(
032        DomainServiceLayout.MenuBar menuBarSelect,
033        Can<MenuDropdown> topLevelEntries) {
034
035    public String cssClass() {
036        return menuBarSelect.name().toLowerCase(Locale.ENGLISH);
037    }
038
039    /**
040     * Depth-first visit of the model.
041     * @param menuVisitor
042     */
043    public void visitMenuItems(final @Nullable MenuVisitor menuVisitor) {
044        if(menuVisitor==null) return;
045
046        topLevelEntries.forEach(topLevel->{
047            menuVisitor.onTopLevel(topLevel);
048            topLevel.subEntries().forEach(subEntry->{
049                val asAction = subEntry.asAction();
050                asAction.ifPresentOrElse(menuVisitor::onMenuAction, ()->{
051                    val asSpacer = subEntry.asSpacer();
052                    asSpacer.ifPresent(spacer->{
053                        if(spacer.isEmpty()) {
054                            menuVisitor.onSectionSpacer();
055                        } else {
056                            menuVisitor.onSectionLabel(spacer.label());
057                        }
058                    });
059                });
060
061            });
062        });
063
064    }
065
066}