Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.8KB

  1. import {AfterViewInit, Component, ElementRef, HostListener, Input, OnInit, ViewChild} from '@angular/core';
  2. @Component({
  3. selector: 'app-image-popup-dialog',
  4. templateUrl: './image-popup-dialog.component.html',
  5. styleUrls: ['./image-popup-dialog.component.scss']
  6. })
  7. export class ImagePopupDialogComponent implements OnInit{
  8. @Input() url = '';
  9. @Input() title = 'Image';
  10. @ViewChild('content', { read: ElementRef }) img: ElementRef;
  11. public opened = false;
  12. public imgWidthPercent = 1;
  13. public iWidth = 100;
  14. public imgOriginalWidth = 0;
  15. public imgOriginalHeight = 0;
  16. public imageLoaded = false;
  17. public scrHeight = 0;
  18. public scrWidth = 0;
  19. public dialogWidth = 0 ;
  20. public margin = 32; // dialog margin;
  21. constructor() { }
  22. ngOnInit(): void {
  23. this.getScreenSize();
  24. this.initDialogWidth();
  25. }
  26. @HostListener('window:resize', ['$event'])
  27. getScreenSize(event?): void {
  28. this.scrHeight = window.innerHeight;
  29. this.scrWidth = window.innerWidth;
  30. this.dialogWidth = 200; // this.scrWidth;
  31. // console.log(this.scrHeight, this.scrWidth);
  32. // console.log(this.scrHeight, this.scrWidth);
  33. }
  34. public showImg(url: string, title?: string): void {
  35. this.initDialogWidth();
  36. this.title = title || 'Image';
  37. const ts = Date.now();
  38. this.url = url + '?date=' + ts;
  39. this.opened = true;
  40. }
  41. private initDialogWidth(): void {
  42. this.imageLoaded = false;
  43. this.dialogWidth = 200;
  44. this.iWidth = this.dialogWidth - this.margin;
  45. }
  46. public close(status: string): void {
  47. this.opened = false;
  48. }
  49. public zoom(diff: number): void {
  50. this.imgWidthPercent += diff;
  51. if (this.imgWidthPercent < 0.05) {
  52. this.imgWidthPercent = 0.05;
  53. }
  54. if (this.imgWidthPercent > 3){
  55. this.imgWidthPercent = 3;
  56. }
  57. this.resizeWindow();
  58. }
  59. public originalSize(): void {
  60. this.imgWidthPercent = 1;
  61. this.resizeWindow();
  62. }
  63. private resizeWindow(): void {
  64. this.iWidth = this.imgOriginalWidth * this.imgWidthPercent;
  65. const margin = this.margin; // the default dialog padding
  66. if (this.iWidth + margin <= this.scrWidth) {
  67. this.dialogWidth = this.iWidth + margin;
  68. }else{
  69. this.dialogWidth = this.scrWidth;
  70. }
  71. // console.log(this.img.nativeElement.naturalWidth, this.img.nativeElement.naturalHeight,
  72. // this.img.nativeElement.width, this.img.nativeElement.height, this.iWidth, this.imgWidthPercent);
  73. }
  74. public onLoad(): void {
  75. this.imageLoaded = true;
  76. this.imgOriginalWidth = this.img.nativeElement.naturalWidth;
  77. this.imgOriginalHeight = this.img.nativeElement.naturalHeight;
  78. if ( this.imgOriginalWidth > this.scrWidth) {
  79. this.iWidth = this.scrWidth - this.margin;
  80. }else{
  81. this.iWidth = this.imgOriginalWidth;
  82. }
  83. this.imgWidthPercent = this.iWidth / this.imgOriginalWidth;
  84. this.resizeWindow();
  85. }
  86. }