@scicom-esolutions/ng-image-viewer
Version:
[](https://www.npmjs.com/package/@haseeamarathunga/ng-image-viewer) [](https://www.npmjs.com/pac
456 lines (443 loc) • 15.9 kB
JavaScript
import { Injectable, ɵɵdefineInjectable, EventEmitter, Component, Optional, Inject, Input, Output, HostListener, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
/**
* @fileoverview added by tsickle
* Generated from: lib/ng-image-viewer.service.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgImageViewerService {
constructor() { }
}
NgImageViewerService.decorators = [
{ type: Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgImageViewerService.ctorParameters = () => [];
/** @nocollapse */ NgImageViewerService.ngInjectableDef = ɵɵdefineInjectable({ factory: function NgImageViewerService_Factory() { return new NgImageViewerService(); }, token: NgImageViewerService, providedIn: "root" });
/**
* @fileoverview added by tsickle
* Generated from: lib/model/custom-image-event-model.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class CustomImageEvent {
/**
* @param {?} name
* @param {?} imageIndex
*/
constructor(name, imageIndex) {
this.name = name;
this.imageIndex = imageIndex;
}
}
if (false) {
/** @type {?} */
CustomImageEvent.prototype.name;
/** @type {?} */
CustomImageEvent.prototype.imageIndex;
}
/**
* @fileoverview added by tsickle
* Generated from: lib/ng-image-viewer.component.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
const DEFAULT_CONFIG = {
btnClass: 'default',
zoomFactor: 0.1,
containerBackgroundColor: '#ccc',
wheelZoom: true,
allowFullscreen: true,
allowKeyboardNavigation: true,
btnShow: {
zoomIn: true,
zoomOut: true,
rotateClockwise: true,
rotateCounterClockwise: true,
},
btnIcons: {
zoomIn: 'fa fa-plus',
zoomOut: 'fa fa-minus',
rotateClockwise: 'fa fa-repeat',
rotateCounterClockwise: 'fa fa-undo',
fullscreen: 'fa fa-refresh',
}
};
class NgImageViewerComponent {
/**
* @param {?} moduleConfig
*/
constructor(moduleConfig) {
this.moduleConfig = moduleConfig;
this.index = 0;
this.indexChange = new EventEmitter();
this.configChange = new EventEmitter();
this.customImageEvent = new EventEmitter();
this.style = { transform: '', msTransform: '', oTransform: '', webkitTransform: '', cursor: 'pointer' };
this.fullscreen = false;
this.loading = true;
this.scale = 1;
this.rotation = 0;
this.translateX = 0;
this.translateY = 0;
this.hovered = false;
}
/**
* @return {?}
*/
ngOnInit() {
/** @type {?} */
const merged = this.mergeConfig(DEFAULT_CONFIG, this.moduleConfig);
this.config = this.mergeConfig(merged, this.config);
this.triggerConfigBinding();
}
/**
* @param {?} event
* @return {?}
*/
nextImage(event) {
if (this.canNavigate(event) && this.index < this.src.length - 1) {
this.loading = true;
this.index++;
this.triggerIndexBinding();
this.reset();
}
}
/**
* @param {?} event
* @return {?}
*/
prevImage(event) {
if (this.canNavigate(event) && this.index > 0) {
this.loading = true;
this.index--;
this.triggerIndexBinding();
this.reset();
}
}
/**
* @return {?}
*/
zoomIn() {
this.scale *= (1 + this.config.zoomFactor);
this.updateStyle();
}
/**
* @return {?}
*/
zoomOut() {
if (this.scale > this.config.zoomFactor) {
this.scale /= (1 + this.config.zoomFactor);
}
this.updateStyle();
}
/**
* @param {?} evt
* @return {?}
*/
scrollZoom(evt) {
if (this.config.wheelZoom) {
evt.deltaY > 0 ? this.zoomOut() : this.zoomIn();
return false;
}
}
/**
* @return {?}
*/
rotateClockwise() {
this.rotation += 90;
this.updateStyle();
}
/**
* @return {?}
*/
rotateCounterClockwise() {
this.rotation -= 90;
this.updateStyle();
}
/**
* @return {?}
*/
onLoad() {
this.loading = false;
}
/**
* @return {?}
*/
onLoadStart() {
this.loading = true;
}
/**
* @param {?} evt
* @return {?}
*/
onDragOver(evt) {
this.translateX += (evt.clientX - this.prevX);
this.translateY += (evt.clientY - this.prevY);
this.prevX = evt.clientX;
this.prevY = evt.clientY;
this.updateStyle();
}
/**
* @param {?} evt
* @return {?}
*/
onDragStart(evt) {
this.prevX = evt.clientX;
this.prevY = evt.clientY;
}
/**
* @return {?}
*/
toggleFullscreen() {
// this.fullscreen = !this.fullscreen;
// if (!this.fullscreen) {
// this.reset();
// }
this.translateX = 0;
this.translateY = 0;
this.scale = 1;
this.rotation = 0;
this.translateX = 0;
this.translateY = 0;
this.zoomIn();
this.zoomOut();
}
/**
* @return {?}
*/
triggerIndexBinding() {
this.indexChange.emit(this.index);
}
/**
* @return {?}
*/
triggerConfigBinding() {
this.configChange.next(this.config);
}
/**
* @param {?} name
* @param {?} imageIndex
* @return {?}
*/
fireCustomEvent(name, imageIndex) {
this.customImageEvent.emit(new CustomImageEvent(name, imageIndex));
}
/**
* @return {?}
*/
reset() {
this.scale = 1;
this.rotation = 0;
this.translateX = 0;
this.translateY = 0;
this.updateStyle();
}
/**
* @return {?}
*/
onMouseOver() {
this.hovered = true;
}
/**
* @return {?}
*/
onMouseLeave() {
this.hovered = false;
}
/**
* @private
* @param {?} event
* @return {?}
*/
canNavigate(event) {
return event == null || (this.config.allowKeyboardNavigation && this.hovered);
}
/**
* @private
* @return {?}
*/
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;
}
/**
* @private
* @param {?} defaultValues
* @param {?} overrideValues
* @return {?}
*/
mergeConfig(defaultValues, overrideValues) {
/** @type {?} */
let result = Object.assign({}, defaultValues);
if (overrideValues) {
result = Object.assign({}, defaultValues, overrideValues);
if (overrideValues.btnIcons) {
result.btnIcons = Object.assign({}, defaultValues.btnIcons, overrideValues.btnIcons);
}
}
return result;
}
}
NgImageViewerComponent.decorators = [
{ type: Component, args: [{
// tslint:disable-next-line:component-selector
selector: 'ng-image-viewer',
template: `
<div class="img-container" [style.backgroundColor]="config.containerBackgroundColor"
(wheel)="scrollZoom($event)" (dragover)="onDragOver($event)">
<div [ngStyle]="{width: width+'px',height: height+'px'}">
<img [src]="src[index]" [ngStyle]="style" alt="Image not found..." (dragstart)="onDragStart($event)"
(load)="onLoad()" (loadstart)="onLoadStart()" />
<!-- Div below will be used to hide the 'ghost' image when dragging -->
</div>
<div></div>
<div class="spinner-container" *ngIf="loading">
<div class="spinner"></div>
</div>
<button type="button" [class]="config.btnClass" *ngIf="config.btnShow.rotateCounterClockwise"
(click)="rotateCounterClockwise()">
<span [class]="config.btnIcons.rotateCounterClockwise"></span>
</button>
<button type="button" [class]="config.btnClass" *ngIf="config.btnShow.rotateClockwise" (click)="rotateClockwise()">
<span [class]="config.btnIcons.rotateClockwise"></span>
</button>
<button type="button" [class]="config.btnClass" *ngIf="config.btnShow.zoomOut" (click)="zoomOut()">
<span [class]="config.btnIcons.zoomOut"></span>
</button>
<button type="button" [class]="config.btnClass" *ngIf="config.btnShow.zoomIn" (click)="zoomIn()">
<span [class]="config.btnIcons.zoomIn"></span>
</button>
<!-- <button type="button" [class]="config.btnClass" *ngFor="let cBtn of config.customBtns"-->
<!-- (click)="fireCustomEvent(cBtn.name, index)">-->
<!-- <span [class]="cBtn.icon"></span>-->
<!-- </button>-->
<button type="button" [class]="config.btnClass" (click)="toggleFullscreen()"
*ngIf="config.allowFullscreen">
<span [class]="config.btnIcons.fullscreen"></span>
</button>
<!-- <div class="nav-button-container" *ngIf="src.length > 1">-->
<!-- <button type="button" [class]="config.btnClass" (click)="prevImage($event)" [disabled]="index === 0">-->
<!-- <span [class]="config.btnIcons.prev"></span>-->
<!-- </button>-->
<!-- <button type="button" [class]="config.btnClass" (click)="nextImage($event)" [disabled]="index === src.length - 1">-->
<!-- <span [class]="config.btnIcons.next"></span>-->
<!-- </button>-->
<!-- </div>-->
</div>
`,
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%;width: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;left:0;right:0;top:0;bottom:0;width:60px;height:60px;margin:auto;padding:10px;background-color:rgba(0,0,0,.4);border-radius:25%}.spinner{border-width:7px;border-style:solid;border-color:#ccc #ccc #222;border-radius:50%;height:100%;width:100%;box-sizing:border-box;-webkit-animation:2s linear infinite rotation;animation:2s linear infinite rotation}@keyframes rotation{from{-webkit-transform:rotate(0)}to{-webkit-transform:rotate(359deg)}}@-webkit-keyframes rotation{from{-webkit-transform:rotate(0)}to{-webkit-transform:rotate(359deg)}}"]
}] }
];
/** @nocollapse */
NgImageViewerComponent.ctorParameters = () => [
{ type: undefined, decorators: [{ type: Optional }, { type: Inject, args: ['config',] }] }
];
NgImageViewerComponent.propDecorators = {
src: [{ type: Input }],
width: [{ type: Input }],
height: [{ type: Input }],
index: [{ type: Input }],
config: [{ type: Input }],
indexChange: [{ type: Output }],
configChange: [{ type: Output }],
customImageEvent: [{ 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',] }]
};
if (false) {
/** @type {?} */
NgImageViewerComponent.prototype.src;
/** @type {?} */
NgImageViewerComponent.prototype.width;
/** @type {?} */
NgImageViewerComponent.prototype.height;
/** @type {?} */
NgImageViewerComponent.prototype.index;
/** @type {?} */
NgImageViewerComponent.prototype.config;
/** @type {?} */
NgImageViewerComponent.prototype.indexChange;
/** @type {?} */
NgImageViewerComponent.prototype.configChange;
/** @type {?} */
NgImageViewerComponent.prototype.customImageEvent;
/** @type {?} */
NgImageViewerComponent.prototype.style;
/** @type {?} */
NgImageViewerComponent.prototype.fullscreen;
/** @type {?} */
NgImageViewerComponent.prototype.loading;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.scale;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.rotation;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.translateX;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.translateY;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.prevX;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.prevY;
/**
* @type {?}
* @private
*/
NgImageViewerComponent.prototype.hovered;
/** @type {?} */
NgImageViewerComponent.prototype.moduleConfig;
}
/**
* @fileoverview added by tsickle
* Generated from: lib/ng-image-viewer.module.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
class NgImageViewerModule {
}
NgImageViewerModule.decorators = [
{ type: NgModule, args: [{
declarations: [NgImageViewerComponent],
imports: [
CommonModule,
FormsModule,
],
exports: [NgImageViewerComponent]
},] }
];
/**
* @fileoverview added by tsickle
* Generated from: public-api.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @fileoverview added by tsickle
* Generated from: scicom-esolutions-ng-image-viewer.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
export { NgImageViewerComponent, NgImageViewerModule, NgImageViewerService };
//# sourceMappingURL=scicom-esolutions-ng-image-viewer.js.map