Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Videos #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11,275 changes: 11,275 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ngx-gallery",
"version": "5.10.0",
"version": "5.20.0",
"description": "Responsive Angular 2 / 4 image gallery plugin",
"scripts": {
"build": "gulp build",
Expand Down
19 changes: 19 additions & 0 deletions src/ngx-gallery-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@ export class NgxGalleryHelperService {
private removeSwipeHandlers(id: string): void {
this.swipeHandlers.delete(id);
}

getFileType (fileSource: string): string {
if (fileSource.startsWith('data:')) {
return fileSource.substr(5, Math.min(fileSource.indexOf(';'), fileSource.indexOf('/')) - 5);
}
let fileExtension = fileSource.split('.').pop().toLowerCase();
if (!fileExtension
Copy link

@geofmigliacci geofmigliacci Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using a indexOf with an array of file type could be better?
Moreover, using mime type could be a better solution (look for mime/type supported by current browsers) & I don't think that using substr is useful there.

fileSource.indexOf("video/mp4") as example is enough if fileSource is a dataUrl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes indexOf is better for most of the cases. But substr I think that here it will make this piece of code compatible with any content type. If this component will support other types later. This line can stay the as-is.
However, there was unnecessary usage of substr inside the next else if. I deleted it in the second commit.

|| fileExtension == 'jpeg' || fileExtension == 'jpg'
|| fileExtension == 'png' || fileExtension == 'bmp'
|| fileExtension == 'gif') {
return 'image';
}
else if (fileExtension == 'avi' || fileExtension == 'flv'
|| fileExtension == 'wmv' || fileExtension == 'mov'
|| fileExtension == 'mp4') {
return 'video';
}
return 'unknown';
}
}
34 changes: 28 additions & 6 deletions src/ngx-gallery-image.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Input, Output, EventEmitter, HostListener, ElementRef, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { Component, Input, Output, EventEmitter, HostListener, ElementRef, OnInit, OnChanges, SimpleChanges } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';

import { NgxGalleryHelperService } from './ngx-gallery-helper.service';
Expand All @@ -10,11 +10,29 @@ import { NgxGalleryAction } from './ngx-gallery-action.model';
selector: 'ngx-gallery-image',
template: `
<div class="ngx-gallery-image-wrapper ngx-gallery-animation-{{animation}} ngx-gallery-image-size-{{size}}">
<div class="ngx-gallery-image" *ngFor="let image of getImages(); let i = index;" [ngClass]="{ 'ngx-gallery-active': selectedIndex == image.index, 'ngx-gallery-inactive-left': selectedIndex > image.index, 'ngx-gallery-inactive-right': selectedIndex < image.index, 'ngx-gallery-clickable': clickable }" [style.background-image]="getSafeUrl(image.src)" (click)="handleClick($event, image.index)">
<div class="ngx-gallery-icons-wrapper">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled" [titleText]="action.titleText" (onClick)="action.onClick($event, image.index)"></ngx-gallery-action>
<div *ngFor="let image of getImages(); let i = index;">
<div *ngIf="image.type === 'image'" class="ngx-gallery-image" [ngClass]="{ 'ngx-gallery-active': selectedIndex == image.index, 'ngx-gallery-inactive-left': selectedIndex > image.index, 'ngx-gallery-inactive-right': selectedIndex < image.index, 'ngx-gallery-clickable': clickable }"
[style.background-image]="getSafeUrl(image.src)" (click)="handleClick($event, image.index)">
<div class="ngx-gallery-icons-wrapper">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled"
[titleText]="action.titleText" (onClick)="action.onClick($event, image.index)"></ngx-gallery-action>
</div>
<div class="ngx-gallery-image-text" *ngIf="showDescription && descriptions[image.index]" [innerHTML]="descriptions[image.index]"
(click)="$event.stopPropagation()"></div>
</div>
<div *ngIf="image.type === 'video'" class="ngx-gallery-image" [ngClass]="{ 'ngx-gallery-active': selectedIndex == image.index, 'ngx-gallery-inactive-left': selectedIndex > image.index, 'ngx-gallery-inactive-right': selectedIndex < image.index, 'ngx-gallery-clickable': clickable }"
(click)="handleClick($event, image.index)">
<video controls style="width: 100%; height: 100%;">
<source [src]="image.src">
Your browser does not support the video tag.
</video>
<div class="ngx-gallery-icons-wrapper">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled"
[titleText]="action.titleText" (onClick)="action.onClick($event, image.index)"></ngx-gallery-action>
</div>
<div class="ngx-gallery-image-text" *ngIf="showDescription && descriptions[image.index]" [innerHTML]="descriptions[image.index]"
(click)="$event.stopPropagation()"></div>
</div>
<div class="ngx-gallery-image-text" *ngIf="showDescription && descriptions[image.index]" [innerHTML]="descriptions[image.index]" (click)="$event.stopPropagation()"></div>
</div>
</div>
<ngx-gallery-bullets *ngIf="bullets" [count]="images.length" [active]="selectedIndex" (onChange)="show($event)"></ngx-gallery-bullets>
Expand Down Expand Up @@ -190,7 +208,7 @@ export class NgxGalleryImageComponent implements OnInit, OnChanges {

if (this.animation === NgxGalleryAnimation.Slide
|| this.animation === NgxGalleryAnimation.Fade) {
timeout = 500;
timeout = 500;
}

setTimeout(() => {
Expand Down Expand Up @@ -218,4 +236,8 @@ export class NgxGalleryImageComponent implements OnInit, OnChanges {
getSafeUrl(image: string): SafeStyle {
return this.sanitization.bypassSecurityTrustStyle(this.helperService.getBackgroundUrl(image));
}

getFileType(fileSource: string) {
return this.helperService.getFileType(fileSource);
}
}
3 changes: 3 additions & 0 deletions src/ngx-gallery-image.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface INgxGalleryImage {
big?: string | SafeResourceUrl;
description?: string;
url?: string;
type?: string;
label?: string;
}

Expand All @@ -15,6 +16,7 @@ export class NgxGalleryImage implements INgxGalleryImage {
big?: string | SafeResourceUrl;
description?: string;
url?: string;
type?: string;
label?: string;

constructor(obj: INgxGalleryImage) {
Expand All @@ -23,6 +25,7 @@ export class NgxGalleryImage implements INgxGalleryImage {
this.big = obj.big;
this.description = obj.description;
this.url = obj.url;
this.type = obj.type;
this.label = obj.label;
}
}
3 changes: 3 additions & 0 deletions src/ngx-gallery-ordered-image.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { SafeResourceUrl } from '@angular/platform-browser';

export interface INgxGalleryOrderedImage {
src: string | SafeResourceUrl;
type: string;
index: number;
}

export class NgxGalleryOrderedImage implements INgxGalleryOrderedImage {
src: string | SafeResourceUrl;
type: string;
index: number;

constructor(obj: INgxGalleryOrderedImage) {
this.src = obj.src;
this.type = obj.type;
this.index = obj.index;
}
}
65 changes: 40 additions & 25 deletions src/ngx-gallery-preview.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,39 @@ import { NgxGalleryHelperService } from './ngx-gallery-helper.service';

@Component({
selector: 'ngx-gallery-preview',
template: `
<ngx-gallery-arrows *ngIf="arrows" (onPrevClick)="showPrev()" (onNextClick)="showNext()" [prevDisabled]="!canShowPrev()" [nextDisabled]="!canShowNext()" [arrowPrevIcon]="arrowPrevIcon" [arrowNextIcon]="arrowNextIcon"></ngx-gallery-arrows>
<div class="ngx-gallery-preview-top">
<div class="ngx-gallery-preview-icons">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled" [titleText]="action.titleText" (onClick)="action.onClick($event, index)"></ngx-gallery-action>
<a *ngIf="download && src" [href]="src" class="ngx-gallery-icon" aria-hidden="true" download>
<i class="ngx-gallery-icon-content {{ downloadIcon }}"></i>
</a>
<ngx-gallery-action *ngIf="zoom" [icon]="zoomOutIcon" [disabled]="!canZoomOut()" (onClick)="zoomOut()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="zoom" [icon]="zoomInIcon" [disabled]="!canZoomIn()" (onClick)="zoomIn()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="rotate" [icon]="rotateLeftIcon" (onClick)="rotateLeft()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="rotate" [icon]="rotateRightIcon" (onClick)="rotateRight()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="fullscreen" [icon]="'ngx-gallery-fullscreen ' + fullscreenIcon" (onClick)="manageFullscreen()"></ngx-gallery-action>
<ngx-gallery-action [icon]="'ngx-gallery-close ' + closeIcon" (onClick)="close()"></ngx-gallery-action>
</div>
template: `
<ngx-gallery-arrows *ngIf="arrows" (onPrevClick)="showPrev()" (onNextClick)="showNext()" [prevDisabled]="!canShowPrev()" [nextDisabled]="!canShowNext()" [arrowPrevIcon]="arrowPrevIcon" [arrowNextIcon]="arrowNextIcon"></ngx-gallery-arrows>
<div class="ngx-gallery-preview-top">
<div class="ngx-gallery-preview-icons">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled" [titleText]="action.titleText" (onClick)="action.onClick($event, index)"></ngx-gallery-action>
<a *ngIf="download && src" [href]="src" class="ngx-gallery-icon" aria-hidden="true" download>
<i class="ngx-gallery-icon-content {{ downloadIcon }}"></i>
</a>
<ngx-gallery-action *ngIf="zoom" [icon]="zoomOutIcon" [disabled]="!canZoomOut()" (onClick)="zoomOut()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="zoom" [icon]="zoomInIcon" [disabled]="!canZoomIn()" (onClick)="zoomIn()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="rotate" [icon]="rotateLeftIcon" (onClick)="rotateLeft()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="rotate" [icon]="rotateRightIcon" (onClick)="rotateRight()"></ngx-gallery-action>
<ngx-gallery-action *ngIf="fullscreen" [icon]="'ngx-gallery-fullscreen ' + fullscreenIcon" (onClick)="manageFullscreen()"></ngx-gallery-action>
<ngx-gallery-action [icon]="'ngx-gallery-close ' + closeIcon" (onClick)="close()"></ngx-gallery-action>
</div>
<div class="ngx-spinner-wrapper ngx-gallery-center" [class.ngx-gallery-active]="showSpinner">
<i class="ngx-gallery-icon ngx-gallery-spinner {{spinnerIcon}}" aria-hidden="true"></i>
</div>
<div class="ngx-gallery-preview-wrapper" (click)="closeOnClick && close()" (mouseup)="mouseUpHandler($event)" (mousemove)="mouseMoveHandler($event)" (touchend)="mouseUpHandler($event)" (touchmove)="mouseMoveHandler($event)">
<div class="ngx-gallery-preview-img-wrapper">
<img *ngIf="src" #previewImage class="ngx-gallery-preview-img ngx-gallery-center" [src]="src" (click)="$event.stopPropagation()" (mouseenter)="imageMouseEnter()" (mouseleave)="imageMouseLeave()" (mousedown)="mouseDownHandler($event)" (touchstart)="mouseDownHandler($event)" [class.ngx-gallery-active]="!loading" [class.animation]="animation" [class.ngx-gallery-grab]="canDragOnZoom()" [style.transform]="getTransform()" [style.left]="positionLeft + 'px'" [style.top]="positionTop + 'px'"/>
<ngx-gallery-bullets *ngIf="bullets" [count]="images.length" [active]="index" (onChange)="showAtIndex($event)"></ngx-gallery-bullets>
</div>
<div class="ngx-gallery-preview-text" *ngIf="showDescription && description" [innerHTML]="description" (click)="$event.stopPropagation()"></div>
</div>
<div class="ngx-spinner-wrapper ngx-gallery-center" [class.ngx-gallery-active]="showSpinner">
<i class="ngx-gallery-icon ngx-gallery-spinner {{spinnerIcon}}" aria-hidden="true"></i>
</div>
<div class="ngx-gallery-preview-wrapper" (click)="closeOnClick && close()" (mouseup)="mouseUpHandler($event)" (mousemove)="mouseMoveHandler($event)" (touchend)="mouseUpHandler($event)" (touchmove)="mouseMoveHandler($event)">
<div class="ngx-gallery-preview-img-wrapper">
<img *ngIf="src && type == 'image'" #previewImage class="ngx-gallery-preview-img ngx-gallery-center" [src]="src" (click)="$event.stopPropagation()" (mouseenter)="imageMouseEnter()" (mouseleave)="imageMouseLeave()" (mousedown)="mouseDownHandler($event)" (touchstart)="mouseDownHandler($event)" [class.ngx-gallery-active]="!loading" [class.animation]="animation" [class.ngx-gallery-grab]="canDragOnZoom()" [style.transform]="getTransform()" [style.left]="positionLeft + 'px'" [style.top]="positionTop + 'px'"/>\
<video *ngIf="src && type == 'video'" #previewImage controls style="width: 100%; height: 100%;" \
class="ngx-gallery-preview-img ngx-gallery-center"\
(click)="$event.stopPropagation()" (mouseenter)="imageMouseEnter()" (mouseleave)="imageMouseLeave()" (mousedown)="mouseDownHandler($event)" (touchstart)="mouseDownHandler($event)" \
[class.ngx-gallery-active]="!loading" [class.animation]="animation" [class.ngx-gallery-grab]="canDragOnZoom()" [style.transform]="getTransform()" [style.left]="positionLeft + 'px'" [style.top]="positionTop + 'px'"> \
<source [src]="src">\
Your browser does not support the video tag.\
</video>
<ngx-gallery-bullets *ngIf="bullets" [count]="images.length" [active]="index" (onChange)="showAtIndex($event)"></ngx-gallery-bullets>
</div>
<div class="ngx-gallery-preview-text" *ngIf="showDescription && description" [innerHTML]="description" (click)="$event.stopPropagation()"></div>
</div>
`,
styleUrls: ['./ngx-gallery-preview.component.scss']
})
Expand All @@ -40,6 +47,7 @@ export class NgxGalleryPreviewComponent implements OnInit, OnChanges {
src: SafeUrl;
srcIndex: number;
description: string;
type: string;
showSpinner = false;
positionLeft = 0;
positionTop = 0;
Expand Down Expand Up @@ -276,6 +284,10 @@ export class NgxGalleryPreviewComponent implements OnInit, OnChanges {
image : this.sanitization.bypassSecurityTrustUrl(image);
}

getFileType (fileSource: string): string {
return this.helperService.getFileType(fileSource);
}

zoomIn(): void {
if (this.canZoomIn()) {
this.zoomValue += this.zoomStep;
Expand Down Expand Up @@ -432,15 +444,18 @@ export class NgxGalleryPreviewComponent implements OnInit, OnChanges {
this.resetPosition();

this.src = this.getSafeUrl(<string>this.images[this.index]);
this.type = this.getFileType(<string>this.images[this.index]);
this.srcIndex = this.index;
this.description = this.descriptions[this.index];
this.changeDetectorRef.markForCheck();

setTimeout(() => {
if (this.isLoaded(this.previewImage.nativeElement)) {
if (this.isLoaded(this.previewImage.nativeElement) || this.type == 'video') {
this.loading = false;
this.startAutoPlay();
this.changeDetectorRef.markForCheck();
} else if (this.type == 'video') {

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that videos could require something different than what is inside the above block. I kept this condition, with an empty block, it to highlight that.

So, this condition else if (this.type == 'video') should either be removed along with its empty block. Or, the condition || this.type == 'video' should be removed and the empty block should be filled of what is required.

} else {
setTimeout(() => {
if (this.loading) {
Expand Down
42 changes: 31 additions & 11 deletions src/ngx-gallery-thumbnails.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@ import { NgxGalleryAction } from './ngx-gallery-action.model';
@Component({
selector: 'ngx-gallery-thumbnails',
template: `
<div class="ngx-gallery-thumbnails-wrapper ngx-gallery-thumbnail-size-{{size}}">
<div class="ngx-gallery-thumbnails" [style.transform]="'translateX(' + thumbnailsLeft + ')'" [style.marginLeft]="thumbnailsMarginLeft">
<a [href]="hasLink(i) ? links[i] : '#'" [target]="linkTarget" class="ngx-gallery-thumbnail" *ngFor="let image of getImages(); let i = index;" [style.background-image]="getSafeUrl(image)" (click)="handleClick($event, i)" [style.width]="getThumbnailWidth()" [style.height]="getThumbnailHeight()" [style.left]="getThumbnailLeft(i)" [style.top]="getThumbnailTop(i)" [ngClass]="{ 'ngx-gallery-active': i == selectedIndex, 'ngx-gallery-clickable': clickable }" [attr.aria-label]="labels[i]">
<div class="ngx-gallery-icons-wrapper">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled" [titleText]="action.titleText" (onClick)="action.onClick($event, i)"></ngx-gallery-action>
<div class="ngx-gallery-thumbnails-wrapper ngx-gallery-thumbnail-size-{{size}}">
<div class="ngx-gallery-thumbnails" [style.transform]="'translateX(' + thumbnailsLeft + ')'" [style.marginLeft]="thumbnailsMarginLeft">
<div *ngFor="let image of getImages(); let i = index;">
<a [href]="hasLink(i) ? links[i] : '#'" [target]="linkTarget" class="ngx-gallery-thumbnail" [style.width]="getThumbnailWidth()"
(click)="handleClick($event, i)"
[style.height]="getThumbnailHeight()" [style.left]="getThumbnailLeft(i)" [style.top]="getThumbnailTop(i)"
[ngClass]="{ 'ngx-gallery-active': i == selectedIndex, 'ngx-gallery-clickable': clickable }"
[attr.aria-label]="labels[i]">
<div *ngIf="getFileType(image) == 'image'" [style.background-image]="getSafeUrl(image)" class="ngx-gallery-thumbnail"
[ngClass]="{ 'ngx-gallery-active': i == selectedIndex, 'ngx-gallery-clickable': clickable }"
style="width: 100%; height: 100%; position:absolute;"></div>
<video *ngIf="getFileType(image) == 'video'" class="ngx-gallery-thumbnail"
[ngClass]="{ 'ngx-gallery-active': i == selectedIndex, 'ngx-gallery-clickable': clickable }"
style="width: 100%; height: 100%; position:absolute;">
<source [src]="image">
Your browser does not support the video tag.
</video>
<div class="ngx-gallery-icons-wrapper">
<ngx-gallery-action *ngFor="let action of actions" [icon]="action.icon" [disabled]="action.disabled"
[titleText]="action.titleText" (onClick)="action.onClick($event, i)"></ngx-gallery-action>
</div>
<div class="ngx-gallery-remaining-count-overlay" *ngIf="remainingCount && remainingCountValue && (i == (rows * columns) - 1)">
<span class="ngx-gallery-remaining-count">+{{remainingCountValue}}</span>
</div>
</a>
</div>
<div class="ngx-gallery-remaining-count-overlay" *ngIf="remainingCount && remainingCountValue && (i == (rows * columns) - 1)">
<span class="ngx-gallery-remaining-count">+{{remainingCountValue}}</span>
</div>
</a>
</div>
</div>
</div>
<ngx-gallery-arrows *ngIf="canShowArrows()" (onPrevClick)="moveLeft()" (onNextClick)="moveRight()" [prevDisabled]="!canMoveLeft()" [nextDisabled]="!canMoveRight()" [arrowPrevIcon]="arrowPrevIcon" [arrowNextIcon]="arrowNextIcon"></ngx-gallery-arrows>
<ngx-gallery-arrows *ngIf="canShowArrows()" (onPrevClick)="moveLeft()" (onNextClick)="moveRight()" [prevDisabled]="!canMoveLeft()" [nextDisabled]="!canMoveRight()" [arrowPrevIcon]="arrowPrevIcon" [arrowNextIcon]="arrowNextIcon"></ngx-gallery-arrows>
`,
styleUrls: ['./ngx-gallery-thumbnails.component.scss']
})
Expand Down Expand Up @@ -266,6 +282,10 @@ export class NgxGalleryThumbnailsComponent implements OnChanges {
return this.sanitization.bypassSecurityTrustStyle(this.helperService.getBackgroundUrl(image));
}

getFileType (fileSource: string): string {
return this.helperService.getFileType(fileSource);
}

private getThumbnailPosition(index: number, count: number): SafeStyle {
return this.getSafeStyle('calc(' + ((100 / count) * index) + '% + '
+ ((this.margin - (((count - 1) * this.margin) / count)) * index) + 'px)');
Expand Down
Loading