Don't wait for onload.

This commit is contained in:
bootstraponline
2012-05-18 15:57:41 -06:00
parent 40fdd2aa74
commit f7d3f6b3f0
@@ -133,58 +133,83 @@ $.save = function( commitMessage ) {
} // end else } // end else
} }
window.onload = function() {
/* Load markdown from /data/page into the ace editor. */ /* Load markdown from /data/page into the ace editor. */
jQuery.ajax({ jQuery.ajax({
type: "GET", type: "GET",
url: "/data/" + $.key("page"), url: "/data/" + $.key("page"),
success: function(data) { success: function(data) {
editorSession.setValue(data); editorSession.setValue(data);
} }
}); });
$("#save").click(function() { $("#save").click(function() {
$.save(); $.save();
}); });
// Hide dimmer, comment tool panel, and comment. // Hide dimmer, comment tool panel, and comment.
$("#commentcancel").click(function() { $("#commentcancel").click(function() {
// Restore focus on commentcancel but not on // Restore focus on commentcancel but not on
// savecommentconfirm because the latter loads // savecommentconfirm because the latter loads
// a new page. // a new page.
hideCommentWindow(); hideCommentWindow();
editor.focus(); editor.focus();
}); });
var isCommentHidden = true; var isCommentHidden = true;
function hideCommentWindow() { function hideCommentWindow() {
isCommentHidden = true; isCommentHidden = true;
darkness.style.visibility = "hidden"; darkness.style.visibility = "hidden";
commentToolPanel.style.visibility = "hidden"; commentToolPanel.style.visibility = "hidden";
comment.style.visibility = "hidden"; comment.style.visibility = "hidden";
} }
// Show dimmer, comment tool panel, and comment. // Show dimmer, comment tool panel, and comment.
$("#savecomment").click(function() { $("#savecomment").click(function() {
isCommentHidden = false; isCommentHidden = false;
darkness.style.visibility = "visible"; darkness.style.visibility = "visible";
commentToolPanel.style.visibility = "visible"; commentToolPanel.style.visibility = "visible";
comment.style.visibility = "visible"; comment.style.visibility = "visible";
// Set focus so typing can begin immediately. // Set focus so typing can begin immediately.
commentEditor.focus(); commentEditor.focus();
}); });
$("#savecommentconfirm").click(function() { $("#savecommentconfirm").click(function() {
$.save(commentEditorSession.getValue()); $.save(commentEditorSession.getValue());
hideCommentWindow(); hideCommentWindow();
}); });
// onChange calls applyTimeout which rate limits the calling of makePreviewHtml based on render time. // onChange calls applyTimeout which rate limits the calling of makePreviewHtml based on render time.
editor.on('change', applyTimeout); editor.on('change', applyTimeout);
makePreviewHtml(); // preview default text on load
function resize() { var cssTextSet = function( element, css ){
element.style.cssText = css;
};
var cssAttrSet = function( element, css ){
element.setAttribute( 'style', css );
};
/*
Redefine the function based on browser support.
element - the element to set the css on
css - a fully formed css string. ex: "top: 0; left: 0;"
Avoid reflow by batching CSS changes.
http://dev.opera.com/articles/view/efficient-javascript/?page=3#stylechanges
*/
var cssSet = function( element, css ) {
if( typeof( element.style.cssText ) != 'undefined' ) {
cssTextSet( element, css );
cssSet = cssTextSet;
} else {
cssAttrSet( element, css );
cssSet = cssAttrSet;
}
};
function resize() {
var width = $(window).width(); var width = $(window).width();
var widthHalf = width / 2; var widthHalf = width / 2;
var widthFourth = widthHalf / 2; var widthFourth = widthHalf / 2;
@@ -235,22 +260,21 @@ window.onload = function() {
"height:" + height + "px;" + "height:" + height + "px;" +
commentHidden; commentHidden;
cssSet(darkness, darknessStyle); cssSet(darkness, darknessStyle);
} }
window.jsm.resize = resize; window.jsm.resize = resize;
/* /*
Resize can be called an absurd amount of times Resize can be called an absurd amount of times
and will crash the page without debouncing. and will crash the page without debouncing.
http://benalman.com/projects/jquery-throttle-debounce-plugin/ http://benalman.com/projects/jquery-throttle-debounce-plugin/
https://github.com/cowboy/jquery-throttle-debounce https://github.com/cowboy/jquery-throttle-debounce
http://unscriptable.com/2009/03/20/debouncing-javascript-methods/ http://unscriptable.com/2009/03/20/debouncing-javascript-methods/
*/ */
$(window).resize( $.debounce( 100, resize ) ); $(window).resize( $.debounce( 100, resize ) );
// resize for the intial page load // resize for the intial page load
resize(); resize();
};
var elapsedTime; var elapsedTime;
var oldInputText = ""; var oldInputText = "";
@@ -276,32 +300,6 @@ var ieSafePreviewSet = function (text) {
parent.insertBefore(content, sibling); parent.insertBefore(content, sibling);
} }
var cssTextSet = function( element, css ){
element.style.cssText = css;
}
var cssAttrSet = function( element, css ){
element.setAttribute( 'style', css );
}
/*
Redefine the function based on browser support.
element - the element to set the css on
css - a fully formed css string. ex: "top: 0; left: 0;"
Avoid reflow by batching CSS changes.
http://dev.opera.com/articles/view/efficient-javascript/?page=3#stylechanges
*/
var cssSet = function( element, css ) {
if( typeof( element.style.cssText ) != 'undefined' ) {
cssTextSet( element, css );
cssSet = cssTextSet;
} else {
cssAttrSet( element, css );
cssSet = cssAttrSet;
}
}
var previewSet = function( text ) { var previewSet = function( text ) {
try { try {
nonSuckyBrowserPreviewSet( text ); nonSuckyBrowserPreviewSet( text );
@@ -379,6 +377,9 @@ var makePreviewHtml = function () {
} }
}; };
// preview default text on load
makePreviewHtml();
// setTimeout is already used. Used as an event listener. // setTimeout is already used. Used as an event listener.
var applyTimeout = function () { var applyTimeout = function () {
if (timeout) { if (timeout) {