zingtouch.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /**
  2. * The global API interface for ZingTouch. Contains a constructor for the
  3. * Region Object, and constructors for each predefined Gesture.
  4. */
  5. declare module "zingtouch" {
  6. /**
  7. * Allows the user to specify a region to capture all events to feed ZingTouch
  8. * into. This can be as narrow as the element itself, or as big as the document
  9. * itself. The more specific an area, the better performant the overall
  10. * application will perform. Contains API methods to bind/unbind specific
  11. * elements to corresponding gestures. Also contains the ability to
  12. * register/unregister new gestures.
  13. */
  14. class Region {
  15. constructor(
  16. element: HTMLElement,
  17. capture?: boolean,
  18. preventDefault?: boolean
  19. );
  20. bind(
  21. element: HTMLElement,
  22. gesture: RegionGestures,
  23. handler: (params: unknown) => unknown,
  24. capture?: boolean
  25. ): void;
  26. bind(element: HTMLElement): ZingChainable;
  27. bindOnce(
  28. element: HTMLElement,
  29. gesture: RegionGestures,
  30. handler: (params: unknown) => unknown,
  31. capture?: boolean
  32. ): void;
  33. bindOnce(element: HTMLElement): ZingChainable;
  34. unbind(element: HTMLElement, gesture?: RegionGestures): RegionGestures[];
  35. register(key: string, gesture: Gesture): Gesture;
  36. unregister(key: string): Gesture;
  37. }
  38. /**
  39. * Constructor function for the Gesture class.
  40. */
  41. class Gesture {
  42. /**
  43. * The generic string type of gesture ('expand'|'pan'|'pinch'|
  44. * 'rotate'|'swipe'|'tap').
  45. */
  46. type: string;
  47. /**
  48. * The unique identifier for each gesture determined at bind time by the
  49. * state object. This allows for distinctions across instance variables of
  50. * Gestures that are created on the fly (e.g. Tap-1, Tap-2, etc).
  51. */
  52. id: string | null;
  53. constructor();
  54. /**
  55. * Set the type of the gesture to be called during an event
  56. * @param type - The unique identifier of the gesture being created.
  57. */
  58. setType(type: string): void;
  59. /**
  60. * getType() - Returns the generic type of the gesture
  61. * @returns - The type of gesture
  62. */
  63. getType(): string;
  64. /**
  65. * Set the id of the gesture to be called during an event
  66. * @param id - The unique identifier of the gesture being created.
  67. */
  68. setId(id: string): void;
  69. /**
  70. * Return the id of the event. If the id does not exist, return the type.
  71. */
  72. getId(): string;
  73. /**
  74. * Updates internal properties with new ones, only if the properties exist.
  75. */
  76. update(object: unknown): void;
  77. /**
  78. * start() - Event hook for the start of a gesture
  79. * @param inputs - The array of Inputs on the screen
  80. * @param state - The state object of the current region.
  81. * @param element - The element associated to the binding.
  82. * @returns - Default of null
  83. */
  84. start(inputs: unknown[], state: unknown, element: Element): null | unknown;
  85. /**
  86. * move() - Event hook for the move of a gesture
  87. * @param inputs - The array of Inputs on the screen
  88. * @param state - The state object of the current region.
  89. * @param element - The element associated to the binding.
  90. * @returns - Default of null
  91. */
  92. move(inputs: unknown[], state: unknown, element: Element): null | unknown;
  93. /**
  94. * end() - Event hook for the move of a gesture
  95. * @param inputs - The array of Inputs on the screen
  96. * @returns - Default of null
  97. */
  98. end(inputs: unknown[]): null | unknown;
  99. /**
  100. * isValid() - Pre-checks to ensure the invariants of a gesture are satisfied.
  101. * @param inputs - The array of Inputs on the screen
  102. * @param state - The state object of the current region.
  103. * @param element - The element associated to the binding.
  104. * @returns - If the gesture is valid
  105. */
  106. isValid(inputs: unknown[], state: unknown, element: Element): boolean;
  107. }
  108. /**
  109. * A Distance is defined as two inputs moving either together or apart.
  110. */
  111. class Distance extends Gesture {
  112. constructor(options: {
  113. /**
  114. * The minimum amount in pixels the inputs must move until it is fired.
  115. */
  116. threshold: number;
  117. });
  118. /**
  119. * Event hook for the start of a gesture. Initialized the lastEmitted
  120. * gesture and stores it in the first input for reference events.
  121. */
  122. start(inputs: unknown[]): void;
  123. /**
  124. * Event hook for the move of a gesture.
  125. * Determines if the two points are moved in the expected direction relative
  126. * to the current distance and the last distance.
  127. * @param inputs - The array of Inputs on the screen.
  128. * @param state - The state object of the current region.
  129. * @param element - The element associated to the binding.
  130. * @returns - Returns the distance in pixels between two inputs
  131. */
  132. move(inputs: unknown[], state: unknown, element: Element): unknown | null;
  133. }
  134. /**
  135. * A Pan is defined as a normal movement in unknown direction on a screen.
  136. * Pan gestures do not track start events and can interact with pinch and \
  137. * expand gestures.
  138. */
  139. class Pan extends Gesture {
  140. constructor(options?: PanOptions);
  141. /**
  142. * Event hook for the start of a gesture. Marks each input as active,
  143. * so it can invalidate unknown end events.
  144. */
  145. start(inputs: unknown[]): void;
  146. /**
  147. * move() - Event hook for the move of a gesture.
  148. * Fired whenever the input length is met, and keeps a boolean flag that
  149. * the gesture has fired at least once.
  150. * @param inputs - The array of Inputs on the screen
  151. * @param state - The state object of the current region.
  152. * @param element - The element associated to the binding.
  153. * @returns - Returns the distance in pixels between the two inputs.
  154. */
  155. move(inputs: unknown[], state: unknown, element: Element): unknown;
  156. /**
  157. * end() - Event hook for the end of a gesture. If the gesture has at least
  158. * fired once, then it ends on the first end event such that unknown remaining
  159. * inputs will not trigger the event until all inputs have reached the
  160. * touchend event. unknown touchend->touchstart events that occur before all
  161. * inputs are fully off the screen should not fire.
  162. * @param inputs - The array of Inputs on the screen
  163. * @returns - null if the gesture is not to be emitted,
  164. * Object with information otherwise.
  165. */
  166. end(inputs: unknown[]): null;
  167. }
  168. /**
  169. * A Rotate is defined as two inputs moving about a circle,
  170. * maintaining a relatively equal radius.
  171. */
  172. class Rotate extends Gesture {
  173. constructor();
  174. /**
  175. * move() - Event hook for the move of a gesture. Obtains the midpoint of two
  176. * the two inputs and calculates the projection of the right most input along
  177. * a unit circle to obtain an angle. This angle is compared to the previously
  178. * calculated angle to output the change of distance, and is compared to the
  179. * initial angle to output the distance from the initial angle to the current
  180. * angle.
  181. * @param inputs - The array of Inputs on the screen
  182. * @param state - The state object of the current listener.
  183. * @param element - The element associated to the binding.
  184. * @returns - null if this event did not occur
  185. * @returns obj.angle - The current angle along the unit circle
  186. * @returns obj.distanceFromOrigin - The angular distance travelled
  187. * from the initial right most point.
  188. * @returns obj.distanceFromLast - The change of angle between the
  189. * last position and the current position.
  190. */
  191. move(inputs: unknown[], state: unknown, element: Element): void;
  192. }
  193. /**
  194. * A swipe is defined as input(s) moving in the same direction in an relatively
  195. * increasing velocity and leaving the screen at some point before it drops
  196. * below it's escape velocity.
  197. */
  198. class Swipe extends Gesture {
  199. constructor(options?: SwipeOptions);
  200. /**
  201. * Event hook for the move of a gesture. Captures an input's x/y coordinates
  202. * and the time of it's event on a stack.
  203. * @param inputs - The array of Inputs on the screen.
  204. * @param state - The state object of the current region.
  205. * @param element - The element associated to the binding.
  206. * @returns - Swipe does not emit from a move.
  207. */
  208. move(inputs: unknown[], state: unknown, element: Element): null;
  209. /**
  210. * Determines if the input's history validates a swipe motion.
  211. * Determines if it did not come to a complete stop (maxRestTime), and if it
  212. * had enough of a velocity to be considered (ESCAPE_VELOCITY).
  213. * @param inputs - The array of Inputs on the screen
  214. * @returns - null if the gesture is not to be emitted,
  215. * Object with information otherwise.
  216. */
  217. end(inputs: unknown[]): null | unknown;
  218. }
  219. /**
  220. * A Tap is defined as a touchstart to touchend event in quick succession.
  221. */
  222. class Tap extends Gesture {
  223. constructor(options?: TapOptions);
  224. /**
  225. * Event hook for the start of a gesture. Keeps track of when the inputs
  226. * trigger the start event.
  227. * @param inputs - The array of Inputs on the screen.
  228. * @returns - Tap does not trigger on a start event.
  229. */
  230. start(inputs: unknown[]): null;
  231. /**
  232. * Event hook for the move of a gesture. The Tap event reaches here if the
  233. * user starts to move their input before an 'end' event is reached.
  234. * @param inputs - The array of Inputs on the screen.
  235. * @param state - The state object of the current region.
  236. * @param element - The element associated to the binding.
  237. * @returns - Tap does not trigger on a move event.
  238. */
  239. move(inputs: unknown[], state: unknown, element: Element): null;
  240. /**
  241. * Event hook for the end of a gesture.
  242. * Determines if this the tap event can be fired if the delay and tolerance
  243. * constraints are met. Also waits for all of the inputs to be off the screen
  244. * before determining if the gesture is triggered.
  245. * @param inputs - The array of Inputs on the screen.
  246. * @returns - null if the gesture is not to be emitted,
  247. * Object with information otherwise. Returns the interval time between start
  248. * and end events.
  249. */
  250. end(inputs: unknown[]): null | unknown;
  251. }
  252. /**
  253. * An Expand is defined as two inputs moving farther away from each other.
  254. * This gesture does not account for unknown start/end events to allow for the
  255. * event to interact with the Pan and Pinch events.
  256. */
  257. class Expand extends Distance {
  258. constructor(options: {
  259. /**
  260. * The minimum amount in pixels the inputs must move until it is fired.
  261. */
  262. threshold: number;
  263. });
  264. }
  265. /**
  266. * An Pinch is defined as two inputs moving closer to each other.
  267. * This gesture does not account for unknown start/end events to allow for the event
  268. * to interact with the Pan and Pinch events.
  269. */
  270. class Pinch extends Distance {
  271. constructor(options: {
  272. /**
  273. * The minimum amount in pixels the inputs must move until it is fired.
  274. */
  275. threshold: number;
  276. });
  277. }
  278. type RegionGestures =
  279. | "tap"
  280. | Tap
  281. | "pan"
  282. | Pan
  283. | "swipe"
  284. | Swipe
  285. | "rotate"
  286. | Rotate
  287. | "pinch"
  288. | Pinch
  289. | "expand"
  290. | Expand
  291. | Gesture;
  292. }
  293. interface ZingChainable {
  294. tap(handler: () => void, capture?: boolean): ZingChainable;
  295. swipe(handler: () => void, capture?: boolean): ZingChainable;
  296. pinch(handler: () => void, capture?: boolean): ZingChainable;
  297. expand(handler: () => void, capture?: boolean): ZingChainable;
  298. pan(handler: () => void, capture?: boolean): ZingChainable;
  299. rotate(handler: () => void, capture?: boolean): ZingChainable;
  300. }
  301. interface TapOptions {
  302. /**
  303. * The minimum amount between a touchstart and a touchend can be configured
  304. * in milliseconds. The minimum delay starts to count down when the expected
  305. * number of inputs are on the screen, and ends when ALL inputs are off the
  306. * screen.
  307. */
  308. minDelay: number;
  309. /**
  310. * The maximum delay between a touchstart and touchend can be configured in
  311. * milliseconds. The maximum delay starts to count down when the expected
  312. * number of inputs are on the screen, and ends when ALL inputs are off the
  313. * screen.
  314. */
  315. maxDelay: number;
  316. /**
  317. * The number of inputs to trigger a Tap can be variable,
  318. * and the maximum number being a factor of the browser.
  319. */
  320. numInputs: number;
  321. /**
  322. * A move tolerance in pixels allows some slop between a user's start to end
  323. * events. This allows the Tap gesture to be triggered more easily.
  324. */
  325. tolerance: number;
  326. }
  327. interface SwipeOptions {
  328. /**
  329. * The number of inputs to trigger a Swipe can be variable,
  330. * and the maximum number being a factor of the browser.
  331. */
  332. numInputs: number;
  333. /**
  334. * The maximum resting time a point has between it's last move and
  335. * current move events.
  336. */
  337. maxRestTime: number;
  338. /**
  339. * The minimum velocity the input has to be at to emit a swipe.
  340. * This is useful for determining the difference between
  341. * a swipe and a pan gesture.
  342. */
  343. escapeVelocity: number;
  344. /**
  345. * (EXPERIMENTAL) A value of time in milliseconds to distort between events.
  346. * Browsers do not accurately measure time with the Date constructor in
  347. * milliseconds, so consecutive events sometimes display the same timestamp
  348. * but different x/y coordinates. This will distort a previous time
  349. * in such cases by the timeDistortion's value.
  350. */
  351. timeDistortion?: number;
  352. /**
  353. * (EXPERIMENTAL) The maximum amount of move events to keep track of for a
  354. * swipe. This helps give a more accurate estimate of the user's velocity.
  355. */
  356. maxProgressStack?: number;
  357. }
  358. interface PanOptions {
  359. /**
  360. * The number of inputs to trigger a Pan can be variable,
  361. * and the maximum number being a factor of the browser.
  362. */
  363. numInputs?: number;
  364. /**
  365. * The minimum amount in pixels the pan must move until it is fired.
  366. */
  367. threshold?: number;
  368. }