Applied theme to pages properly

New utility UnaryChain:
- Allows writing nested modifications as a chain
This commit is contained in:
Risto Lahtela 2020-03-10 11:20:33 +02:00
parent 77d33c991a
commit aca9d22e1f
8 changed files with 100 additions and 25 deletions

View File

@ -20,6 +20,8 @@ import com.djrapitops.plan.delivery.formatting.PlaceholderReplacer;
import com.djrapitops.plan.delivery.rendering.html.Contributors;
import com.djrapitops.plan.delivery.rendering.html.icon.Icon;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.utilities.java.UnaryChain;
import com.djrapitops.plan.version.VersionChecker;
/**
@ -35,30 +37,32 @@ public class ErrorMessagePage implements Page {
private final String errorMsg;
private final Locale locale;
private final Theme theme;
private final VersionChecker versionChecker;
public ErrorMessagePage(
String template, Icon icon, String errorTitle, String errorMsg,
Locale locale, VersionChecker versionChecker
Locale locale, Theme theme, VersionChecker versionChecker
) {
this.template = template;
this.icon = icon;
this.errorTitle = errorTitle;
this.errorMsg = errorMsg;
this.locale = locale;
this.theme = theme;
this.versionChecker = versionChecker;
}
public ErrorMessagePage(
String template, String errorTitle, String errorMsg,
VersionChecker versionChecker,
Locale locale) {
this(template, Icon.called("exclamation-circle").build(), errorTitle, errorMsg, locale, versionChecker);
Locale locale,
Theme theme) {
this(template, Icon.called("exclamation-circle").build(), errorTitle, errorMsg, locale, theme, versionChecker);
}
@Override
public String toHtml() {
PlaceholderReplacer placeholders = new PlaceholderReplacer();
placeholders.put("title", icon.toHtml() + " " + errorTitle);
placeholders.put("titleText", errorTitle);
@ -66,6 +70,10 @@ public class ErrorMessagePage implements Page {
placeholders.put("version", versionChecker.getUpdateButton().orElse(versionChecker.getCurrentVersionButton()));
placeholders.put("updateModal", versionChecker.getUpdateModal());
placeholders.put("contributors", Contributors.generateContributorHtml());
return locale.replaceLanguageInHtml(placeholders.apply(template));
return UnaryChain.of(template)
.chain(theme::replaceThemeColors)
.chain(placeholders::apply)
.chain(locale::replaceLanguageInHtml)
.apply();
}
}

View File

@ -31,6 +31,7 @@ import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.settings.theme.ThemeVal;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.utilities.java.UnaryChain;
import com.djrapitops.plan.version.VersionChecker;
import org.apache.commons.lang3.StringUtils;
@ -102,15 +103,18 @@ public class NetworkPage implements Page {
return new ServerPluginTabs(extensionData, formatters);
});
String html = locale.replaceLanguageInHtml(placeholders.apply(templateHtml));
String nav = JSONCache.getOrCacheString(DataID.EXTENSION_NAV, serverUUID, () -> pluginTabs.get().getNav());
String tabs = JSONCache.getOrCacheString(DataID.EXTENSION_TABS, serverUUID, () -> pluginTabs.get().getTabs());
placeholders = new PlaceholderReplacer();
placeholders.put("navPluginsTabs", nav);
placeholders.put("tabsPlugins", StringUtils.remove(tabs, "${backButton}"));
PlaceholderReplacer pluginPlaceholders = new PlaceholderReplacer();
pluginPlaceholders.put("navPluginsTabs", nav);
pluginPlaceholders.put("tabsPlugins", StringUtils.remove(tabs, "${backButton}"));
return placeholders.apply(html);
return UnaryChain.of(templateHtml)
.chain(theme::replaceThemeColors)
.chain(placeholders::apply)
.chain(locale::replaceLanguageInHtml)
.chain(pluginPlaceholders::apply)
.apply();
}
}

View File

@ -45,8 +45,6 @@ import java.util.*;
/**
* Factory for creating different {@link Page} objects.
* <p>
* TODO Set theme and locale stuff in here
*
* @author Rsl1122
*/
@ -101,7 +99,8 @@ public class PageFactory {
}
public PlayersPage playersPage() throws IOException {
return new PlayersPage(getResource("web/players.html"), versionChecker.get(), config.get(), locale.get(), serverInfo.get());
return new PlayersPage(getResource("web/players.html"), versionChecker.get(),
config.get(), locale.get(), theme.get(), serverInfo.get());
}
/**
@ -195,13 +194,13 @@ public class PageFactory {
public Page errorPage(String title, String error) throws IOException {
return new ErrorMessagePage(
getResource("web/error.html"), title, error,
versionChecker.get(), locale.get());
versionChecker.get(), locale.get(), theme.get());
}
public Page errorPage(Icon icon, String title, String error) throws IOException {
return new ErrorMessagePage(
getResource("web/error.html"), icon, title, error,
locale.get(), versionChecker.get());
locale.get(), theme.get(), versionChecker.get());
}
public String getResource(String name) throws IOException {

View File

@ -113,6 +113,6 @@ public class PlayerPage implements Page {
placeholders.put("navPluginsTabs", pluginTabs.getNav());
placeholders.put("pluginsTabs", pluginTabs.getTab());
return placeholders.apply(html);
return placeholders.apply(theme.replaceThemeColors(html));
}
}

View File

@ -23,6 +23,7 @@ import com.djrapitops.plan.settings.config.PlanConfig;
import com.djrapitops.plan.settings.config.paths.PluginSettings;
import com.djrapitops.plan.settings.config.paths.ProxySettings;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.version.VersionChecker;
/**
@ -36,6 +37,7 @@ public class PlayersPage implements Page {
private final VersionChecker versionChecker;
private final PlanConfig config;
private final Locale locale;
private final Theme theme;
private final ServerInfo serverInfo;
PlayersPage(
@ -43,12 +45,14 @@ public class PlayersPage implements Page {
VersionChecker versionChecker,
PlanConfig config,
Locale locale,
Theme theme,
ServerInfo serverInfo
) {
this.templateHtml = templateHtml;
this.versionChecker = versionChecker;
this.config = config;
this.locale = locale;
this.theme = theme;
this.serverInfo = serverInfo;
}
@ -65,6 +69,6 @@ public class PlayersPage implements Page {
placeholders.put("networkName", config.get(PluginSettings.SERVER_NAME));
}
return locale.replaceLanguageInHtml(placeholders.apply(templateHtml));
return locale.replaceLanguageInHtml(placeholders.apply(theme.replaceThemeColors(templateHtml)));
}
}

View File

@ -32,6 +32,7 @@ import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.theme.Theme;
import com.djrapitops.plan.settings.theme.ThemeVal;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.utilities.java.UnaryChain;
import com.djrapitops.plan.version.VersionChecker;
import java.util.List;
@ -97,15 +98,18 @@ public class ServerPage implements Page {
return new ServerPluginTabs(extensionData, formatters);
});
String html = locale.replaceLanguageInHtml(placeholders.apply(templateHtml));
String nav = JSONCache.getOrCacheString(DataID.EXTENSION_NAV, serverUUID, () -> pluginTabs.get().getNav());
String tabs = JSONCache.getOrCacheString(DataID.EXTENSION_TABS, serverUUID, () -> pluginTabs.get().getTabs());
placeholders = new PlaceholderReplacer();
placeholders.put("navPluginsTabs", nav);
placeholders.put("tabsPlugins", tabs);
PlaceholderReplacer pluginPlaceholders = new PlaceholderReplacer();
pluginPlaceholders.put("navPluginsTabs", nav);
pluginPlaceholders.put("tabsPlugins", tabs);
return placeholders.apply(html);
return UnaryChain.of(templateHtml)
.chain(theme::replaceThemeColors)
.chain(placeholders::apply)
.chain(locale::replaceLanguageInHtml)
.chain(pluginPlaceholders::apply)
.apply();
}
}

View File

@ -34,6 +34,7 @@ import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.queries.containers.ContainerFetchQueries;
import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.utilities.java.Maps;
import com.djrapitops.plan.utilities.java.UnaryChain;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -164,7 +165,10 @@ public class ResponseFactory {
public Response javaScriptResponse(String fileName) {
try {
String content = locale.replaceLanguageInJavascript(files.getCustomizableResourceOrDefault(fileName).asString());
String content = UnaryChain.of(files.getCustomizableResourceOrDefault(fileName).asString())
.chain(theme::replaceThemeColors)
.chain(locale::replaceLanguageInJavascript)
.apply();
return Response.builder()
.setMimeType(MimeType.JS)
.setContent(content)

View File

@ -0,0 +1,52 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.utilities.java;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* Utility for combining multiple UnaryOperator modifications.
*
* @author Rsl1122
*/
public class UnaryChain<T> {
private final T modifying;
private Function<T, T> modifier;
private UnaryChain(T modifying) {
this.modifying = modifying;
}
public static <V> UnaryChain<V> of(V modifying) {
return new UnaryChain<>(modifying);
}
public UnaryChain<T> chain(UnaryOperator<T> operation) {
if (modifier == null) {
modifier = operation;
} else {
modifier = modifier.andThen(operation);
}
return this;
}
public T apply() {
return modifier.apply(modifying);
}
}