2018-10-05 16:34:08 +02:00
|
|
|
import { Component, Input, Output, EventEmitter, OnInit, NgZone } from '@angular/core';
|
|
|
|
import { Location } from '@angular/common';
|
|
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
|
|
import { ScrollDispatcher, CdkScrollable } from '@angular/cdk/scrolling';
|
|
|
|
import { map } from 'rxjs/operators';
|
2018-10-12 11:05:24 +02:00
|
|
|
|
2018-10-05 16:34:08 +02:00
|
|
|
import { ViewportService } from '../../../core/services/viewport.service';
|
|
|
|
import { MainMenuService } from '../../../core/services/main-menu.service';
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reusable head bar component for Apps.
|
|
|
|
*
|
|
|
|
* Will translate the title automatically.
|
|
|
|
*
|
|
|
|
* Use `PlusButton=true` and `(plusButtonClicked)=myFunction()` if a plus button is needed
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ## Examples:
|
|
|
|
*
|
|
|
|
* ### Usage of the selector:
|
|
|
|
*
|
|
|
|
* ```html
|
2018-09-03 17:57:20 +02:00
|
|
|
* <os-head-bar
|
2018-08-28 13:54:25 +02:00
|
|
|
* appName="Files"
|
2018-09-19 15:18:57 +02:00
|
|
|
* plusButton=true
|
2018-08-28 13:54:25 +02:00
|
|
|
* (plusButtonClicked)=onPlusButton()
|
2018-09-20 12:25:37 +02:00
|
|
|
* (ellipsisMenuItem)=onEllipsisItem($event)>
|
2018-09-03 17:57:20 +02:00
|
|
|
* </os-head-bar>
|
2018-08-28 13:54:25 +02:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@Component({
|
2018-09-03 17:57:20 +02:00
|
|
|
selector: 'os-head-bar',
|
2018-08-28 13:54:25 +02:00
|
|
|
templateUrl: './head-bar.component.html',
|
|
|
|
styleUrls: ['./head-bar.component.scss']
|
|
|
|
})
|
|
|
|
export class HeadBarComponent implements OnInit {
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* determine weather the toolbar should be sticky or not
|
|
|
|
*/
|
|
|
|
public stickyToolbar = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the the navigation "hamburger" icon should be displayed in mobile mode
|
|
|
|
*/
|
|
|
|
@Input()
|
|
|
|
public nav = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show or hide edit features
|
|
|
|
*/
|
|
|
|
@Input()
|
|
|
|
public allowEdit = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom edit icon if necessary
|
|
|
|
*/
|
|
|
|
@Input()
|
|
|
|
public editIcon = 'edit';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine edit mode
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-09-19 15:18:57 +02:00
|
|
|
@Input()
|
2018-10-05 16:34:08 +02:00
|
|
|
public editMode = false;
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if there should be a plus button.
|
|
|
|
*/
|
2018-09-19 15:18:57 +02:00
|
|
|
@Input()
|
2018-10-05 16:34:08 +02:00
|
|
|
public plusButton = false;
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* Determine if there should be a back button.
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-09-19 15:18:57 +02:00
|
|
|
@Input()
|
2018-10-05 16:34:08 +02:00
|
|
|
public backButton = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set to true if the component should use location.back instead
|
|
|
|
* of navigating to the parent component
|
|
|
|
*/
|
|
|
|
@Input()
|
|
|
|
public goBack = false;
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Emit a signal to the parent component if the plus button was clicked
|
|
|
|
*/
|
2018-09-19 15:18:57 +02:00
|
|
|
@Output()
|
|
|
|
public plusButtonClicked = new EventEmitter<boolean>();
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* Sends a signal if a detail view should be edited or editing should be canceled
|
|
|
|
*/
|
|
|
|
@Output()
|
|
|
|
public editEvent = new EventEmitter<boolean>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a signal if a detail view should be saved
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-09-19 15:18:57 +02:00
|
|
|
@Output()
|
2018-10-05 16:34:08 +02:00
|
|
|
public saveEvent = new EventEmitter<boolean>();
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty constructor
|
|
|
|
*/
|
2018-10-05 16:34:08 +02:00
|
|
|
public constructor(
|
|
|
|
public vp: ViewportService,
|
|
|
|
private scrollDispatcher: ScrollDispatcher,
|
|
|
|
private ngZone: NgZone,
|
|
|
|
private menu: MainMenuService,
|
|
|
|
private router: Router,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private location: Location
|
|
|
|
) {}
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* Emits a signal to the parent if
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-10-05 16:34:08 +02:00
|
|
|
public clickPlusButton(): void {
|
|
|
|
this.plusButtonClicked.emit(true);
|
|
|
|
}
|
2018-08-28 13:54:25 +02:00
|
|
|
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* Clicking the burger-menu-icon should toggle the menu
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-10-05 16:34:08 +02:00
|
|
|
public clickHamburgerMenu(): void {
|
|
|
|
this.menu.toggleMenu();
|
2018-08-28 13:54:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-05 16:34:08 +02:00
|
|
|
* Toggle edit mode and send a signal to listeners
|
2018-08-28 13:54:25 +02:00
|
|
|
*/
|
2018-10-05 16:34:08 +02:00
|
|
|
public toggleEditMode(): void {
|
|
|
|
this.editEvent.next(!this.editMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a save signal and set edit mode
|
|
|
|
*/
|
|
|
|
public save(): void {
|
|
|
|
if (this.editMode) {
|
|
|
|
this.saveEvent.next(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exits the view to return to the previous page or
|
|
|
|
* visit the parent view again.
|
|
|
|
*/
|
|
|
|
public onBackButton(): void {
|
|
|
|
if (this.goBack) {
|
|
|
|
this.location.back();
|
|
|
|
} else {
|
|
|
|
this.router.navigate(['../'], { relativeTo: this.route });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init function. Subscribe to the scrollDispatcher and decide when to set the top bar to fixed
|
|
|
|
*
|
|
|
|
* Not working for now.
|
|
|
|
*/
|
|
|
|
public ngOnInit(): void {
|
|
|
|
this.scrollDispatcher
|
|
|
|
.scrolled()
|
|
|
|
.pipe(map((event: CdkScrollable) => this.getScrollPosition(event)))
|
|
|
|
.subscribe(scrollTop => {
|
|
|
|
this.ngZone.run(() => {
|
|
|
|
if (scrollTop > 60) {
|
|
|
|
this.stickyToolbar = true;
|
|
|
|
} else {
|
|
|
|
this.stickyToolbar = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the scroll position
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
public getScrollPosition(event: CdkScrollable): number {
|
|
|
|
if (event) {
|
|
|
|
return event.getElementRef().nativeElement.scrollTop;
|
|
|
|
} else {
|
|
|
|
return window.scrollY;
|
|
|
|
}
|
2018-08-28 13:54:25 +02:00
|
|
|
}
|
|
|
|
}
|