Skip to content

Commit

Permalink
Load @HTMLImport before @javascript (#1038)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Artur- authored Jun 22, 2016
1 parent 4de8bcc commit 9a538b5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -651,14 +651,14 @@ public DependencyList getDependencyList() {
public void addComponentDependencies(
Class<? extends Component> componentClass) {
Page page = ui.getPage();
List<JavaScript> javaScripts = AnnotationReader
.getJavaScriptAnnotations(componentClass);
javaScripts.forEach(js -> page.addJavaScript(js.value()));

List<HtmlImport> htmlImports = AnnotationReader
.getHtmlImportAnnotations(componentClass);
htmlImports.forEach(html -> page.addHtmlImport(html.value()));

List<JavaScript> javaScripts = AnnotationReader
.getJavaScriptAnnotations(componentClass);
javaScripts.forEach(js -> page.addJavaScript(js.value()));

List<StyleSheet> styleSheets = AnnotationReader
.getStyleSheetAnnotations(componentClass);
styleSheets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This should always be loaded after htmlimport4.html
logMessage("HTML import 4 companion JS loaded");
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9a538b5

Please sign in to comment.