<template>
  <div>
    <svg-icon :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'" @click="click" />
  </div>
</template>

<script>
import ScreenFull from "screenfull";

export default {
  name: "ScreenFull",
  data() {
    return {
      isFullscreen: false,
    };
  },
  mounted() {
    this.init();
  },
  beforeDestroy() {
    this.destroy();
  },
  methods: {
    click() {
      if (ScreenFull.isEnabled) {
        ScreenFull.toggle();
      } else {
        this.$message({
          message: "您的浏览器不支持",
          type: "warning",
        });
        return false;
      }
    },
    change() {
      this.isFullscreen = ScreenFull.isFullscreen;
    },
    init() {
      if (ScreenFull.isEnabled) {
        ScreenFull.on("change", this.change);
      }
    },
    destroy() {
      if (ScreenFull.isEnabled) {
        ScreenFull.off("change", this.change);
      }
    },
  },
};
</script>

<style scoped>
.screenfull-svg {
  display: inline-block;
  cursor: pointer;
  fill: #5a5e66;
  width: 20px;
  height: 20px;
  vertical-align: 10px;
}
</style>
