Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-115484 As a developer I can declare theme's customizable CSS variables #17

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/apps/frontend-css/frontend-css-variables-api/bnd.bnd
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Bundle-Name: Liferay Frontend CSS Variables API
Bundle-SymbolicName: com.liferay.frontend.css.variables.api
Bundle-Version: 1.0.0
Export-Package: com.liferay.frontend.css.variables
Export-Package:\
com.liferay.frontend.css.variables,\
com.liferay.frontend.css.variables.theme
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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.
*/

package com.liferay.frontend.css.variables;

/**
* @author Iván Zaera Avellón
*/
public interface CSSVariableDescription {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a TokenDescription now...


public CSSVariableType getCSSVariableType();
Copy link

@jbalsas jbalsas Jun 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokenType or simply type?


public String getName();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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.
*/

package com.liferay.frontend.css.variables;

/**
* @author Iván Zaera Avellón
*/
public enum CSSVariableType {

COLOR, STRING

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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.
*/

package com.liferay.frontend.css.variables.theme;

import com.liferay.frontend.css.variables.CSSVariableDescription;
import com.liferay.portal.kernel.model.Theme;

import java.util.Map;

/**
* @author Iván Zaera Avellón
*/
public interface ThemeCSSVariableDescriptionsRegistry {

public Map<String, CSSVariableDescription> getCSSVariableDescriptions(
Theme theme);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version 1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ dependencies {
compileOnly project(":core:osgi-service-tracker-collections")
compileOnly project(":core:petra:petra-string")

testCompile group: "com.liferay.portal", name: "com.liferay.portal.impl", version: "default"
testCompile group: "junit", name: "junit", version: "4.12"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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.
*/

package com.liferay.frontend.css.variables.web.internal;

import com.liferay.frontend.css.variables.CSSVariableDescription;
import com.liferay.frontend.css.variables.CSSVariableType;

/**
* @author Iván Zaera Avellón
*/
public class CSSVariableDescriptionImpl implements CSSVariableDescription {

public CSSVariableDescriptionImpl(
CSSVariableType cssVariableType, String name) {

_cssVariableType = cssVariableType;
_name = name;
}

@Override
public CSSVariableType getCSSVariableType() {
return _cssVariableType;
}

@Override
public String getName() {
return _name;
}

private final CSSVariableType _cssVariableType;
private final String _name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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.
*/

package com.liferay.frontend.css.variables.web.internal.theme;

import com.liferay.frontend.css.variables.CSSVariableDescription;
import com.liferay.frontend.css.variables.CSSVariableType;
import com.liferay.frontend.css.variables.web.internal.CSSVariableDescriptionImpl;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONException;
import com.liferay.portal.kernel.json.JSONFactory;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.osgi.framework.Bundle;

/**
* @author Iván Zaera Avellón
*/
public class ThemeBundleInspector {

public static boolean isTheme(Bundle bundle) {
izaera marked this conversation as resolved.
Show resolved Hide resolved
boolean theme = false;

URL url = bundle.getResource("WEB-INF/liferay-look-and-feel.xml");

if (url != null) {
try (InputStream inputStream = url.openStream()) {
String xml = StringUtil.read(inputStream);

if (xml.contains("</theme>")) {
theme = true;
}
}
catch (IOException ioException) {
throw new RuntimeException(ioException);
}
}

return theme;
}

/**
* @throws IllegalArgumentException if the bundle is not a theme bundle
*/
public ThemeBundleInspector(Bundle bundle, JSONFactory jsonFactory)
throws IllegalArgumentException {

_bundle = bundle;
_jsonFactory = jsonFactory;

if (!isTheme(bundle)) {
throw new IllegalArgumentException(
StringBundler.concat(
"Bundle ", _bundle.getSymbolicName(), " is not a theme"));
}
}

public Map<String, CSSVariableDescription> getCSSVariableDescriptions()
throws JSONException {

JSONObject jsonObject = _parseJSONObject(_getTokensPath());

if (jsonObject == null) {
return null;
}

JSONArray variablesJSONArray = jsonObject.getJSONArray("variables");

if (variablesJSONArray == null) {
return null;
}

Map<String, CSSVariableDescription> cssVariableDescriptions =
new HashMap<>();

for (int i = 0; i < variablesJSONArray.length(); i++) {
JSONObject cssVariableDefinitionJSONObject =
variablesJSONArray.getJSONObject(i);

String name = cssVariableDefinitionJSONObject.getString("name");

cssVariableDescriptions.put(
name,
new CSSVariableDescriptionImpl(
_getCSSVariableType(cssVariableDefinitionJSONObject),
name));
}

if (cssVariableDescriptions.isEmpty()) {
return null;
}

return cssVariableDescriptions;
}

public String getSymbolicName() {
return _bundle.getSymbolicName();
}

private CSSVariableType _getCSSVariableType(JSONObject jsonObject) {
String type = jsonObject.getString("type");

if (type.equals("color")) {
return CSSVariableType.COLOR;
}

return CSSVariableType.STRING;
}

private String _getTokensPath() {
Locale defaultLocale = LocaleUtil.getDefault();

Dictionary<String, String> headers = _bundle.getHeaders(
defaultLocale.toString());

String tokensPath = headers.get("Tokens-Path");

if (Validator.isNull(tokensPath)) {
tokensPath = "WEB-INF/tokens.json";
}

if (tokensPath.charAt(0) == '/') {
tokensPath = tokensPath.substring(1);
}

return tokensPath;
}

private JSONObject _parseJSONObject(String fileName) throws JSONException {
URL resource = _bundle.getResource(fileName);

if (resource == null) {
return null;
}

try (InputStream is = resource.openStream()) {
return _jsonFactory.createJSONObject(StringUtil.read(is));
}
catch (IOException ioException) {
throw new JSONException("Unable to parse " + fileName, ioException);
}
}

private final Bundle _bundle;
private final JSONFactory _jsonFactory;

}
Loading