<template>
  <view
    class="waveRipple"
    :class="[isWaving ? 'waveActive' : '']"
    :style="{
      top: rippleToTop + 'px',
      left: rippleToLeft + 'px',
      width: domInfo.targetWidth + 'px',
      height: domInfo.targetWidth + 'px',
      backgroundColor: rippleBgColor ? rippleBgColor : 'rgba(0, 0, 0, 0.6)',
    }"
  ></view>
</template>

<script>
export default {
  name: "s-wave",
  props: {
    rippleBgColor: {
      type: String,
      default: "rgba(0, 0, 0, 0.6)",
    },
  },
  data() {
    return {
      repple_button: {
        animate: false,
        toggle: false,
      },
      rippleToTop: 0,
      rippleToLeft: 0,
      domInfo: {},
      isWaving: false,
    };
  },
  methods: {
    getWaveQuery(e, domName) {
      this.isWaving = false;
      // this.isWavingChange().then(() => {
      this.$nextTick(function () {
        this.waveQueryChange(e, domName);
      });
      // });
    },
    waveQueryChange(e, domName) {
      this.getElQuery(domName).then((res) => {
      
        const data = res[0];
          // console.log(data, "...........data");
        if (!data.width || !data.width) return;
        data.targetWidth = data.height > data.width ? data.height : data.width;
        if (!data.targetWidth) return;
        this.domInfo = data;
        let touchesX = "";
        let touchesY = "";
        // #ifndef MP-BAIDU || MP-ALIPAY
        touchesX = e.touches[0].clientX;
        touchesY = e.touches[0].clientY;
        // #endif
        // #ifdef MP-BAIDU
        touchesX = e.changedTouches[0].clientX;
        touchesY = e.changedTouches[0].clientY;
        // #endif
        // #ifdef MP-ALIPAY
        touchesX = e.detail.clientX;
        touchesY = e.detail.clientY;
        // #endif
        this.rippleToTop = touchesY - data.top - data.targetWidth / 2;
        this.rippleToLeft = touchesX - data.left - data.targetWidth / 2;
        this.$nextTick(() => {
          this.isWaving = true;
        });
      });
    },
    getElQuery(domName) {
      // console.log(domName)
      return new Promise((resolve) => {
        let queryInfo = "";
        queryInfo = uni.createSelectorQuery().in(this.$parent);
        //#ifdef MP-ALIPAY
        queryInfo = uni.createSelectorQuery();
        //#endif
        queryInfo.select(domName).boundingClientRect();
        queryInfo.exec((data) => {
          resolve(data);
        });
      });
    },
  },
};
</script>

<style scoped>
@import "./waves.css";
.wave {
  width: 400px;
  height: 400px;
  position: relative;
  overflow: hidden;
}
.waveRipple {
  overflow: hidden;
}
.__cov-ripple {
  position: absolute;
  background: hsla(0, 0, 0, 0.66);
  border-radius: 100%;
  transform: scale(0);
}
.__cov-ripple.animate {
  animation: ripple 0.7s linear;
}

@keyframes ripple {
  100% {
    opacity: 0;
    transform: scale(2.5);
  }
}
</style>
