clickEvent.ts 5.19 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-11-30 14:10:56
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-12-21 09:20:19
 * @FilePath: \glsx-tracker-javascript\src\modules\clickEvent.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AEupo
 */
import Events from './events'
import utils from '../utils/helper'
import type { ClickDescriptor, ElDescriptor, ClickParmasDescriptor } from '../types/click'
import { TAG_PROP_NAME, TAGS } from '../utils/constants'
import collect from './collect'
class clickEvent extends Events {
  _options: any
  /**
   * 获取元素所有信息数据
   * @param  target 接收的数据
   */
  getEleDetail(target: HTMLElement) {
    let prop = this.getEleInfo(target)
    let res = this.getPointerEventProp(target)
    return utils.extend(prop, res)
  }
  /**
     * 获取dom元素信息数据
      class_name:类名
      element_type:标签名
      element_content:文本内容
      element_id:id
      element_name:name属性
     * @param  obj 接收的数据
     */
  getEleInfo(target: HTMLElement) {
    var tagName = target.tagName.toLowerCase()
    var props: ElDescriptor = {
      element_type: tagName,
      element_name: target.getAttribute('name')!,
      element_id: target.getAttribute('id')!,
      class_name: target.className,
      element_content: this.getElementContent(target, tagName)
    }
    return props
  }
  /**
   * 获取元素坐标
   * @param  e
   * @param  target
   */
  getPointerEventProp(target: HTMLElement) {
    let targetEle = target.getBoundingClientRect()
    let scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft || 0
    let scrollTop = document.body.scrollTop || document.documentElement.scrollTop || 0
    return {
      $page_x: +(targetEle.left + scrollLeft || 0).toFixed(2),
      $page_y: +(targetEle.top + scrollTop || 0).toFixed(2)
    }
  }

  /**
   * 获取元素内容
   * @param  element  dom元素
   * @param  tagName  标签名
   */
  getElementContent(target: HTMLElement, tagName: string) {
    if (tagName.toLowerCase() === 'input') {
      return (target as HTMLInputElement).value
    }
    let textContent = ''
    if (target.textContent) {
      textContent = utils.trim(target.textContent)
    } else if (target.innerText) {
      textContent = utils.trim(target.innerText)
    }
    return textContent
  }
  /**
   * 判断哪些元素是目标元素
   * @param  target  dom元素
   */
  getTargetElement(target: HTMLElement) {
    if (typeof target.tagName !== 'string') {
      return null
    }
    let tagName = target.tagName.toLowerCase()
    if (tagName.toLowerCase() === 'body' || tagName.toLowerCase() === 'html') {
      return null
    }
    if (!target || !target.parentNode || !target.parentNode.children) {
      return null
    }
    let extraTags = TAG_PROP_NAME,
      collect_doms = this._options.collect_doms

    if (TAGS.includes(tagName) || target.hasAttribute(extraTags)) {
      return target
    }
    if (collect_doms && Array.isArray(collect_doms) && collect_doms.includes(tagName)) {
      return target
    }
  }
  /**
   * 手动触发上报数据
   * @param  target 目标元素
   * @param  customProps 自定义属性
   * @param  callback  回调函数
   */
  activeTrigger(target: HTMLElement, customProps: any, callback: Function) {
    let tagName = target.tagName.toLowerCase()
    if (this.getTargetElement(target) || this._options.open_all_click) {
      return false
    }
    this.start({ event: null, target, tagName, customProps, callback })
  }
  /**
   * 获取点击事件的所有数据
   * @param  event
   * @param  target,
   * @param  tagName,
   * @param  customProps
   */
  getBasicEleInfo({ event, target, tagName, customProps }: ClickParmasDescriptor): ClickDescriptor {
    let _customProps = typeof customProps === 'object' && customProps !== null ? customProps : {}
    var prop = utils.extend(this.getEleDetail(target), _customProps)
    return {
      event,
      target: target,
      props: prop,
      tagName: tagName
    }
  }
  /**
   * 初始化,并绑定监听事件
   * params 配置信息
   */
  init<T>(this: clickEvent, data: T) {
    this._options = data
    if (this._options.open_all_click) {
      this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
        let _target = e.target as HTMLElement
        this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
      })
    } else {
      this.addEvent(document.documentElement, 'click', (e: MouseEvent) => {
        let target = e.target as HTMLElement
        let _target = this.getTargetElement(target)
        if (_target) {
          this.start({ event: e, target: _target, tagName: _target.tagName.toLowerCase() })
        }
      })
    }
  }
  /**
   * 开始收集数据
   * params 配置信息
   */
  start({ event, target, tagName, customProps, callback }: ClickParmasDescriptor) {
    if (typeof callback == 'function') {
      callback.call(null)
    }
    let para = this.getBasicEleInfo({ event, target, tagName, customProps })
    collect.getClickEvent(para.props)
  }
}
export default new clickEvent()