2023-04-14 19:18:35 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import {
|
2023-07-31 14:19:34 +03:00
|
|
|
AfterContentChecked,
|
2023-04-14 19:18:35 +03:00
|
|
|
AfterContentInit,
|
2023-07-31 14:19:34 +03:00
|
|
|
AfterViewInit,
|
2023-04-14 19:18:35 +03:00
|
|
|
ChangeDetectorRef,
|
|
|
|
|
Component,
|
2023-07-03 17:09:35 +03:00
|
|
|
ContentChildren,
|
|
|
|
|
Directive,
|
|
|
|
|
ElementRef,
|
|
|
|
|
EventEmitter,
|
2023-07-31 14:19:34 +03:00
|
|
|
HostBinding,
|
2023-04-14 19:18:35 +03:00
|
|
|
Input,
|
2023-07-03 17:09:35 +03:00
|
|
|
OnDestroy,
|
|
|
|
|
OnInit,
|
|
|
|
|
Output,
|
2023-07-31 14:19:34 +03:00
|
|
|
QueryList,
|
|
|
|
|
ViewChild
|
2023-04-14 19:18:35 +03:00
|
|
|
} from '@angular/core';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
2023-07-03 17:09:35 +03:00
|
|
|
import { Subject, Subscription } from 'rxjs';
|
2023-04-17 19:09:23 +03:00
|
|
|
import { BreakpointObserver, BreakpointState } from '@angular/cdk/layout';
|
|
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
2023-05-04 17:46:43 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2023-07-03 17:09:35 +03:00
|
|
|
import { startWith, takeUntil } from 'rxjs/operators';
|
2023-07-31 14:19:34 +03:00
|
|
|
import { Platform } from '@angular/cdk/platform';
|
|
|
|
|
import { MatButtonToggle, MatButtonToggleGroup } from '@angular/material/button-toggle';
|
2023-04-17 19:09:23 +03:00
|
|
|
|
|
|
|
|
export interface ToggleHeaderOption {
|
|
|
|
|
name: string;
|
|
|
|
|
value: any;
|
|
|
|
|
}
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-05-25 17:03:52 +03:00
|
|
|
export type ToggleHeaderAppearance = 'fill' | 'fill-invert' | 'stroked';
|
2023-05-16 20:00:53 +03:00
|
|
|
|
2023-07-31 14:19:34 +03:00
|
|
|
export type ScrollDirection = 'after' | 'before';
|
|
|
|
|
|
2023-07-03 17:09:35 +03:00
|
|
|
@Directive(
|
|
|
|
|
{
|
|
|
|
|
// eslint-disable-next-line @angular-eslint/directive-selector
|
|
|
|
|
selector: 'tb-toggle-option',
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
// eslint-disable-next-line @angular-eslint/directive-class-suffix
|
|
|
|
|
export class ToggleOption {
|
|
|
|
|
|
|
|
|
|
@Input() value: any;
|
|
|
|
|
|
|
|
|
|
get viewValue(): string {
|
|
|
|
|
return (this._element?.nativeElement.textContent || '').trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private _element: ElementRef<HTMLElement>
|
|
|
|
|
) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Directive()
|
|
|
|
|
export abstract class _ToggleBase extends PageComponent implements AfterContentInit, OnDestroy {
|
|
|
|
|
|
|
|
|
|
@ContentChildren(ToggleOption) toggleOptions: QueryList<ToggleOption>;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
options: ToggleHeaderOption[] = [];
|
|
|
|
|
|
2023-07-31 14:19:34 +03:00
|
|
|
protected _destroyed = new Subject<void>();
|
2023-07-03 17:09:35 +03:00
|
|
|
|
|
|
|
|
protected constructor(protected store: Store<AppState>) {
|
|
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterContentInit(): void {
|
|
|
|
|
this.toggleOptions.changes.pipe(startWith(null), takeUntil(this._destroyed)).subscribe(() => {
|
|
|
|
|
this.syncToggleHeaderOptions();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
this._destroyed.next();
|
|
|
|
|
this._destroyed.complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private syncToggleHeaderOptions() {
|
|
|
|
|
if (this.toggleOptions?.length) {
|
|
|
|
|
this.options.length = 0;
|
|
|
|
|
this.toggleOptions.forEach(option => {
|
|
|
|
|
this.options.push(
|
|
|
|
|
{ name: option.viewValue,
|
|
|
|
|
value: option.value
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 19:18:35 +03:00
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-toggle-header',
|
|
|
|
|
templateUrl: './toggle-header.component.html',
|
|
|
|
|
styleUrls: ['./toggle-header.component.scss']
|
|
|
|
|
})
|
2023-07-31 14:19:34 +03:00
|
|
|
export class ToggleHeaderComponent extends _ToggleBase implements OnInit, AfterViewInit, AfterContentInit, AfterContentChecked, OnDestroy {
|
|
|
|
|
|
|
|
|
|
@ViewChild('toggleGroup', {static: false})
|
|
|
|
|
toggleGroup: ElementRef<HTMLElement>;
|
|
|
|
|
|
|
|
|
|
@ViewChild(MatButtonToggleGroup, {static: false})
|
|
|
|
|
buttonToggleGroup: MatButtonToggleGroup;
|
|
|
|
|
|
|
|
|
|
@ViewChild('toggleGroupContainer', {static: false})
|
|
|
|
|
toggleGroupContainer: ElementRef<HTMLElement>;
|
|
|
|
|
|
|
|
|
|
@HostBinding('class.tb-toggle-header-pagination-controls-enabled')
|
|
|
|
|
private showPaginationControls = false;
|
|
|
|
|
|
|
|
|
|
private toggleGroupResize$: ResizeObserver;
|
|
|
|
|
|
|
|
|
|
leftPaginationEnabled = false;
|
|
|
|
|
rightPaginationEnabled = false;
|
|
|
|
|
|
|
|
|
|
private _scrollDistance = 0;
|
|
|
|
|
private _scrollDistanceChanged: boolean;
|
|
|
|
|
|
|
|
|
|
get scrollDistance(): number {
|
|
|
|
|
return this._scrollDistance;
|
|
|
|
|
}
|
|
|
|
|
set scrollDistance(value: number) {
|
|
|
|
|
this._scrollTo(value);
|
|
|
|
|
}
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-04-17 19:09:23 +03:00
|
|
|
@Input()
|
|
|
|
|
value: any;
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-04-19 13:55:51 +03:00
|
|
|
@Output()
|
|
|
|
|
valueChange = new EventEmitter<any>();
|
|
|
|
|
|
2023-04-17 19:09:23 +03:00
|
|
|
@Input()
|
|
|
|
|
name: string;
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-07-31 14:19:34 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
disablePagination = false;
|
|
|
|
|
|
2023-04-14 19:18:35 +03:00
|
|
|
@Input()
|
2023-04-17 19:09:23 +03:00
|
|
|
@coerceBoolean()
|
|
|
|
|
useSelectOnMdLg = true;
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-05-16 20:00:53 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
ignoreMdLgSize = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
appearance: ToggleHeaderAppearance = 'stroked';
|
|
|
|
|
|
2023-07-04 18:02:11 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
disabled = false;
|
|
|
|
|
|
2023-04-17 19:09:23 +03:00
|
|
|
isMdLg: boolean;
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-04-17 19:09:23 +03:00
|
|
|
private observeBreakpointSubscription: Subscription;
|
2023-04-14 19:18:35 +03:00
|
|
|
|
2023-04-17 19:09:23 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
private cd: ChangeDetectorRef,
|
2023-07-31 14:19:34 +03:00
|
|
|
private platform: Platform,
|
2023-04-17 19:09:23 +03:00
|
|
|
private breakpointObserver: BreakpointObserver) {
|
2023-04-14 19:18:35 +03:00
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2023-04-17 19:09:23 +03:00
|
|
|
this.isMdLg = this.breakpointObserver.isMatched(MediaBreakpoints['md-lg']);
|
|
|
|
|
this.observeBreakpointSubscription = this.breakpointObserver
|
|
|
|
|
.observe(MediaBreakpoints['md-lg'])
|
|
|
|
|
.subscribe((state: BreakpointState) => {
|
|
|
|
|
this.isMdLg = state.matches;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-07-31 14:19:34 +03:00
|
|
|
if (!this.disablePagination) {
|
|
|
|
|
this.valueChange.pipe(takeUntil(this._destroyed)).subscribe(() => {
|
|
|
|
|
this.scrollToToggleOptionValue();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
if (this.toggleGroupResize$) {
|
|
|
|
|
this.toggleGroupResize$.disconnect();
|
|
|
|
|
}
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
|
if (!this.disablePagination && !this.useSelectOnMdLg) {
|
|
|
|
|
this.toggleGroupResize$ = new ResizeObserver(() => {
|
|
|
|
|
this.updatePagination();
|
|
|
|
|
});
|
|
|
|
|
this.toggleGroupResize$.observe(this.toggleGroupContainer.nativeElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterContentChecked() {
|
|
|
|
|
if (this._scrollDistanceChanged) {
|
|
|
|
|
this.updateToggleHeaderScrollPosition();
|
|
|
|
|
this._scrollDistanceChanged = false;
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
2023-04-14 19:18:35 +03:00
|
|
|
}
|
2023-05-25 17:03:52 +03:00
|
|
|
|
|
|
|
|
trackByHeaderOption(index: number, option: ToggleHeaderOption){
|
|
|
|
|
return option.value;
|
|
|
|
|
}
|
2023-07-31 14:19:34 +03:00
|
|
|
|
|
|
|
|
handlePaginatorClick(direction: ScrollDirection, $event: Event) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
this.scrollHeader(direction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handlePaginatorTouchStart(direction: ScrollDirection, $event: Event) {
|
|
|
|
|
if (direction === 'before' && !this.leftPaginationEnabled ||
|
|
|
|
|
direction === 'after' && !this.rightPaginationEnabled) {
|
|
|
|
|
$event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private scrollHeader(direction: ScrollDirection) {
|
|
|
|
|
const viewLength = this.toggleGroup.nativeElement.offsetWidth;
|
|
|
|
|
// Move the scroll distance one-third the length of the tab list's viewport.
|
|
|
|
|
const scrollAmount = ((direction === 'before' ? -1 : 1) * viewLength) / 3;
|
|
|
|
|
return this._scrollTo(this._scrollDistance + scrollAmount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private scrollToToggleOptionValue() {
|
|
|
|
|
if (this.buttonToggleGroup && this.buttonToggleGroup.selected) {
|
|
|
|
|
const selectedToggleButton = this.buttonToggleGroup.selected as MatButtonToggle;
|
|
|
|
|
const viewLength = this.toggleGroupContainer.nativeElement.offsetWidth;
|
|
|
|
|
const {offsetLeft, offsetWidth} = (selectedToggleButton._buttonElement.nativeElement.offsetParent as HTMLElement);
|
|
|
|
|
const labelBeforePos = offsetLeft; // this.toggleGroup.nativeElement.offsetWidth - offsetLeft;
|
|
|
|
|
const labelAfterPos = labelBeforePos + offsetWidth;
|
|
|
|
|
const beforeVisiblePos = this.scrollDistance;
|
|
|
|
|
const afterVisiblePos = this.scrollDistance + viewLength;
|
|
|
|
|
if (labelBeforePos < beforeVisiblePos) {
|
|
|
|
|
this.scrollDistance -= beforeVisiblePos - labelBeforePos;
|
|
|
|
|
} else if (labelAfterPos > afterVisiblePos) {
|
|
|
|
|
this.scrollDistance += Math.min(
|
|
|
|
|
labelAfterPos - afterVisiblePos,
|
|
|
|
|
labelBeforePos - beforeVisiblePos,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updatePagination() {
|
|
|
|
|
this.checkPaginationEnabled();
|
|
|
|
|
this.checkPaginationControls();
|
|
|
|
|
this.updateToggleHeaderScrollPosition();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private checkPaginationEnabled() {
|
|
|
|
|
if (this.toggleGroupContainer) {
|
|
|
|
|
const isEnabled = this.toggleGroup.nativeElement.scrollWidth > this.toggleGroupContainer.nativeElement.offsetWidth;
|
|
|
|
|
if (isEnabled !== this.showPaginationControls) {
|
|
|
|
|
if (!isEnabled) {
|
|
|
|
|
this.scrollDistance = 0;
|
|
|
|
|
} else {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.scrollToToggleOptionValue();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
this.showPaginationControls = isEnabled;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.showPaginationControls = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private checkPaginationControls() {
|
|
|
|
|
if (!this.showPaginationControls) {
|
|
|
|
|
this.leftPaginationEnabled = this.rightPaginationEnabled = false;
|
|
|
|
|
} else {
|
|
|
|
|
// Check if the pagination arrows should be activated.
|
|
|
|
|
this.leftPaginationEnabled = this.scrollDistance > 0;
|
|
|
|
|
this.rightPaginationEnabled = this.scrollDistance < this.getMaxScrollDistance();
|
|
|
|
|
this.cd.markForCheck();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getMaxScrollDistance(): number {
|
|
|
|
|
const lengthOfToggleGroup = this.toggleGroup.nativeElement.scrollWidth;
|
|
|
|
|
const viewLength = this.toggleGroupContainer.nativeElement.offsetWidth;
|
|
|
|
|
return lengthOfToggleGroup - viewLength || 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _scrollTo(position: number) {
|
|
|
|
|
if (!this.showPaginationControls) {
|
|
|
|
|
return {maxScrollDistance: 0, distance: 0};
|
|
|
|
|
} else {
|
|
|
|
|
const maxScrollDistance = this.getMaxScrollDistance();
|
|
|
|
|
this._scrollDistance = Math.max(0, Math.min(maxScrollDistance, position));
|
|
|
|
|
this._scrollDistanceChanged = true;
|
|
|
|
|
this.checkPaginationControls();
|
|
|
|
|
return {maxScrollDistance, distance: this._scrollDistance};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateToggleHeaderScrollPosition() {
|
|
|
|
|
const scrollDistance = this.scrollDistance;
|
|
|
|
|
const translateX = -scrollDistance;
|
|
|
|
|
this.toggleGroup.nativeElement.style.transform = `translateX(${Math.round(translateX)}px)`;
|
|
|
|
|
if (this.platform.TRIDENT || this.platform.EDGE) {
|
|
|
|
|
this.toggleGroupContainer.nativeElement.scrollLeft = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 19:18:35 +03:00
|
|
|
}
|