DomEventMixin.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. if (typeof window.RadControlsNamespace == "undefined")
  2. {
  3. window.RadControlsNamespace = {};
  4. }
  5. if (
  6. typeof(window.RadControlsNamespace.DomEventMixin) == "undefined" ||
  7. typeof(window.RadControlsNamespace.DomEventMixin.Version) == null ||
  8. window.RadControlsNamespace.DomEventMixin.Version < 3
  9. )
  10. {
  11. RadControlsNamespace.DomEventMixin =
  12. {
  13. Version : 3, // Change the version when make changes. Change the value in the IF also
  14. Initialize : function(obj)
  15. {
  16. obj.CreateEventHandler = this.CreateEventHandler;
  17. obj.AttachDomEvent = this.AttachDomEvent;
  18. obj.DetachDomEvent = this.DetachDomEvent;
  19. obj.DisposeDomEventHandlers = this.DisposeDomEventHandlers;
  20. obj._domEventHandlingEnabled = true;
  21. obj.EnableDomEventHandling = this.EnableDomEventHandling;
  22. obj.DisableDomEventHandling = this.DisableDomEventHandling;
  23. obj.RemoveHandlerRegister = this.RemoveHandlerRegister;
  24. obj.GetHandlerRegister = this.GetHandlerRegister;
  25. obj.AddHandlerRegister = this.AddHandlerRegister;
  26. obj.handlerRegisters = [];
  27. },
  28. EnableDomEventHandling : function ()
  29. {
  30. this._domEventHandlingEnabled = true;
  31. },
  32. DisableDomEventHandling : function ()
  33. {
  34. this._domEventHandlingEnabled = false;
  35. },
  36. CreateEventHandler : function (methodName, fireIfDisabled)
  37. {
  38. var instance = this;
  39. return function (e)
  40. {
  41. if (!instance._domEventHandlingEnabled && !fireIfDisabled)
  42. {
  43. return;
  44. }
  45. return instance[methodName](e || window.event);
  46. }
  47. },
  48. AttachDomEvent : function(element, eventName, eventHandlerName, fireIfDisabled)
  49. {
  50. var eventHandler = this.CreateEventHandler(eventHandlerName, fireIfDisabled);
  51. // if such entry exist already - detach it first
  52. var oldRegister = this.GetHandlerRegister(element, eventName, eventHandlerName);
  53. if (oldRegister != null)
  54. {
  55. this.DetachDomEvent(oldRegister.Element, oldRegister.EventName, eventHandlerName);
  56. }
  57. // register the new entry
  58. var eventRegister = {
  59. "Element" : element,
  60. "EventName" : eventName,
  61. "HandlerName" : eventHandlerName,
  62. "Handler" : eventHandler
  63. };
  64. this.AddHandlerRegister(eventRegister);
  65. // now do the "real" job
  66. if (element.addEventListener)
  67. {
  68. element.addEventListener(eventName, eventHandler, false);
  69. }
  70. else if (element.attachEvent)
  71. {
  72. element.attachEvent("on" + eventName, eventHandler);
  73. }
  74. },
  75. DetachDomEvent : function(element, eventName, eventHandler)
  76. {
  77. var eventRegister = null;
  78. var eventHandlerName = "";
  79. if (typeof eventHandler == "string")
  80. {
  81. eventHandlerName = eventHandler;
  82. eventRegister = this.GetHandlerRegister(element, eventName, eventHandlerName);
  83. if(eventRegister == null)
  84. return;
  85. eventHandler = eventRegister.Handler;
  86. }
  87. if (!element)
  88. {
  89. return;
  90. }
  91. if (element.removeEventListener)
  92. {
  93. element.removeEventListener(eventName, eventHandler, false);
  94. }
  95. else if (element.detachEvent)
  96. {
  97. element.detachEvent("on" + eventName, eventHandler);
  98. }
  99. if (eventRegister != null && eventHandlerName != "")
  100. {
  101. this.RemoveHandlerRegister(eventRegister);
  102. eventRegister = null;
  103. }
  104. },
  105. DisposeDomEventHandlers : function()
  106. {
  107. for (var i=0; i < this.handlerRegisters.length; i ++)
  108. {
  109. var eventRegister = this.handlerRegisters[i];
  110. if (eventRegister != null)
  111. {
  112. this.DetachDomEvent(
  113. eventRegister.Element,
  114. eventRegister.EventName,
  115. eventRegister.Handler);
  116. }
  117. }
  118. this.handlerRegisters = [];
  119. },
  120. RemoveHandlerRegister : function(eventRegister)
  121. {
  122. try {
  123. var regIndex = eventRegister.index;
  124. for (var i in eventRegister)
  125. {
  126. eventRegister[i] = null;
  127. }
  128. this.handlerRegisters[regIndex] = null;
  129. } catch (e) {}
  130. },
  131. GetHandlerRegister : function(element, eventName, handlerName)
  132. {
  133. for (var i=0; i < this.handlerRegisters.length; i ++)
  134. {
  135. var eventRegister = this.handlerRegisters[i];
  136. if (eventRegister != null &&
  137. eventRegister.Element == element &&
  138. eventRegister.EventName == eventName &&
  139. eventRegister.HandlerName == handlerName
  140. )
  141. {
  142. return this.handlerRegisters[i];
  143. }
  144. }
  145. return null;
  146. },
  147. AddHandlerRegister : function(props)
  148. {
  149. props.index = this.handlerRegisters.length;
  150. this.handlerRegisters[this.handlerRegisters.length] = props;
  151. }
  152. }
  153. RadControlsNamespace.DomEvent = {};
  154. RadControlsNamespace.DomEvent.PreventDefault = function (e)
  155. {
  156. if (!e) return true;
  157. if (e.preventDefault)
  158. {
  159. e.preventDefault();
  160. }
  161. e.returnValue = false;
  162. return false;
  163. }
  164. RadControlsNamespace.DomEvent.StopPropagation = function (e)
  165. {
  166. if (!e) return;
  167. if (e.stopPropagation)
  168. {
  169. e.stopPropagation();
  170. }
  171. else
  172. {
  173. e.cancelBubble = true;
  174. }
  175. }
  176. RadControlsNamespace.DomEvent.GetTarget = function (e)
  177. {
  178. if (!e) return null;
  179. return e.target || e.srcElement;
  180. }
  181. RadControlsNamespace.DomEvent.GetRelatedTarget = function (e)
  182. {
  183. if (!e) return null;
  184. return e.relatedTarget || (e.type == "mouseout" ? e.toElement : e.fromElement);
  185. }
  186. RadControlsNamespace.DomEvent.GetKeyCode = function (e)
  187. {
  188. if (!e) return 0;
  189. return e.which || e.keyCode;
  190. }
  191. }
  192. //BEGIN_ATLAS_NOTIFY
  193. if (typeof(Sys) != "undefined")
  194. {
  195. if (Sys.Application != null && Sys.Application.notifyScriptLoaded != null)
  196. {
  197. Sys.Application.notifyScriptLoaded();
  198. }
  199. }
  200. //END_ATLAS_NOTIFY