1
|
|
|
/* |
2
|
|
|
* MikoPBX - free phone system for small business |
3
|
|
|
* Copyright (C) 2017-2021 Alexey Portnov and Nikolay Beketov |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
16
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/* global globalRootUrl, Form, extension */ |
20
|
|
|
|
21
|
|
|
const avatar = { |
22
|
|
|
$picture: $('#avatar'), |
23
|
|
|
initialize() { |
24
|
|
|
if (avatar.$picture.attr('src') === '') { |
25
|
|
|
avatar.$picture.attr('src', `${globalRootUrl}assets/img/unknownPerson.jpg`); |
26
|
|
|
} |
27
|
|
|
$('#upload-new-avatar').on('click', () => { |
28
|
|
|
$('#file-select').click(); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
$('#clear-avatar').on('click', () => { |
32
|
|
|
avatar.$picture.attr('src', `${globalRootUrl}assets/img/unknownPerson.jpg`); |
33
|
|
|
extension.$formObj.form('set value', 'user_avatar', null); |
34
|
|
|
extension.$sip_secret.trigger('change'); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
$('#file-select').on('change', (e) => { |
38
|
|
|
let image; |
39
|
|
|
e.preventDefault(); |
40
|
|
|
const dataTransfer = 'dataTransfer' in e ? e.dataTransfer.files : []; |
41
|
|
|
const images = 'files' in e.target ? e.target.files : dataTransfer; |
42
|
|
|
if (images && images.length) { |
43
|
|
|
Array.from(images).forEach((curImage) => { |
44
|
|
|
if (typeof curImage !== 'object') return; |
45
|
|
|
image = new Image(); |
|
|
|
|
46
|
|
|
image.src = avatar.createObjectURL(curImage); |
47
|
|
|
image.onload = (event) => { |
48
|
|
|
const args = { |
49
|
|
|
src: event.target, |
50
|
|
|
width: 200, |
51
|
|
|
height: 200, |
52
|
|
|
type: 'image/png', |
53
|
|
|
compress: 90, |
54
|
|
|
}; |
55
|
|
|
const mybase64resized = avatar.resizeCrop(args); |
56
|
|
|
avatar.$picture.attr('src', mybase64resized); |
57
|
|
|
extension.$formObj.form('set value', 'user_avatar', mybase64resized); |
58
|
|
|
extension.$sip_secret.trigger('change'); |
59
|
|
|
}; |
60
|
|
|
}); |
61
|
|
|
} |
62
|
|
|
}); |
63
|
|
|
}, |
64
|
|
|
resizeCrop({ |
65
|
|
|
src, width, height, type, compress, |
66
|
|
|
}) { |
67
|
|
|
let newWidth = width; |
68
|
|
|
let newHeight = height; |
69
|
|
|
const crop = newWidth === 0 || newHeight === 0; |
70
|
|
|
// not resize |
71
|
|
|
if (src.width <= newWidth && newHeight === 0) { |
72
|
|
|
newWidth = src.width; |
73
|
|
|
newHeight = src.height; |
74
|
|
|
} |
75
|
|
|
// resize |
76
|
|
|
if (src.width > newWidth && newHeight === 0) { |
77
|
|
|
newHeight = src.height * (newWidth / src.width); |
78
|
|
|
} |
79
|
|
|
// check scale |
80
|
|
|
const xscale = newWidth / src.width; |
81
|
|
|
const yscale = newHeight / src.height; |
82
|
|
|
const scale = crop ? Math.min(xscale, yscale) : Math.max(xscale, yscale); |
83
|
|
|
// create empty canvas |
84
|
|
|
const canvas = document.createElement('canvas'); |
85
|
|
|
canvas.width = newWidth || Math.round(src.width * scale); |
86
|
|
|
canvas.height = newHeight || Math.round(src.height * scale); |
87
|
|
|
canvas.getContext('2d').scale(scale, scale); |
88
|
|
|
// crop it top center |
89
|
|
|
canvas.getContext('2d').drawImage(src, ((src.width * scale) - canvas.width) * -0.5, ((src.height * scale) - canvas.height) * -0.5); |
90
|
|
|
return canvas.toDataURL(type, compress); |
91
|
|
|
}, |
92
|
|
|
createObjectURL(i) { |
93
|
|
|
const URL = window.URL || window.webkitURL || window.mozURL || window.msURL; |
94
|
|
|
return URL.createObjectURL(i); |
95
|
|
|
}, |
96
|
|
|
|
97
|
|
|
}; |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.