lazy loading and other optimizations
This commit is contained in:
parent
dbb42629ad
commit
1906946a49
1 changed files with 44 additions and 39 deletions
75
js/tree.js
75
js/tree.js
|
@ -6,8 +6,17 @@ const magConst = 150;
|
||||||
const itemWidth = 120;
|
const itemWidth = 120;
|
||||||
const itemHeight = 90;
|
const itemHeight = 90;
|
||||||
|
|
||||||
|
let cache = '';
|
||||||
|
|
||||||
|
function escapeHtml(unsafe) {
|
||||||
|
return (unsafe + '')
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """)
|
||||||
|
.replace(/'/g, "'");
|
||||||
|
}
|
||||||
function genElement(type, area, content, attribs) {
|
function genElement(type, area, content, attribs) {
|
||||||
var elem = document.createElementNS("http://www.w3.org/2000/svg",type);
|
|
||||||
|
|
||||||
var directAttribs = attribs.direct || {};
|
var directAttribs = attribs.direct || {};
|
||||||
var styleAttribs = attribs.style || {};
|
var styleAttribs = attribs.style || {};
|
||||||
|
@ -20,13 +29,15 @@ function genElement(type, area, content, attribs) {
|
||||||
|
|
||||||
directAttribs.style = fullStyle;
|
directAttribs.style = fullStyle;
|
||||||
|
|
||||||
|
var elem = '';
|
||||||
|
|
||||||
Object.keys(directAttribs).forEach(function (attrib) {
|
Object.keys(directAttribs).forEach(function (attrib) {
|
||||||
elem.setAttribute(attrib, directAttribs[attrib]);
|
elem += `${escapeHtml(attrib)}="${escapeHtml(directAttribs[attrib])}" `;
|
||||||
});
|
});
|
||||||
|
|
||||||
elem.textContent = content;
|
elem = `<${type} ${elem}>${content}</${type}>`
|
||||||
|
|
||||||
area.appendChild(elem);
|
cache += elem;
|
||||||
|
|
||||||
return elem;
|
return elem;
|
||||||
}
|
}
|
||||||
|
@ -37,6 +48,11 @@ function genEntry(posEntry, area, fetchData) {
|
||||||
var x = posEntry.x * magConst;
|
var x = posEntry.x * magConst;
|
||||||
var y = posEntry.y * treeHeight;
|
var y = posEntry.y * treeHeight;
|
||||||
|
|
||||||
|
var x2 = x * zoom + mpos[0];
|
||||||
|
var y2 = y * zoom + mpos[1];
|
||||||
|
|
||||||
|
if (y2 < -250 || y2 > area2.clientHeight+ 250) return;
|
||||||
|
|
||||||
if (y > 0) {
|
if (y > 0) {
|
||||||
genElement('path', area, '', {
|
genElement('path', area, '', {
|
||||||
'direct': {
|
'direct': {
|
||||||
|
@ -59,17 +75,14 @@ function genEntry(posEntry, area, fetchData) {
|
||||||
'direct': {
|
'direct': {
|
||||||
'href': `https://scratch.mit.edu/projects/${posEntry.id}`,
|
'href': `https://scratch.mit.edu/projects/${posEntry.id}`,
|
||||||
'class': `${posEntry.visible ? '' : 'red'}`,
|
'class': `${posEntry.visible ? '' : 'red'}`,
|
||||||
},
|
|
||||||
'style': {
|
|
||||||
'transform': `translate(${x}px, ${y}px)`
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
genElement('image', a, '', {
|
genElement('image', a, '', {
|
||||||
'direct': {
|
'direct': {
|
||||||
'href': `https://uploads.scratch.mit.edu/get_image/project/${posEntry.id}_1920x1080.png`,
|
'href': `https://uploads.scratch.mit.edu/get_image/project/${posEntry.id}_1920x1080.png`,
|
||||||
'x': '10',
|
'x': x - 10,
|
||||||
'y': '-15',
|
'y': y - 15,
|
||||||
'width': '100'
|
'width': '100'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -85,25 +98,11 @@ function genEntry(posEntry, area, fetchData) {
|
||||||
|
|
||||||
genElement('text', a, text[i], {
|
genElement('text', a, text[i], {
|
||||||
'direct': {
|
'direct': {
|
||||||
'x': '75',
|
'x': x + 75,
|
||||||
'y': 75 + 8 * i
|
'y': y + 75 + 8 * i
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
{#if Math.abs((mpos[0] + elem.x * zoom) - width/2) < width/2 * 1.2 && Math.abs((mpos[1] + elem.y * zoom) - height/2) < height/2 * 1.2 }
|
|
||||||
<image href='https://uploads.scratch.mit.edu/get_image/project/{elem.id}_1920x1080.png' x='10' y='-15' width='100' />
|
|
||||||
{:else}
|
|
||||||
<circle r="25" cx="60" cy="40"></circle>
|
|
||||||
{/if}
|
|
||||||
<text x='75' y='75'>{elem.name}</text>
|
|
||||||
<text x='75' y='83'>{elem.y / treeHeight} proj. deep</text>
|
|
||||||
<text x='75' y='91'>{(elem.date) ? (new Date(elem.date.$date) + '').split('GMT')[0] : 'Date not available'}</text>
|
|
||||||
<text x='75' y='99'>{elem.user}</text>
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -118,22 +117,28 @@ async function genTree(id) {
|
||||||
|
|
||||||
let keys = Object.keys(fetchData);
|
let keys = Object.keys(fetchData);
|
||||||
|
|
||||||
keys.forEach(id => {
|
let r = fetchData[fetchData.root_id];
|
||||||
if (id == fetchData.root_id || id == 'root_id') return;
|
|
||||||
|
|
||||||
let allChild = keys.map(x => fetchData[x].children).flat();
|
let i = 0;
|
||||||
|
for (let id of keys) {
|
||||||
|
if (id == fetchData.root_id || id == 'root_id') continue;
|
||||||
|
|
||||||
if (allChild.indexOf(id) == -1) {
|
if (!fetchData[id].parent_id && keys.findIndex(x => fetchData[x].children && fetchData[x].children.indexOf(id) != -1) == -1) {
|
||||||
fetchData[fetchData.root_id].children.push(id);
|
r.children.push(id);
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
});
|
console.log(i)
|
||||||
|
};
|
||||||
|
|
||||||
let pos = await main(fetchData);
|
let pos = await main(fetchData);
|
||||||
|
|
||||||
pos.forEach(function (posEntry) {
|
setInterval(function () {
|
||||||
genEntry(posEntry, area, fetchData);
|
cache = '';
|
||||||
})
|
for (let posEntry of pos) {
|
||||||
|
genEntry(posEntry, area, fetchData);
|
||||||
|
}
|
||||||
|
area.innerHTML = cache;
|
||||||
|
}, 1000)
|
||||||
|
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
Loading…
Reference in a new issue