-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoInputPreview.js
More file actions
144 lines (125 loc) · 5.88 KB
/
Copy pathVideoInputPreview.js
File metadata and controls
144 lines (125 loc) · 5.88 KB
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
/**
* Video input preview handler
*/
class VideoInputPreview{
constructor(target, parameters){
/**
* Base input
* @type {Element}
*/
this.input = target instanceof Element ? target : document.querySelector(target);
/**
* Has the input changed ?
* @type {Boolean}
*/
this.changed = false;
/**
* Parameters holder
* @private
*/
this._parameters = {
preview: null,
uploadIcon: null,
lang: 'en',
dictionary: null,
...(parameters || {})
};
//Errors checking
if(!this.input){
throw console.warn('VideoInputPreview: '+(typeof target == 'string' ? 'The selector `'+target+'` didn\'t match any element.' : 'The element you provided was undefined'));
}else if(this.input.classList.contains('vip-initialized')){
throw console.warn('VideoInputPreview: The element has already been initialized.');
}else{
/**
* Upload icon
* @private
*/
this._uploadIcon = this._parameters.uploadIcon || 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDYwIDYwIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCA2MCA2MCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJtNTAuOT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4xMDEtMC41Mi0wLjEwMS0wLjc2NCAwLTAuMTIzIDAuMDUxLTAuMjMzIDAuMTI0LTAuMzI2IDAuMjE3bC03Ljk5OSA3Ljk5OWMtMC4zOTEgMC4zOTEtMC4zOTEgMS4wMjMgMCAxLjQxNCAwLjE5NSAwLjE5NSAwLjQ1MSAwLjI5MyAwLjcwNyAwLjI5M3MwLjUxMi0wLjA5OCAwLjcwNy0wLjI5M2w2LjI5My02LjI5M3YyMC41ODZjMCAwLjU1MyAwLjQ0OCAxIDEgMXMxLTAuNDQ3IDEtMXYtMjAuNTg2bDYuMjkzIDYuMjkzYzAuMTk1IDAuMTk1IDAuNDUxIDAuMjkzIDAuNzA3IDAuMjkzczAuNTEyLTAuMDk4IDAuNzA3LTAuMjkzYzAuMzkxLTAuMzkxIDAuMzkxLTEuMDIzIDAtMS40MTRsLTcuOTk5LTcuOTk5eiIvPjwvc3ZnPg==';
this._loadDictionary();
this._build();
this._listen();
}
}
/**
* Loads the dictionary
* @private
*/
_loadDictionary(){
/** @private */
this._dictionary = {
...this._parameters.dictionary,
en: {
upload: 'Upload video',
...(this._parameters.dictionary || {}).en
},
fr: {
upload: 'Télécharger une vidéo',
...(this._parameters.dictionary || {}).fr
}
};
}
/**
* Builds the VideoInputPreview module in the DOM
* @private
*/
_build(){
this.input.classList.add('vip-input', 'vip-initialized');
/**
* Input preview
* @type {Element}
*/
this.preview = document.createElement('video');
this.preview.classList.add('vip-preview');
if(!this._parameters.preview) this.preview.poster = this._uploadIcon;
this.preview.controls = 'true';
this.preview.width = 200;
this.preview.insertAdjacentHTML('beforeend', /*html*/`
<source ${this._parameters.preview ? `src="${this._parameters.preview}"` : ''}>
`);
this.input.insertAdjacentElement('beforebegin', this.preview);
}
/**
* Listens to necessary events
* @private
*/
_listen(){
this.input.addEventListener('change', () => {
if(this.input.files && this.input.files[0]){
if(this.input.files[0].type.startsWith('video/')){
this.preview.firstElementChild.src = URL.createObjectURL(this.input.files[0]);
this.preview.load();
this.preview.poster = '';
}
this.changed = true;
}
});
}
/**
* Changes the preview image
* @param {String} src The preview URL
* @returns {ImageInputPreview} The current VideoInputPreview
*/
setPreview(src){
this.preview.poster = src;
return this;
}
/**
* Resets the input and the preview
* @returns {VideoInputPreview} The current VideoInputPreview
*/
reset(){
this.input.value = '';
this.preview.firstChild.src = '';
this.changed = true;
return this;
}
}
// Initialize inputs the data- way
window.addEventListener('load', () => {
let inputs = document.querySelectorAll('input[type="file"][data-video-preview]');
inputs.forEach(input => {
new VideoInputPreview(input, {
preview: input.getAttribute('data-video-preview')
});
});
});