use of elemental.html.ScriptElement in project flow by vaadin.
the class ResourceLoader method inlineScript.
/**
* Inlines a script and notify a listener when the script is loaded. Calling
* this method when the script is currently loading or already loaded
* doesn't cause the script to be loaded again, but the listener will still
* be notified when appropriate.
*
* @param scriptContents
* the script contents to inline
* @param resourceLoadListener
* listener to notify when script is loaded
*/
public void inlineScript(String scriptContents, final ResourceLoadListener resourceLoadListener) {
ResourceLoadEvent event = new ResourceLoadEvent(this, scriptContents);
if (loadedResources.has(scriptContents)) {
if (resourceLoadListener != null) {
resourceLoadListener.onLoad(event);
}
return;
}
if (addListener(scriptContents, resourceLoadListener, loadListeners)) {
ScriptElement scriptElement = getDocument().createScriptElement();
scriptElement.setTextContent(scriptContents);
scriptElement.setType("text/javascript");
addOnloadHandler(scriptElement, new SimpleLoadListener(), event);
getHead().appendChild(scriptElement);
}
}
use of elemental.html.ScriptElement in project flow by vaadin.
the class ResourceLoader method loadScript.
private void loadScript(String scriptUrl, ResourceLoadListener resourceLoadListener, boolean async, boolean defer, String type) {
final String url = WidgetUtil.getAbsoluteUrl(scriptUrl);
ResourceLoadEvent event = new ResourceLoadEvent(this, url);
if (loadedResources.has(url)) {
if (resourceLoadListener != null) {
resourceLoadListener.onLoad(event);
}
return;
}
if (addListener(url, resourceLoadListener, loadListeners)) {
ScriptElement scriptTag = Browser.getDocument().createScriptElement();
scriptTag.setSrc(url);
scriptTag.setType(type);
scriptTag.setAsync(async);
scriptTag.setDefer(defer);
addOnloadHandler(scriptTag, new SimpleLoadListener(), event);
getHead().appendChild(scriptTag);
}
}
use of elemental.html.ScriptElement 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