src/app/components/picture-detail/picture-detail.component.ts
Picture detail modal component
selector | app-picture-detail |
styleUrls | picture-detail.component.scss |
templateUrl | ./picture-detail.component.html |
Properties |
|
Methods |
constructor(service: AlertService)
|
||||||||
Constructor
Parameters :
|
addToShoppingCart |
addToShoppingCart()
|
add image to shopping cart
Returns :
void
|
changeFormat |
changeFormat(type: string, formatName: string)
|
radio button Listener for updating price if other format is selected
Returns :
void
|
createPrintPicturePriceList |
createPrintPicturePriceList()
|
create array to save print-picture pricelist
Returns :
void
|
loadNextImage |
loadNextImage()
|
load next image of event-images array for modal-gallery
Returns :
void
|
loadPreviousImage |
loadPreviousImage()
|
load previous image of event-images array for modal-gallery
Returns :
void
|
ngOnInit |
ngOnInit()
|
Initalize component
Returns :
void
|
rateImage | ||||||||
rateImage(image: EventPicture)
|
||||||||
Parameters :
Returns :
void
|
reportImage | ||||||||
reportImage(image: EventPicture)
|
||||||||
Parameters :
Returns :
void
|
showModal | ||||||||||||||||||||||||
showModal(image: EventPicture, imageIndex: number, imagesLength: number, eventUserComponent: EventUserComponent, priceList: PriceList)
|
||||||||||||||||||||||||
Show modal
Parameters :
Returns :
void
|
download |
download:
|
Type : TemplateRef<any>
|
Decorators : ViewChild
|
TemplateRef download |
Public eventUserComponent |
eventUserComponent:
|
Type : EventUserComponent
|
Parent Component |
Public format |
format:
|
Type : string
|
format of actual image |
Public image |
image:
|
Type : EventPicture
|
Default value : {
event: '',
info: { height: 0, size: 0, type: '', width: 0 },
name: '',
preview: '',
ratings: 0,
thumbnail: '',
id: '',
selected: false
}
|
Image |
Public imageIndex |
imageIndex:
|
Type : number
|
index of images array |
Public images |
images:
|
Type : EventPicture[]
|
Images |
Public imagesLength |
imagesLength:
|
Type : number
|
length of images array |
Private log |
log:
|
Default value : Log.create('PictureDetailComponent')
|
Logger |
Public nextFlag |
nextFlag:
|
Type : boolean
|
next image flag |
Public pictureModal |
pictureModal:
|
Type : ModalDirective
|
Decorators : ViewChild
|
Create new event modal |
Public previousFlag |
previousFlag:
|
Type : boolean
|
previous image flag |
Public price |
price:
|
Type : number
|
price of actual image |
Public priceList |
priceList:
|
Type : PriceList
|
Price list |
print:
|
Type : TemplateRef<any>
|
Decorators : ViewChild
|
TemplateRef print |
Public printingHouse |
printingHouse:
|
Type : PrintingHouse
|
Printing house |
Public printPicturePriceList |
printPicturePriceList:
|
Type : PrintingHouseArticle[]
|
define certain price list for simplicity |
Public radioModel |
radioModel:
|
Default value : 'left'
|
define which register is preselected |
Public templateType |
templateType:
|
Type : TemplateRef<any>
|
Template ref |
import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import * as localforage from 'localforage';
import { ModalDirective } from 'ng-mdb-pro/free/modals/modal.directive';
import { Log } from 'ng2-logger';
import { PriceList } from '../../classes/price-list';
import { PRINTTYPE } from '../../enums/print-type';
import { SHOPPINGCARTITEMTYPE } from '../../enums/shopping-cart-item-type';
import { Alert } from '../../interfaces/alert';
import { EventPicture } from '../../interfaces/event-picture';
import { PrintingHouse } from '../../interfaces/printing-house';
import { PrintingHouseArticle } from '../../interfaces/printing-house-article';
import { ShoppingCartItem } from '../../interfaces/shopping-cart-item';
import { AlertService } from '../../services/alert/alert.service';
import { EventUserComponent } from '../event-user/event-user.component';
/**
* Picture detail modal component
* @author Daniel Sogl, Tim Kriessler
*/
@Component({
selector: 'app-picture-detail',
templateUrl: './picture-detail.component.html',
styleUrls: ['./picture-detail.component.scss']
})
export class PictureDetailComponent implements OnInit {
/** Logger */
private log = Log.create('PictureDetailComponent');
/** Create new event modal */
@ViewChild('pictureModal') public pictureModal: ModalDirective;
/** TemplateRef download */
@ViewChild('download') download: TemplateRef<any>;
/** TemplateRef print */
@ViewChild('print') print: TemplateRef<any>;
/** Template ref */
public templateType: TemplateRef<any>;
/** Parent Component */
public eventUserComponent: EventUserComponent;
/** Image */
public image: EventPicture = {
event: '',
info: { height: 0, size: 0, type: '', width: 0 },
name: '',
preview: '',
ratings: 0,
thumbnail: '',
id: '',
selected: false
};
/** index of images array */
public imageIndex: number;
/** length of images array */
public imagesLength: number;
/** previous image flag */
public previousFlag: boolean;
/** next image flag */
public nextFlag: boolean;
/** Printing house */
public printingHouse: PrintingHouse;
/** Images */
public images: EventPicture[];
/** Price list */
public priceList: PriceList;
/** define certain price list for simplicity */
public printPicturePriceList: PrintingHouseArticle[];
/** price of actual image */
public price: number;
/** format of actual image */
public format: string;
/** define which register is preselected */
public radioModel = 'left';
/**
* Constructor
* @param {AlertService} service AlertService
*/
constructor(private service: AlertService) {}
/**
* Initalize component
*/
ngOnInit() {
this.log.color = 'orange';
this.log.d('Component initialized');
this.templateType = this.download;
this.imagesLength = 0;
this.previousFlag = false;
this.nextFlag = false;
}
/**
* Show modal
* @param {EventPicture} image Image
*/
showModal(
image: EventPicture,
imageIndex: number,
imagesLength: number,
eventUserComponent: EventUserComponent,
priceList: PriceList
) {
this.log.d('Open picture modal');
this.image = image;
this.imageIndex = imageIndex;
this.imagesLength = imagesLength;
this.eventUserComponent = eventUserComponent;
this.priceList = priceList;
this.previousFlag = false;
this.nextFlag = false;
this.createPrintPicturePriceList();
/** deactivate previous button if selected first picture */
if (this.imageIndex === 0) {
this.previousFlag = true;
}
/** deactivate next button if selected last picture */
if (this.imageIndex + 1 === this.imagesLength) {
this.nextFlag = true;
}
this.pictureModal.show();
}
/**
* @param {EventPicture} image
*/
rateImage(image: EventPicture) {
this.eventUserComponent.rateImage(image);
}
/**
* @param {EventPicture} image
*/
reportImage(image: EventPicture) {
this.eventUserComponent.reportImage(image);
}
/**
* load previous image of event-images array for modal-gallery
*/
loadPreviousImage() {
this.nextFlag = false;
if (this.imageIndex > 0) {
this.imageIndex--;
this.image = this.eventUserComponent.getFollowingImage(this.imageIndex);
}
// hide previous button if last picture is reached
if (this.imageIndex === 0) {
this.previousFlag = true;
}
}
/**
* load next image of event-images array for modal-gallery
*/
loadNextImage() {
this.previousFlag = false;
// hide next button if last picture is reached
if (
this.eventUserComponent.getFollowingImage(this.imageIndex + 2) ===
undefined
) {
this.nextFlag = true;
}
this.imageIndex++;
this.image = this.eventUserComponent.getFollowingImage(this.imageIndex);
}
/**
* create array to save print-picture pricelist
*/
createPrintPicturePriceList() {
for (let i = 0; i < this.priceList.printingHouseItems.length; i++) {
if (this.priceList.printingHouseItems[i].name === PRINTTYPE.PICTURE) {
this.printPicturePriceList = this.priceList.printingHouseItems[
i
].articles;
}
}
}
/**
* radio button Listener for updating price if other format is selected
* @param {string} type type
* @param {string} formatName format name
*/
changeFormat(type: string, formatName: string) {
if (type === 'print') {
for (let i = 0; i < this.printPicturePriceList.length; i++) {
if (formatName === this.printPicturePriceList[i].name) {
this.format = this.printPicturePriceList[i].name;
this.price = this.printPicturePriceList[i].price;
}
}
}
if (type === 'download') {
for (let i = 0; i < this.priceList.downloadItems.length; i++) {
if (formatName === this.priceList.downloadItems[i].name) {
this.format = this.priceList.downloadItems[i].name;
this.price = this.priceList.downloadItems[i].price;
}
}
}
}
/**
* add image to shopping cart
*/
addToShoppingCart() {
if (this.format === undefined) {
const alert: Alert = {
title: 'Kein Format ausgewählt!'
};
this.service.showError(alert);
this.log.info('No format selected');
} else {
this.log.info('addtoShoppingCart: ' + this.radioModel);
let itemType: SHOPPINGCARTITEMTYPE;
if (this.radioModel === 'left') {
itemType = SHOPPINGCARTITEMTYPE.DOWNLOAD;
}
if (this.radioModel === 'right') {
itemType = SHOPPINGCARTITEMTYPE.PRINT;
}
this.log.info('itemType: ' + itemType);
// Load items from local storage
const shoppingCartItem: ShoppingCartItem = {
eventname: this.image.event,
name: this.image.name,
// TODO: differentiation between Download and Print
info: this.image.info,
format: this.format,
itemType: itemType,
amount: 1,
totalPrice: 0,
preview: this.image.preview,
thumbnail: this.image.thumbnail,
price: this.price,
photographer: this.eventUserComponent.event.photographerUid
};
localforage.getItem<ShoppingCartItem[]>('cart-items').then(items => {
if (items) {
items.push(shoppingCartItem);
} else {
items = [];
items.push(shoppingCartItem);
}
localforage
.setItem('cart-items', items)
.then(() => {
this.log.d('Saved item to cart');
})
.catch(err => {
this.log.er('Error saving item', err);
});
});
const alert: Alert = {
title: 'Bild zum Warenkorb hinzugefügt'
};
this.service.showSuccess(alert);
}
}
}
<div mdbModal #pictureModal="mdb-modal" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog modal-fluid" role="document">
<div class="modal-content">
<div class="modal-header indigo">
<h4 class="modal-title w-100 text-white" id="myModalLabel">{{ image.name }}</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-12 col-lg-8 p-3">
<img [src]="image.preview" class="img-size" [alt]="image.name">
<p class="bildname">{{image.name}}</p>
<div class="row">
<div class="col-12 col-lg-5 p-3">
<table>
<tr>
<td>
<a class="icon-button" [ngStyle]="image.selected && { 'color': '#3f51b5' }" (click)="image.selected = !image.selected">
<i class="fa fa-check fa-2x" aria-hidden="true"> </i>
</a>
</td>
<td>
<a class="icon-button" style="color: #3f51b5" (click)="rateImage(image)">
<i class="fa fa-heart fa-2x" aria-hidden="true"> {{ image.ratings }}</i>
</a>
</td>
<td>
<a class="icon-button" style="color: #ff4848" (click)="reportImage(image)">
<i class="fa fa-exclamation-triangle fa-2x" aria-hidden="true"> </i>
</a>
</td>
</tr>
</table>
</div>
<div class="col-12 col-lg-7 p-3">
<button *ngIf="!previousFlag" class="btn pink waves-light navigation" mdbRippleRadius (click)="loadPreviousImage()">
<i class="fa fa-angle-double-left mr-1"></i>{{ 'LABELS.PREVIOUS' | translate }}
</button>
<button *ngIf="!nextFlag" class="btn pink waves-light navigation" mdbRippleRadius (click)="loadNextImage()">{{ 'LABELS.NEXT' | translate }}
<i class="fa fa-angle-double-right ml-1"></i>
</button>
</div>
</div>
</div>
<div class="col-12 col-lg-4 vertical-ruler p-3">
<div class="btn-group">
<label class="btn btn-primary waves-light" [(ngModel)]="radioModel" mdbRadio="left" mdbRippleRadius (click)="this.templateType = this.download; this.price = undefined; this.format = undefined">
<i class="fa fa-download fa-5x" aria-hidden="true"></i>
</label>
<label class="btn btn-primary waves-light" [(ngModel)]="radioModel" mdbRadio="right" mdbRippleRadius (click)="this.templateType = this.print; this.price = undefined; this.format = undefined">
<i class="fa fa-print fa-5x" aria-hidden="true"></i>
</label>
</div>
<ng-container *ngTemplateOutlet="templateType"></ng-container>
<button type="button" class="btn btn-pink btn-lg btn-block waves-light" mdbRippleRadius (click)="addToShoppingCart()">
<i class="fa fa-shopping-cart mr-1"></i> {{ 'BUTTONS.ADD_TO_SHOPPING_CART' | translate }}</button>
</div>
</div>
<!--<div class="modal-footer justify-content-center"></div>-->
</div>
</div>
</div>
<ng-template #download>
<div class="p-4">
<table>
<tr>
<td>
<p>{{ 'LABELS.FORMAT' | translate }}:</p>
<div *ngIf="priceList" class="form-group">
<div *ngFor="let downloadItem of priceList.downloadItems; let i = index">
<input name="group1" type="radio" class="with-gap" id="radio{{i}}" (click)="changeFormat('download', downloadItem.name)">
<label for="radio{{i}}">{{downloadItem.name}} - {{ downloadItem.price | number:'0.2-2' }}€</label>
</div>
</div>
</td>
</tr>
</table>
</div>
</ng-template>
<ng-template #print>
<div class="p-4">
<table>
<tr>
<td>
<p>{{ 'LABELS.FORMAT' | translate }}:</p>
<div *ngIf="priceList" class="form-group">
<div *ngFor="let article of printPicturePriceList; let i = index">
<input name="group1" type="radio" class="with-gap" id="radio{{i}}" (click)="changeFormat('print', article.name)">
<label for="radio{{i}}">{{article.name}} - {{ article.price | number:'0.2-2' }}€</label>
</div>
</div>
</td>
</tr>
</table>
</div>
</ng-template>