2020-08-29 00:06:28 +03:00
|
|
|
import appDownloadManager from "../lib/appManagers/appDownloadManager";
|
2020-10-13 00:06:02 +03:00
|
|
|
import resizeableImage from "../lib/cropper";
|
|
|
|
import { PopupElement } from "./popup";
|
|
|
|
import { ripple } from "./ripple";
|
|
|
|
|
|
|
|
export default class PopupAvatar extends PopupElement {
|
|
|
|
private cropContainer: HTMLElement;
|
|
|
|
private input: HTMLInputElement;
|
|
|
|
private btnSubmit: HTMLElement;
|
|
|
|
private h6: HTMLElement;
|
2020-05-09 15:02:07 +03:00
|
|
|
|
|
|
|
private image = new Image();
|
|
|
|
|
|
|
|
private canvas: HTMLCanvasElement;
|
|
|
|
private blob: Blob;
|
|
|
|
private cropper = {
|
|
|
|
crop: () => {},
|
|
|
|
removeHandlers: () => {}
|
|
|
|
};
|
|
|
|
|
2020-09-17 22:33:23 +03:00
|
|
|
private onCrop: (upload: () => ReturnType<typeof appDownloadManager.upload>) => void;
|
2020-05-09 15:02:07 +03:00
|
|
|
|
|
|
|
constructor() {
|
2020-10-13 00:06:02 +03:00
|
|
|
super('popup-avatar', null, {closable: true});
|
|
|
|
|
|
|
|
this.h6 = document.createElement('h6');
|
|
|
|
this.h6.innerText = 'Drag to Reposition';
|
|
|
|
|
|
|
|
this.closeBtn.classList.remove('btn-icon');
|
|
|
|
|
|
|
|
this.header.append(this.h6);
|
|
|
|
|
|
|
|
this.cropContainer = document.createElement('div');
|
|
|
|
this.cropContainer.classList.add('crop');
|
2020-05-09 15:02:07 +03:00
|
|
|
this.cropContainer.append(this.image);
|
|
|
|
|
2020-10-13 00:06:02 +03:00
|
|
|
this.input = document.createElement('input');
|
|
|
|
this.input.type = 'file';
|
|
|
|
this.input.style.display = 'none';
|
2020-05-09 15:02:07 +03:00
|
|
|
this.input.addEventListener('change', (e: any) => {
|
2020-10-13 00:06:02 +03:00
|
|
|
const file = e.target.files[0];
|
2020-05-09 15:02:07 +03:00
|
|
|
if(!file) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-13 00:06:02 +03:00
|
|
|
const reader = new FileReader();
|
2020-05-09 15:02:07 +03:00
|
|
|
reader.onload = (e) => {
|
2020-10-13 00:06:02 +03:00
|
|
|
const contents = e.target.result as string;
|
2020-05-09 15:02:07 +03:00
|
|
|
|
|
|
|
this.image = new Image();
|
|
|
|
this.cropContainer.append(this.image);
|
|
|
|
this.image.src = contents;
|
|
|
|
|
|
|
|
this.image.onload = () => {
|
|
|
|
/* let {w, h} = calcImageInBox(this.image.naturalWidth, this.image.naturalHeight, 460, 554);
|
|
|
|
cropContainer.style.width = w + 'px';
|
|
|
|
cropContainer.style.height = h + 'px'; */
|
2020-10-13 00:06:02 +03:00
|
|
|
this.show();
|
2020-05-09 15:02:07 +03:00
|
|
|
|
|
|
|
this.cropper = resizeableImage(this.image, this.canvas);
|
|
|
|
this.input.value = '';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
}, false);
|
|
|
|
|
2020-10-13 00:06:02 +03:00
|
|
|
this.btnSubmit = document.createElement('button');
|
|
|
|
this.btnSubmit.className = 'btn-primary btn-circle btn-crop btn-icon tgico-check z-depth-1';
|
|
|
|
ripple(this.btnSubmit);
|
|
|
|
this.btnSubmit.addEventListener('click', () => {
|
2020-05-09 15:02:07 +03:00
|
|
|
this.cropper.crop();
|
|
|
|
this.closeBtn.click();
|
|
|
|
|
|
|
|
this.canvas.toBlob(blob => {
|
|
|
|
this.blob = blob; // save blob to send after reg
|
2020-05-13 18:26:40 +03:00
|
|
|
this.darkenCanvas();
|
2020-05-09 15:02:07 +03:00
|
|
|
this.resolve();
|
|
|
|
}, 'image/jpeg', 1);
|
|
|
|
});
|
|
|
|
|
2020-10-13 00:06:02 +03:00
|
|
|
this.container.append(this.cropContainer, this.btnSubmit, this.input);
|
2020-05-09 15:02:07 +03:00
|
|
|
|
2020-10-13 00:06:02 +03:00
|
|
|
this.onCloseAfterTimeout = () => {
|
|
|
|
this.cropper.removeHandlers();
|
|
|
|
if(this.image) {
|
|
|
|
this.image.remove();
|
|
|
|
}
|
|
|
|
};
|
2020-05-09 15:02:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private resolve() {
|
|
|
|
this.onCrop(() => {
|
2020-08-29 00:06:28 +03:00
|
|
|
return appDownloadManager.upload(this.blob);
|
2020-05-09 15:02:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:33:23 +03:00
|
|
|
public open(postCanvas: HTMLCanvasElement, onCrop: PopupAvatar['onCrop']) {
|
2020-05-09 15:02:07 +03:00
|
|
|
this.canvas = postCanvas;
|
|
|
|
this.onCrop = onCrop;
|
|
|
|
|
|
|
|
this.input.click();
|
|
|
|
}
|
2020-05-13 18:26:40 +03:00
|
|
|
|
|
|
|
public darkenCanvas() {
|
|
|
|
let ctx = this.canvas.getContext('2d');
|
|
|
|
ctx.fillStyle = "rgba(0, 0, 0, 0.3)";
|
|
|
|
ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
}
|
2020-05-09 15:02:07 +03:00
|
|
|
}
|