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-115485 As a developer I can inject custom CSS variables declaring an OSGi component #21

Closed
wants to merge 8 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
Empty file.
4 changes: 4 additions & 0 deletions modules/apps/frontend-css/frontend-css-variables-api/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies {
compileOnly group: "com.liferay", name: "biz.aQute.bnd.annotation", version: "4.2.0.LIFERAY-PATCHED-1"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "default"
compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.1"
compileOnly group: "org.apache.felix", name: "org.apache.felix.http.servlet-api", version: "1.1.2"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
compileOnly group: "org.osgi", name: "osgi.core", version: "6.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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;

import java.util.Map;

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

public Map<String, String> getCSSVariables();

public String getScope();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

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

public Collection<ScopedCSSVariables> getScopedCSSVariablesCollection(
HttpServletRequest httpServletRequest);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version 1.0.0
Empty file.
4 changes: 4 additions & 0 deletions modules/apps/frontend-css/frontend-css-variables-web/bnd.bnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Bundle-Name: Liferay Frontend CSS Variables Web
Bundle-SymbolicName: com.liferay.frontend.css.variables.web
Bundle-Version: 1.0.0
Web-ContextPath: /frontend-css-variables-web
15 changes: 15 additions & 0 deletions modules/apps/frontend-css/frontend-css-variables-web/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dependencies {
compileOnly group: "com.liferay", name: "biz.aQute.bnd.annotation", version: "4.2.0.LIFERAY-PATCHED-1"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "default"
compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.1"
compileOnly group: "org.apache.felix", name: "org.apache.felix.http.servlet-api", version: "1.1.2"
compileOnly group: "org.osgi", name: "org.osgi.service.component.annotations", version: "1.3.0"
compileOnly group: "org.osgi", name: "osgi.core", version: "6.0.0"
compileOnly project(":apps:configuration-admin:configuration-admin-api")
compileOnly project(":apps:frontend-css:frontend-css-variables-api")
compileOnly project(":apps:static:portal-configuration:portal-configuration-metatype-api")
compileOnly project(":core:osgi-service-tracker-collections")
compileOnly project(":core:petra:petra-string")

testCompile group: "junit", name: "junit", version: "4.12"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* 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.servlet.taglib;

import com.liferay.frontend.css.variables.ScopedCSSVariables;
import com.liferay.frontend.css.variables.ScopedCSSVariablesProvider;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerList;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerListFactory;
import com.liferay.osgi.service.tracker.collections.map.PropertyServiceReferenceComparator;
import com.liferay.portal.kernel.servlet.taglib.BaseDynamicInclude;
import com.liferay.portal.kernel.servlet.taglib.DynamicInclude;

import java.io.IOException;
import java.io.PrintWriter;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

/**
* @author Iván Zaera Avellón
*/
@Component(service = DynamicInclude.class)
public class ScopedCSSVariablesTopHeadDynamicInclude
extends BaseDynamicInclude {

@Override
public void include(
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, String dynamicIncludeKey)
throws IOException {

PrintWriter printWriter = httpServletResponse.getWriter();

printWriter.print("<style data-senna-track=\"temporary\" ");
printWriter.print("type=\"text/css\">\n");

for (ScopedCSSVariablesProvider scopedCSSVariablesProvider :
_scopedCSSVariablesProviders) {

_writeCSSVariables(
printWriter,
scopedCSSVariablesProvider.getScopedCSSVariablesCollection(
httpServletRequest));
}

printWriter.print("</style>\n");
}

@Override
public void register(DynamicIncludeRegistry dynamicIncludeRegistry) {
dynamicIncludeRegistry.register(
"/html/common/themes/top_head.jsp#post");
izaera marked this conversation as resolved.
Show resolved Hide resolved
}

@Activate
protected void activate(BundleContext bundleContext) {
_scopedCssVariablesProviderServiceTrackerList =
ServiceTrackerListFactory.open(
bundleContext, ScopedCSSVariablesProvider.class,
new PropertyServiceReferenceComparator<>("service.ranking"));

setScopedCSSVariablesProviders(
_scopedCssVariablesProviderServiceTrackerList);
}

@Deactivate
protected void deactivate() {
setScopedCSSVariablesProviders(null);

_scopedCssVariablesProviderServiceTrackerList.close();

_scopedCssVariablesProviderServiceTrackerList = null;
}

protected void setScopedCSSVariablesProviders(
Iterable<ScopedCSSVariablesProvider> scopedCSSVariablesProviders) {

_scopedCSSVariablesProviders = scopedCSSVariablesProviders;
}

private void _writeCSSVariables(
PrintWriter printWriter,
Collection<ScopedCSSVariables> scopedCSSVariablesCollection) {

for (ScopedCSSVariables scopedCSSVariables :
scopedCSSVariablesCollection) {

printWriter.print(scopedCSSVariables.getScope());
printWriter.print(" {\n");

Map<String, String> cssVariables =
scopedCSSVariables.getCSSVariables();

for (Map.Entry<String, String> entry : cssVariables.entrySet()) {
printWriter.print("--");
printWriter.print(entry.getKey());
printWriter.print(": ");
printWriter.print(entry.getValue());
printWriter.print(";\n");
}

printWriter.print("}\n");
}
}

private Iterable<ScopedCSSVariablesProvider> _scopedCSSVariablesProviders;
private ServiceTrackerList
<ScopedCSSVariablesProvider, ScopedCSSVariablesProvider>
_scopedCssVariablesProviderServiceTrackerList;

}
Loading