eventHandlerUtil.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**************************************************************************
  2. * Licensed Materials - Property of IBM *
  3. * *
  4. * IBM Cognos Products: AGS *
  5. * *
  6. * (C) Copyright IBM Corp. 2005, 2017 *
  7. * *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure *
  9. * restricted by GSA ADP Schedule Contract with IBM Corp. *
  10. ***************************************************************************/
  11. // -----------------------------------------------------------------------------
  12. //
  13. // --- event listener utility ---
  14. //
  15. // -----------------------------------------------------------------------------
  16. //object to ease the chaining of event handlers, in order to allow users to add more than one handler to a control
  17. function EventHandlerChain(name, handler){
  18. this.eventName = name;
  19. this.functionRefs = new Array();
  20. this.addHandler(handler);
  21. }
  22. EventHandlerChain.prototype.setEventHandlerChain = function(an_object){
  23. if (an_object) {
  24. if (cf.browserCheck.isIE5Up()) {
  25. an_object.attachEvent("on" + this.eventName,this.handleEvent);
  26. }
  27. else if (cf.browserCheck.isNav6Up()) {
  28. an_object.addEventListener(this.eventName,this.handleEvent,true);
  29. }
  30. an_object[eventHandlerChainUtil.getPropertyName(this.eventName)] = this;
  31. }
  32. }
  33. EventHandlerChain.prototype.addHandler = function(handler){
  34. this.functionRefs.push(handler);
  35. }
  36. EventHandlerChain.prototype.handleEvent = function(event){
  37. if(!this.eventName){
  38. //find the right instance
  39. if (!event) event=arguments.callee.caller.arguments[0];
  40. var control=event.srcElement || event.currentTarget || event.target;
  41. var evtName = eventHandlerChainUtil.getPropertyName(event.type);
  42. var ehc = eventHandlerChainUtil.getEventHandlerChain(control, evtName);
  43. if (ehc) {
  44. ehc.handleEvent(event);
  45. }
  46. return;
  47. }
  48. for(var i = 0; i < this.functionRefs.length; i++){
  49. if(this.functionRefs[i]){
  50. if (typeof this.functionRefs[i] == "object" && this.functionRefs[i].update) {
  51. //callback on the object itself (used by eventNotification framework)
  52. this.functionRefs[i].update(event);
  53. } else {
  54. //should be a function
  55. this.functionRefs[i](event);
  56. }
  57. }
  58. }
  59. }
  60. function EventHandlerChainUtil(){
  61. return this.PROPERTY_NAME = "event_handler_chain";
  62. }
  63. EventHandlerChainUtil.prototype.getPropertyName = function(event_name){
  64. return this.PROPERTY_NAME + "_" + event_name;
  65. }
  66. EventHandlerChainUtil.prototype.getEventHandlerChain = function(an_object, property_name){
  67. //var property_name = this.getPropertyName(event_name);
  68. var ehc;
  69. //the event source object may not have had the handler applied
  70. if(an_object && an_object[property_name]){
  71. ehc = an_object[property_name];
  72. }else if(an_object){
  73. ehc = this.getEventHandlerChain(an_object.parentNode, property_name);
  74. }
  75. return ehc;
  76. }
  77. //utility to check and then initialise or add to event handler chain for this object event and handler
  78. EventHandlerChainUtil.prototype.doEventHandlerChain = function(an_object, event_name, handler){
  79. var property_name = this.getPropertyName(event_name);
  80. var ehc = this.getEventHandlerChain(an_object,property_name);
  81. if(!ehc){
  82. ehc = new EventHandlerChain(event_name, handler);
  83. ehc.setEventHandlerChain(an_object);
  84. }else{
  85. ehc.addHandler(handler);
  86. }
  87. }
  88. var eventHandlerChainUtil = new EventHandlerChainUtil();