All files / src/inputType InputType.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86              11x 11x 11x 11x 11x                                                           11x 4063x 6429x 6039x   6429x       207x 207x 207x 207x   207x 359x   114x 114x   182x 182x   63x       207x 62x 145x 68x 77x 66x 11x 7x   4x     11x     332x    
/*
 * Copyright (c) 2015 NAVER Corp.
 * egjs projects are licensed under the MIT license
 */
import { Axis } from "../AxisManager";
import { AxesOption } from "../Axes";
import { ActiveEvent } from "../types";
import { MouseEventInput } from "../eventInput/MouseEventInput";
import { TouchEventInput } from "../eventInput/TouchEventInput";
import { PointerEventInput } from "../eventInput/PointerEventInput";
import { TouchMouseEventInput } from "../eventInput/TouchMouseEventInput";
import {
  SUPPORT_POINTER_EVENTS,
  SUPPORT_TOUCH,
} from "../eventInput/EventInput";
 
export interface InputType {
  axes: string[];
  element: HTMLElement;
  mapAxes(axes: string[]);
  connect(observer: InputTypeObserver): InputType;
  disconnect();
  destroy();
  enable?();
  disable?();
  isEnable?(): boolean;
}
 
export interface InputTypeObserver {
  options: AxesOption;
  get(inputType: InputType): Axis;
  change(inputType: InputType, event, offset: Axis, useAnimation?: boolean);
  hold(inputType: InputType, event);
  release(
    inputType: InputType,
    event,
    velocity: number[],
    inputDuration?: number
  );
}
 
export const toAxis = (source: string[], offset: number[]): Axis => {
  return offset.reduce((acc, v, i) => {
    if (source[i]) {
      acc[source[i]] = v;
    }
    return acc;
  }, {});
};
 
export const convertInputType = (IinputType: string[] = []): ActiveEvent => {
  let hasTouch = false;
  let hasMouse = false;
  let hasPointer = false;
 
  inputType.forEach((v) => {
    switch (v) {
      case "mouse":
        hasMouse = true;
        break;
      case "touch":
        hasTouch = SUPPORT_TOUCH;
        break;
      case "pointer":
        hasPointer = SUPPORT_POINTER_EVENTS;
      // no default
    }
  });
  if (hasPointer) {
    return new PointerEventInput();
  } else if (hasTouch && hasMouse) {
    return new TouchMouseEventInput();
  } else if (hasTouch) {
    return new TouchEventInput();
  } else if (hasMouse) {
    return new MouseEventInput();
  }
  return null;
};
 
export function getAddEventOptions(eventName: string) {
  // The passive default value of the touch event is true.
  // If not a touch event, return false to support ie11
  return eventName.indexOf("touch") > -1 ? { passive: false } : false;
}