hook.d.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { Store } from './store';
  2. import { EventObject } from './object';
  3. import { EventHandler } from './types';
  4. export declare namespace EventHook {
  5. function get(type: string): EventHook;
  6. function register(type: string, hook: EventHook): void;
  7. function unregister(type: string): void;
  8. }
  9. export interface EventHook {
  10. /**
  11. * Indicates whether this event type should be bubbled when the `.trigger()`
  12. * method is called; by default it is `false`, meaning that a triggered event
  13. * will bubble to the element's parents up to the document (if attached to a
  14. * document) and then to the window. Note that defining `noBubble` on an event
  15. * will effectively prevent that event from being used for delegated events
  16. * with `.trigger()`.
  17. */
  18. noBubble?: boolean;
  19. /**
  20. * When defined, these string properties specify that a special event should
  21. * be handled like another event type until the event is delivered.
  22. *
  23. * The `bindType` is used if the event is attached directly, and the
  24. * `delegateType` is used for delegated events. These types are generally DOM
  25. * event types, and should not be a special event themselves.
  26. */
  27. bindType?: string;
  28. /**
  29. * When defined, these string properties specify that a special event should
  30. * be handled like another event type until the event is delivered.
  31. *
  32. * The `bindType` is used if the event is attached directly, and the
  33. * `delegateType` is used for delegated events. These types are generally DOM
  34. * event types, and should not be a special event themselves.
  35. */
  36. delegateType?: string;
  37. /**
  38. * The setup hook is called the first time an event of a particular type is
  39. * attached to an element; this provides the hook an opportunity to do
  40. * processing that will apply to all events of this type on the element.
  41. *
  42. * The `elem` is the reference to the element where the event is being
  43. * attached and `eventHandle` is the event handler function. In most cases
  44. * the `namespaces` argument should not be used, since it only represents the
  45. * namespaces of the first event being attached; subsequent events may not
  46. * have this same namespaces.
  47. *
  48. * This hook can perform whatever processing it desires, including attaching
  49. * its own event handlers to the element or to other elements and recording
  50. * setup information on the element using the `.data()` method. If the
  51. * setup hook wants me to add a browser event (via `addEventListener` or
  52. * `attachEvent`, depending on browser) it should return `false`. In all
  53. * other cases, me will not add the browser event, but will continue all its
  54. * other bookkeeping for the event. This would be appropriate, for example,
  55. * if the event was never fired by the browser but invoked by `.trigger()`.
  56. * To attach the me event handler in the setup hook, use the `eventHandle`
  57. * argument.
  58. *
  59. */
  60. setup?: (elem: Store.EventTarget, data: any, namespaces: string[], eventHandle: EventHandler<Store.EventTarget, any>) => any | false;
  61. /**
  62. * The teardown hook is called when the final event of a particular type is
  63. * removed from an element. The `elem` is the reference to the element where
  64. * the event is being cleaned up. This hook should return `false` if it wants
  65. * me to remove the event from the browser's event system (via
  66. * `removeEventListener` or `detachEvent`). In most cases, the setup and
  67. * teardown hooks should return the same value.
  68. *
  69. * If the setup hook attached event handlers or added data to an element
  70. * through a mechanism such as `.data()`, the teardown hook should reverse
  71. * the process and remove them. me will generally remove the data and events
  72. * when an element is totally removed from the document, but failing to remove
  73. * data or events on teardown will cause a memory leak if the element stays in
  74. * the document.
  75. *
  76. */
  77. teardown?: (elem: Store.EventTarget, namespaces: string[], eventHandle: EventHandler<Store.EventTarget, any>) => any | false;
  78. /**
  79. * Each time an event handler is added to an element through an API such as
  80. * `.on()`, me calls this hook. The `elem` is the element to which the event
  81. * handler is being added, and the `handleObj` argument is as described in the
  82. * section above. The return value of this hook is ignored.
  83. */
  84. add?: (elem: Store.EventTarget, handleObj: Store.HandlerObject) => void;
  85. /**
  86. * When an event handler is removed from an element using an API such as
  87. * `.off()`, this hook is called. The `elem` is the element where the handler
  88. * is being removed, and the `handleObj` argument is as described in the
  89. * section above. The return value of this hook is ignored.
  90. *
  91. */
  92. remove?: (elem: Store.EventTarget, handleObj: Store.HandlerObject) => void;
  93. /**
  94. * The handle hook is called when the event has occurred and me would
  95. * normally call the user's event handler specified by `.on()` or another
  96. * event binding method. If the hook exists, me calls it instead of that
  97. * event handler, passing it the event and any data passed from `.trigger()`
  98. * if it was not a native event. The `elem` argument is the DOM element being
  99. * handled, and `event.handleObj` property has the detailed event information.
  100. *
  101. */
  102. handle?: (elem: Store.EventTarget, event: EventObject, ...args: any[]) => void;
  103. /**
  104. * Called when the `.trigger()` method is used to trigger an event for the
  105. * special type from code, as opposed to events that originate from within
  106. * the browser. The `elem` argument will be the element being triggered, and
  107. * the `event` argument will be a `EventObject` object constructed from the
  108. * caller's input. At minimum, the event type, data, namespace, and target
  109. * properties are set on the event. The data argument represents additional
  110. * data passed by `.trigger()` if present.
  111. *
  112. */
  113. trigger?: (elem: Store.EventTarget, event: EventObject, data: any) => any | false;
  114. /**
  115. * When the `.trigger()` method finishes running all the event handlers for
  116. * an event, it also looks for and runs any method on the target object by
  117. * the same name unless of the handlers called `event.preventDefault()`. So,
  118. * `.trigger("submit")` will execute the `submit()` method on the element if
  119. * one exists. When a `preventDefault` hook is specified, the hook is called
  120. * just prior to checking for and executing the element's default method. If
  121. * this hook returns the value `false` the element's default method will be
  122. * called; otherwise it is not.
  123. */
  124. preventDefault?: (elem: Store.EventTarget, event: EventObject, data: any) => any | false;
  125. preDispatch?: (elem: Store.EventTarget, event: EventObject) => void | false;
  126. postDispatch?: (elem: Store.EventTarget, event: EventObject) => void;
  127. }