Skip to content

Instantly share code, notes, and snippets.

@woollsta
Created December 1, 2015 09:44
Show Gist options
  • Save woollsta/fe8e855cd933fe83139f to your computer and use it in GitHub Desktop.
Save woollsta/fe8e855cd933fe83139f to your computer and use it in GitHub Desktop.
<script>
/**
* Safer method of loading and using jQuery and other libraries into
* an IBM Congos Report (version 10.2)
* Normally this is overkill and you can get away with a simple
* <script src="path/to/jquery.js">
* However when you start to add prompts, because the Prompt API uses
* the same '$' variable as jQuery defaults to, you start to see some
* odd inconsistencies.
*/
(function(window){
var cognosJqFix = {};
var readyCallbacks = [],
jqSafe = false;
cognosJqFix.complete = false;
cognosJqFix.ready = function(func){
if(!jqSafe){
readyCallbacks.push(func);
}else {
func(window.jQuery);
}
};
cognosJqFix.setup = function(configObject){
if(configObject.jqueryPath===undefined){
console.log('Must specify a jquery path');
return;
}
if(!window.jQuery){
var jqs = document.createElement('script');
jqs.setAttribute('src', configObject.jqueryPath);
var reportViewer = window.oCV_NS_ || window.oCVRS;
//wait for cognos view to fully load first
var checkComplete = setInterval(function(){
if(reportViewer.m_sStatus === "complete"){
clearInterval(checkComplete);
document.getElementsByTagName('head')[0].appendChild(jqs);
}
},50);
}else {
//run noConflict to remove global assignment of $ to jQuery
window.jQuery.noConflict();
}
//now loop until we have loaded jQuery...
var waiter = setInterval(function(){
if(window.jQuery!==undefined){
clearInterval(waiter);
var deferred = new window.jQuery.Deferred();
//load any stylesheets into the DOM
if(configObject.styles && configObject.styles.length){
window.jQuery.each(configObject.styles, function(i, src){
window.jQuery('<link/>', {
rel: 'stylesheet',
type: 'text/css',
href: src
}).appendTo('head');
});
}
if(configObject.scripts && configObject.scripts.length){
loadScripts(configObject.scripts, function(){
deferred.resolve();
});
}else {
deferred.resolve();
}
window.jQuery.when(deferred.promise()).then(function(){
var x;
jqSafe = true;
while(x = readyCallbacks.shift()){
x(window.jQuery);
}
cognosJqFix.complete = true;
});
}
},25);
};
function loadScripts(urls, callback) {
var i = 0;
(function loadNextScript() {
if (i < urls.length) {
window.jQuery.getScript(urls[i]).done(function() {
++i;
loadNextScript();
});
}else if (callback) callback();
})();
}
//publish to window object
window.cognosJqFix = cognosJqFix;
}(window));
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment