-
Notifications
You must be signed in to change notification settings - Fork 55
/
Mime.js
218 lines (201 loc) · 5.13 KB
/
Mime.js
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @copyright Copyright (c) 2019 John Molakvoæ <[email protected]>
*
* @author John Molakvoæ <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import debounce from 'debounce'
import PreviewUrl from '../mixins/PreviewUrl.js'
import parsePath from 'path-parse'
export default {
inheritAttrs: false,
mixins: [PreviewUrl],
props: {
// Is the current component shown
active: {
type: Boolean,
default: false,
},
// file name
basename: {
type: String,
required: true,
},
// file path relative to user folder
filename: {
type: String,
required: true,
},
// file source to fetch contents from
source: {
type: String,
default: undefined,
},
// file path relative to user folder
hasPreview: {
type: Boolean,
default: false,
},
// unique file id
fileid: {
type: [Number, String],
required: false,
},
// list of all the visible files
fileList: {
type: Array,
default: () => [],
},
// file mime (aliased if specified in the model)
mime: {
type: String,
required: true,
},
// can the user swipe
canSwipe: {
type: Boolean,
default: true,
},
// is the content loaded?
// synced with parent
loaded: {
type: Boolean,
default: false,
},
// is the sidebar currently opened ?
isSidebarShown: {
type: Boolean,
default: false,
},
// are we in fullscreen mode ?
isFullScreen: {
type: Boolean,
default: false,
},
},
data() {
return {
height: null,
width: null,
naturalHeight: null,
naturalWidth: null,
isLoaded: false,
}
},
computed: {
name() {
return parsePath(this.basename).name
},
ext() {
return parsePath(this.basename).ext
},
src() {
return this.source ?? this.davPath
},
},
watch: {
active(val, old) {
// the item was hidden before and is now the current view
if (val === true && old === false) {
// just in case the file was preloaded, let's warn the viewer
if (this.isLoaded) {
this.doneLoading()
}
}
},
// update image size on sidebar toggle
isSidebarShown() {
// wait for transition to complete (100ms)
setTimeout(this.updateHeightWidth, 200)
},
},
mounted() {
// detect error and let the viewer know
this.$el.addEventListener('error', e => {
console.error('Error loading', this.filename, e)
this.$emit('error', e)
})
// update image size on window resize
window.addEventListener('resize', debounce(() => {
this.updateHeightWidth()
}, 100))
},
methods: {
/**
* This is used to make the viewer know this file is complete or ready
* ! you NEED to use it to make the viewer aware of the current loading state
*/
doneLoading() {
// send the current state
this.$emit('update:loaded', true)
// save the current state
this.isLoaded = true
},
/**
* Updates the current height and width data
* based on the viewer maximum size
*/
updateHeightWidth() {
const modalWrapper = this.$parent.$el.querySelector('.modal-wrapper')
if (modalWrapper && this.naturalHeight > 0 && this.naturalWidth > 0) {
const modalContainer = modalWrapper.querySelector('.modal-container')
const parentHeight = modalContainer.clientHeight - 50 // header height
const parentWidth = modalContainer.clientWidth
const heightRatio = parentHeight / this.naturalHeight
const widthRatio = parentWidth / this.naturalWidth
// if the video height is capped by the parent height
// AND the video is bigger than the parent
if (heightRatio < widthRatio && heightRatio < 1) {
this.height = parentHeight
this.width = Math.round(this.naturalWidth / this.naturalHeight * parentHeight)
// if the video width is capped by the parent width
// AND the video is bigger than the parent
} else if (heightRatio > widthRatio && widthRatio < 1) {
this.width = parentWidth
this.height = Math.round(this.naturalHeight / this.naturalWidth * parentWidth)
// RESET
} else {
this.height = this.naturalHeight
this.width = this.naturalWidth
}
}
},
/**
* Enable the viewer swiping previous/next capability
*/
enableSwipe() {
this.$emit('update:canSwipe', true)
},
/**
* Disable the viewer swiping previous/next capability
*/
disableSwipe() {
this.$emit('update:canSwipe', false)
},
/**
* Toggle the fullscreen on the current visible element
*/
toggleFullScreen() {
if (this.isFullScreen) {
document.exitFullscreen()
} else {
this.$el.requestFullscreen()
}
},
},
}