123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- // Licensed Materials - Property of IBM
- //
- // IBM Cognos Products: cpscrn
- //
- // (C) Copyright IBM Corp. 2005, 2016
- //
- // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- //
- //
- /* Copyright IBM Corp. 2009 2011Rights Reserved. */
- /**
- * @author rjd
- */
- dojo.declare("com.ibm.cognos.bux.fragmentBridge", null,
- {
- resources:
- [
- [ "SCRIPT", "$CGI$/dashboard/messages/messages/pfmessages?section=JS" ],
- [ "SCRIPT", "$WEB$/fragments/xdojo/core.js" ],
- [ "SCRIPT", "$WEB$/fragments/cps.core.js" ],
- [ "SCRIPT", "$WEB$/common/framework/validator/CValidator.js" ]
- ],
- onLoad: function()
- {
- this.domID = "_" + this.iContext.widgetId + "_";
- if (window._F_Config === undefined)
- {
- var config = this.iContext.bridgeConfig;
- var gateway = window.bux ? config.gateway : this.iContext.io.rewriteURI(config.gateway);
- var webcontent = window.bux ? config.webContent : this.iContext.io.rewriteURI(config.webContent);
- window._F_Config = new Object();
- _F_Config.init = function()
- {
- this.gateway = gateway;
- this.application = config.application;
- this.webContent = webcontent;
- this.appGateway = this.gateway + this.application;
- this.version = "1.1.0";
- // signed values for portlet bridge
- this.bridgeGateway = config.bridgeGateway;
- this.bridgeWebContent = config.bridgeWebContent;
- };
- _F_Config.init();
- }
-
- _IW_Resource.initialize();
- // detect and download fragment controller js
- var i, l = this.resources.length;
- for (i = 0; i < l; i++)
- {
- _IW_runQueue.add(_IW_Resource, _IW_Resource.loadResource, [ this.resources[i][0], this._expandMacros(this.resources[i][1]) ]);
- }
- // queue the creation and retrieval of fragment
- _IW_runQueue.add(this, this._loadFragment, []);
-
- _IW_runQueue.run();
- // Bux specific code
- this.setBuxMenu();
- },
- onUnload: function()
- {
- if (this.fragment)
- {
- this.fragment.destroy();
- }
- },
- onView: function()
- {
- if (this.fragment)
- {
- this.fragment.setMode("view");
- }
- },
- onEdit: function()
- {
- if (this.fragment)
- {
- this.fragment.setMode("edit");
- }
- },
- setBuxMenu: function() {
- // Add refresh to bux menu
- if (window.bux && BUXMSG !== undefined) {
- this.iContext.iEvents.fireEvent("com.ibm.bux.widgetchrome.toolbar.init", null, [ this.getMenuObject() ]);
- }
- },
- getMenuObject: function() {
- return {
- "_root": [{
- "name" : "refresh",
- "label" : BUXMSG.GEN.IDS_GEN_REFRESH,
- "iconClass" : "bux-refresh-button",
- "action" : "widget.refresh",
- "items" : null
- }]
- };
- },
- onToolbarAction: function(iEvent) {
- if (String(iEvent.payload) === "widget.refresh") {
- this.fragment.raiseEvent("fragment.refresh");
- }
- },
- onWidgetRefresh: function() {
- this.fragment.raiseEvent("fragment.refresh");
- },
- _expandMacros: function(text)
- {
- return text.replace(/\$CGI\$/g, _F_Config.gateway).replace(/\$WEB\$/g, _F_Config.webContent);
- },
- /*
- * This method hooks up event handlers that will remove the "onBeforeUnload" handler and
- * put it back accordingly when the user clicks somwhere inside the fragment content div.
- * IE will trigger this event when we have a href, form submit event if the location is a javascript function.
- * see defect: 257696
- */
- _attachHandlerForIEBeforeUnloadFix: function(div){
- if (dojo.isIE && div && !div.beforeUnloadFixAttached){
- xAddEventListener(div,"mouseup", _IW_mouseUpHandler);
- xAddEventListener(div,"keydown", _IW_keyDownHandler);
- xAddEventListener(div,"keyup", _IW_keyUpHandler);
- div.beforeUnloadFixAttached = true;
- }
- },
- _loadFragment: function()
- {
-
- var overrideFragmentController = function(){
- if (fragment && !fragment.prototype.widgetOverride){
- fragment.prototype.widgetOverride = true;
-
- /* Override BUX specific functionality */
- if (window.bux){
- /*Needed for Ajax response error handling.*/
- fragment.prototype._processFragmentXMLBase = fragment.prototype.processFragmentXML;
- fragment.prototype.processFragmentXML = function(){
- this._processFragmentXMLBase.apply(this, arguments);
- if (this._ajaxDeferredCall){
- this._ajaxDeferredCall = null;
- }
- }
-
- /*Error handling override*/
- fragment.prototype.processError = function(code, message, details){
- if (this._ajaxDeferredCall){
- /*use the deferred call if we have one (used in BUX)*/
- var e = new Error();
- if (code == "invalidCredentials"){
- e.code = "camAuthUserRecoverable";
- }else {
- e.code = code;
- }
-
- if (message){
- e.message = message;
- }else{
- e.message = dojo.string.substitute(BUXMSG.EDG.IDS_EDG_HTTP_ERROR, [code])
- }
- e.details = details;
- this._ajaxDeferredCall.reject(e);
- } else {
- _F_alert(code + ":\n" + message, this.title);
- }
- }
-
- /*Replace fragment alert with BUX message box*/
- window._F_alert = function(msg, title){
- bux.messageBox(bux.MB_ERROR, title, msg, null);
- }
-
- /*Replace fragment _F_Ajax with bux xhr to handle errors properly (e.g. timeout)*/
- var baseAjaxPrototype = _F_Ajax.Request;
- if (!baseAjaxPrototype.widgetOverride){
- _F_Ajax.Request.prototype.widgetOverride = true;
- _F_Ajax.Request = function(url, options, caller){
- // we don't care about the parsed dojo response, we simply set the 'handleAs' to 'text' in IE for performance reason (when the response is html). Firefox is faster when it's set to xml
- var requestObject = {handleAs: dojo.isIE ? "text" : "xml"};
- requestObject.sync= (options.asynchronous !== undefined) ? !options.asynchronous : false;
-
- requestObject.headers ={
- "X-Fragment-Version": _F_Config.version,
- "X-Controller-Agent": "fragment/" + _F_Config.version,
- "Accept": "text/javascript, text/html, application/xml, text/xml, */*"
- };
- if (typeof options.parameters == "object"){
- requestObject.content = options.parameters;
- }
-
- var method = (options.method || "post").toLowerCase();
- if (method == 'post'){
- var contentType = options.contentType || "application/x-www-form-urlencoded";
- var encoding = options.encoding;
- requestObject.headers['Content-Type'] = contentType + (encoding ? '; charset=' + encoding : '');
- }
-
- url = _F_Config.expandMacros(url);
- if ((method == "post" || method == "put")){
- if (options.postBody === undefined){
- var parts = url.split('?');
- url = parts[0];
- options.postBody = parts[1];
- }
- if (method == 'post'){
- requestObject.postData = options.postBody;
- } else if (method == 'put'){
- requestObject.putData = options.postBody;
- }
- }
-
- requestObject.url = url;
-
- var _self = this;
- requestObject.load = function(content, ioargs){
- _self.transport = ioargs.xhr;
- var fdc = null;
- if (caller){
- /* Give the caller a deferred call so it can control the error handling*/
- fdc = new dojo.Deferred();
- caller._ajaxDeferredCall = fdc;
-
- }
- /* start ajax processing.*/
- options.onSuccess.call(_self, _self.transport);
- if (caller){
- return fdc;
- }
- }
-
- var dfd;
- if (method == 'get'){
- dfd = bux.xhrGet(requestObject);
- }else if (method == 'put'){
- dfd = bux.xhrPut(requestObject);
- }else{
- dfd = bux.xhrPost(requestObject);
- }
- dfd.then(null, function(e){bux.errorBox(e.message, null, e);});
- }
-
- _F_Ajax.Request.prototype = baseAjaxPrototype.prototype;
- baseAjaxPrototype = null;
- }
-
- }
- }
- }
-
- overrideFragmentController();
-
- _F_init();
- // make sure there's a dummy root fragment for eventing
- if (fragment["FRAG_"] === undefined)
- new fragment("/ROOT", "FRAG_");
-
- // create destination div
- var id = "FRAG_" + this.iContext.widgetId + "_";
- var div = $(id + "content");
- this._attachHandlerForIEBeforeUnloadFix(div);
-
- div.setAttribute("fragmentid", id);
- var sPath = this.iContext.bridgeConfig.fragmentPath;
- sPath += (sPath.indexOf('?') == -1) ? '?' : '&';
- // Tell the fragment rewriter the values for the proxied gateway and webcontent
- sPath += "frag-gateway=" + _F_Strings.urlEncode(_F_Config.gateway) + "&frag-webcontent=" + _F_Strings.urlEncode(_F_Config.webContent);
- // create fragment object from path provided by iWidget content
- this.fragment = new fragment(sPath, id);
-
- // give fragment access to iwidget
- this.fragment.iContext = this.iContext;
- this.fragment.iWidget = this;
- this._attachFragmentEvents();
- // hook fragment transient property mechanism
- if (!window.bux){
- this.fragment.encodedTransients = this.fragmentEncodedTransients;
- }
- this.fragment.transientUpdate = this.fragmentTransientUpdate;
- this.fragment.transientUpdateList = this.fragmentTransientUpdateList;
- // copy itemset items to the fragment custom properties
- this.customInit();
- // Ajax
- this.fragment.retrieve();
- return false;
- },
- /**
- * customInit - copy attribute itemSet to fragment.customProperties
- * @returns boolean - true if there were items to copy
- */
- customInit: function() {
- this.customUpdated = false;
- // copy itemset attributes as custom properties
- var att = this.iContext.getItemSet("attributes");
- var names = att.getAllNames();
- if (names) {
- var params = {}, hasParams = false, i, l = names.length;
- for (i = 0; i < l; i++) {
- if (names[i] != "version" && names[i] != "path" && names[i] != "undefined") {
- params[names[i]] = att.getItemValue(names[i]);
- hasParams = true;
- }
- }
- if (hasParams) {
- this.fragment.customProperties = params;
- }
- }
- return hasParams;
- },
- /**
- * fragmentCustomUpdate - update attribute itemSet from fragment.customProperties
- */
- customUpdate: function() {
- if (this.customUpdated && this.fragment.customProperties) {
- var att = this.iContext.getiWidgetAttributes();
- for (var name in this.fragment.customProperties) {
- att.setItemValue(name, this.fragment.customProperties[name]);
- }
- att.save();
- this.customUpdated = false;
- }
- },
- /*
- * This methods hooks up all fragment event handler needed to by the iWidget bridge.
- * We currently hook up handler for:
- * - 'fragment.mode.change'
- * - 'fragment.retrieve.after'.
- */
- _attachFragmentEvents: function()
- {
- if (this.fragment)
- {
- this.fragment.addEventListener("fragment.mode.change", this._fragmentModeChangeHandler, false);
- this.fragment.addEventListener("fragment.retrieve.after", this._fragmentRetrieveAfterHandler, false);
- this.fragment.addEventListener("fragment.title.change", this._fragmentTitleChangeHandler, false);
- }
- },
- /*
- * Called when "fragment.title.change' occurs.
- * The proper widget event will be fired
- *
- */
- _fragmentTitleChangeHandler: function(ev){
- this.iWidget.iContext.iEvents.fireEvent( "com.ibm.bux.widget.action", null, {
- action: "chrome.property.set",
- name: "title",
- value: ev.payload.newTitle
- });
- },
- /*
- * Called when "fragment.retrieve.after' occurs.
- * If the fragment customs are different than the one saved by the widget,
- * we will update the iWidget and save the attributes.
- *
- * NOTE: Context of function is fragment, use this.iWidget to access this class
- */
- _fragmentRetrieveAfterHandler: function(ev)
- {
- // update windows state in case the widget was maximized before the fragment was loaded
- this.iWidget.updateWindowState();
-
- this.iWidget.customUpdate();
- },
- /*
- * Called when "fragment.mode.change' occurs.
- * In this method we will switch the fragment div to the edit div if we are goinf into edit mode.
- *
- * NOTE: Context of function is fragment, use this.iWidget to access this class
- */
- _fragmentModeChangeHandler: function(ev)
- {
- try
- {
- if (ev.payload.newMode == "edit")
- {
- this.viewDiv = this.div;
- this.div = this.id + "edit_content";
- var editDiv = $(this.div);
- this.iWidget._attachHandlerForIEBeforeUnloadFix(editDiv);
- $(this.viewDiv).innerHTML = "";
- }
- else if (ev.payload.newMode == "view" && ev.payload.currentMode == "edit")
- {
- this.div = this.viewDiv;
- $(this.div).innerHTML = $(this.id + "edit_content").innerHTML;
- $(this.id + "edit_content").innerHTML = "";
-
- this.iContext.iEvents.fireEvent("onModeChanged", null, { newMode: "view" });
- // at this point customs have not yet been updated, signal copy to be made in "after" handler
- this.iWidget.customUpdated = true;
- // special builder event to close lotus mashups dialog
- // event constant: com.ibm.mm.builder.Topics.Widget_ConfigurationFinished
- dojo.publish("/widget/action/finished", [this.iContext.widgetId]);
- }
- }
- catch (e)
- {
- _F_alert( e)
- }
- },
- /*
- * Hook for 'fragment.encodedTransients'
- *
- * Note: "this" is the fragment object
- *
- */
- fragmentEncodedTransients: function()
- {
- // assemble everything as url
- var s = "";
- for (var name in this.transientState)
- s += "&" + _F_Strings.urlEncode(name) + "=" + _F_Strings.urlEncode(this.transientState[name]);
- // Tell the fragment rewriter the values for the proxied gateway and webcontent
- s += "&frag-gateway=" + _F_Strings.urlEncode(_F_Config.gateway) + "&frag-webcontent=" + _F_Strings.urlEncode(_F_Config.webContent);
- s += "&bridge-gatewayURL=" + _F_Strings.urlEncode(_F_Config.bridgeGateway) + "&bridge-webContentURL=" + _F_Strings.urlEncode(_F_Config.bridgeWebContent);
- return s;
- },
- /*
- * Hook for 'fragment.transientUpdate'
- *
- * Note: "this" is the fragment object
- *
- */
- fragmentTransientUpdate: function(sName, sValue, sScope, sChannel)
- {
- this.iContext.iEvents.fireEvent("com.ibm.cognos.cps.transients", "string", sName);
- },
- fragmentTransientUpdateList: function(list)
- {
- this.iContext.iEvents.fireEvent("com.ibm.cognos.cps.transients", "string", list);
- },
- onCognosTransients: function(evt)
- {
- var list = evt.payload;
- if (list && list.length > 0) {
- var i, l = list.length;
- for (i = 0; i < l; i++) {
- this.fragment.transientUpdateCheck(list[i].name, list[i].value, list[i].channel, true);
- }
- }
- },
-
- onWidgetResize: function(evt){
- this.isMaximized = evt.payload.maximized;
- this.updateWindowState();
- },
-
- updateWindowState: function(){
- if (this.fragment && this.fragment.loaded){
- if (this.fragment.mode == "view"){
- if (this.isMaximized){
- this.fragment.setWindowState("maximized");
- }else{
- this.fragment.setWindowState("normal");
- }
- }
- }
- }
- });
- function _IW_removeBeforeUnload(){
- if (window.onbeforeunload){
- window._IW_onbeforeunloadBackup = window.onbeforeunload;
- window.onbeforeunload = null;
- setTimeout("window.onbeforeunload = window._IW_onbeforeunloadBackup;", 10);
- }
- };
- function _IW_mouseUpHandler(){
- _IW_removeBeforeUnload();
- };
- function _IW_keyDownHandler(){
- if (window.event.keyCode == 13){
- _IW_removeBeforeUnload();
- }
- };
- function _IW_keyUpHandler(){
- if (window.event.keyCode == 32){
- _IW_removeBeforeUnload();
- }
- };
- function arrayIndexOf(arr, value, begin, strict)
- {
- for (var i = +begin || 0, l = arr.length; i < l; i++)
- if (arr[i]===value || strict && arr[i]==value)
- return i;
- return -1;
- };
- if (window._IW_runQueue === undefined) {
-
- /**
- * _IW_Resource : Resource loading (LINK & SCRIPT)
- */
- var _IW_Resource =
- {
- initialized : false,
-
- loaded: new Array(),
-
- loadState: 0,
- initialize: function()
- {
- if (!this.initialized) {
-
- var nodes = document.getElementsByTagName("SCRIPT");
- var l = nodes.length;
- for (var i = 0; i < l; i++)
- {
- var attrs = nodes[i].attributes;
- for (var j = 0; j < attrs.length; j++)
- if (attrs[j].nodeName == "src")
- {
- this.loaded.push(attrs[j].nodeValue);
- break;
- }
- }
-
- var nodes = document.getElementsByTagName("LINK");
- var l = nodes.length;
- for (var i = 0; i < l; i++)
- {
- var attrs = nodes[i].attributes;
- for (var j = 0; j < attrs.length; j++)
- if (attrs[j].nodeName == "href")
- {
- this.loaded.push(attrs[j].nodeValue);
- break;
- }
- }
-
- this.initialized = true;
- }
- },
-
- loadResource: function(tagName, href, arg1)
- {
- if (arrayIndexOf(this.loaded, href) == -1)
- {
- var tagName = tagName.toUpperCase();
- var head = document.getElementsByTagName('HEAD').item(0);
- var node = document.createElement(tagName);
- var _self = this;
-
- switch (tagName)
- {
- case "SCRIPT":
- this.loadState++;
- node.onload = node.onreadystatechange = function()
- {
- if (this.readyState && !(this.readyState == 'loaded' || this.readyState == 'complete'))
- {
- return;
- }
- if (arrayIndexOf(_self.loaded, href) != -1)
- {
- return;
- }
- _self.loaded.push(href);
- _self.loadState--;
- if (_self.loadState == 0) {
- _IW_runQueue.resume();
- }
- };
- node.type = 'text/javascript';
- node.src = href;
- head.appendChild(node);
- break;
-
- case "LINK":
- node.type = 'text/css';
- node.rel = "stylesheet";
- if (arg1 != null && arg1 != "")
- node.media = arg1;
- node.href = href;
- head.appendChild(node);
- _self.loaded.push(href);
- _IW_runQueue.resume();
- break;
-
- default:
- _IW_log("unsupported tag named: " + tagName);
- break;
- }
-
- // indicate to stop the run of the queue
- return true;
- }
-
- return false;
- }
- };
- //------------------------------------------------------------------------------
- // execute a method on an object with arguments
- var _IW_Queue =
- {
- };
- _IW_Queue.Runnable = function(obj, func, args)
- {
- this.obj = obj;
- this.func = func;
- this.args = args;
- };
- _IW_Queue.Runnable.prototype =
- {
- run: function()
- {
- var stopRun = this.func.apply(this.obj, this.args);
- this.func = null;
- this.obj = null;
- this.args = null;
- return stopRun;
- }
- };
- _IW_Queue.Queue = function()
- {
- this.head = null;
- this.tail = null;
- };
- _IW_Queue.Queue.prototype =
- {
- add: function(obj)
- {
- var newEntry =
- {
- value: obj,
- next: null
- };
- if (this.tail)
- this.tail.next = newEntry;
- else
- this.head = newEntry;
- this.tail = newEntry;
- },
-
- hasNext: function()
- {
- return (this.head != null);
- },
-
- next: function()
- {
- var entry = this.head;
- var obj = entry.value;
- this.head = entry.next;
- if (entry.next == null)
- this.tail = null;
-
- entry.next = null;
- delete (entry);
- return obj;
- }
- };
- _IW_Queue.RunQueue = function()
- {
- this.queue = new _IW_Queue.Queue();
- this.isRunning = false;
- };
- _IW_Queue.RunQueue.prototype =
- {
- add: function(obj, func, args)
- {
- var canRun = !this.queue.hasNext();
- this.queue.add(new _IW_Queue.Runnable(obj, func, args));
- return canRun;
- },
- run: function()
- {
-
- if (!this.isRunning){
- this.isRunning = true;
- this.resume();
- }
-
- },
-
- resume: function()
- {
- while (this.queue.hasNext())
- if (this.queue.next().run()){
- break;
- }
-
-
- this.isRunning = this.queue.hasNext();
- }
- };
- window._IW_runQueue = new _IW_Queue.RunQueue();
- }
|