Preview.vue 4.24 KB
Newer Older
wanli's avatar
wanli committed
1
<template>
wanli's avatar
wanli committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  <div class="modal-content fm-modal-preview">
    <div class="modal-header">
      <h5 class="modal-title w-75 text-truncate">
        {{
          showCropperModule
            ? lang.modal.cropper.title
            : lang.modal.preview.title
        }}
        <small class="text-muted pl-3">{{ selectedItem.basename }}</small>
      </h5>
      <button
        type="button"
        class="close"
        aria-label="Close"
        v-on:click="hideModal"
      >
        <span aria-hidden="true">&times;</span>
      </button>
wanli's avatar
wanli committed
20
    </div>
wanli's avatar
wanli committed
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
    <div class="modal-body text-center">
      <template v-if="showCropperModule">
        <cropper-module
          v-bind:imgSrc="imgSrc"
          v-bind:maxHeight="maxHeight"
          v-on:closeCropper="closeCropper"
        />
      </template>
      <transition v-else name="fade" mode="out-in">
        <i v-if="!imgSrc" class="fas fa-spinner fa-spin fa-5x p-5 text-muted" />
        <img
          v-else
          v-bind:src="imgSrc"
          v-bind:alt="selectedItem.basename"
          v-bind:style="{ 'max-height': maxHeight + 'px' }"
        />
      </transition>
    </div>
    <div v-if="showFooter" class="d-flex justify-content-between">
      <span class="d-block">
        <button
          class="btn btn-info"
          v-bind:title="lang.modal.cropper.title"
          v-on:click="showCropperModule = true"
        >
          <i class="fas fa-crop-alt" />
        </button>
      </span>
      <span class="d-block">
        <button class="btn btn-light" v-on:click="hideModal">
          {{ lang.btn.cancel }}
        </button>
      </span>
    </div>
  </div>
wanli's avatar
wanli committed
56 57 58
</template>

<script>
wanli's avatar
wanli committed
59 60 61 62
import CropperModule from "../additions/Cropper.vue";
import modal from "../mixins/modal";
import translate from "@/utils/translate";
import helper from "@/utils/helper";
63
import GET from "@/api/get";
wanli's avatar
wanli committed
64 65

export default {
wanli's avatar
wanli committed
66
  name: "Preview",
wanli's avatar
wanli committed
67 68 69 70 71
  mixins: [modal, translate, helper],
  components: { CropperModule },
  data() {
    return {
      showCropperModule: false,
wanli's avatar
wanli committed
72
      imgSrc: "",
wanli's avatar
wanli committed
73 74 75 76 77 78 79 80 81 82 83
    };
  },
  created() {
    this.loadImage();
  },
  computed: {
    /**
     * Authorization required
     * @return {*}
     */
    auth() {
wanli's avatar
wanli committed
84
      return this.$store.getters["fm/settings/authHeader"];
wanli's avatar
wanli committed
85 86 87 88 89 90 91
    },

    /**
     * Selected disk
     * @returns {*}
     */
    selectedDisk() {
wanli's avatar
wanli committed
92
      return this.$store.getters["fm/selectedDisk"];
wanli's avatar
wanli committed
93 94 95 96 97 98 99
    },

    /**
     * Selected file
     * @returns {*}
     */
    selectedItem() {
wanli's avatar
wanli committed
100
      return this.$store.getters["fm/selectedItems"][0];
wanli's avatar
wanli committed
101 102 103 104 105 106 107
    },

    /**
     * Show modal footer
     * @return boolean
     */
    showFooter() {
wanli's avatar
wanli committed
108 109 110
      return (
        this.canCrop(this.selectedItem.extension) && !this.showCropperModule
      );
wanli's avatar
wanli committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    },

    /**
     * Calculate the max height for image
     * @returns {number}
     */
    maxHeight() {
      if (this.$store.state.fm.modal.modalBlockHeight) {
        return this.$store.state.fm.modal.modalBlockHeight - 170;
      }

      return 300;
    },
  },
  methods: {
    /**
     * Can we crop this image?
     * @param extension
     * @returns {boolean}
     */
    canCrop(extension) {
wanli's avatar
wanli committed
132 133 134
      return this.$store.state.fm.settings.cropExtensions.includes(
        extension.toLowerCase()
      );
wanli's avatar
wanli committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    },

    /**
     * Close cropper
     */
    closeCropper() {
      this.showCropperModule = false;
      this.loadImage();
    },

    /**
     * Load image
     */
    loadImage() {
      // if authorization required
      if (this.auth) {
wanli's avatar
wanli committed
151 152 153 154 155 156
        GET.preview(this.selectedDisk, this.selectedItem.path).then(
          (response) => {
            const mimeType = response.headers["content-type"].toLowerCase();
            const imgBase64 = Buffer.from(response.data, "binary").toString(
              "base64"
            );
wanli's avatar
wanli committed
157

wanli's avatar
wanli committed
158 159 160
            this.imgSrc = `data:${mimeType};base64,${imgBase64}`;
          }
        );
wanli's avatar
wanli committed
161
      } else {
wanli's avatar
wanli committed
162 163 164 165 166
        this.imgSrc = `${
          this.$store.getters["fm/settings/baseUrl"]
        }preview?disk=${this.selectedDisk}&path=${encodeURIComponent(
          this.selectedItem.path
        )}&v=${this.selectedItem.timestamp}`;
wanli's avatar
wanli committed
167 168 169 170 171 172 173
      }
    },
  },
};
</script>

<style lang="scss">
wanli's avatar
wanli committed
174 175 176
.fm-modal-preview {
  .modal-body {
    padding: 0;
wanli's avatar
wanli committed
177

wanli's avatar
wanli committed
178 179
    img {
      max-width: 100%;
wanli's avatar
wanli committed
180
    }
wanli's avatar
wanli committed
181 182 183 184 185 186 187
  }

  & > .d-flex {
    padding: 1rem;
    border-top: 1px solid #e9ecef;
  }
}
wanli's avatar
wanli committed
188
</style>