Skip to content

Commit

Permalink
dumper.js: refactoring of init()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 29, 2021
1 parent a0c6fa6 commit d01a577
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/Tracy/Dumper/assets/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,35 @@ const
class Dumper
{
static init(context) {
(context || document).querySelectorAll('[itemprop=tracy-snapshot], [data-tracy-snapshot]').forEach((el) => {
let preList, snapshot = JSON.parse(el.getAttribute('data-tracy-snapshot'));

if (el.tagName === 'META') { // <meta itemprop=tracy-snapshot>
snapshot = JSON.parse(el.getAttribute('content'));
preList = el.parentElement.querySelectorAll('[data-tracy-dump]');
} else if (el.matches('[data-tracy-dump]')) { // <pre data-tracy-snapshot data-tracy-dump>
preList = [el];
el.removeAttribute('data-tracy-snapshot');
} else { // <span data-tracy-dump>
el.querySelectorAll('[data-tracy-dump]').forEach((el) => {
if (!el.nextSibling) {
el.after(document.createTextNode('\n')); // ensures \n after toggler
}
el.nextSibling.after(buildStruct(JSON.parse(el.getAttribute('data-tracy-dump')), snapshot, el, true, []));
});
return;
}
// full lazy
(context || document).querySelectorAll('[data-tracy-snapshot][data-tracy-dump]').forEach((el) => { // <pre>
let snapshot = JSON.parse(el.getAttribute('data-tracy-snapshot'));
el.removeAttribute('data-tracy-snapshot');
el.appendChild(build(JSON.parse(el.getAttribute('data-tracy-dump')), snapshot, el.classList.contains('tracy-collapsed')));
el.removeAttribute('data-tracy-dump');
el.classList.remove('tracy-collapsed');
});

preList.forEach((el) => { // <pre>
let built = build(JSON.parse(el.getAttribute('data-tracy-dump')), snapshot, el.classList.contains('tracy-collapsed'));
el.appendChild(built);
el.classList.remove('tracy-collapsed');
// lazy
(context || document).querySelectorAll('[data-tracy-snapshot]').forEach((el) => { // <pre>
let snapshot = JSON.parse(el.getAttribute('data-tracy-snapshot'));
el.removeAttribute('data-tracy-snapshot');
el.querySelectorAll('[data-tracy-dump]').forEach((el) => { // <span>
if (!el.nextSibling) {
el.after(document.createTextNode('\n')); // enforce \n after toggler
}
el.nextSibling.after(buildStruct(JSON.parse(el.getAttribute('data-tracy-dump')), snapshot, el, true, []));
el.removeAttribute('data-tracy-dump');
});
});

// snapshots
(context || document).querySelectorAll('meta[itemprop=tracy-snapshot]').forEach((el) => {
let snapshot = JSON.parse(el.getAttribute('content'));
el.parentElement.querySelectorAll('[data-tracy-dump]').forEach((el) => { // <pre>
el.appendChild(build(JSON.parse(el.getAttribute('data-tracy-dump')), snapshot, el.classList.contains('tracy-collapsed')));
el.removeAttribute('data-tracy-dump');
el.classList.remove('tracy-collapsed');
});
});

Expand Down

0 comments on commit d01a577

Please sign in to comment.