// Licensed Materials - Property of IBM // // IBM Cognos Products: cogadmin // // (C) Copyright IBM Corp. 2005, 2013 // // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. // // // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated). //---------------------------------------------------------- com.cognos.admin.ObjectFactory("com.cognos.admin.widget"); /* J2HTMLEvent class deal with all the stuff related to the event handler and its management method * @param context is a object contains all information of a widget, table widget, dialog widget and etc * The interface of context: * table - the id of the widget container * recipe - the mapping of the cogType and event * fragment - fragment object * set/getPublicVariables - function, a way to set and get a widget scope public variables */ com.cognos.admin.widget.Event = function (context) { //shareing the variables for general use. this.tools = com.cognos.admin.util.Tools; this.context = context; this.table = context.table; this.recipes = context.recipe.getAll(); this.fragmentRecipes = context.fragmentRecipe.getAll(); this.fragment = context.fragment; if (!this.fragment._retrieve) { this.fragment._retrieve = this.fragment.retrieve; this.fragment.retrieve = this._evt_retrieve; } //the init for costomized attribues of j2html table, will be modified after walking through the DOM. this.numRow = 1; this.contentTable = this.fragment.id.toString() + "contentTable";; this.frameTable = this.fragment.id.toString() + "frame";; //internal used private variables. this.selectedRecipe = new this.tools.Recipe(); this.selectedFragmentRecipe = new this.tools.Recipe(); this.filterColumns = []; this.expandRowsExp = []; this.tooltip; this.menuCache = []; this.extensionFuncs = []; //state management this.state = new com.cognos.admin.util.ClientState(this.fragment.id); this.state.restoreStates(); //backup the object in global scope. com.cognos.admin.publicParam.callbackEnv = this; }; com.cognos.admin.widget.Event.VERSION = "0.1.0"; com.cognos.admin.widget.Event.EVENT = "_EVENT_"; com.cognos.admin.widget.Event.TAG = "_TAG_"; com.cognos.admin.widget.Event.ENV = "_ENV_"; com.cognos.admin.widget.Event.prototype = { /* * recusive func to mount the event handlers to a proper cogType according to the recipe. */ mountEvent : function (targetElt){ for (var i = 0; i < this.recipes.length; i++ ){ if (targetElt.getAttribute("cogType").toLowerCase() == this.recipes[i].cogType.toLowerCase()) { var functionName = "_evt_" + this.recipes[i].fnName; if (typeof this[functionName] == "function") { if (this.recipes[i].evt == "init") this[functionName](targetElt); else { var evtHandler = _F_Document.associate(targetElt,this,functionName); xAddEventListener(targetElt,this.recipes[i].evt,evtHandler,this.recipes[i].cap); this.selectedRecipe.add(targetElt,evtHandler,this.recipes[i].evt,this.recipes[i].cap); } } } } }, unregisterEvent : function () { xAddEventListener(window,"unload",_F_Document.associate(window,this,"_evt_unloadTable"),false); if (this.fragment){ this.fragment.addEventListener("fragment.unload",_F_Document.associate(this.fragment,this,"_evt_unloadTable"),false); } }, /* * register the outter function as event handler */ addCogEvent : function (funcName,func) { com.cognos.admin.widget.Event.prototype["_evt_"+funcName] = func; }, /* * 1) a string with single quote, which will call the internal pre-defined function ; * 2) a function name (without any quote), which should be defined by developer somewhere in a legal scope; * 3) a anonymous function beginning with "function () { function body here }" */ parseFunc : function (funcName,run) { var run = run || false; var retFlag = false; var func; if (typeof funcName == "function"){ func = funcName; retFlag = true; } else if (typeof funcName == "string" && typeof this["_evt_"+funcName] == "function") { func = this["_evt_"+funcName]; } if (func && run) func(); return retFlag; }, //************************Event handler depot begin*********************************// /* * unregister all the events when the fragment or window object unload */ _evt_unloadTable : function (e,tag) { var sr = this.selectedRecipe.getAll(); var sfr = this.selectedFragmentRecipe.getAll(); for (var i = 0; i < sr.length; i++){ xRemoveEventListener(sr[i].cogType,sr[i].evt,sr[i].fnName,sr[i].cap); } if (this.fragment) { for (var i = 0; i < sfr.length; i++){ this.fragment.removeEventListener(sfr[i].evt,sfr[i].fnName,sfr[i].cap); } this.fragment.removeEventListener("fragment.unload",_F_Document.associate(this.fragment,this,"_evt_unloadTable"),false); } com.cognos.admin.publicParam.evtLoaded[this.fragment.id] = false; if (com.cognos.admin.config.debug){ _F_log("D","Unregister all event handlers.") } }, /* Cogtable initializer, init the CogTable scope public variables * bogus event handler */ _evt_initTable : function (tag) { var initParam = this.tools.cogEval(tag); var self = this; if (!initParam) return false; //init for fragment event if (this.fragment) _initFragment(this.fragmentRecipes); //parser the parameters to init the cogTable for (var o in initParam){ if (typeof initParam[o] !== 'function') { switch (o) { case "fragment": if (this.fragment) _initFragment(initParam[o]); break; case "publicVar": if (initParam[o]){ for (var key in initParam[o]){ if (typeof initParam[o][key] !== 'function') { this.context.setPublicVariables(key,initParam[o][key]); } } } break; case "state": if (initParam[o]){ for (var key in initParam[o]){ if (typeof initParam[o][key] !== 'function') { this.state.setState(key,initParam[o][key]); } } } break; default: } } } function _initFragment(recipe){ if (recipe && recipe instanceof Array){ for (var i = 0; i < recipe.length; i++){ var funcName = recipe[i].fnName; if (funcName) { //register the event against it's function var evtHandler; if (typeof self["_evt_" + funcName] == "function") { evtHandler = _F_Document.associate(self.fragment, self, "_evt_" + funcName); } else { continue; } //hack for the incompleted implementation in Fragment for resize event handler. if (recipe[i].evt == "fragment.resize") self.fragment.onresize = evtHandler; self.fragment.addEventListener(recipe[i].evt, evtHandler, recipe[i].cap); self.selectedFragmentRecipe.add ("fragment", evtHandler, recipe[i].evt, recipe[i].cap); } if (recipe[i].evtContext) { //fire the event self.fragment.raiseEvent(recipe[i].evt,recipe[i].evtContext); } } } } }, _evt_maskElement : function (tag){ var initParam = this.tools.cogEval(tag); var hint = (initParam && initParam.title) ? initParam.title : ""; var masker = new this.tools.Masker(tag,hint,this.fragment); masker.show(true); }, /* init the filter column * bogus event handler * */ _evt_initFilter : function (tag){ var initParam = this.tools.cogEval(tag) if (!initParam) return false; if (!(this.filterColumns[initParam.filterName] instanceof Array)) this.filterColumns[initParam.filterName] = []; this.filterColumns[initParam.filterName].push (tag); tag.value = initParam.value; }, /* Generic toggler * */ _evt_toggle : function (e,tag){ xStopPropagation (e); var tagId = tag.id.replace(/_toggle/i,"_row"); var exp = "^" + tagId + "_\\d+"; var isDsp = true; var key = this.tools.getKey($(tagId)); if (tag.src.indexOf("expand.gif") > -1){ exp += "$"; this.expandRowsExp.push (exp); if (key){ this.state.addState("expandRows",key,true); } exp = this.expandRowsExp; } else { isDsp = false; _F_Array.remove(this.expandRowsExp,exp+"$"); this.state.removeState ("expandRows",key,true); } var gelt = new this.tools.GetElementsById(exp,this.table,true) gelt.getElt(); var selectedElts = gelt.selectedElements; for (var i = 0; i < selectedElts.length; i++){ if (selectedElts[i].id.indexOf("_row") > -1) { this.tools.revertDsp(selectedElts[i],isDsp); } } this.tools.revertImg (tag); }, /* * TODO:Toggle Buttons */ _evt_toggleButton : function (e,tag){ var initParam = this.tools.cogEval(tag); var tb = new this.tools.ToggleButton(tag); tb.click(); if (initParam && initParam.action) { this._evt_doAction(e,tag,initParam.action); } }, /* Group checkbox toggler * */ _evt_toggleCheckbox : function (e,tag){ xStopPropagation (e); var initParam = this.tools.cogEval(tag); var exp = "^" + tag.id + "_\\d+"; var tools = com.cognos.admin.util.Tools; var gelt = new tools.GetElementsById(exp,this.table,true); gelt.getElt(); var selectedElts = gelt.selectedElements; //include the current element itself selectedElts.push(tag); for (var i = 0; i < selectedElts.length; i++){ var key = this.tools.getKey(selectedElts[i]); if (key) { selectedElts[i].checked = tag.checked; if (tag.checked){ this.state.addState(com.cognos.admin.util.ClientState.GROUP_ACTION_ROWS,key,true) } else { this.state.removeState(com.cognos.admin.util.ClientState.GROUP_ACTION_ROWS,key,true); } } } if (initParam) { this._evt_doAction(e,tag,initParam); } }, /* Toggle all * */ _evt_toggleAll : function (e,tag){ if (!tag.src) return false; xStopPropagation (e); var initParam = this.tools.cogEval(tag); var toLevel = 1; if (initParam && initParam.toLevel) toLevel = initParam.toLevel; var tools = this.tools; var groupDsp = tools.revertImg (tag); var exp = tag.id + "_"; var tagStatus = tag.src.indexOf("collapse.gif") > -1 var gt = new tools.GetElementsById(exp,this.table,false); gt.getElt(); var selectedElts = gt.selectedElements; this.state.clearState("expandRows",true); for (var i = 0; i < selectedElts.length; i++){ if (selectedElts[i].id) { //for the toggler icons if (selectedElts[i].id.indexOf("_toggle") > -1) { this.tools.revertImg(selectedElts[i],tagStatus) //for the rows } else if (selectedElts[i].id.indexOf("_row") > -1) { //do the expanding and collapsing if it's not a group if (this.tools.getLevelById(tag.id,selectedElts[i].id) > toLevel) { this.tools.revertDsp(selectedElts[i],tagStatus); } else { //otherwise, collect the state for expanding group if (tagStatus) { var key = this.tools.getKey(selectedElts[i]); this.state.addState("expandRows",key,true); } } //TODO:for other togglees } /*else { var togglees = this.context.getPublicVariables("togglees"); if (togglees) { for (var j = 0; j < togglees.length; j++) { if (selectedElts[i].id.indexOf("_"+togglees[j].name) > -1) { var dsp = (togglees[j].isReversed)? tagStatus : !tagStatus; this.tools.revertDsp(selectedElts[i],dsp); } } } }*/ } } }, /* doAction * */ _evt_doAction : function (e,tag,param) { var param = e.payload || param || this.tools.cogEval(tag); var self = this; var evt = new xEvent(e); var ret; //for stop propergation if (!param || (param.stopPropagation == undefined) || (param.stopPropagation == true) || (param.stopPropagation == 'true')){ xStopPropagation (evt); } //for keyboard event processer, only allowed the registered event key to trigger the action. if (evt.type == "keypress"){ var charCode = (typeof e.which == "number") ? e.which : e.keyCode if (param && param.eventKey != undefined && charCode == dojo.keys[param.eventKey]) { evt.preventDefault(); } else { return; } } if (param && param.grpAction){ var selectedGrpKeys = this.state.getState(com.cognos.admin.util.ClientState.GROUP_ACTION_ROWS,true); if (!selectedGrpKeys || (selectedGrpKeys.length == 0)) { alert(J2H.GLB.IDS_GRP_ACTION_CHOOSE_ENTRY); return; } } if (param && param.prompt){ var binded = param.prompt.binded || 'none'; if (binded == 'none' || ((binded == 'binded') && tag.checked) || ((binded == 'reversed') && !tag.checked)){ switch (param.prompt.type){ case "message": alert (param.prompt.text); break; case "confirm": if (!confirm (param.prompt.text)){ xPreventDefault(evt); tag.checked = !tag.checked; return; } } } } /* this recusive function will walk through the JSON for cascading menu, * and attach the anonymous function for each click event, inside of which, it recall the main * function (_evt_doAction) with different arguments. See the j2html JS behaviour controller * framework functional SPEC for more details. */ function parseItems (oItems) { for (var i = 0; i < oItems.length; i++){ var oAction = oItems[i].action; if (oAction && !self.parseFunc(oAction)) oItems[i].action = getAction (oAction); if (oItems[i].items) parseItems(oItems[i].items); } }; //this function is introduced to avoid the closure in parseItems. function getAction (oAction) { //TODO: the "e" cannot get the event from popup menu return function (e) { var oEvent = new xEvent(e); var oTag = oEvent.target; if (com.cognos.admin.config.debug){ _F_log ("D","Event for popup menu:"); _F_log ("D",oEvent); } self._evt_doAction (oEvent,oTag,oAction); }; }; //apply a predefined variables to a reserved key word in args function applyArgs (args) { if (args){ for (var i=0; i 1) this.fragment.raiseEvent("cogadmin.fragment.windowstate.change",e.payload.newWindowState); }, /* * */ _evt_fgmtModeChange : function (e,tag) { if (com.cognos.admin.config.debug){ _F_log('D','fragment.mode.change'); } }, _evt_menuSelect : function (e,tag) { var menuDef = e.payload.menuDef; var selectedIdx = e.payload.selectedIdx; var target = $(this.fragment.id+menuDef.id); if (target) { target[target.getAttribute("textAttr")] = menuDef.items[selectedIdx].title; var valueAttr = document.createAttribute("pvalue"); valueAttr.nodeValue = menuDef.items[selectedIdx].id; target.setAttributeNode(valueAttr); } if (com.cognos.admin.config.debug){ _F_log('D','uicommon.menu.select'); } }, //*****************************init for fragment end*********************************** //*****************************init for j2html customized fragment event start*********************************** _evt_cadminFragRefresh : function (e,tag) { if (com.cognos.admin.config.debug){ _F_log('D','cogadmin.refresh'); } this._evt_fgmtRefresh(e,tag); }, _evt_distCadminFragWschange : function (e,tag) { if (com.cognos.admin.config.debug){ _F_log('D','_evt_distCadminFragWschange'); } if ($(this.contentTable)){ if (e.source.id != this.fragment.id) { if (this.numRow > 1) { switch (e.payload){ case "minimized": this.fragment.cogadminWindowState = "extended"; this.state.setState(com.cognos.admin.util.ClientState.COGADMIN_WINDOW_STATE,"extended",true); this.tools.adjustTableHeight(this.frameTable,this.contentTable,"extended",this.numRow); break; case "normal": this.fragment.cogadminWindowState = null; this.state.clearState(com.cognos.admin.util.ClientState.COGADMIN_WINDOW_STATE,true); this.tools.adjustTableHeight(this.frameTable,this.contentTable,"normal",this.numRow); break; default: } } } } }, _evt_retrieve : function(sParams, dest, synchronous) { if (com.cognos.admin.config.debug){ _F_log('D','_evt_retrieve'); } paramsObj = new ParamsObject(sParams); this._retrieve(paramsObj, dest, synchronous); function ParamsObject (params) { this.value = params; this.valueOf = function() { return this.value; } this.indexOf = function(value) { return this.value.indexOf(value); } } } //*****************************init for cogadmin customized fragment event end*********************************** };