File size: 2,495 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<template>
  <div v-if="showDrag" class="dragging-dropping">
    <!-- <input
      type="file"
      id="fileElem"
      multiple
      :accept="accept"
      onchange="handleFiles(this.files)"
    /> -->
  </div>
</template>

<script lang="ts">
import { fileTypesAccepts } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/ParseMoleculeFiles";
import { Options, Vue } from "vue-class-component";
import * as api from "@/Api";

/**
 * DragDropFileLoad component
 */
@Options({
  components: {},
})
export default class DragDropFileLoad extends Vue {
  // @Prop({ required: true }) modelValue!: boolean;

  accept = fileTypesAccepts;
  showDrag = false;
  timeoutId: any = null;

  /**
   * Mounted function.
   */
  mounted() {
    // Inspired by
    // https://www.smashingmagazine.com/2018/01/drag-drop-file-uploader-vanilla-js/

    const dropArea = document.body;

    dropArea.addEventListener(
      "dragenter",
      (e) => {
        e.preventDefault();
        e.stopPropagation();
        this.showDrag = true;
        if (this.timeoutId !== null) {
          clearInterval(this.timeoutId);
          this.timeoutId = null;
        }

      },
      false
    );

    dropArea.addEventListener(
      "dragleave",
      (e) => {
        e.preventDefault();
        e.stopPropagation();
        this.timeoutId = setTimeout(() => {
          this.showDrag = false;
        }, 250);
      },
      false
    );

    dropArea.addEventListener(
      "dragover",
      (e) => {
        e.preventDefault();
        e.stopPropagation();
        this.showDrag = true;
        if (this.timeoutId !== null) {
          clearInterval(this.timeoutId);
          this.timeoutId = null;
        }
      },
      false
    );

    dropArea.addEventListener(
      "drop",
      (e) => {
        e.preventDefault();
        e.stopPropagation();

        if (this.timeoutId !== null) {
          clearInterval(this.timeoutId);
          this.timeoutId = null;
        }
        this.showDrag = false;

        let dt = e.dataTransfer as DataTransfer;
        let files = dt.files;
        api.plugins.runPlugin("openmolecules", files);
      },
      false
    );
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss">
// .highlight {
//   // border-color: purple;
//   border: 5px dashed purple;
// }
.dragging-dropping {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: black;
  z-index: 5000;
  opacity: 50%;
}
</style>