Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify inline script loader #86

Merged
merged 1 commit into from Oct 8, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 26 additions & 55 deletions StackExchange.Profiling/UI/include.partial.html
@@ -1,62 +1,33 @@
<script type="text/javascript">
(function(){{
var init = function() {{
var load = function(s,f){{
var sc = document.createElement('script');
sc.async = 'async';
sc.type = 'text/javascript';
sc.src = s;
var l = false;
sc.onload = sc.onreadystatechange = function(_, abort) {{
if (!l && (!sc.readyState || /loaded|complete/.test(sc.readyState))) {{
if (!abort){{l=true; f();}}
}}
}};

document.getElementsByTagName('head')[0].appendChild(sc);
}};

var initMp = function(){{
load('{path}includes.js?v={version}',function(){{
MiniProfiler.init({{
ids: {ids},
path: '{path}',
version: '{version}',
renderPosition: '{position}',
showTrivial: {showTrivial},
showChildrenTime: {showChildren},
maxTracesToShow: {maxTracesToShow},
showControls: {showControls},
currentId: '{currentId}',
authorized: {authorized}
}});
}});
}};
if ({useExistingjQuery} && typeof(jQuery) === 'function') {{
jQueryMP = jQuery;
initMp();
}} else {{
load('{path}jquery.1.7.1.js?v={version}', initMp);
var load = function(s,f){{
var sc = document.createElement('script');
sc.async = 'async';
sc.type = 'text/javascript';
sc.src = s;
var l = false;
sc.onload = sc.onreadystatechange = function(_, abort) {{
if (!l && (!sc.readyState || /loaded|complete/.test(sc.readyState))) {{
if (!abort){{l=true; f();}}
}}

}};
document.getElementsByTagName('head')[0].appendChild(sc);
}};

var w = 0;
var f = false;
var deferInit = function(){{
if (f) return;
if (window.performance && window.performance.timing && window.performance.timing.loadEventEnd == 0 && w < 10000){{
setTimeout(deferInit, 100);
w += 100;
}} else {{
f = true;
init();
}}
}};
if (document.addEventListener) {{
document.addEventListener('DOMContentLoaded',deferInit);
}}
var o = window.onload;
window.onload = function(){{if(o)o(); deferInit()}};
load('{path}includes.js?v={version}',function(){{
MiniProfiler.init({{
usingExistingjQuery: {useExistingjQuery},
ids: {ids},
path: '{path}',
version: '{version}',
renderPosition: '{position}',
showTrivial: {showTrivial},
showChildrenTime: {showChildren},
maxTracesToShow: {maxTracesToShow},
showControls: {showControls},
currentId: '{currentId}',
authorized: {authorized}
}});
}});
}})();
</script>
50 changes: 37 additions & 13 deletions StackExchange.Profiling/UI/includes.js
@@ -1,5 +1,6 @@
"use strict";
var MiniProfiler = (function ($) {
var MiniProfiler = (function () {
var $;

var options,
container,
Expand Down Expand Up @@ -547,24 +548,47 @@ var MiniProfiler = (function ($) {
document.getElementsByTagName('head')[0].appendChild(sc);
};

if (options.authorized) {
var url = options.path + "includes.css?v=" + options.version;
if (document.createStyleSheet) {
document.createStyleSheet(url);
var wait = 0;
var finish = false;
var deferInit = function() {
if (finish) return;
if (window.performance && window.performance.timing && window.performance.timing.loadEventEnd == 0 && wait < 10000) {
setTimeout(deferInit, 100);
wait += 100;
} else {
$('head').append($('<link rel="stylesheet" type="text/css" href="' + url + '" />'));
finish = true;
init();
}
};

if (!$.tmpl) {
load(options.path + 'jquery.tmpl.js?v=' + options.version, doInit);
} else {
var init = function() {
if (options.authorized) {
var url = options.path + "includes.css?v=" + options.version;
if (document.createStyleSheet) {
document.createStyleSheet(url);
} else {
$('head').append($('<link rel="stylesheet" type="text/css" href="' + url + '" />'));
}
if (!$.tmpl) {
load(options.path + 'jquery.tmpl.js?v=' + options.version, doInit);
} else {
doInit();
}
}
else {
doInit();
}
}
else {
doInit();
}

if (options.useExistingjQuery && typeof(jQuery) === 'function') {
MiniProfiler.jQuery = $ = jQuery;
$(deferInit);
} else {
load(options.path + "jquery.1.7.1.js?v=" + options.version, function() {
MiniProfiler.jQuery = $ = jQuery.noConflict(true);
$(deferInit);
});
}
},

getClientTimingByName: function (clientTiming, name) {
Expand Down Expand Up @@ -784,7 +808,7 @@ var MiniProfiler = (function ($) {
return (duration || 0).toFixed(1);
}
};
})(jQueryMP);
})();

// prettify.js
// http://code.google.com/p/google-code-prettify/
Expand Down
2 changes: 1 addition & 1 deletion StackExchange.Profiling/UI/jquery.1.7.1.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion StackExchange.Profiling/UI/jquery.tmpl.js
Expand Up @@ -483,4 +483,4 @@
jQuery.tmpl(null, null, null, this).insertBefore(coll[0]);
jQuery(coll).remove();
}
})(jQueryMP);
})(MiniProfiler.jQuery);
13 changes: 7 additions & 6 deletions StackExchange.Profiling/UI/list.js
Expand Up @@ -2,16 +2,17 @@
MiniProfiler.list = {
init:
function (options) {
var $ = MiniProfiler.jQuery;
var opt = options || {};

var updateGrid = function (id) {
jQueryMP.ajax({
$.ajax({
url: options.path + 'results-list',
data: { "last-id": id },
dataType: 'json',
type: 'GET',
success: function (data) {
jQueryMP('table tbody').append(jQueryMP("#rowTemplate").tmpl(data));
$('table tbody').append($("#rowTemplate").tmpl(data));
var oldId = id;
var oldData = data;
setTimeout(function () {
Expand All @@ -26,12 +27,12 @@ MiniProfiler.list = {
}

MiniProfiler.path = options.path;
jQueryMP.get(options.path + 'list.tmpl?v=' + options.version, function (data) {
$.get(options.path + 'list.tmpl?v=' + options.version, function (data) {
if (data) {
jQueryMP('body').append(data);
jQueryMP('body').append(jQueryMP('#tableTemplate').tmpl());
$('body').append(data);
$('body').append($('#tableTemplate').tmpl());
updateGrid();
}
});
}
};
};