Skip to content

Commit

Permalink
feat(scroll): enable scroll to top
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Joseph committed May 3, 2016
1 parent 89b2293 commit cd2956c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
14 changes: 11 additions & 3 deletions app/components/dex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter } from 'angular2/core';
import { Component, ElementRef, EventEmitter } from 'angular2/core';

import { BoxComponent } from './box';
import { Capture } from '../classes/capture';
Expand All @@ -10,19 +10,27 @@ const HTML = require('../views/dex.html');

@Component({
directives: [HeaderComponent, BoxComponent],
events: ['activeChange', 'collapsedChange'],
inputs: ['captures', 'user'],
events: ['activeChange', 'collapsedChange', 'scrollUp'],
inputs: ['captures', 'showScroll', 'user'],
pipes: [GroupPipe],
providers: [ElementRef],
selector: 'dex',
template: HTML
})
export class DexComponent {

public captures: Capture[];
public _el: ElementRef;
public region: string = 'national';
public showScroll: boolean;
public user: User;

public activeChange = new EventEmitter<Capture>();
public collapsedChange = new EventEmitter<boolean>();
public scrollUp = new EventEmitter<void>();

constructor (_el: ElementRef) {
this._el = _el;
}

}
19 changes: 18 additions & 1 deletion app/components/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { UserService } from '../services/user';

const HTML = require('../views/tracker.html');

const MOBILE_WIDTH = 1100;
const MOBILE_WIDTH = 1100;
const SHOW_SCROLL_THRESHOLD = 400;

@Component({
directives: [DexComponent, InfoComponent, NavComponent, NotFoundComponent],
Expand All @@ -30,6 +31,7 @@ export class TrackerComponent implements OnInit {
public loading: boolean = true;
public collapsed: boolean = false;
public _session: SessionService;
public showScroll: boolean = false;
public user: User;

private _capture: CaptureService;
Expand Down Expand Up @@ -66,4 +68,19 @@ export class TrackerComponent implements OnInit {
.catch((err) => this.loading = false);
}

public scroll (dex: DexComponent) {
const el: HTMLElement = dex._el.nativeElement;
if (!this.showScroll && el.scrollTop >= SHOW_SCROLL_THRESHOLD) {
this.showScroll = true;
} else if (this.showScroll && el.scrollTop < SHOW_SCROLL_THRESHOLD) {
this.showScroll = false;
}
}

public scrollUp (dex: DexComponent) {
const el: HTMLElement = dex._el.nativeElement;
el.scrollTop = 0;
this.showScroll = false;
}

}
10 changes: 8 additions & 2 deletions app/styles/tracker.scss
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,15 @@ dex {
padding: 100px 0;

.scroll-up {
@include transition(all .1s ease-out);
@include transition(all .5s ease-out);
@include flexbox();
@include align-items(center);
@include justify-content(center);

opacity: 0;
position: fixed;
left: 10px;
bottom: 10px;
bottom: -100px;
width: 50px;
height: 50px;
border-radius: 50%;
Expand All @@ -262,6 +263,11 @@ dex {
cursor: pointer;
background-color: $brand-secondary-dark;
}

&.visible {
bottom: 10px;
opacity: 1;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/dex.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="scroll-up"><i class="fa fa-long-arrow-up"></i></div>
<div class="scroll-up" [class.visible]="showScroll" (click)="scrollUp.emit()"><i class="fa fa-long-arrow-up"></i></div>
<header [captures]="captures" [(region)]="region" [user]="user"></header>
<box *ngFor="#group of captures | group : 30" (activeChange)="activeChange.emit($event)" (collapsedChange)="collapsedChange.emit($event)" [captures]="group" [region]="region" [user]="user"></box>
2 changes: 1 addition & 1 deletion app/views/tracker.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div *ngIf="loading" class="loading">Loading...</div>
<nav *ngIf="!loading && user" [user]="user"></nav>
<div *ngIf="!loading && user" class="tracker">
<dex *ngIf="!loading" (activeChange)="active = $event" (collapsedChange)="collapsed = $event" [captures]="captures" [user]="user"></dex>
<dex #dex (scroll)="scroll(dex)" *ngIf="!loading" (activeChange)="active = $event" (collapsedChange)="collapsed = $event" (scrollUp)="scrollUp(dex)" [captures]="captures" [showScroll]="showScroll" [user]="user"></dex>
<info [(collapsed)]="collapsed" [class.collapsed]="collapsed" [active]="active"></info>
</div>
<not-found *ngIf="!loading && !user"></not-found>

0 comments on commit cd2956c

Please sign in to comment.