summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJohn Ahlroos <john@vaadin.com>2013-03-06 11:23:48 +0200
committerVaadin Code Review <review@vaadin.com>2013-03-13 08:05:24 +0000
commit41798f820c60c43c89d8fb480821d8bda4c42bfb (patch)
treec31d1e1da2b39ff215c185ce2064611d4a62549f /server
parent28aa793d8ede78aef0b458d6362b1475288d6495 (diff)
downloadvaadin-framework-41798f820c60c43c89d8fb480821d8bda4c42bfb.tar.gz
vaadin-framework-41798f820c60c43c89d8fb480821d8bda4c42bfb.zip
Implemented injection of css styles #5500
Change-Id: Iaccffb4a3e137968d5f51672cdd56f803a9e9d2e
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/server/Page.java128
1 files changed, 127 insertions, 1 deletions
diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java
index 8737b478c3..f7b65a8e9b 100644
--- a/server/src/com/vaadin/server/Page.java
+++ b/server/src/com/vaadin/server/Page.java
@@ -303,6 +303,115 @@ public class Page implements Serializable {
}
}
+ /**
+ * Contains dynamically injected styles injected in the HTML document at
+ * runtime.
+ *
+ * @since 7.1
+ */
+ public static class StyleSheet implements Serializable {
+
+ /*
+ * Points to last injected string injection
+ */
+ private int injectedStringPointer;
+
+ private final List<String> stringInjections = new LinkedList<String>();
+
+ /*
+ * Points to last injected resource injection
+ */
+ private int injectedResourcesPointer;
+
+ private final List<Resource> resourceInjections = new LinkedList<Resource>();
+
+ private final UI ui;
+
+ private StyleSheet(UI ui) {
+ this.ui = ui;
+ }
+
+ /**
+ * Injects a raw CSS string into the page.
+ *
+ * @param css
+ * The CSS to inject
+ */
+ public void inject(String css) {
+ if (css == null) {
+ throw new IllegalArgumentException(
+ "Cannot inject null CSS string");
+ }
+
+ /*
+ * Check if last injection was the same, in that case ignore it.
+ */
+ if (!stringInjections.isEmpty() && injectedStringPointer > 0) {
+ String lastInjection = stringInjections
+ .get(injectedStringPointer - 1);
+ if (lastInjection.equals(css.trim())) {
+ return;
+ }
+ }
+
+ stringInjections.add(css.trim());
+ ui.markAsDirty();
+ }
+
+ /**
+ * Injects a CSS resource into the page
+ *
+ * @param resource
+ * The resource to inject.
+ */
+ public void inject(Resource resource) {
+ if (resource == null) {
+ throw new IllegalArgumentException(
+ "Cannot inject null resource");
+ }
+
+ resourceInjections.add(resource);
+ ui.markAsDirty();
+ }
+
+ private void paint(PaintTarget target) throws PaintException {
+
+ // If full repaint repaint all injections
+ if (target.isFullRepaint()) {
+ injectedStringPointer = 0;
+ injectedResourcesPointer = 0;
+ }
+
+ target.startTag("css-injections");
+
+ // Paint pending string injections
+ List<String> injections = stringInjections.subList(
+ injectedStringPointer, stringInjections.size());
+
+ for (String css : injections) {
+ target.startTag("css-string");
+ target.addText(css);
+ target.endTag("css-string");
+ }
+
+ injectedStringPointer = stringInjections.size();
+
+ // Paint pending resource injections
+ List<Resource> resInjections = resourceInjections.subList(
+ injectedResourcesPointer, resourceInjections.size());
+
+ for (Resource res : resInjections) {
+ target.startTag("css-resource");
+ target.addAttribute("url", res);
+ target.endTag("css-resource");
+ }
+
+ target.endTag("css-injections");
+
+ injectedResourcesPointer = resourceInjections.size();
+ }
+ }
+
private EventRouter eventRouter;
private final UI uI;
@@ -312,6 +421,8 @@ public class Page implements Serializable {
private JavaScript javaScript;
+ private StyleSheet styleSheet;
+
/**
* The current browser location.
*/
@@ -576,10 +687,22 @@ public class Page implements Serializable {
javaScript = new JavaScript();
javaScript.extend(uI);
}
-
return javaScript;
}
+ /**
+ * Returns that stylesheet associated with this Page. The stylesheet
+ * contains additional styles injected at runtime into the HTML document.
+ *
+ * @since 7.1
+ */
+ public StyleSheet getStyleSheet() {
+ if (styleSheet == null) {
+ styleSheet = new StyleSheet(uI);
+ }
+ return styleSheet;
+ }
+
public void paintContent(PaintTarget target) throws PaintException {
if (!openList.isEmpty()) {
for (final Iterator<OpenResource> i = openList.iterator(); i
@@ -637,6 +760,9 @@ public class Page implements Serializable {
location.toString());
}
+ if (styleSheet != null) {
+ styleSheet.paint(target);
+ }
}
/**