-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
576 additions
and
540 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
"description": "Angular2 ScrollSpy Service", | ||
"author": "João Ribeiro <[email protected]> (http://github.com/JonnyBGod)", | ||
"dependencies": { | ||
"immutable": "^3.8.0" | ||
"immutable": "^3.8.1" | ||
}, | ||
"devDependencies": { | ||
"@angular/common": "^2.0.0-rc.1", | ||
|
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,31 +1,30 @@ | ||
import {Directive, Injectable, OnInit} from '@angular/core'; | ||
import {ReplaySubject} from 'rxjs/ReplaySubject'; | ||
import {ScrollSpyService} from './service'; | ||
import { Directive, Injectable, OnInit } from '@angular/core'; | ||
import { ReplaySubject } from 'rxjs/ReplaySubject'; | ||
import { ScrollSpyService } from './service'; | ||
|
||
@Injectable() | ||
@Directive({ | ||
selector: '[scrollSpy]', | ||
host: { | ||
selector: '[scrollSpy]', | ||
host: { | ||
'(window:scroll)': 'onScroll($event)' | ||
} | ||
}) | ||
export class ScrollSpyDirective implements OnInit { | ||
private _scrollStream: ReplaySubject<any> = new ReplaySubject(1); | ||
private scrollStream$: ReplaySubject<any> = new ReplaySubject(1); | ||
|
||
constructor( | ||
private scrollSpy: ScrollSpyService | ||
) {} | ||
constructor( | ||
private scrollSpy: ScrollSpyService | ||
) {} | ||
|
||
ngOnInit() { | ||
|
||
if (!!this.scrollSpy.getObservable('window')) { | ||
console.warn('ScrollSpy: duplicate id "window". Instance will be skipped!'); | ||
} else { | ||
this.scrollSpy.setObservable('window', this._scrollStream); | ||
} | ||
} | ||
ngOnInit() { | ||
if (!!this.scrollSpy.getObservable('window')) { | ||
console.warn('ScrollSpy: duplicate id "window". Instance will be skipped!'); | ||
} else { | ||
this.scrollSpy.setObservable('window', this.scrollStream$); | ||
} | ||
} | ||
|
||
onScroll($event: any) { | ||
this._scrollStream.next($event); | ||
} | ||
onScroll($event: any) { | ||
this.scrollStream$.next($event); | ||
} | ||
} |
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,80 +1,87 @@ | ||
import {Directive, Injectable, ElementRef, Input, AfterViewInit, OnDestroy} from '@angular/core'; | ||
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter'; | ||
import { | ||
Directive, | ||
Injectable, | ||
ElementRef, | ||
Input, | ||
AfterViewInit, | ||
OnDestroy | ||
} from '@angular/core'; | ||
import { getDOM } from '@angular/platform-browser/src/dom/dom_adapter'; | ||
|
||
import {ScrollSpyService} from '../index'; | ||
import { ScrollSpyService } from '../index'; | ||
|
||
export interface ScrollSpyAffixOptions { | ||
topMargin?: number; | ||
bottomMargin?: number; | ||
topMargin?: number; | ||
bottomMargin?: number; | ||
} | ||
|
||
@Injectable() | ||
@Directive({ | ||
selector: '[scrollSpyAffix]', | ||
host: { | ||
'[class.affixTop]': 'affixTop', | ||
'[class.affixBottom]': 'affixBottom' | ||
} | ||
selector: '[scrollSpyAffix]', | ||
host: { | ||
'[class.affixTop]': 'affixTop', | ||
'[class.affixBottom]': 'affixBottom' | ||
} | ||
}) | ||
export class ScrollSpyAffixDirective implements AfterViewInit, OnDestroy { | ||
@Input('scrollSpyAffix') options: ScrollSpyAffixOptions; | ||
@Input('scrollSpyAffix') options: ScrollSpyAffixOptions; | ||
|
||
private defaultOptions: ScrollSpyAffixOptions = { | ||
topMargin: 0, | ||
bottomMargin: 0 | ||
}; | ||
private defaultOptions: ScrollSpyAffixOptions = { | ||
topMargin: 0, | ||
bottomMargin: 0 | ||
}; | ||
|
||
private _scrollStream: any; | ||
private scrollStream$: any; | ||
|
||
private el: HTMLElement; | ||
private parentEl: any; | ||
private el: HTMLElement; | ||
private parentEl: any; | ||
|
||
private elementTop: number; | ||
private elementBottom: number; | ||
private affixTop: boolean = false; | ||
private affixBottom: boolean = false; | ||
private elementTop: number; | ||
private elementBottom: number; | ||
private affixTop: boolean = false; | ||
private affixBottom: boolean = false; | ||
|
||
constructor( | ||
private elRef: ElementRef, | ||
private scrollSpy: ScrollSpyService | ||
) { | ||
this.el = elRef.nativeElement; | ||
} | ||
constructor( | ||
private elRef: ElementRef, | ||
private scrollSpy: ScrollSpyService | ||
) { | ||
this.el = elRef.nativeElement; | ||
} | ||
|
||
ngAfterViewInit() { | ||
if (!this.options) { | ||
this.options = {}; | ||
} | ||
ngAfterViewInit() { | ||
if (!this.options) { | ||
this.options = {}; | ||
} | ||
|
||
this.options = Object.assign(this.defaultOptions, this.options); | ||
this.options = Object.assign(this.defaultOptions, this.options); | ||
|
||
this.parentEl = getDOM().parentElement(this.el); | ||
this.elementTop = getDOM().getProperty(this.parentEl, 'scrollTop'); | ||
this.elementBottom = this.elementTop + getDOM().getBoundingClientRect(this.parentEl).height; | ||
this.parentEl = getDOM().parentElement(this.el); | ||
this.elementTop = getDOM().getProperty(this.parentEl, 'scrollTop'); | ||
this.elementBottom = this.elementTop + getDOM().getBoundingClientRect(this.parentEl).height; | ||
|
||
if (!!this.scrollSpy.getObservable('window')) { | ||
//TODO: Remove delay once: https://github.com/angular/angular/issues/7443 | ||
this._scrollStream = this.scrollSpy.getObservable('window').delay(0).subscribe((e: any) => { | ||
this.update(e.target.scrollingElement.scrollTop); | ||
}); | ||
} | ||
} | ||
if (!!this.scrollSpy.getObservable('window')) { | ||
// TODO: Remove delay once: https://github.com/angular/angular/issues/7443 | ||
this.scrollStream$ = this.scrollSpy.getObservable('window').delay(0).subscribe((e: any) => { | ||
this.update(e.target.scrollingElement.scrollTop); | ||
}); | ||
} | ||
} | ||
|
||
update(currentTop: number) { | ||
if (currentTop >= this.elementTop + this.options.topMargin) { | ||
if (currentTop > this.elementBottom - this.options.bottomMargin - getDOM().getBoundingClientRect(this.el).height) { | ||
this.affixTop = false; | ||
this.affixBottom = true; | ||
} else { | ||
this.affixTop = true; | ||
this.affixBottom = false; | ||
} | ||
} else { | ||
this.affixTop = false; | ||
} | ||
} | ||
update(currentTop: number) { | ||
if (currentTop >= this.elementTop + this.options.topMargin) { | ||
if (currentTop > this.elementBottom - this.options.bottomMargin - getDOM().getBoundingClientRect(this.el).height) { | ||
this.affixTop = false; | ||
this.affixBottom = true; | ||
} else { | ||
this.affixTop = true; | ||
this.affixBottom = false; | ||
} | ||
} else { | ||
this.affixTop = false; | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this._scrollStream.unsubscribe(); | ||
ngOnDestroy() { | ||
this.scrollStream$.unsubscribe(); | ||
} | ||
} |
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
Oops, something went wrong.