123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- // Licensed Materials - Property of IBM
- //
- // IBM Cognos Products: cogadmin
- //
- // (C) Copyright IBM Corp. 2005, 2012
- //
- // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- //
- //
- /**
- * Attach an event to the given element
- * capture flag is not specified
- * @param object
- * @param eventType
- * @param func
- * @return
- */
- function addEventToObject(object, eventType, func){
- var isEventAdded = false;
-
- if (object && eventType && func) {
- eventType = eventType.toLowerCase();
- if (object.addEventListener){
- object.addEventListener(eventType, func, false);
- isEventAdded = true;
- } else if (object.attachEvent){
- isEventAdded = object.attachEvent("on" + eventType, func);
- }
- }
- return isEventAdded;
- }
- /*
- This class allows a fragment to be embedded in any html document
- */
- /**
- * Constructor
- * The parameters are either:
- * An object with the following attribute:
- * gateway: path to the gateway [mandatory]
- * webRoot: path to the webroot [mandatory]
- * skinRoot: path to the skin root [optional for backward compatibility]
- *
- *
- */
- function FragmentEmbedder() {
- var args = arguments;
- this._configuration = null;
- this._frags = [];
- this._loadedScripts = 0;
- this._loadedCssStyles = 0;
- this._retrievedFragments = 0;
-
- //for backward compatibility
- if (args && args.length == 1 && typeof args[0] != 'string') {
- this._configuration = args[0];
- } else if (args && args.length >= 2) {
- this._configuration = {"gateway":args[0],"webRoot":args[1]};
- }
-
- var _self = this;
- FragmentEmbedder.ERROR_UNDEFINED_CONF = "Illegal Argument Exception: No configuration is defined";
- FragmentEmbedder.ERROR_UNDEFINED_GATEWAY = "Illegal Argument Exception: No gateway is defined";
- FragmentEmbedder.ERROR_UNDEFINED_WEBROOT = "Illegal Argument Exception: No webRoot is defined";
- FragmentEmbedder.ERROR_UNDEFINED_FRAGDEF = "Illegal Argument Exception: No fragment definition is defined";
-
- /**
- * verifies the configuration
- */
- this._verifyConfiguration = function() {
- if (!_self._configuration || _self._configuration == null) {
- throw FragmentEmbedder.ERROR_UNDEFINED_CONF;
- }
- if (_self._configuration.gateway == null || _self._configuration.gateway == "") {
- throw FragmentEmbedder.ERROR_UNDEFINED_GATEWAY;
- }
- if (_self._configuration.webRoot == null) {
- throw FragmentEmbedder.ERROR_UNDEFINED_WEBROOT;
- }
- }
-
- this._verifyConfiguration();
-
- var scripts = [
- this._configuration.webRoot+'/dojo16/dojo/dojo.js',
- this._configuration.webRoot+'/fragments/xdojo/core.js',
- this._configuration.gateway+'/dashboard/messages/messages/pfmessages?section=JS,GEN',
- this._configuration.webRoot+'/fragments/fragments.js',
- this._configuration.webRoot+'/common/framework/validator/CValidator.js',
- this._configuration.webRoot+'/fragments/uicommon.js'
- ];
-
- /**
- * The following stylesheet is required when showing the details of an error.
- */
- var cssStyles = [
- this._configuration.skinRoot+'/fragments/portlet.css'
- ];
-
-
- /**
- * This method registers a fragment to be loaded; the parameter is either an object or a sequence of 3 strings:
- * fragUrl: url for the fragment
- * fragDivId: id of the fragment div,
- * retrieveParams: string or function providing the parameters to pass when retrieving the fragment
- * It is recommended to use the object
- */
- this.embed = function () {
- var fragDef = null;
- var args = arguments;
- if (!args || args.length == 0) {
- throw FragmentEmbedder.ERROR_UNDEFINED_FRAGDEF;
- }
- if (args.length == 1 && typeof args[0] != 'string') {
- _self._frags[_self._frags.length] = args[0];
- } else {
- fragDef = {};
- if (args[0]) {
- fragDef.fragUrl = args[0];
- } else {
- fragDef.fragUrl = null;
- }
- if (args[1]) {
- fragDef.fragDivId = args[1];
- } else {
- fragDef.fragDivId = null;
- }
- if (args[2]) {
- fragDef.retrieveParams = args[2];
- } else {
- fragDef.retrieveParams = null;
- }
- _self._frags[_self._frags.length] = fragDef;
- }
- /*
- * We return the fragDef object, but it should be treated as an opaque object by the client
- */
- return _self._frags[_self._frags.length];
- }
-
- /**
- * This methods loads the embedded fragments
- * the onload parameter is a flag indicating whether or not the load has to happen once the page is loaded
- */
- this.loadFragments = function(onLoad) {
- if (onLoad) {
- addEventToObject(window,"load",function() {_self._loadScriptsAndFragments();});
- } else {
- this._loadScriptsAndFragments();
- }
- }
- /**
- * This method raised the specified event on the specified embedded fragment
- */
- this.raiseEvent = function(fragDef, eventName, payload, type){
- fragDef.raiseEvent(eventName, payload, type);
- }
-
- /**
- * This method loads the necessary scripts, css stylesheets and then the fragments
- */
- this._loadScriptsAndFragments = function() {
- if (_self._loadedScripts < scripts.length) {
- _self._loadScript(scripts[_self._loadedScripts++],_self._loadScriptsAndFragments);
- } else if (_self._loadedCssStyles < cssStyles.length) {
- _self._loadCssStyle(cssStyles[_self._loadedCssStyles++],_self._loadScriptsAndFragments);
- } else {
- _self._loadFrags();
- }
- }
-
- this._canShowErrorDetails = function() {
- var canShow = false;
- if (_self._configuration.skinRoot) {
- canShow = true;
- }
- return canShow;
- }
-
- this._loadFrags = function() {
- if (!window._F_Config) {
- _F_Config = new Object();
- }
- _F_Config.gateway = _self._configuration.gateway;
- _F_Config.application = "/cogadmin";
- _F_Config.webContent = _self._configuration.webRoot;
- if (_self._canShowErrorDetails()) {
- ui_error.prototype.template = '<div id="_THIS_errormsg"></div><div><a id="_THIS_error_showdetails">' + PFM.GEN.IDS_GEN_DETAILS + '</a></div>';
- } else {
- ui_error.prototype.template = '<div id="_THIS_errormsg"></div>';
- }
- for (var i=0;i<_self._frags.length;i++) {
- _self._fragInit(_self._frags[i]);
- }
- this._retrieveFragment();
- }
- this._isScriptLoaded = function(src) {
- return this._isFileLoaded("src",src);
- }
-
- this._isCssStyleLoaded = function(src) {
- return this._isFileLoaded("href",src);
- }
-
- this._isFileLoaded = function(attribute,value) {
- var head = document.getElementsByTagName("head")[0];
- var isLoaded = false;
- var i=0;
-
- while (i<head.childNodes.length && !isLoaded) {
- isLoaded = (head.childNodes[i].attributes != null)
- && (head.childNodes[i].attributes[attribute] != null)
- && (value==head.childNodes[i].attributes[attribute].value);
- i++;
- }
- return isLoaded;
- }
-
- this._loadScript = function(scriptUrl,onloadHandler) {
- if (_self._isScriptLoaded(scriptUrl)) {
- if (onloadHandler) {
- onloadHandler();
- }
- } else {
- var s = document.createElement("script");
- s.src = scriptUrl;
- s.type="text/javascript";
- if (onloadHandler) {
- s.onload=onloadHandler; //Firefox
- s.onreadystatechange = function() { //IE
- if (this.readyState == 'complete' || this.readyState == 'loaded') {
- onloadHandler();
- }
- }
- }
- document.getElementsByTagName("head")[0].appendChild(s);
- }
- }
-
- this._loadCssStyle = function(cssStyleUrl,onloadHandler) {
- if (!_self._isCssStyleLoaded(cssStyleUrl)) {
- var node = document.createElement("link");
- node.type = 'text/css';
- node.rel = "stylesheet"
- node.href = cssStyleUrl;
- document.getElementsByTagName("head")[0].appendChild(node);
- }
- if (onloadHandler) {
- onloadHandler();
- }
- }
- this._addFragContentDiv = function(frag) {
- var fragId=frag.id;
- var contentId = fragId+'content';
- var contentDiv = $(contentId);
-
- if (!contentDiv) {
- contentDiv = document.createElement("div");
- contentDiv.id = contentId;
- contentDiv.className = "boxBody";
- $(fragId).appendChild(contentDiv);
- }
- var waitDiv = ui_templates.get(
- ui_templates.contentTemplate,
- 'CONTENTID', fragId + "wait",
- 'CONTENTCLASS', 'cogstyle-htabs-page-content'
- );
- contentDiv.innerHTML = waitDiv;
- return contentDiv;
- }
-
- this._generateRetrieveParams = function(registeredFrag) {
- var params = null;
- if (registeredFrag.retrieveParams) {
- if (typeof registeredFrag.retrieveParams == "string"){
- params = registeredFrag.retrieveParams;
- } else if (typeof registeredFrag.retrieveParams == "function") {
- params = registeredFrag.retrieveParams();
- } else {
- //unknown type: ignore it
- _F_log("W", "embedFragment(): Unsupported value for the retrieve params: " + registeredFrag.retrieveParams);
- }
- }
- return params;
- }
-
- this._fragInit = function(registeredFrag) {
- var params = null;
- fragDiv = $(registeredFrag.fragDivId);
- if (!fragDiv) {
- _F_log("E", "embedFragment(): DOM element not found for fragDivId="+registeredFrag.fragDivId);
- } else {
- _F_init();
- embeddedFrag = new fragment(registeredFrag.fragUrl, registeredFrag.fragDivId);
- embeddedFrag.setOnloadHandler(ui_init);
- embeddedFrag.div = _self._addFragContentDiv(embeddedFrag);
- embeddedFrag.addEventListener("fragment.retrieve.after",this._postFragRetrieve,false);
- registeredFrag.fragment = embeddedFrag;
- }
- }
-
- this._retrieveFragment = function() {
- var registeredFrag = null;
- var params = null;
-
- if (this._retrievedFragments < this._frags.length) {
- registeredFrag = this._frags[this._retrievedFragments];
- this._retrievedFragments++;
- if (registeredFrag.fragment) {
- params = _self._generateRetrieveParams(registeredFrag);
- registeredFrag.fragment.retrieve(params);
- } else {
- this._retrieveFragment();
- }
- }
- }
-
- this._postFragRetrieve = function(event) {
- var fragContentDiv = null;
-
- if (event && event.source && event.source.error) {
- fragContentDiv = $(event.source.div);
- if (fragContentDiv) {
- fragContentDiv.style.display = "none";
- }
- }
- _self._retrieveFragment();
- }
- }
|