mirror of https://github.com/halo-dev/halo
feat: add setting item for primary menu selector (#2667)
parent
8d6838a956
commit
a2bb3d4a00
|
@ -81,4 +81,10 @@ public class SystemSetting {
|
||||||
Boolean requireReviewForNew;
|
Boolean requireReviewForNew;
|
||||||
Boolean systemUserOnly;
|
Boolean systemUserOnly;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Menu {
|
||||||
|
public static final String GROUP = "menu";
|
||||||
|
public String primary;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,5 +12,5 @@ public interface MenuFinder {
|
||||||
|
|
||||||
MenuVo getByName(String name);
|
MenuVo getByName(String name);
|
||||||
|
|
||||||
MenuVo getDefault();
|
MenuVo getPrimary();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,15 @@ import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.comparator.Comparators;
|
import org.springframework.util.comparator.Comparators;
|
||||||
import run.halo.app.core.extension.Menu;
|
import run.halo.app.core.extension.Menu;
|
||||||
import run.halo.app.core.extension.MenuItem;
|
import run.halo.app.core.extension.MenuItem;
|
||||||
import run.halo.app.extension.ReactiveExtensionClient;
|
import run.halo.app.extension.ReactiveExtensionClient;
|
||||||
|
import run.halo.app.infra.SystemConfigurableEnvironmentFetcher;
|
||||||
|
import run.halo.app.infra.SystemSetting;
|
||||||
import run.halo.app.theme.finders.Finder;
|
import run.halo.app.theme.finders.Finder;
|
||||||
import run.halo.app.theme.finders.MenuFinder;
|
import run.halo.app.theme.finders.MenuFinder;
|
||||||
import run.halo.app.theme.finders.vo.MenuItemVo;
|
import run.halo.app.theme.finders.vo.MenuItemVo;
|
||||||
|
@ -26,13 +30,11 @@ import run.halo.app.theme.finders.vo.MenuVo;
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@Finder("menuFinder")
|
@Finder("menuFinder")
|
||||||
|
@AllArgsConstructor
|
||||||
public class MenuFinderImpl implements MenuFinder {
|
public class MenuFinderImpl implements MenuFinder {
|
||||||
|
|
||||||
private final ReactiveExtensionClient client;
|
private final ReactiveExtensionClient client;
|
||||||
|
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
|
||||||
public MenuFinderImpl(ReactiveExtensionClient client) {
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MenuVo getByName(String name) {
|
public MenuVo getByName(String name) {
|
||||||
|
@ -43,14 +45,19 @@ public class MenuFinderImpl implements MenuFinder {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MenuVo getDefault() {
|
public MenuVo getPrimary() {
|
||||||
List<MenuVo> menuVos = listAsTree();
|
List<MenuVo> menuVos = listAsTree();
|
||||||
if (CollectionUtils.isEmpty(menuVos)) {
|
if (CollectionUtils.isEmpty(menuVos)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// TODO If there are multiple groups of menus,
|
return environmentFetcher.fetch(SystemSetting.Menu.GROUP, SystemSetting.Menu.class)
|
||||||
// return the first as the default, and consider optimizing it later
|
.blockOptional()
|
||||||
return menuVos.get(0);
|
.map(SystemSetting.Menu::getPrimary)
|
||||||
|
.filter(StringUtils::isNotBlank)
|
||||||
|
.flatMap(primary -> menuVos.stream()
|
||||||
|
.filter(menuVo -> menuVo.getMetadata().getName().equals(primary))
|
||||||
|
.findAny())
|
||||||
|
.orElse(menuVos.get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<MenuVo> listAll() {
|
List<MenuVo> listAll() {
|
||||||
|
|
|
@ -19,3 +19,7 @@ data:
|
||||||
"globalHead": "",
|
"globalHead": "",
|
||||||
"footer": ""
|
"footer": ""
|
||||||
}
|
}
|
||||||
|
menu: |
|
||||||
|
{
|
||||||
|
"primary": "primary"
|
||||||
|
}
|
|
@ -7,9 +7,9 @@ import static org.mockito.ArgumentMatchers.eq;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
@ -34,13 +34,9 @@ class MenuFinderImplTest {
|
||||||
@Mock
|
@Mock
|
||||||
private ReactiveExtensionClient client;
|
private ReactiveExtensionClient client;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
private MenuFinderImpl menuFinder;
|
private MenuFinderImpl menuFinder;
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
menuFinder = new MenuFinderImpl(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void listAsTree() {
|
void listAsTree() {
|
||||||
Tuple2<List<Menu>, List<MenuItem>> tuple = testTree();
|
Tuple2<List<Menu>, List<MenuItem>> tuple = testTree();
|
||||||
|
|
Loading…
Reference in New Issue