-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageInputPreview.js
170 lines (145 loc) · 6.29 KB
/
ImageInputPreview.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
/**
* Image input preview handler
*/
class ImageInputPreview{
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('ImageInputPreview: '+(typeof target == 'string' ? 'The selector `'+target+'` didn\'t match any element.' : 'The element you provided was undefined'));
}else if(this.input.classList.contains('iip-initialized')){
throw console.warn('ImageInputPreview: The element has already been initialized.');
}else{
/**
* Upload icon
* @private
*/
this._uploadIcon = this._parameters.uploadIcon || 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz48c3ZnIGVuYWJsZS1iYWNrZ3JvdW5kPSJuZXcgMCAwIDYwIDYwIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCA2MCA2MCIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJ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yNDQtMC4xMDEtMC41Mi0wLjEwMS0wLjc2NCAwLTAuMTIzIDAuMDUxLTAuMjMzIDAuMTI0LTAuMzI2IDAuMjE3bC03Ljk5OSA3Ljk5OWMtMC4zOTEgMC4zOTEtMC4zOTEgMS4wMjMgMCAxLjQxNCAwLjE5NSAwLjE5NSAwLjQ1MSAwLjI5MyAwLjcwNyAwLjI5M3MwLjUxMi0wLjA5OCAwLjcwNy0wLjI5M2w2LjI5My02LjI5M3YyMC41ODZjMCAwLjU1MyAwLjQ0OCAxIDEgMXMxLTAuNDQ3IDEtMXYtMjAuNTg2bDYuMjkzIDYuMjkzYzAuMTk1IDAuMTk1IDAuNDUxIDAuMjkzIDAuNzA3IDAuMjkzczAuNTEyLTAuMDk4IDAuNzA3LTAuMjkzYzAuMzkxLTAuMzkxIDAuMzkxLTEuMDIzIDAtMS40MTRsLTcuOTk5LTcuOTk5eiIvPjwvc3ZnPg==';
this._loadDictionary();
this._build();
this._listen();
}
}
/**
* Loads the dictionary
* @private
*/
_loadDictionary(){
/** @private */
this._dictionary = {
...this._parameters.dictionary,
en: {
upload: 'Upload image',
...(this._parameters.dictionary || {}).en
},
fr: {
upload: 'Télécharger une image',
...(this._parameters.dictionary || {}).fr
}
};
}
/**
* Builds the ImageInputPreview module in the DOM
* @private
*/
_build(){
this.input.classList.add('iip-input', 'iip-initialized');
/**
* Input preview
* @type {Element}
*/
this.preview = document.createElement('img');
this.preview.classList.add('iip-preview');
this.preview.src = this._parameters.preview || this._uploadIcon;
this.preview.setAttribute('title', this._dictionary[this._parameters.lang].upload)
this.input.after(this.preview);
/**
* Image title
* @type {Element}
* @private
*/
this._title = document.createElement('span');
this._title.classList.add('iip-title');
this.preview.after(this._title);
}
/**
* Listens to necessary events
* @private
*/
_listen(){
this.preview.addEventListener('click', () => {
this.input.click();
});
this.input.addEventListener('change', () => {
if(this.input.files && this.input.files[0]){
if(this.input.files[0].type.startsWith('image/')){
const reader = new FileReader();
reader.onload = r => {
this.preview.src = r.target.result;
};
reader.readAsDataURL(this.input.files[0]);
}
this._title.innerText = this.input.files[0].name;
this.changed = true;
}
});
}
/**
* Changes the preview image
* @param {String} src The preview URL
* @returns {ImageInputPreview} The current ImageInputPreview
*/
setPreview(src){
this.preview.src = src;
return this;
}
/**
* Changes the preview title
* @param {String} title The preview title
* @returns {ImageInputPreview} The current ImageInputPreview
*/
setTitle(title){
this._title.innerText = title;
return this;
}
/**
* Resets the input and the preview
* @returns {ImageInputPreview} The current ImageInputPreview
*/
reset(){
this.input.value = '';
this.preview.src = this._uploadIcon;
this._title.innerText = '';
this.changed = true;
return this;
}
}
// Initialize inputs the data- way
window.addEventListener('load', () => {
let inputs = document.querySelectorAll('input[type="file"][data-preview]');
inputs.forEach(input => {
new ImageInputPreview(input, {
preview: input.getAttribute('data-preview')
});
});
});