-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Resize): completed resize module
- Loading branch information
1 parent
1fad30a
commit 968c1a0
Showing
18 changed files
with
333 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
projects/ng-utils/src/resize/animate-resize.directive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AnimateResizeDirective } from './animate-resize.directive'; | ||
|
||
describe('AnimateResizeDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new AnimateResizeDirective(null, null, null); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Directive, ElementRef, Input, Output, EventEmitter } from '@angular/core'; | ||
import { AnimationBuilder, AnimationFactory, style, animate } from '@angular/animations'; | ||
import { Unsubscriber } from '../utils/unsubscriber'; | ||
import { ResizeService } from './resize.service'; | ||
import { of, merge, interval } from 'rxjs'; | ||
import { distinctUntilChanged, takeUntil, mapTo, exhaustMap, take, filter, pairwise } from 'rxjs/operators'; | ||
import { ResizeEvent } from '@thalesrc/resize-manager'; | ||
import { ResizeMode } from './resize-mode'; | ||
import { isTruthy } from '@thalesrc/js-utils/legacy'; | ||
|
||
@Directive({ | ||
selector: '[thaAnimateResize]', | ||
exportAs: 'thaAnimateResize' | ||
}) | ||
export class AnimateResizeDirective extends Unsubscriber { | ||
@Input('thaAnimateResize') | ||
// tslint:disable-next-line:variable-name | ||
public mode: ResizeMode; | ||
|
||
@Input('duration') | ||
// tslint:disable-next-line:variable-name | ||
public _duration: number; | ||
|
||
@Output() | ||
public animationComplete = new EventEmitter<void>(); | ||
|
||
private get duration(): number { | ||
return Number.isNaN(+this._duration) ? 180 : +this._duration; | ||
} | ||
|
||
constructor( | ||
service: ResizeService, | ||
{nativeElement}: ElementRef, | ||
builder: AnimationBuilder | ||
) { | ||
super(); | ||
|
||
service.observe(nativeElement, 0).pipe( | ||
distinctUntilChanged(({width, height}, next) => { | ||
switch (this.mode) { | ||
case 'width': | ||
return width === next.width; | ||
case 'height': | ||
return height === next.height; | ||
default: | ||
return width === next.width && height === next.height; | ||
} | ||
}), | ||
pairwise(), | ||
exhaustMap(event => | ||
merge( | ||
of(null).pipe(mapTo(event)), | ||
interval(this.duration + 100).pipe(mapTo(null)) | ||
).pipe(take(2))), | ||
filter<[ResizeEvent, ResizeEvent]>(isTruthy), | ||
takeUntil(this.onDestroy$) | ||
).subscribe(([prev, {width, height}]) => { | ||
let factory: AnimationFactory; | ||
|
||
switch (this.mode) { | ||
case 'width': | ||
factory = builder.build([style({width: prev.width}), animate(this.duration, style({width}))]); | ||
break; | ||
case 'height': | ||
factory = builder.build([style({height: prev.height}), animate(this.duration, style({height}))]); | ||
break; | ||
default: | ||
factory = builder.build([style({width: prev.width, height: prev.height}), animate(this.duration, style({width, height}))]); | ||
} | ||
|
||
const player = factory.create(nativeElement); | ||
|
||
player.play(); | ||
|
||
player.onDone(() => { | ||
player.destroy(); | ||
this.animationComplete.emit(); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './animate-resize.directive'; | ||
export * from './resize.directive'; | ||
export * from './resize.module'; | ||
export * from './resize.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type ResizeMode = 'both' | 'width' | 'height'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ResizeDirective } from './resize.directive'; | ||
|
||
describe('ResizeDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new ResizeDirective(null, null); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Directive, ElementRef, Input, Output, EventEmitter } from '@angular/core'; | ||
import { Unsubscriber } from '../utils/unsubscriber'; | ||
import { ResizeService } from './resize.service'; | ||
import { InputStream } from '../utils/input-stream'; | ||
import { Observable } from 'rxjs'; | ||
import { combineLatest, map, distinctUntilChanged, takeUntil } from 'rxjs/operators'; | ||
import { ResizeEvent } from '@thalesrc/resize-manager'; | ||
import { ResizeMode } from './resize-mode'; | ||
|
||
@Directive({ | ||
selector: '[thaResize]', | ||
exportAs: 'thaResize' | ||
}) | ||
export class ResizeDirective extends Unsubscriber { | ||
@Input('thaResize') | ||
@InputStream('both') | ||
public mode: Observable<ResizeMode>; | ||
|
||
// tslint:disable-next-line:no-output-on-prefix | ||
@Output() | ||
public onResize = new EventEmitter<ResizeEvent | number>(); | ||
|
||
constructor( | ||
service: ResizeService, | ||
{nativeElement}: ElementRef | ||
) { | ||
super(); | ||
|
||
service.observe(nativeElement, 0).pipe( | ||
combineLatest(this.mode.pipe( | ||
map(mode => mode || 'both'), | ||
distinctUntilChanged() | ||
)), | ||
takeUntil(this.onDestroy$) | ||
).subscribe(([{width, height}, mode]) => { | ||
this.onResize.emit(mode === 'both' ? {width, height} : mode === 'width' ? width : height); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { ResizeDirective } from './resize.directive'; | ||
import { AnimateResizeDirective } from './animate-resize.directive'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
BrowserAnimationsModule, | ||
], | ||
declarations: [ | ||
ResizeDirective, | ||
AnimateResizeDirective | ||
], | ||
exports: [ | ||
ResizeDirective, | ||
AnimateResizeDirective | ||
] | ||
}) | ||
export class ResizeModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ResizeService } from './resize.service'; | ||
|
||
describe('ResizeService', () => { | ||
beforeEach(() => TestBed.configureTestingModule({})); | ||
|
||
it('should be created', () => { | ||
const service: ResizeService = TestBed.get(ResizeService); | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { ResizeManager, ResizeEvent } from '@thalesrc/resize-manager'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ResizeService { | ||
private static manager = new ResizeManager(0); | ||
|
||
public windowResize$: Observable<ResizeEvent> = ResizeService.manager.root.resize; | ||
|
||
public observe(target: HTMLElement, throttleBy = 90): Observable<ResizeEvent> { | ||
return ResizeService.manager.observe(target).throttleBy(throttleBy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
img { | ||
width: 300px; | ||
height: auto; | ||
// width: 300px; | ||
// height: auto; | ||
} |
Oops, something went wrong.