Passed
Push — develop ( a9fa19...44b1e6 )
by Nikolay
06:22 queued 13s
created

sites/admin-cabinet/assets/js/src/Extensions/extension-modify-avatar.js   A

Complexity

Total Complexity 16
Complexity/F 2

Size

Lines of Code 77
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 54
c 0
b 0
f 0
dl 0
loc 77
rs 10
mnd 8
bc 8
fnc 8
bpm 1
cpm 2
noi 1
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();
0 ignored issues
show
Bug introduced by
The variable Image seems to be never declared. If this is a global, consider adding a /** global: Image */ comment.

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.

Loading history...
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
};