HoverButton.JS 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. load: Apply a style to all controls on page
  3. init: Load the custom control
  4. unload: Popup feedback form
  5. addComponent: Internal
  6. dispose: No sample needed.
  7. findComponent: button creates span in div, find span if present, otherwise handle null result
  8. $find: see above -- use shortcut command for both
  9. getComponents: use sample from load. Note that $create includes addComponent
  10. initialize: Put at end of init sample
  11. notifyScriptLoaded: done
  12. queueScriptReference: 3 scripts in linear dependency
  13. raiseLoad: override initialize to call base, then if IE7, load control, then raiseLoad
  14. removeComponent: put in dispose sample -- remove datacontrol if after hours
  15. registerDisposableObject: ask Bertrand/Simon
  16. unregisterDisposableObject: ask Bertrand/Simon
  17. isCreatingComponents: put in init sample
  18. */
  19. Type.registerNamespace("Demo");
  20. // Constructor
  21. Demo.HoverButton = function(element) {
  22. Demo.HoverButton.initializeBase(this, [element]);
  23. this._clickDelegate = null;
  24. this._hoverDelegate = null;
  25. this._unhoverDelegate = null;
  26. }
  27. Demo.HoverButton.prototype = {
  28. // text property accessors.
  29. get_text: function() {
  30. return this.get_element().innerHTML;
  31. },
  32. set_text: function(value) {
  33. this.get_element().innerHTML = value;
  34. },
  35. // Bind and unbind to click event.
  36. add_click: function(handler) {
  37. this.get_events().addHandler('click', handler);
  38. },
  39. remove_click: function(handler) {
  40. this.get_events().removeHandler('click', handler);
  41. },
  42. // Bind and unbind to hover event.
  43. add_hover: function(handler) {
  44. this.get_events().addHandler('hover', handler);
  45. },
  46. remove_hover: function(handler) {
  47. this.get_events().removeHandler('hover', handler);
  48. },
  49. // Bind and unbind to unhover event.
  50. add_unhover: function(handler) {
  51. this.get_events().addHandler('unhover', handler);
  52. },
  53. remove_unhover: function(handler) {
  54. this.get_events().removeHandler('unhover', handler);
  55. },
  56. // Release resources before control is disposed.
  57. dispose: function() {
  58. var element = this.get_element();
  59. if (this._clickDelegate) {
  60. Sys.UI.DomEvent.removeHandler(element, 'click', this._clickDelegate);
  61. delete this._clickDelegate;
  62. }
  63. if (this._hoverDelegate) {
  64. Sys.UI.DomEvent.removeHandler(element, 'focus', this._hoverDelegate);
  65. Sys.UI.DomEvent.removeHandler(element, 'mouseover', this._hoverDelegate);
  66. delete this._hoverDelegate;
  67. }
  68. if (this._unhoverDelegate) {
  69. Sys.UI.DomEvent.removeHandler(element, 'blur', this._unhoverDelegate);
  70. Sys.UI.DomEvent.removeHandler(element, 'mouseout', this._unhoverDelegate);
  71. delete this._unhoverDelegate;
  72. }
  73. Demo.HoverButton.callBaseMethod(this, 'dispose');
  74. },
  75. initialize: function() {
  76. var element = this.get_element();
  77. if (!element.tabIndex) element.tabIndex = 0;
  78. if (this._clickDelegate === null) {
  79. this._clickDelegate = Function.createDelegate(this, this._clickHandler);
  80. }
  81. Sys.UI.DomEvent.addHandler(element, 'click', this._clickDelegate);
  82. if (this._hoverDelegate === null) {
  83. this._hoverDelegate = Function.createDelegate(this, this._hoverHandler);
  84. }
  85. Sys.UI.DomEvent.addHandler(element, 'mouseover', this._hoverDelegate);
  86. Sys.UI.DomEvent.addHandler(element, 'focus', this._hoverDelegate);
  87. if (this._unhoverDelegate === null) {
  88. this._unhoverDelegate = Function.createDelegate(this, this._unhoverHandler);
  89. }
  90. Sys.UI.DomEvent.addHandler(element, 'mouseout', this._unhoverDelegate);
  91. Sys.UI.DomEvent.addHandler(element, 'blur', this._unhoverDelegate);
  92. Demo.HoverButton.callBaseMethod(this, 'initialize');
  93. },
  94. _clickHandler: function(event) {
  95. var h = this.get_events().getHandler('click');
  96. if (h) h(this, Sys.EventArgs.Empty);
  97. },
  98. _hoverHandler: function(event) {
  99. var h = this.get_events().getHandler('hover');
  100. if (h) h(this, Sys.EventArgs.Empty);
  101. },
  102. _unhoverHandler: function(event) {
  103. var h = this.get_events().getHandler('unhover');
  104. if (h) h(this, Sys.EventArgs.Empty);
  105. }
  106. }
  107. Demo.HoverButton.registerClass('Demo.HoverButton', Sys.UI.Control);
  108. // Notify ScriptManager that this is the end of the script.
  109. if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();