Skip to content

Commit 96ea124

Browse files
committed
Report - improve page load performance and displaying xml files
Before: every iframe is loaded -> 58 requests Now: only phpqa dashboard is loaded -> 4 requests
1 parent 1acf149 commit 96ea124

File tree

1 file changed

+88
-54
lines changed

1 file changed

+88
-54
lines changed

app/report/phpqa.html.twig

Lines changed: 88 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ set tabs = {
5757
<meta charset="utf-8">
5858
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
5959
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
60-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
61-
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
6260
<style type="text/css">
6361
html, body {
6462
height: 100%;
@@ -232,11 +230,11 @@ set tabs = {
232230
{% for tool, result in summary.tools %}
233231
{% if tool != 'phpqa' %}
234232
<div role="tabpanel" class="tab-pane" id="{{ tool }}">
235-
<iframe data-tool id="iframe-{{ tool }}" src="{{ result.htmlReport|replace({(buildDir): ''}) }}"></iframe>
233+
<iframe data-tool id="iframe-{{ tool }}" data-src="{{ result.htmlReport|replace({(buildDir): ''}) }}"></iframe>
236234
</div>
237235
{% for report in result.otherReports %}
238236
<div role="tabpanel" class="tab-pane" id="{{ report.id }}">
239-
<iframe id="iframe-{{ report.id }}" src="{{ report.file|replace({(buildDir): ''}) }}"></iframe>
237+
<iframe id="iframe-{{ report.id }}" data-src="{{ report.file|replace({(buildDir): ''}) }}"></iframe>
240238
</div>
241239
{% endfor %}
242240
{% endif %}
@@ -253,6 +251,8 @@ set tabs = {
253251
</div>
254252
</div>
255253

254+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
255+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
256256
<script>
257257
(function () {
258258
var tabsInIframe = {
@@ -266,28 +266,10 @@ set tabs = {
266266
},
267267
'#psalm': openBootstrap,
268268
};
269+
var loadedIframes = [];
269270
listenOnTabChange();
270-
hideHeadingsH1();
271271
activateTooltips();
272272
listenOnFileModal();
273-
274-
function hideHeadingsH1() {
275-
var interval = 1000;
276-
$("iframe[data-tool]").each(function () {
277-
var iframe = $(this)[0];
278-
setTimeout(
279-
function () {
280-
hideFirstHeading(iframe);
281-
},
282-
interval
283-
);
284-
interval += 200;
285-
});
286-
287-
function hideFirstHeading(iframe) {
288-
iframe.contentDocument.getElementsByTagName("h1")[0].style.display = 'none';
289-
}
290-
}
291273
292274
function openBootstrap(iframe, tab) {
293275
iframe.$("nav [aria-controls=" + tab + "]").tab('show');
@@ -301,22 +283,67 @@ set tabs = {
301283
function onNavLink() {
302284
$('.navbar-nav a').click(function (e) {
303285
e.preventDefault();
304-
$(this).tab('show');
305-
updateBrowserHistory($(this));
306286
307-
var target = $(this).data('iframe-tab');
308-
if (target) {
309-
var tabOpener = tabsInIframe[$(this).attr('href')];
310-
var iframe = $(".tab-pane.active iframe")[0].contentWindow;
311-
tabOpener(iframe, target);
287+
var navLink = $(this);
288+
var href = navLink.attr('href');
289+
290+
if (href === '#') {
291+
return;
292+
} else if (href === '#phpqa') {
293+
return showTab(navLink);
294+
}
295+
296+
var iframe = $('div' + href + ' iframe').first();
297+
if (!iframe.length) {
298+
return;
299+
}
300+
301+
if (loadedIframes.indexOf(href) === -1) {
302+
loadedIframes.push(href);
303+
loadReport({
304+
src: iframe.data('src'),
305+
loadCode: function (html) {
306+
iframe.replaceWith(html);
307+
},
308+
loadIframe: function (src) {
309+
iframe.attr('src', src);
310+
iframe.on('load', function () {
311+
hideFirstHeading(iframe[0]);
312+
openTabInIframe(navLink, iframe[0]);
313+
});
314+
},
315+
});
316+
} else {
317+
openTabInIframe(navLink, iframe[0]);
312318
}
319+
showTab(navLink);
313320
});
321+
322+
function showTab(navLink) {
323+
navLink.tab('show');
324+
updateBrowserHistory(navLink);
325+
}
314326
315327
function updateBrowserHistory(link) {
316328
if (window.history && window.history.pushState) {
317329
history.pushState(null, null, link.attr('href'));
318330
}
319331
}
332+
333+
function openTabInIframe(navLink, iframe) {
334+
var target = navLink.data('iframe-tab');
335+
if (target) {
336+
var tabOpener = tabsInIframe[navLink.attr('href')];
337+
tabOpener(iframe.contentWindow, target);
338+
}
339+
}
340+
341+
function hideFirstHeading(iframe) {
342+
var h1 = iframe.contentDocument.getElementsByTagName("h1")[0];
343+
if (h1) {
344+
h1.style.display = 'none';
345+
}
346+
}
320347
}
321348
322349
function onExternalLink() {
@@ -346,36 +373,43 @@ set tabs = {
346373
var modalBody = modal.find('.modal-body');
347374
$('.well a').click(function (e) {
348375
e.preventDefault();
349-
var url = $(this).attr('href');
350-
var isXml = url.indexOf('.xml') != -1;
351-
if (isXml) {
352-
modalBody.html('Rendering xml...');
353-
354-
$.get({
355-
url: url,
356-
dataType: 'text',
357-
}).then(function (data) {
358-
modalBody.html('<pre><code>' + escapeHtml(data) + '</pre></code>');
359-
})
360-
} else {
361-
modalBody.html(
362-
'<iframe src="' + url + '"></iframe>'
363-
);
364-
}
376+
loadReport({
377+
src: $(this).attr('href'),
378+
loadCode: function (html) {
379+
modalBody.html(html);
380+
},
381+
loadIframe: function (src) {
382+
modalBody.html('<iframe src="' + src + '"></iframe>');
383+
},
384+
});
365385
modal.modal({
366386
show: true,
367387
});
368388
});
369-
370-
function escapeHtml(text) {
371-
return text
372-
.replace(/&/g, "&amp;")
373-
.replace(/</g, "&lt;")
374-
.replace(/>/g, "&gt;")
375-
.replace(/"/g, "&quot;")
376-
.replace(/'/g, "&#039;");
389+
}
390+
391+
function loadReport(settings) {
392+
var isXml = settings.src.indexOf('.xml') != -1;
393+
if (isXml) {
394+
$.get({
395+
url: settings.src,
396+
dataType: 'text',
397+
}).then(function (data) {
398+
settings.loadCode('<pre><code>' + escapeHtml(data) + '</pre></code>');
399+
})
400+
} else {
401+
settings.loadIframe(settings.src);
377402
}
378403
}
404+
405+
function escapeHtml(text) {
406+
return text
407+
.replace(/&/g, "&amp;")
408+
.replace(/</g, "&lt;")
409+
.replace(/>/g, "&gt;")
410+
.replace(/"/g, "&quot;")
411+
.replace(/'/g, "&#039;");
412+
}
379413
}());
380414
</script>
381415
</body>

0 commit comments

Comments
 (0)