Reputation: 806
to get idea about my question here is an explanation about what is happening and what is not :
I've got upload function which uploads .zip files, get's all html code, images and converts it to strings. For every upload it repeats, after that when I load that .zip file it always extract it and converts all of it's content to string -> and I am putting it in . The problem is that it works only once ( putting strings to iframe) .
Here is how it works :
I get zip files and call ShowVisualContent() to convert all of it's content to strings. All of the strings are in my navService -> this.html: string;
this.uploads = this.db.list(`profile/${this.auth.userId}/projects/${this.projectId}`).snapshotChanges().map((actions) => {
return actions.map((a) => {
const data = a.payload.val();
this.navSrv.showVisualContent(data.url, data.name);
const $key = a.payload.key;
const $ref = a.payload.ref;
return { $key, ...data, $ref };
});
});
Then in my component i get that html:string;
@ViewChild('html') html: ElementRef;
loadHtml(){
setTimeout(()=> {
this.html.nativeElement.src = 'data:text/html,' + encodeURIComponent(this.navSrv.html);
},4000);
console.log("Loading html into iframe now")
}
And load it in html like this :
<div *ngFor="let upload of uploads | async">
<iframe style="min-width:1300px;min-height:800px;" id="framex" #html scrolling="no" frameborder="0"></iframe>
</div>
The main problem is that when I have lot's of zip files loaded in one page, I get only 1 with converted html strings in it.
How to make that this.html would be in all not only in one.
How to get value as an array from each function call :
html: string;
this.html = xmlDoc.documentElement.innerHTML;
(this is the part where I put all html in this.html string, and it get's only the last value.)
How to get all html's as an array ?
this is the function where I get innerHTML of all .zip files :
html:string;
public showVisualContent(contentUrl:string, fileName:string) {
console.log(contentUrl);
console.log(fileName);
let fileInfo = getFileInfo(fileName);
if(fileInfo) {
switch(fileInfo.type) {
case 'archive' : {
getAllFileContentsFromRemoteZip(contentUrl, (files) => {
//TODO: Remove timeouts. This is just a temporary option for demonstration purposes.
//Also this is a massive function that must be separated into multiple functions
setTimeout(() => {
let storageRef = firebase.storage().ref();
for(let i = 0; i < files.length; i++) {
if(files[i].fileInfo.type == 'image') {
// Create a storage reference from our app
// Create a reference with an initial file path and name
let imageRef = storageRef.child(files[i].fileInfo.fileName);
files[i].url = imageRef.getDownloadURL().then((url) => {
return url;
});
}
}
setTimeout(() => {
for(let i = 0; i < files.length; i++) {
//console.log(files[i].fileInfo.fileName);
//console.log(files[i].url);
if(files[i].fileInfo.type == 'web') {
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(files[i].content, "text/html");
//scripts
let scriptElements = xmlDoc.getElementsByTagName('script');
for(let j = 0; j < scriptElements.length; j++) {
let attr = scriptElements[j].getAttribute('src');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
scriptElements[j].removeAttribute('src');
scriptElements[j].innerHTML = files[k].content;
}
}
}
}
//styles
let linkElements = xmlDoc.getElementsByTagName('link');
for(let j = 0; j < linkElements.length; j++) {
if(linkElements[j].getAttribute('rel') == 'stylesheet')
{
let attr = linkElements[j].getAttribute('href');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
//do stuff
let parentElement = linkElements[k].parentElement;
if(parentElement) {
let styleElement = parentElement.appendChild(xmlDoc.createElement('style'))
styleElement.innerHTML = files[k].content;
}
}
}
}
}
}
//images
let imgElements = xmlDoc.getElementsByTagName('img');
for(let j = 0; j < imgElements.length; j++) {
let attr = imgElements[j].getAttribute('src');
if(attr) {
for(let k = 0; k < files.length; k++) {
if(attr.includes(files[k].fileInfo.fileName)) {
//do stuff
//imgElements[k].setAttribute('src', 'data:image/' + files[k].fileInfo.ext + ';base64,' + files[k].content);
imgElements[k].setAttribute('src', files[k].url.i);
}
}
}
}
//console.log(xmlDoc.documentElement.innerHTML);
this.html = xmlDoc.documentElement.innerHTML;
for(let j = 0; j < files.length; j++) {
if(files[j].fileInfo.type == 'image') {
let strings = getStringsToReplace(files[j].fileInfo.fileName, this.html);
for(let k = 0; k < strings.length; k++) {
this.html = this.html.replace(strings[k], files[j].url.i);
this.html = this.html.replace('/' + strings[k], files[j].url.i);
}
}
}
// console.log(this.html);
}
}
}, 500);
}, 1000);
});
}
case 'web' : {
//show simple html here. Unlikely to happen
}
case 'image' : {
//show image here
}
}
}
}
Upvotes: 1
Views: 644
Reputation: 800
ViewChild by design get's you one instance, use viewChildren :
@ViewChildren('html') htmls: QueryList<ElementRef>;
loadHtml(){
setTimeout(()=> {
this.htmls.map((elem) => {
elem.nativeElement.src = 'data:text/html,' + encodeURIComponent(this.navSrv.html);
}
},4000);
console.log("Loading html into iframe now")
}
for a better understanding of ViewChild & ViewChildren, queryList checkout this article https://netbasal.com/understanding-viewchildren-contentchildren-and-querylist-in-angular-896b0c689f6e
Upvotes: 4