Instantly share code, notes, and snippets.
Created
October 26, 2012 16:58
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save juntalis/3959908 to your computer and use it in GitHub Desktop.
Abc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global, doc) { | |
var boxTree, | |
head, | |
basename = "InsertedWidget", | |
untilResultGood = function (checkFunc, callback) { | |
var args = arguments, | |
func = args.callee, | |
caller = func.caller, | |
check = checkFunc(); | |
if (!check) { | |
setTimeout(function () { | |
func.apply(caller, args); | |
}, 25); | |
return; | |
} | |
callback.apply(caller, [check]); | |
}, | |
isFunc = function (x) { | |
return typeof(x) === 'function'; | |
}, | |
byid = global.byid || function (x) { | |
return doc.getElementById(x); | |
}, | |
newE = global.newE || function (x) { | |
return doc.createElement(x); | |
}, | |
untilIDExists = function (id, callback) { | |
untilResultGood(function () { | |
return byid(id); | |
}, callback); | |
}, | |
initResizer = function (basename) { | |
var frm = global.theForm || byid('aspnetForm'), | |
createInput = function (id, el) { | |
if (!(el = byid(id))) { | |
(el = newE('input')).type = 'hidden'; | |
el.name = el.id = id; | |
frm.appendChild(el); | |
} | |
return el.id; | |
}; | |
return new Microsoft.Office.Server.Ajax.NavResizer( | |
createInput(basename + '_Width'), | |
createInput(basename + '_Height'), | |
basename + 'TreeViewRememberScroll', | |
'', | |
basename + 'NavResizer'); | |
}, | |
setOpacity = function (element, level) { | |
var alpha = (level >= 0 && level <= 100) ? level / 100 : 0; | |
if (!element.currentStyle.hasLayout) { | |
element.style.width = "150px"; | |
} | |
element.style.opacity = element.style.MozOpacity = element.style.KhtmlOpacity = alpha; | |
element.style.filter = "alpha(opacity=" + level + ");"; | |
return level; | |
}, | |
fadeInStep = function (element, level, callback) { | |
level += 5; | |
if (setOpacity(element, level) >= 100) { | |
if (isFunc(callback)) { | |
callback(); | |
} | |
} else { | |
setTimeout(function () { | |
fadeInStep(element, level, callback); | |
}, 50); | |
} | |
}, | |
fadeOutStep = function (element, level, callback) { | |
level -= 5; | |
if (level < 0) { | |
level = 0; | |
} | |
if (setOpacity(element, level) <= 0) { | |
if (isFunc(callback)) { | |
callback(); | |
} | |
} else { | |
setTimeout(function () { | |
fadeOutStep(element, level, callback); | |
}, 50); | |
} | |
}, | |
fadeIn = function (element, callback) { | |
setOpacity(element, 0); | |
setTimeout(function () { | |
fadeInStep(element, 0, callback); | |
}, 50); | |
}, | |
fadeOut = function (element, callback) { | |
setOpacity(element, 100); | |
setTimeout(function () { | |
fadeOutStep(element, 0, callback); | |
}, 50); | |
}, | |
blOneLoaded = false, | |
onResizerLoad = function () { | |
if (blOneLoaded) { | |
initResizer(basename); | |
} else { | |
blOneLoaded = true; | |
} | |
}; | |
// Get the document head. | |
untilResultGood(function () { | |
var h = doc.head || doc.getElementsByTagName('head'); | |
return "item" in h ? h[0] : h; | |
}, function (h) { | |
head = h; | |
}); | |
// As soon as the quicklaunch is constructed, shove a loading screen in there. | |
untilIDExists("ctl00_PlaceHolderLeftNavBar_QuickLaunchNavigationManager", function (element) { | |
element.appendChild(boxTree = newE('div')); | |
boxTree.id = basename; | |
boxTree.className = "s4-treeView"; | |
boxTree.style.borderTop = "1px solid #dbddde"; | |
boxTree.innerHTML = | |
"<div class=\"ms-treeviewouter\">" + | |
"<span id=\"" + basename + "NavResizer\">" + | |
"<div id=\"" + basename + "TreeViewRememberScroll\" onscroll=\"javascript:_spOnScroll(this);return false;\" style=\"width: 150px; min-height: 400px; overflow-x: auto; overflow-y: auto; margin-right:3px;\">" + | |
"<div id=\"" + basename + "TreeView\" style=\"display:none;\"></div>" + | |
"<div id=\"" + basename + "TreeViewLoading\" class=\"s4-simple-gearpage\" style=\"text-align:center;\">" + | |
"<div id=\"s4-simple-card-content\">" + | |
"<h4 style=\"margin:25px auto;\">" + | |
"<img id=\"gearsImage\" alt=\"This animation indicates the operation is in progress.\" src=\"/_layouts/images/gears_anv4.gif\" style=\"width:24px; height:24px; font-size:0px;border:0;\" align=\"absmiddle\" />" + | |
"</h4>" + | |
"<div>Loading..</div>" + | |
"</div>" + | |
"</div>" + | |
"</div>" + | |
"</span>" + | |
"</div>"; | |
}); | |
function AjaxWrapper(url, method, callback, args, cachekill, query) { | |
if (url == null || method == null) { | |
throw('AjaxWrapper instantiated with invalid arguments'); | |
} | |
this.url = url; | |
this.query == null ? this.query = "" : this.query = "?" + query; | |
this.method = method; | |
this.callback = callback; | |
this.args = args; | |
this.cachekill == null ? this.cachekill = true : this.cachekill = cachekill; | |
} | |
AjaxWrapper.prototype = { | |
makeAjaxCall : function () { | |
//alert('makeAjaxCall called'); | |
try { | |
this.callURL(); | |
} catch (e) { | |
alert(e); // request unavailable | |
} | |
}, | |
callURL : function (req) { | |
// alert('callURL called'); | |
try { | |
xmlHttp = this.getAjaxRequest(); | |
if (xmlHttp == null) { | |
throw('AJAX Error: XMLHttpRequest unavailable in this browser'); | |
} | |
} catch (e) { | |
throw('AJAX Error: Request invalid.'); | |
} | |
// cache killing functionality | |
if (this.cacheKill) { | |
var ckstr = "cachekill=" + (new Date().getTime()); | |
if (this.query && this.query != "") { | |
this.query = this.query + "&" + ckstr; | |
} else { | |
this.query = "?" + ckstr; | |
} | |
} | |
try { | |
wrapper = this; | |
xmlHttp.onreadystatechange = this.processAjaxResponse; | |
xmlHttp.open(this.method, (this.url + this.query), true); | |
xmlHttp.send(null); | |
} catch (e) { | |
throw 'AJAX Error: Request Send Failure'; | |
} | |
}, | |
getAjaxRequest : function () { | |
// alert('getAjaxRequest called'); | |
var ajaxRequest = null; | |
try { | |
ajaxRequest = new XMLHttpRequest(); | |
} catch (e) { | |
try { | |
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); | |
} catch (e) { | |
try { | |
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); | |
} catch (E) { | |
ajaxRequest = false; | |
} | |
} | |
} | |
if (!ajaxRequest && window.createRequest) { | |
try { | |
ajaxRequest = window.createRequest(); | |
} catch (e) { | |
throw 'XMLHttpRequest Unavailable'; | |
} | |
} | |
return ajaxRequest; | |
}, | |
processAjaxResponse : function () { | |
//alert('processing response'); | |
if (xmlHttp.readyState == 4) { | |
if (xmlHttp.status == 200) { | |
//alert('success!'); | |
if (wrapper.args == null) | |
wrapper.args = new Array(); | |
wrapper.args.push(xmlHttp.responseText); | |
wrapper.callback.apply(this, wrapper.args); | |
wrapper = null; | |
} else { | |
alert("There was a problem retrieving the XML data:\n" + | |
xmlHttp.statusText); | |
} | |
} | |
} | |
}; | |
// Load NavResizer.js | |
setTimeout(function () { | |
if (!head) { | |
setTimeout(arguments.callee, 25); | |
return; | |
} | |
var scriptElem = newE("script"), | |
scriptdone = false; | |
scriptElem.onload = scriptElem.onreadystatechange = function () { | |
if ((scriptElem.readyState && scriptElem.readyState !== "complete" && scriptElem.readyState !== "loaded") || scriptdone) { | |
return false; | |
} | |
scriptElem.onload = scriptElem.onreadystatechange = null; | |
scriptdone = true; | |
return onResizerLoad(); | |
}; | |
scriptElem.src = global._SPWebRoot + '/_layouts/NavResizer.js'; | |
head.insertBefore(scriptElem, head.firstChild); | |
}, 0); | |
// required: shim for FF <= 3.5 not having document.readyState | |
if (doc.readyState == null && doc.addEventListener) { | |
doc.readyState = "loading"; | |
doc.addEventListener("DOMContentLoaded", handler = function () { | |
doc.removeEventListener("DOMContentLoaded", handler, false); | |
doc.readyState = "complete"; | |
}, false); | |
} | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment