Refactor Javascript and Stylesheet loading
This commit is contained in:
@ -27,9 +27,9 @@ Oqtane.Interop = {
|
||||
document.title = title;
|
||||
}
|
||||
},
|
||||
includeMeta: function (id, attribute, name, content) {
|
||||
includeMeta: function (id, attribute, name, content, key) {
|
||||
var meta;
|
||||
if (id !== "") {
|
||||
if (id !== "" && key === "id") {
|
||||
meta = document.getElementById(id);
|
||||
}
|
||||
else {
|
||||
@ -50,13 +50,13 @@ Oqtane.Interop = {
|
||||
}
|
||||
}
|
||||
},
|
||||
includeLink: function (id, rel, url, type, integrity, crossorigin) {
|
||||
includeLink: function (id, rel, href, type, integrity, crossorigin, key) {
|
||||
var link;
|
||||
if (id !== "") {
|
||||
if (id !== "" && key === "id") {
|
||||
link = document.getElementById(id);
|
||||
}
|
||||
else {
|
||||
link = document.querySelector("link[href=\"" + CSS.escape(url) + "\"]");
|
||||
link = document.querySelector("link[href=\"" + CSS.escape(href) + "\"]");
|
||||
}
|
||||
if (link === null) {
|
||||
link = document.createElement("link");
|
||||
@ -67,7 +67,7 @@ Oqtane.Interop = {
|
||||
if (type !== "") {
|
||||
link.type = type;
|
||||
}
|
||||
link.href = url;
|
||||
link.href = href;
|
||||
if (integrity !== "") {
|
||||
link.integrity = integrity;
|
||||
}
|
||||
@ -87,10 +87,10 @@ Oqtane.Interop = {
|
||||
} else {
|
||||
link.removeAttribute('type');
|
||||
}
|
||||
if (link.href !== url) {
|
||||
if (link.href !== this.getAbsoluteUrl(href)) {
|
||||
link.removeAttribute('integrity');
|
||||
link.removeAttribute('crossorigin');
|
||||
link.setAttribute('href', url);
|
||||
link.setAttribute('href', href);
|
||||
}
|
||||
if (integrity !== "") {
|
||||
if (link.integrity !== integrity) {
|
||||
@ -108,11 +108,19 @@ Oqtane.Interop = {
|
||||
}
|
||||
}
|
||||
},
|
||||
includeScript: function (id, src, content, location, integrity, crossorigin) {
|
||||
includeLinks: function (links) {
|
||||
for (let i = 0; i < links.length; i++) {
|
||||
this.includeLink(links[i].id, links[i].rel, links[i].href, links[i].type, links[i].integrity, links[i].crossorigin, links[i].key);
|
||||
}
|
||||
},
|
||||
includeScript: function (id, src, integrity, crossorigin, content, location, key) {
|
||||
var script;
|
||||
if (id !== "") {
|
||||
if (id !== "" && key === "id") {
|
||||
script = document.getElementById(id);
|
||||
}
|
||||
else {
|
||||
script = document.querySelector("script[src=\"" + CSS.escape(src) + "\"]");
|
||||
}
|
||||
if (script === null) {
|
||||
script = document.createElement("script");
|
||||
if (id !== "") {
|
||||
@ -131,16 +139,17 @@ Oqtane.Interop = {
|
||||
script.innerHTML = content;
|
||||
}
|
||||
script.async = false;
|
||||
if (location === 'head') {
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
if (location === 'body') {
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
this.loadScript(script, location)
|
||||
.then(() => {
|
||||
console.log(src + ' loaded');
|
||||
})
|
||||
.catch(() => {
|
||||
console.error(src + ' failed');
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (src !== "") {
|
||||
if (script.src !== src) {
|
||||
if (script.src !== this.getAbsoluteUrl(src)) {
|
||||
script.removeAttribute('integrity');
|
||||
script.removeAttribute('crossorigin');
|
||||
script.src = src;
|
||||
@ -167,6 +176,32 @@ Oqtane.Interop = {
|
||||
}
|
||||
}
|
||||
},
|
||||
loadScript: function (script, location) {
|
||||
if (location === 'head') {
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
if (location === 'body') {
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
script.onload = res();
|
||||
script.onerror = rej();
|
||||
});
|
||||
},
|
||||
includeScripts: function (scripts) {
|
||||
for (let i = 0; i < scripts.length; i++) {
|
||||
this.includeScript(scripts[i].id, scripts[i].src, scripts[i].integrity, scripts[i].crossorigin, scripts[i].content, scripts[i].location, scripts[i].key);
|
||||
}
|
||||
},
|
||||
getAbsoluteUrl: function (url) {
|
||||
var a = document.createElement('a');
|
||||
getAbsoluteUrl = function (url) {
|
||||
a.href = url;
|
||||
return a.href;
|
||||
}
|
||||
return getAbsoluteUrl(url);
|
||||
},
|
||||
removeElementsById: function (prefix, first, last) {
|
||||
var elements = document.querySelectorAll('[id^=' + prefix + ']');
|
||||
for (var i = elements.length - 1; i >= 0; i--) {
|
||||
|
Reference in New Issue
Block a user