UNPKG

ngx-image-viewer-3

Version:

This package is an updated version of the ngx-image-viewer package for Angular17.

278 lines (270 loc) 19.4 kB
import * as i0 from '@angular/core'; import { Directive, Input, EventEmitter, Component, Optional, Inject, Output, HostListener, NgModule } from '@angular/core'; import * as screenfull from 'screenfull'; import * as i1 from '@angular/common'; import { CommonModule } from '@angular/common'; class ToggleFullscreenDirective { constructor(el) { this.el = el; this.isFullscreen = false; } ngOnChanges() { if (this.isFullscreen && screenfull.default.isEnabled) { screenfull.default.request(this.el.nativeElement); } else if (screenfull.default.isEnabled) { screenfull.default.exit(); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ToggleFullscreenDirective, deps: [{ token: i0.ElementRef }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "17.3.12", type: ToggleFullscreenDirective, selector: "[ngxToggleFullscreen]", inputs: { isFullscreen: ["ngxToggleFullscreen", "isFullscreen"] }, usesOnChanges: true, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ToggleFullscreenDirective, decorators: [{ type: Directive, args: [{ selector: '[ngxToggleFullscreen]' }] }], ctorParameters: () => [{ type: i0.ElementRef }], propDecorators: { isFullscreen: [{ type: Input, args: ['ngxToggleFullscreen'] }] } }); class CustomEvent { constructor(name, imageIndex) { this.name = name; this.imageIndex = imageIndex; } } const DEFAULT_CONFIG = { btnClass: 'default', zoomFactor: 0.1, containerBackgroundColor: '#ccc', wheelZoom: false, allowFullscreen: true, allowKeyboardNavigation: true, btnShow: { zoomIn: true, zoomOut: true, rotateClockwise: true, rotateCounterClockwise: true, next: true, prev: true }, btnIcons: { zoomIn: 'fa fa-plus', zoomOut: 'fa fa-minus', rotateClockwise: 'fa fa-repeat', rotateCounterClockwise: 'fa fa-undo', next: 'fa fa-arrow-right', prev: 'fa fa-arrow-left', fullscreen: 'fa fa-arrows-alt', } }; class ImageViewerComponent { constructor(moduleConfig) { this.moduleConfig = moduleConfig; this.index = 0; this.config = {}; this.indexChange = new EventEmitter(); this.configChange = new EventEmitter(); this.customEvent = new EventEmitter(); this.style = { transform: '', msTransform: '', oTransform: '', webkitTransform: '' }; this.fullscreen = false; this.loading = true; this.scale = 1; this.rotation = 0; this.translateX = 0; this.translateY = 0; this.hovered = false; } ngOnInit() { const merged = this.mergeConfig(DEFAULT_CONFIG, this.moduleConfig); this.config = this.mergeConfig(merged, this.config); this.triggerConfigBinding(); this.config.wheelZoom = true; } nextImage(event) { if (this.canNavigate(event) && this.index < this.src.length - 1) { this.loading = true; this.index++; this.triggerIndexBinding(); this.reset(); } } prevImage(event) { if (this.canNavigate(event) && this.index > 0) { this.loading = true; this.index--; this.triggerIndexBinding(); this.reset(); } } zoomIn() { if (this.config.zoomFactor) this.scale *= (1 + this.config.zoomFactor); this.updateStyle(); } zoomOut() { if (this.config.zoomFactor) if (this.scale > this.config.zoomFactor) { this.scale /= (1 + this.config.zoomFactor); } this.updateStyle(); } scrollZoom(evt) { let sonuc = true; if (this.config.wheelZoom) { evt.deltaY > 0 ? this.zoomOut() : this.zoomIn(); sonuc = false; } return sonuc; } rotateClockwise() { this.rotation += 90; this.updateStyle(); } rotateCounterClockwise() { this.rotation -= 90; this.updateStyle(); } onLoad() { this.loading = false; } onLoadStart() { this.loading = true; } onDragOver(evt) { this.translateX += (evt.clientX - this.prevX); this.translateY += (evt.clientY - this.prevY); this.prevX = evt.clientX; this.prevY = evt.clientY; this.updateStyle(); } onDragStart(evt) { if (evt.dataTransfer && evt.dataTransfer.setDragImage) { evt.dataTransfer.setDragImage(evt.target.nextElementSibling, 0, 0); } this.prevX = evt.clientX; this.prevY = evt.clientY; } toggleFullscreen() { this.fullscreen = !this.fullscreen; if (!this.fullscreen) { this.reset(); } } triggerIndexBinding() { this.indexChange.emit(this.index); } triggerConfigBinding() { this.configChange.next(this.config); } fireCustomEvent(name, imageIndex) { this.customEvent.emit(new CustomEvent(name, imageIndex)); } reset() { this.scale = 1; this.rotation = 0; this.translateX = 0; this.translateY = 0; this.updateStyle(); } onMouseOver() { this.hovered = true; } onMouseLeave() { this.hovered = false; } canNavigate(event) { return event == null || (this.config.allowKeyboardNavigation && this.hovered); } updateStyle() { this.style.transform = `translate(${this.translateX}px, ${this.translateY}px) rotate(${this.rotation}deg) scale(${this.scale})`; this.style.msTransform = this.style.transform; this.style.webkitTransform = this.style.transform; this.style.oTransform = this.style.transform; } mergeConfig(defaultValues, overrideValues) { let result = { ...defaultValues }; if (overrideValues) { result = { ...defaultValues, ...overrideValues }; if (overrideValues.btnIcons) { result.btnIcons = { ...defaultValues.btnIcons, ...overrideValues.btnIcons }; } } return result; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerComponent, deps: [{ token: 'config', optional: true }], target: i0.ɵɵFactoryTarget.Component }); } static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "17.3.12", type: ImageViewerComponent, selector: "ngx-image-viewer", inputs: { src: "src", index: "index", config: "config" }, outputs: { indexChange: "indexChange", configChange: "configChange", customEvent: "customEvent" }, host: { listeners: { "window:keyup.ArrowRight": "nextImage($event)", "window:keyup.ArrowLeft": "prevImage($event)", "mouseover": "onMouseOver()", "mouseleave": "onMouseLeave()" } }, ngImport: i0, template: "<div [ngxToggleFullscreen]=\"fullscreen\" class=\"img-container\" [style.backgroundColor]=\"config.containerBackgroundColor\"\r\n (wheel)=\"scrollZoom($event)\" (dragover)=\"onDragOver($event)\">\r\n <img [src]=\"src[index]\" [ngStyle]=\"style\" alt=\"Image not found...\" (dragstart)=\"onDragStart($event)\" (load)=\"onLoad()\" (loadstart)=\"onLoadStart()\"/>\r\n <!-- Div below will be used to hide the 'ghost' image when dragging -->\r\n <div></div>\r\n <div class=\"spinner-container\" *ngIf=\"loading\">\r\n <div class=\"spinner\"></div>\r\n </div>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.rotateCounterClockwise\" (click)=\"rotateCounterClockwise()\">\r\n <span [class]=\"config.btnIcons?.rotateCounterClockwise\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.rotateClockwise\" (click)=\"rotateClockwise()\">\r\n <span [class]=\"config.btnIcons?.rotateClockwise\"></span>\r\n </button>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.zoomOut\" (click)=\"zoomOut()\">\r\n <span [class]=\"config.btnIcons?.zoomOut\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.zoomIn\" (click)=\"zoomIn()\">\r\n <span [class]=\"config.btnIcons?.zoomIn\"></span>\r\n </button>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngFor=\"let cBtn of config.customBtns\" (click)=\"fireCustomEvent(cBtn.name, index)\">\r\n <span [class]=\"cBtn.icon\"></span>\r\n </button>\r\n\r\n <button type=\"button\" id=\"ngx-fs-btn\" [class]=\"config.btnClass\" (click)=\"toggleFullscreen()\" *ngIf=\"config.allowFullscreen\">\r\n <span [class]=\"config.btnIcons?.fullscreen\"></span>\r\n </button>\r\n\r\n <div class=\"nav-button-container\" *ngIf=\"src.length > 1\">\r\n <button type=\"button\" [class]=\"config.btnClass\" (click)=\"prevImage($event)\" [disabled]=\"index === 0\">\r\n <span [class]=\"config.btnIcons?.prev\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" (click)=\"nextImage($event)\" [disabled]=\"index === src.length - 1\">\r\n <span [class]=\"config.btnIcons?.next\"></span>\r\n </button>\r\n </div>\r\n</div>\r\n", styles: [".img-container{height:100%;width:100%;overflow:hidden;position:relative}.img-container img{z-index:2;margin:0 auto;display:block;max-width:100%;max-height:100%}.img-container button{z-index:99;position:absolute;right:15px}.img-container button:not(:disabled){cursor:pointer}.img-container>button:nth-of-type(1):not(#ngx-fs-btn){bottom:15px}.img-container>button:nth-of-type(2):not(#ngx-fs-btn){bottom:65px}.img-container>button:nth-of-type(3):not(#ngx-fs-btn){bottom:115px}.img-container>button:nth-of-type(4):not(#ngx-fs-btn){bottom:165px}.img-container>button:nth-of-type(5):not(#ngx-fs-btn){bottom:215px}.img-container>button:nth-of-type(6):not(#ngx-fs-btn){bottom:265px}.img-container>button:nth-of-type(7):not(#ngx-fs-btn){bottom:315px}#ngx-fs-btn{top:15px}button.default{height:40px;width:40px;border:1px solid #555;border-radius:50%;background-color:#fff;opacity:.7;transition:opacity .2s}button.default:hover{opacity:1}button.default:disabled{opacity:.25}.nav-button-container>button{position:relative;right:0;margin:0 10px}.nav-button-container{text-align:center;position:absolute;z-index:98;bottom:10px;left:0;right:0}.spinner-container{position:absolute;inset:0;width:60px;height:60px;margin:auto;padding:10px;background-color:#0006;border-radius:25%}.spinner{border-width:7px;border-style:solid;border-color:#ccc;border-bottom-color:#222;border-radius:50%;height:100%;width:100%;box-sizing:border-box;-webkit-animation:rotation 2s linear infinite;-moz-animation:rotation 2s linear infinite;-o-animation:rotation 2s linear infinite;animation:rotation 2s linear infinite}@keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-webkit-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-moz-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-o-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@media print{button{display:none}}\n"], dependencies: [{ kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }, { kind: "directive", type: ToggleFullscreenDirective, selector: "[ngxToggleFullscreen]", inputs: ["ngxToggleFullscreen"] }] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerComponent, decorators: [{ type: Component, args: [{ selector: 'ngx-image-viewer', template: "<div [ngxToggleFullscreen]=\"fullscreen\" class=\"img-container\" [style.backgroundColor]=\"config.containerBackgroundColor\"\r\n (wheel)=\"scrollZoom($event)\" (dragover)=\"onDragOver($event)\">\r\n <img [src]=\"src[index]\" [ngStyle]=\"style\" alt=\"Image not found...\" (dragstart)=\"onDragStart($event)\" (load)=\"onLoad()\" (loadstart)=\"onLoadStart()\"/>\r\n <!-- Div below will be used to hide the 'ghost' image when dragging -->\r\n <div></div>\r\n <div class=\"spinner-container\" *ngIf=\"loading\">\r\n <div class=\"spinner\"></div>\r\n </div>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.rotateCounterClockwise\" (click)=\"rotateCounterClockwise()\">\r\n <span [class]=\"config.btnIcons?.rotateCounterClockwise\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.rotateClockwise\" (click)=\"rotateClockwise()\">\r\n <span [class]=\"config.btnIcons?.rotateClockwise\"></span>\r\n </button>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.zoomOut\" (click)=\"zoomOut()\">\r\n <span [class]=\"config.btnIcons?.zoomOut\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngIf=\"config.btnShow?.zoomIn\" (click)=\"zoomIn()\">\r\n <span [class]=\"config.btnIcons?.zoomIn\"></span>\r\n </button>\r\n\r\n <button type=\"button\" [class]=\"config.btnClass\" *ngFor=\"let cBtn of config.customBtns\" (click)=\"fireCustomEvent(cBtn.name, index)\">\r\n <span [class]=\"cBtn.icon\"></span>\r\n </button>\r\n\r\n <button type=\"button\" id=\"ngx-fs-btn\" [class]=\"config.btnClass\" (click)=\"toggleFullscreen()\" *ngIf=\"config.allowFullscreen\">\r\n <span [class]=\"config.btnIcons?.fullscreen\"></span>\r\n </button>\r\n\r\n <div class=\"nav-button-container\" *ngIf=\"src.length > 1\">\r\n <button type=\"button\" [class]=\"config.btnClass\" (click)=\"prevImage($event)\" [disabled]=\"index === 0\">\r\n <span [class]=\"config.btnIcons?.prev\"></span>\r\n </button>\r\n <button type=\"button\" [class]=\"config.btnClass\" (click)=\"nextImage($event)\" [disabled]=\"index === src.length - 1\">\r\n <span [class]=\"config.btnIcons?.next\"></span>\r\n </button>\r\n </div>\r\n</div>\r\n", styles: [".img-container{height:100%;width:100%;overflow:hidden;position:relative}.img-container img{z-index:2;margin:0 auto;display:block;max-width:100%;max-height:100%}.img-container button{z-index:99;position:absolute;right:15px}.img-container button:not(:disabled){cursor:pointer}.img-container>button:nth-of-type(1):not(#ngx-fs-btn){bottom:15px}.img-container>button:nth-of-type(2):not(#ngx-fs-btn){bottom:65px}.img-container>button:nth-of-type(3):not(#ngx-fs-btn){bottom:115px}.img-container>button:nth-of-type(4):not(#ngx-fs-btn){bottom:165px}.img-container>button:nth-of-type(5):not(#ngx-fs-btn){bottom:215px}.img-container>button:nth-of-type(6):not(#ngx-fs-btn){bottom:265px}.img-container>button:nth-of-type(7):not(#ngx-fs-btn){bottom:315px}#ngx-fs-btn{top:15px}button.default{height:40px;width:40px;border:1px solid #555;border-radius:50%;background-color:#fff;opacity:.7;transition:opacity .2s}button.default:hover{opacity:1}button.default:disabled{opacity:.25}.nav-button-container>button{position:relative;right:0;margin:0 10px}.nav-button-container{text-align:center;position:absolute;z-index:98;bottom:10px;left:0;right:0}.spinner-container{position:absolute;inset:0;width:60px;height:60px;margin:auto;padding:10px;background-color:#0006;border-radius:25%}.spinner{border-width:7px;border-style:solid;border-color:#ccc;border-bottom-color:#222;border-radius:50%;height:100%;width:100%;box-sizing:border-box;-webkit-animation:rotation 2s linear infinite;-moz-animation:rotation 2s linear infinite;-o-animation:rotation 2s linear infinite;animation:rotation 2s linear infinite}@keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-webkit-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-moz-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@-o-keyframes rotation{0%{-webkit-transform:rotate(0deg)}to{-webkit-transform:rotate(359deg)}}@media print{button{display:none}}\n"] }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: ['config'] }] }], propDecorators: { src: [{ type: Input }], index: [{ type: Input }], config: [{ type: Input }], indexChange: [{ type: Output }], configChange: [{ type: Output }], customEvent: [{ type: Output }], nextImage: [{ type: HostListener, args: ['window:keyup.ArrowRight', ['$event']] }], prevImage: [{ type: HostListener, args: ['window:keyup.ArrowLeft', ['$event']] }], onMouseOver: [{ type: HostListener, args: ['mouseover'] }], onMouseLeave: [{ type: HostListener, args: ['mouseleave'] }] } }); class ImageViewerModule { constructor() { } static forRoot(config) { return { ngModule: ImageViewerModule, providers: [{ provide: 'config', useValue: config }] }; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerModule, declarations: [ImageViewerComponent, ToggleFullscreenDirective], imports: [CommonModule], exports: [ImageViewerComponent, ToggleFullscreenDirective] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerModule, imports: [CommonModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: ImageViewerModule, decorators: [{ type: NgModule, args: [{ imports: [ CommonModule, ], declarations: [ ImageViewerComponent, ToggleFullscreenDirective ], exports: [ ImageViewerComponent, ToggleFullscreenDirective ] }] }], ctorParameters: () => [] }); /* * Public API Surface of ngx-image-viewer-17 */ /** * Generated bundle index. Do not edit. */ export { CustomEvent, ImageViewerComponent, ImageViewerModule, ToggleFullscreenDirective }; //# sourceMappingURL=ngx-image-viewer-3.mjs.map