Skip to content

Commit

Permalink
fix(all): formating
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBGod committed Jun 10, 2016
1 parent abe53ac commit 0f8bab1
Show file tree
Hide file tree
Showing 13 changed files with 576 additions and 540 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/core/element.directive.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Directive, Injectable, Input, OnInit, OnDestroy} from '@angular/core';
import {ReplaySubject} from 'rxjs/ReplaySubject';
import {ScrollSpyService} from './service';
import { Directive, Injectable, Input, OnInit, OnDestroy } from '@angular/core';
import { ReplaySubject } from 'rxjs/ReplaySubject';
import { ScrollSpyService } from './service';

@Injectable()
@Directive({
Expand All @@ -12,7 +12,7 @@ import {ScrollSpyService} from './service';
export class ScrollSpyElementDirective implements OnInit, OnDestroy {
@Input('scrollSpyElement') scrollSpyId: string;

private _scrollStream: ReplaySubject<any> = new ReplaySubject(1);
private scrollStream$: ReplaySubject<any> = new ReplaySubject(1);

constructor(
private scrollSpy: ScrollSpyService
Expand All @@ -26,7 +26,7 @@ export class ScrollSpyElementDirective implements OnInit, OnDestroy {
if (!!this.scrollSpy.getObservable(this.scrollSpyId)) {
console.warn('ScrollSpy: duplicate id "' + this.scrollSpyId + '". Instance will be skipped!');
} else {
this.scrollSpy.setObservable(this.scrollSpyId, this._scrollStream);
this.scrollSpy.setObservable(this.scrollSpyId, this.scrollStream$);
}
}

Expand All @@ -35,6 +35,6 @@ export class ScrollSpyElementDirective implements OnInit, OnDestroy {
}

onScroll($event: any) {
this._scrollStream.next($event);
this.scrollStream$.next($event);
}
}
10 changes: 5 additions & 5 deletions src/core/service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Injectable, EventEmitter} from '@angular/core';
import {Map} from 'immutable';
import { Injectable, EventEmitter } from '@angular/core';
import { Map } from 'immutable';

@Injectable()
export class ScrollSpyService {
public changes: EventEmitter<any> = new EventEmitter();
public changes$: EventEmitter<any> = new EventEmitter();
private observables: Map<string, any> = Map({});

public getObservable(key: string): any {
Expand All @@ -12,11 +12,11 @@ export class ScrollSpyService {

public setObservable(key: string, observable: any) {
this.observables = this.observables.set(key, observable);
this.changes.emit({ index: key, change: 'set' });
this.changes$.emit({ index: key, change: 'set' });
}

public deleteObservable(key: string) {
this.observables = this.observables.delete(key);
this.changes.emit({ index: key, change: 'delete' });
this.changes$.emit({ index: key, change: 'delete' });
}
}
39 changes: 19 additions & 20 deletions src/core/window.directive.ts
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);
}
}
123 changes: 65 additions & 58 deletions src/plugin/affix.directive.ts
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();
}
}
2 changes: 1 addition & 1 deletion src/plugin/affix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ScrollSpyAffixDirective} from './affix.directive';
import { ScrollSpyAffixDirective } from './affix.directive';

export * from './affix.directive';

Expand Down
Loading

0 comments on commit 0f8bab1

Please sign in to comment.