From 9a538b548ca6f833a58be446ab9774c9451c5768 Mon Sep 17 00:00:00 2001 From: Artur Date: Wed, 22 Jun 2016 10:28:10 +0300 Subject: [PATCH] Load @HTMLImport before @JavaScript (#1038) Typically HTML imports do not have external dependencies but define their dependencies inside the HTML. On the other hand, there are situations where you want to load an extension/plugin for an HTML import (web component) after the HTML import has been loaded. To order is therefore changed to HTML first, JS later. To load HTML/JS in a predefined order, use Page.addHtmlImport/Page.addJavaScript which will load the files in the order the methods are called. --- .../main/java/com/vaadin/ui/UIInternals.java | 8 +-- .../server/communication/UidlWriterTest.java | 56 +++++++++---------- .../test-files/html/htmlimport4-js.js | 2 + .../hummingbird/uitest/ui/DependencyView.java | 2 + .../hummingbird/uitest/ui/DependencyIT.java | 2 + 5 files changed, 36 insertions(+), 34 deletions(-) create mode 100644 hummingbird-tests/test-resources/src/main/resources/META-INF/resources/test-files/html/htmlimport4-js.js diff --git a/hummingbird-server/src/main/java/com/vaadin/ui/UIInternals.java b/hummingbird-server/src/main/java/com/vaadin/ui/UIInternals.java index 78f395962d6..716b0823b4c 100644 --- a/hummingbird-server/src/main/java/com/vaadin/ui/UIInternals.java +++ b/hummingbird-server/src/main/java/com/vaadin/ui/UIInternals.java @@ -651,14 +651,14 @@ public DependencyList getDependencyList() { public void addComponentDependencies( Class componentClass) { Page page = ui.getPage(); - List javaScripts = AnnotationReader - .getJavaScriptAnnotations(componentClass); - javaScripts.forEach(js -> page.addJavaScript(js.value())); - List htmlImports = AnnotationReader .getHtmlImportAnnotations(componentClass); htmlImports.forEach(html -> page.addHtmlImport(html.value())); + List javaScripts = AnnotationReader + .getJavaScriptAnnotations(componentClass); + javaScripts.forEach(js -> page.addJavaScript(js.value())); + List styleSheets = AnnotationReader .getStyleSheetAnnotations(componentClass); styleSheets diff --git a/hummingbird-server/src/test/java/com/vaadin/server/communication/UidlWriterTest.java b/hummingbird-server/src/test/java/com/vaadin/server/communication/UidlWriterTest.java index 4f6bee6f753..43f34be07c0 100644 --- a/hummingbird-server/src/test/java/com/vaadin/server/communication/UidlWriterTest.java +++ b/hummingbird-server/src/test/java/com/vaadin/server/communication/UidlWriterTest.java @@ -177,30 +177,25 @@ private void addInitialComponentDependencies(UI ui, UidlWriter uidlWriter) { DependencyList.TYPE_JAVASCRIPT); assertDependency("UI-", dependencies.get(idx++), DependencyList.TYPE_JAVASCRIPT); - assertDependency("super-", dependencies.get(idx++), - DependencyList.TYPE_JAVASCRIPT); - - assertDependency("anotherinterface-", dependencies.get(idx++), - DependencyList.TYPE_JAVASCRIPT); - - assertDependency("interface-", dependencies.get(idx++), - DependencyList.TYPE_JAVASCRIPT); - - assertDependency("", dependencies.get(idx++), - DependencyList.TYPE_JAVASCRIPT); assertDependency("super-", dependencies.get(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("anotherinterface-", dependencies.get(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("interface-", dependencies.get(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("", dependencies.get(idx++), DependencyList.TYPE_HTML_IMPORT); + assertDependency("super-", dependencies.get(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("anotherinterface-", dependencies.get(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("interface-", dependencies.get(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("", dependencies.get(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("super-", dependencies.get(idx++), DependencyList.TYPE_STYLESHEET); @@ -229,30 +224,31 @@ public void testComponentInterfaceDependencies() { JsonArray dependencies = response .getArray(DependencyList.DEPENDENCY_KEY); + int idx = 0; Assert.assertEquals(12, dependencies.length()); - assertDependency("childinterface1-", dependencies.getObject(0), - DependencyList.TYPE_JAVASCRIPT); - assertDependency("childinterface2-", dependencies.getObject(1), - DependencyList.TYPE_JAVASCRIPT); - assertDependency("child1-", dependencies.getObject(2), - DependencyList.TYPE_JAVASCRIPT); - assertDependency("child2-", dependencies.getObject(3), - DependencyList.TYPE_JAVASCRIPT); - assertDependency("childinterface1-", dependencies.getObject(4), + assertDependency("childinterface1-", dependencies.getObject(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("childinterface2-", dependencies.getObject(5), + assertDependency("childinterface2-", dependencies.getObject(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("child1-", dependencies.getObject(6), + assertDependency("child1-", dependencies.getObject(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("child2-", dependencies.getObject(7), + assertDependency("child2-", dependencies.getObject(idx++), DependencyList.TYPE_HTML_IMPORT); - assertDependency("childinterface1-", dependencies.getObject(8), + assertDependency("childinterface1-", dependencies.getObject(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("childinterface2-", dependencies.getObject(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("child1-", dependencies.getObject(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("child2-", dependencies.getObject(idx++), + DependencyList.TYPE_JAVASCRIPT); + assertDependency("childinterface1-", dependencies.getObject(idx++), DependencyList.TYPE_STYLESHEET); - assertDependency("childinterface2-", dependencies.getObject(9), + assertDependency("childinterface2-", dependencies.getObject(idx++), DependencyList.TYPE_STYLESHEET); - assertDependency("child1-", dependencies.getObject(10), + assertDependency("child1-", dependencies.getObject(idx++), DependencyList.TYPE_STYLESHEET); - assertDependency("child2-", dependencies.getObject(11), + assertDependency("child2-", dependencies.getObject(idx++), DependencyList.TYPE_STYLESHEET); } diff --git a/hummingbird-tests/test-resources/src/main/resources/META-INF/resources/test-files/html/htmlimport4-js.js b/hummingbird-tests/test-resources/src/main/resources/META-INF/resources/test-files/html/htmlimport4-js.js new file mode 100644 index 00000000000..17b3b6f2974 --- /dev/null +++ b/hummingbird-tests/test-resources/src/main/resources/META-INF/resources/test-files/html/htmlimport4-js.js @@ -0,0 +1,2 @@ +// This should always be loaded after htmlimport4.html +logMessage("HTML import 4 companion JS loaded"); diff --git a/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/DependencyView.java b/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/DependencyView.java index 0c2f261851c..9aa5a2ed5ff 100644 --- a/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/DependencyView.java +++ b/hummingbird-tests/test-root-context/src/main/java/com/vaadin/hummingbird/uitest/ui/DependencyView.java @@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets; import com.vaadin.annotations.HtmlImport; +import com.vaadin.annotations.JavaScript; import com.vaadin.annotations.Tag; import com.vaadin.hummingbird.html.Button; import com.vaadin.hummingbird.html.Div; @@ -41,6 +42,7 @@ public class DependencyView extends AbstractDivView { private StreamResourceRegistration htmlMixedImport2; @Tag("div") + @JavaScript("/test-files/html/htmlimport4-js.js") @HtmlImport("/test-files/html/htmlimport4.html") static class HtmlComponent extends Component implements HasText { diff --git a/hummingbird-tests/test-root-context/src/test/java/com/vaadin/hummingbird/uitest/ui/DependencyIT.java b/hummingbird-tests/test-root-context/src/test/java/com/vaadin/hummingbird/uitest/ui/DependencyIT.java index b8c30663397..d0e0d1424f6 100644 --- a/hummingbird-tests/test-root-context/src/test/java/com/vaadin/hummingbird/uitest/ui/DependencyIT.java +++ b/hummingbird-tests/test-root-context/src/test/java/com/vaadin/hummingbird/uitest/ui/DependencyIT.java @@ -73,6 +73,8 @@ public void htmlInjection() { messages.get(0)); Assert.assertEquals("Messagehandler initialized in HTML import 4", messages.get(1)); + Assert.assertEquals("HTML import 4 companion JS loaded", + messages.get(2)); // Inject html findElementById("loadHtml").click();