use of elemental.html.LinkElement in project flow by vaadin.
the class ResourceLoader method loadHtml.
/**
* Loads an HTML import and notify a listener when the HTML import is
* loaded. Calling this method when the HTML import is currently loading or
* already loaded doesn't cause the HTML import to be loaded again, but the
* listener will still be notified when appropriate.
*
* @param htmlUrl
* url of HTML import to load
* @param resourceLoadListener
* listener to notify when the HTML import is loaded
* @param async
* loads the import asynchronously, if {@code true},
* synchronously otherwise
*/
public void loadHtml(final String htmlUrl, final ResourceLoadListener resourceLoadListener, boolean async) {
final String url = WidgetUtil.getAbsoluteUrl(htmlUrl);
ResourceLoadEvent event = new ResourceLoadEvent(this, url);
if (loadedResources.has(url)) {
if (resourceLoadListener != null) {
resourceLoadListener.onLoad(event);
}
return;
}
if (addListener(url, resourceLoadListener, loadListeners)) {
LinkElement linkTag = getDocument().createLinkElement();
linkTag.setAttribute("rel", "import");
linkTag.setAttribute("href", url);
if (async) {
linkTag.setAttribute("async", "true");
}
HtmlLoadListener listener = new HtmlLoadListener(event);
addOnloadHandler(linkTag, listener, event);
getHead().appendChild(linkTag);
if (supportsHtmlWhenReady) {
addHtmlImportsReadyHandler(listener);
}
}
}
use of elemental.html.LinkElement in project flow by vaadin.
the class ResourceLoader method loadStylesheet.
/**
* Load a stylesheet and notify a listener when the stylesheet is loaded.
* Calling this method when the stylesheet is currently loading or already
* loaded doesn't cause the stylesheet to be loaded again, but the listener
* will still be notified when appropriate.
*
* @param stylesheetUrl
* the url of the stylesheet to load
* @param resourceLoadListener
* the listener that will get notified when the stylesheet is
* loaded
*/
public void loadStylesheet(final String stylesheetUrl, final ResourceLoadListener resourceLoadListener) {
final String url = WidgetUtil.getAbsoluteUrl(stylesheetUrl);
final ResourceLoadEvent event = new ResourceLoadEvent(this, url);
if (loadedResources.has(url)) {
if (resourceLoadListener != null) {
resourceLoadListener.onLoad(event);
}
return;
}
if (addListener(url, resourceLoadListener, loadListeners)) {
LinkElement linkElement = getDocument().createLinkElement();
linkElement.setRel("stylesheet");
linkElement.setType("text/css");
linkElement.setHref(url);
if (BrowserInfo.get().isSafariOrIOS()) {
// Safari doesn't fire any events for link elements
// See http://www.phpied.com/when-is-a-stylesheet-really-loaded/
Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
private final Duration duration = new Duration();
@Override
public boolean execute() {
int styleSheetLength = getStyleSheetLength(url);
if (getStyleSheetLength(url) > 0) {
fireLoad(event);
// Stop repeating
return false;
} else if (styleSheetLength == 0) {
// "Loaded" empty sheet -> most likely 404 error
fireError(event);
return true;
} else if (duration.elapsedMillis() > 60 * 1000) {
fireError(event);
return false;
} else {
// Continue repeating
return true;
}
}
}, 10);
} else {
addOnloadHandler(linkElement, new StyleSheetLoadListener(url), event);
if (BrowserInfo.get().isOpera()) {
// Opera onerror never fired, assume error if no onload in x
// seconds
new Timer() {
@Override
public void run() {
if (!loadedResources.has(url)) {
fireError(event);
}
}
}.schedule(5 * 1000);
}
}
getHead().appendChild(linkElement);
}
}
use of elemental.html.LinkElement in project flow by vaadin.
the class ResourceLoader method initLoadedResourcesFromDom.
/**
* Populates the resource loader with the scripts currently added to the
* page.
*/
private void initLoadedResourcesFromDom() {
Document document = Browser.getDocument();
// detect already loaded scripts and stylesheets
NodeList scripts = document.getElementsByTagName("script");
for (int i = 0; i < scripts.getLength(); i++) {
ScriptElement element = (ScriptElement) scripts.item(i);
String src = element.getSrc();
if (src != null && src.length() != 0) {
loadedResources.add(src);
}
}
NodeList links = document.getElementsByTagName("link");
for (int i = 0; i < links.getLength(); i++) {
LinkElement linkElement = (LinkElement) links.item(i);
String rel = linkElement.getRel();
String href = linkElement.getHref();
if (("stylesheet".equalsIgnoreCase(rel) || "import".equalsIgnoreCase(rel)) && href != null && href.length() != 0) {
loadedResources.add(href);
}
}
}
Aggregations