Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
let header,sidebar;
let reportViewer = 'report-viewer';
let reportDesigner = 'report-designer';
let lastUpdatedScripts = [];


document.addEventListener('DOMContentLoaded', onDOMContentLoaded, false);
Expand Down Expand Up @@ -78,10 +79,50 @@ export async function updataSample(sampleData, isReportViewer) {
let demo = document.getElementsByTagName("ej-sample")[0];
let html = await fetchFile(`src/controls/${dirName}/${sampleData.routerPath}/index.html`);
let js = await fetchFile(`src/controls/${dirName}/${sampleData.routerPath}/index.js`);
demo.innerHTML = html;
let tags = new DOMParser().parseFromString(html, 'text/html');
let container = tags.querySelector('div');
demo.innerHTML = container ? container.outerHTML : '';
await loadScriptsFromHTML(tags);
eval(js);
}

async function loadScriptsFromHTML(container) {
if (lastUpdatedScripts && lastUpdatedScripts.length > 0) {
removeScriptsFromHTML(lastUpdatedScripts);
lastUpdatedScripts = [];
}

if (!container) return;
let scriptTags = container.querySelectorAll('script');
if (scriptTags && scriptTags.length > 0) {
for (let index = 0; index < scriptTags.length; index++) {
let existingScript = document.head.querySelector(`script[src="${scriptTags[index].src}"]`);
if (!existingScript) {
let newScript = document.createElement('script');
newScript.src = scriptTags[index].src;
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject;
document.head.appendChild(newScript);
lastUpdatedScripts.push(newScript.src);
});
}
}
}
}

function removeScriptsFromHTML(scriptList) {
if (scriptList && scriptList.length > 0) {
for (let index = 0; index < scriptList.length; index++) {
let selector = `head script[src="${CSS.escape(scriptList[index])}"]`;
let targetTag = document.querySelector(selector);
if (targetTag && targetTag.parentNode === document.head) {
targetTag.remove();
}
}
}
}

function onResize() {
setReportsHeight();
updateOverlay();
Expand Down