Skip to content

Commit

Permalink
Basic rtl (#997)
Browse files Browse the repository at this point in the history
* RTL for vanilla bootstrap is added

* RTL support for bootswatch was added

(cherry picked from commit c1fe914)
  • Loading branch information
solomax authored and martin-g committed Aug 1, 2023
1 parent 7398ba8 commit fcec5f0
Show file tree
Hide file tree
Showing 159 changed files with 348,881 additions and 14,941 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import java.util.Locale;

import org.apache.wicket.Session;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.head.HeaderItem;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.html.TransparentWebMarkupContainer;
import org.apache.wicket.protocol.http.WebSession;

import de.agilecoders.wicket.core.Bootstrap;
import de.agilecoders.wicket.core.markup.html.references.ModernizrJavaScriptReference;
Expand Down Expand Up @@ -46,6 +48,7 @@
*/
public class HtmlTag extends TransparentWebMarkupContainer {
private static final long serialVersionUID = 1L;
private final boolean isRtl;
private final boolean useModernizr;
private final Locale locale;

Expand All @@ -61,6 +64,7 @@ public HtmlTag(final String markupId, final Locale locale, final boolean useMode

this.locale = locale;
this.useModernizr = useModernizr;
this.isRtl = locale == null ? false : Session.isRtlLanguage(locale);
}

/**
Expand Down Expand Up @@ -114,6 +118,9 @@ protected void onComponentTag(final ComponentTag tag) {
if (locale != null) {
tag.put("lang", toAttributeValue(locale));
}
if (isRtl) {
tag.put("dir", "rtl");
}

final CssClassNames.Builder cssClassNames = CssClassNames.newBuilder();
if (useModernizr) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.agilecoders.wicket.core.markup.html.themes.bootstrap;

import de.agilecoders.wicket.webjars.request.resource.WebjarsCssResourceReference;

/**
* #### Description
*
* The RTL (right-to-left) bootstrap css. To update the bootstrap version add the following dependency to
* your maven `dependencyManagement` section:
*
* ```xml
* <dependency>
* <groupId>org.webjars</groupId>
* <artifactId>bootstrap</artifactId>
* <version>your-bootstrap-version</version>
* </dependency>
* ```
*
* The css resource will be loaded by wicket-webjars.
*
* @author Michael Haitz <michael.haitz@agilecoders.de>
*/
public class BootstrapCssRtlReference extends WebjarsCssResourceReference {
private static final long serialVersionUID = 1L;

/**
* Singleton instance of this reference
*/
private static final class Holder {
private static final BootstrapCssRtlReference INSTANCE = new BootstrapCssRtlReference();
}

/**
* Normally you should not use this method, but use
* {@link de.agilecoders.wicket.core.settings.IBootstrapSettings#getCssResourceReference()} ()} to prevent version conflicts.
*
* @return the single instance of the resource reference
*/
public static BootstrapCssRtlReference instance() {
return Holder.INSTANCE;
}

/**
* Private constructor.
*/
private BootstrapCssRtlReference() {
super("/bootstrap/current/css/bootstrap.rtl.css");
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.agilecoders.wicket.core.settings;

import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.request.resource.UrlResourceReference;

import de.agilecoders.wicket.core.markup.html.references.BootstrapJavaScriptReference;
import de.agilecoders.wicket.core.markup.html.references.ModernizrJavaScriptReference;
import de.agilecoders.wicket.core.markup.html.themes.bootstrap.BootstrapCssReference;
import de.agilecoders.wicket.core.markup.html.themes.bootstrap.BootstrapCssRtlReference;

/**
* #### Description
Expand All @@ -22,11 +24,13 @@ private static final class Holder {
private static ResourceReference bootstrapJavaScriptReference = BootstrapJavaScriptReference.instance();
private static ResourceReference modernizrJavaScriptReference = ModernizrJavaScriptReference.instance();
private static ResourceReference bootstrapCssReference = BootstrapCssReference.instance();
private static ResourceReference bootstrapCssRtlReference = BootstrapCssRtlReference.instance();
}

private ResourceReference bootstrapJavaScriptReference = null;
private ResourceReference modernizrJavaScriptReference = null;
private ResourceReference bootstrapCssReference = null;
private ResourceReference bootstrapCssRtlReference = null;

private ThemeProvider themeProvider;
private ActiveThemeProvider activeThemeProvider;
Expand Down Expand Up @@ -88,14 +92,19 @@ public boolean autoAppendResources() {
public ResourceReference getCssResourceReference() {
ResourceReference ref;

final boolean isRtl = WebSession.exists() && WebSession.get().isRtlLocale();

if (useCdnResources()) {
String cdnUrl = String.format(CSS_CDN_PATTERN, getVersion());
String cdnUrl = String.format(isRtl ? CSS_RTL_CDN_PATTERN : CSS_CDN_PATTERN, getVersion());
ref = new UrlResourceReference(Url.parse(cdnUrl));
} else {
ref = bootstrapCssReference;
ref = isRtl ? bootstrapCssRtlReference : bootstrapCssReference;
}
if (ref == null) {
ref = isRtl ? Holder.bootstrapCssRtlReference : Holder.bootstrapCssReference;
}

return ref != null ? ref : Holder.bootstrapCssReference;
return ref;
}

@Override
Expand Down Expand Up @@ -160,6 +169,12 @@ public IBootstrapSettings setCssResourceReference(ResourceReference reference) {
return this;
}

@Override
public IBootstrapSettings setCssRtlResourceReference(ResourceReference reference) {
bootstrapCssRtlReference = reference;
return this;
}

@Override
public BootstrapSettings setJsResourceReference(final ResourceReference bootstrapJavaScriptReference) {
this.bootstrapJavaScriptReference = bootstrapJavaScriptReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public interface IBootstrapSettings {
*/
String CSS_CDN_PATTERN = "//stackpath.bootstrapcdn.com/bootstrap/%s/css/bootstrap.min.css";

/**
* The url to the right-to-left css resource at a CDN network
*/
String CSS_RTL_CDN_PATTERN = "//stackpath.bootstrapcdn.com/bootstrap/%s/css/bootstrap.rtl.min.css";

/**
* @param version The version of Bootstrap. CDN resources use it to construct their urls
* @return same instance for chaining
Expand Down Expand Up @@ -90,6 +95,13 @@ public interface IBootstrapSettings {
*/
IBootstrapSettings setCssResourceReference(ResourceReference reference);

/**
* @param reference a reference to the RTL (right-to-left) bootstrap css library.
* Defaults to the embedded bootstrap.rtl.css
* @return same instance for chaining
*/
IBootstrapSettings setCssRtlResourceReference(ResourceReference reference);

/**
* @param reference a reference to the base bootstrap JavaScript library.
* Defaults to the embedded bootstrap.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.agilecoders.wicket.core.settings;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.head.CssHeaderItem;
Expand Down Expand Up @@ -44,7 +43,7 @@ public String name() {

@Override
public List<HeaderItem> getDependencies() {
return Collections.emptyList();
return List.of();
}

@Override
Expand All @@ -54,7 +53,7 @@ public void renderHead(IHeaderResponse response) {

@Override
public Iterable<String> getCdnUrls() {
return Collections.emptyList();
return List.of();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.apache.wicket.request.resource.UrlResourceReference;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -97,7 +96,7 @@ private boolean useCdnResources() {

@Override
public Iterable<String> getCdnUrls() {
return Collections.emptyList();
return List.of();
}

private void renderPackageReferences(IHeaderResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

Expand Down Expand Up @@ -62,7 +63,7 @@ abstract class BasePage extends GenericWebPage<Void> {
public BasePage(final PageParameters parameters) {
super(parameters);

add(new HtmlTag("html"));
add(new HtmlTag("html", WebSession.get().getLocale()));
MobileViewportMetaTag mvt = new MobileViewportMetaTag("viewport");
mvt.setWidth("device-width");
mvt.setInitialScale("1");
Expand Down
Loading

0 comments on commit fcec5f0

Please sign in to comment.