2019-09-03 19:31:16 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2019 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.
|
|
|
|
|
///
|
|
|
|
|
|
2019-09-05 21:15:40 +03:00
|
|
|
import {
|
2019-10-31 10:06:57 +02:00
|
|
|
AfterViewInit,
|
2019-09-05 21:15:40 +03:00
|
|
|
Component,
|
2019-09-25 19:37:29 +03:00
|
|
|
DoCheck,
|
2019-09-05 21:15:40 +03:00
|
|
|
Input,
|
2020-02-03 19:38:28 +02:00
|
|
|
IterableDiffers, KeyValueDiffers,
|
2019-10-31 10:06:57 +02:00
|
|
|
NgZone,
|
2019-09-05 21:15:40 +03:00
|
|
|
OnChanges,
|
2019-10-31 10:06:57 +02:00
|
|
|
OnDestroy,
|
2019-09-05 21:15:40 +03:00
|
|
|
OnInit,
|
|
|
|
|
SimpleChanges,
|
2019-09-25 19:37:29 +03:00
|
|
|
ViewChild
|
2019-09-05 21:15:40 +03:00
|
|
|
} from '@angular/core';
|
2019-09-03 19:31:16 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { AuthUser } from '@shared/models/user.model';
|
|
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { Timewindow, toHistoryTimewindow } from '@shared/models/time/time.models';
|
2019-09-03 19:31:16 +03:00
|
|
|
import { TimeService } from '@core/services/time.service';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { GridsterComponent, GridsterComponentInterface, GridsterConfig } from 'angular-gridster2';
|
2019-09-05 21:15:40 +03:00
|
|
|
import {
|
|
|
|
|
DashboardCallbacks,
|
|
|
|
|
DashboardWidget,
|
2019-09-25 19:37:29 +03:00
|
|
|
DashboardWidgets,
|
2019-10-31 10:06:57 +02:00
|
|
|
IDashboardComponent
|
2019-09-05 21:15:40 +03:00
|
|
|
} from '../../models/dashboard-component.models';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { ReplaySubject, Subject, Subscription } from 'rxjs';
|
2020-02-03 19:38:28 +02:00
|
|
|
import { WidgetLayout, WidgetLayouts } from '@shared/models/dashboard.models';
|
2019-09-03 19:31:16 +03:00
|
|
|
import { DialogService } from '@core/services/dialog.service';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { animatedScroll, deepClone, isDefined } from '@app/core/utils';
|
2019-09-05 21:15:40 +03:00
|
|
|
import { BreakpointObserver } from '@angular/cdk/layout';
|
2019-09-03 19:31:16 +03:00
|
|
|
import { MediaBreakpoints } from '@shared/models/constants';
|
2019-09-05 21:15:40 +03:00
|
|
|
import { IAliasController, IStateController } from '@app/core/api/widget-api.models';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { Widget, WidgetPosition } from '@app/shared/models/widget.models';
|
2019-09-25 19:37:29 +03:00
|
|
|
import { MatMenuTrigger } from '@angular/material';
|
2019-10-31 10:06:57 +02:00
|
|
|
import { SafeStyle } from '@angular/platform-browser';
|
2019-09-03 19:31:16 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-dashboard',
|
|
|
|
|
templateUrl: './dashboard.component.html',
|
|
|
|
|
styleUrls: ['./dashboard.component.scss']
|
|
|
|
|
})
|
2019-10-31 10:06:57 +02:00
|
|
|
export class DashboardComponent extends PageComponent implements IDashboardComponent, DoCheck, OnInit, OnDestroy, AfterViewInit, OnChanges {
|
2019-09-03 19:31:16 +03:00
|
|
|
|
|
|
|
|
authUser: AuthUser;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2019-10-31 10:06:57 +02:00
|
|
|
widgets: Iterable<Widget>;
|
2019-09-20 20:30:43 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
widgetLayouts: WidgetLayouts;
|
2019-09-05 21:15:40 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
callbacks: DashboardCallbacks;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
aliasController: IAliasController;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
stateController: IStateController;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
columns: number;
|
|
|
|
|
|
|
|
|
|
@Input()
|
2019-10-31 10:06:57 +02:00
|
|
|
margin: number;
|
2019-09-05 21:15:40 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isEdit: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
autofillHeight: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
mobileAutofillHeight: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
mobileRowHeight: number;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isMobile: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isMobileDisabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isEditActionEnabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isExportActionEnabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
isRemoveActionEnabled: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
dashboardStyle: {[klass: string]: any};
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
@Input()
|
|
|
|
|
backgroundImage: SafeStyle | string;
|
|
|
|
|
|
2019-09-05 21:15:40 +03:00
|
|
|
@Input()
|
|
|
|
|
dashboardClass: string;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
ignoreLoading: boolean;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
dashboardTimewindow: Timewindow;
|
2019-09-03 19:31:16 +03:00
|
|
|
|
2019-09-12 19:58:42 +03:00
|
|
|
dashboardTimewindowChangedSubject: Subject<Timewindow> = new ReplaySubject<Timewindow>();
|
|
|
|
|
|
|
|
|
|
dashboardTimewindowChanged = this.dashboardTimewindowChangedSubject.asObservable();
|
|
|
|
|
|
2019-09-10 15:12:10 +03:00
|
|
|
originalDashboardTimewindow: Timewindow;
|
|
|
|
|
|
2019-09-03 19:31:16 +03:00
|
|
|
gridsterOpts: GridsterConfig;
|
|
|
|
|
|
|
|
|
|
isWidgetExpanded = false;
|
|
|
|
|
isMobileSize = false;
|
|
|
|
|
|
|
|
|
|
@ViewChild('gridster', {static: true}) gridster: GridsterComponent;
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
@ViewChild('dashboardMenuTrigger', {static: true}) dashboardMenuTrigger: MatMenuTrigger;
|
|
|
|
|
|
|
|
|
|
dashboardMenuPosition = { x: '0px', y: '0px' };
|
|
|
|
|
|
|
|
|
|
dashboardContextMenuEvent: MouseEvent;
|
|
|
|
|
|
|
|
|
|
@ViewChild('widgetMenuTrigger', {static: true}) widgetMenuTrigger: MatMenuTrigger;
|
|
|
|
|
|
|
|
|
|
widgetMenuPosition = { x: '0px', y: '0px' };
|
|
|
|
|
|
|
|
|
|
widgetContextMenuEvent: MouseEvent;
|
2019-09-03 19:31:16 +03:00
|
|
|
|
2019-09-20 20:30:43 +03:00
|
|
|
dashboardLoading = true;
|
2019-09-03 19:31:16 +03:00
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
dashboardWidgets = new DashboardWidgets(this,
|
|
|
|
|
this.differs.find([]).create<Widget>((index, item) => {
|
|
|
|
|
return item;
|
2020-02-03 19:38:28 +02:00
|
|
|
}),
|
|
|
|
|
this.kvDiffers.find([]).create<string, WidgetLayout>()
|
2019-09-25 19:37:29 +03:00
|
|
|
);
|
2019-09-03 19:31:16 +03:00
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
breakpointObserverSubscription: Subscription;
|
|
|
|
|
|
|
|
|
|
private optionsChangeNotificationsPaused = false;
|
|
|
|
|
|
|
|
|
|
private gridsterResizeListener = null;
|
|
|
|
|
|
2019-09-03 19:31:16 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
private timeService: TimeService,
|
|
|
|
|
private dialogService: DialogService,
|
2019-09-25 19:37:29 +03:00
|
|
|
private breakpointObserver: BreakpointObserver,
|
|
|
|
|
private differs: IterableDiffers,
|
2020-02-03 19:38:28 +02:00
|
|
|
private kvDiffers: KeyValueDiffers,
|
2019-10-24 19:52:19 +03:00
|
|
|
private ngZone: NgZone) {
|
2019-09-03 19:31:16 +03:00
|
|
|
super(store);
|
|
|
|
|
this.authUser = getCurrentAuthUser(store);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2019-09-05 21:15:40 +03:00
|
|
|
if (!this.dashboardTimewindow) {
|
|
|
|
|
this.dashboardTimewindow = this.timeService.defaultTimewindow();
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
this.gridsterOpts = {
|
|
|
|
|
gridType: 'scrollVertical',
|
|
|
|
|
keepFixedHeightInMobile: true,
|
2019-11-04 15:47:36 +02:00
|
|
|
disableWarnings: false,
|
|
|
|
|
disableAutoPositionOnConflict: false,
|
2019-09-03 19:31:16 +03:00
|
|
|
pushItems: false,
|
|
|
|
|
swap: false,
|
|
|
|
|
maxRows: 100,
|
2019-09-05 21:15:40 +03:00
|
|
|
minCols: this.columns ? this.columns : 24,
|
2019-10-31 18:33:51 +02:00
|
|
|
maxCols: 3000,
|
|
|
|
|
maxItemCols: 1000,
|
|
|
|
|
maxItemRows: 1000,
|
|
|
|
|
maxItemArea: 1000000,
|
2019-09-03 19:31:16 +03:00
|
|
|
outerMargin: true,
|
2019-10-31 10:06:57 +02:00
|
|
|
margin: this.margin ? this.margin : 10,
|
2019-09-03 19:31:16 +03:00
|
|
|
minItemCols: 1,
|
|
|
|
|
minItemRows: 1,
|
|
|
|
|
defaultItemCols: 8,
|
|
|
|
|
defaultItemRows: 6,
|
2019-09-05 21:15:40 +03:00
|
|
|
resizable: {enabled: this.isEdit},
|
|
|
|
|
draggable: {enabled: this.isEdit},
|
2019-09-25 19:37:29 +03:00
|
|
|
itemChangeCallback: item => this.dashboardWidgets.sortWidgets(),
|
|
|
|
|
itemInitCallback: (item, itemComponent) => {
|
|
|
|
|
(itemComponent.item as DashboardWidget).gridsterItemComponent = itemComponent;
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
};
|
|
|
|
|
|
2019-09-05 21:15:40 +03:00
|
|
|
this.updateMobileOpts();
|
2019-09-03 19:31:16 +03:00
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
this.breakpointObserverSubscription = this.breakpointObserver
|
2019-09-05 21:15:40 +03:00
|
|
|
.observe(MediaBreakpoints['gt-sm']).subscribe(
|
2019-09-03 19:31:16 +03:00
|
|
|
() => {
|
2019-09-05 21:15:40 +03:00
|
|
|
this.updateMobileOpts();
|
2019-09-25 19:37:29 +03:00
|
|
|
this.notifyGridsterOptionsChanged();
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
);
|
2019-09-20 20:30:43 +03:00
|
|
|
|
|
|
|
|
this.updateWidgets();
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
if (this.gridsterResizeListener) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
removeResizeListener(this.gridster.el, this.gridsterResizeListener);
|
|
|
|
|
}
|
|
|
|
|
if (this.breakpointObserverSubscription) {
|
|
|
|
|
this.breakpointObserverSubscription.unsubscribe();
|
|
|
|
|
}
|
|
|
|
|
this.gridster = null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
ngDoCheck() {
|
2019-11-04 15:47:36 +02:00
|
|
|
if (!this.optionsChangeNotificationsPaused) {
|
|
|
|
|
this.dashboardWidgets.doCheck();
|
|
|
|
|
}
|
2019-09-25 19:37:29 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-05 21:15:40 +03:00
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
let updateMobileOpts = false;
|
|
|
|
|
let updateLayoutOpts = false;
|
|
|
|
|
let updateEditingOpts = false;
|
2019-09-20 20:30:43 +03:00
|
|
|
let updateWidgets = false;
|
2019-09-05 21:15:40 +03:00
|
|
|
for (const propName of Object.keys(changes)) {
|
|
|
|
|
const change = changes[propName];
|
|
|
|
|
if (!change.firstChange && change.currentValue !== change.previousValue) {
|
|
|
|
|
if (['isMobile', 'isMobileDisabled', 'autofillHeight', 'mobileAutofillHeight', 'mobileRowHeight'].includes(propName)) {
|
|
|
|
|
updateMobileOpts = true;
|
2019-10-31 10:06:57 +02:00
|
|
|
} else if (['margin', 'columns'].includes(propName)) {
|
2019-09-05 21:15:40 +03:00
|
|
|
updateLayoutOpts = true;
|
|
|
|
|
} else if (propName === 'isEdit') {
|
|
|
|
|
updateEditingOpts = true;
|
2019-09-20 20:30:43 +03:00
|
|
|
} else if (['widgets', 'widgetLayouts'].includes(propName)) {
|
|
|
|
|
updateWidgets = true;
|
2019-09-25 19:37:29 +03:00
|
|
|
} else if (propName === 'dashboardTimewindow') {
|
|
|
|
|
this.dashboardTimewindowChangedSubject.next(this.dashboardTimewindow);
|
2019-09-05 21:15:40 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-20 20:30:43 +03:00
|
|
|
if (updateWidgets) {
|
|
|
|
|
this.updateWidgets();
|
|
|
|
|
}
|
2019-09-05 21:15:40 +03:00
|
|
|
if (updateMobileOpts) {
|
|
|
|
|
this.updateMobileOpts();
|
|
|
|
|
}
|
|
|
|
|
if (updateLayoutOpts) {
|
|
|
|
|
this.updateLayoutOpts();
|
|
|
|
|
}
|
|
|
|
|
if (updateEditingOpts) {
|
|
|
|
|
this.updateEditingOpts();
|
|
|
|
|
}
|
|
|
|
|
if (updateMobileOpts || updateLayoutOpts || updateEditingOpts) {
|
|
|
|
|
this.notifyGridsterOptionsChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-20 20:30:43 +03:00
|
|
|
private updateWidgets() {
|
|
|
|
|
this.dashboardWidgets.setWidgets(this.widgets, this.widgetLayouts);
|
2020-01-22 20:05:30 +02:00
|
|
|
this.dashboardWidgets.doCheck();
|
2019-09-20 20:30:43 +03:00
|
|
|
this.dashboardLoading = false;
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
private updateWidgetLayouts() {
|
|
|
|
|
this.dashboardWidgets.widgetLayoutsUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 19:31:16 +03:00
|
|
|
ngAfterViewInit(): void {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.gridsterResizeListener = this.onGridsterParentResize.bind(this);
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
addResizeListener(this.gridster.el, this.gridsterResizeListener);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-12 19:58:42 +03:00
|
|
|
onUpdateTimewindow(startTimeMs: number, endTimeMs: number, interval?: number): void {
|
2019-10-24 19:52:19 +03:00
|
|
|
this.ngZone.run(() => {
|
|
|
|
|
if (!this.originalDashboardTimewindow) {
|
|
|
|
|
this.originalDashboardTimewindow = deepClone(this.dashboardTimewindow);
|
|
|
|
|
}
|
|
|
|
|
this.dashboardTimewindow = toHistoryTimewindow(this.dashboardTimewindow,
|
|
|
|
|
startTimeMs, endTimeMs, interval, this.timeService);
|
|
|
|
|
this.dashboardTimewindowChangedSubject.next(this.dashboardTimewindow);
|
|
|
|
|
});
|
2019-09-10 15:12:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onResetTimewindow(): void {
|
2019-10-24 19:52:19 +03:00
|
|
|
this.ngZone.run(() => {
|
|
|
|
|
if (this.originalDashboardTimewindow) {
|
|
|
|
|
this.dashboardTimewindow = deepClone(this.originalDashboardTimewindow);
|
|
|
|
|
this.originalDashboardTimewindow = null;
|
|
|
|
|
this.dashboardTimewindowChangedSubject.next(this.dashboardTimewindow);
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-09-10 15:12:10 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-03 19:31:16 +03:00
|
|
|
isAutofillHeight(): boolean {
|
|
|
|
|
if (this.isMobileSize) {
|
2019-09-05 21:15:40 +03:00
|
|
|
return isDefined(this.mobileAutofillHeight) ? this.mobileAutofillHeight : false;
|
2019-09-03 19:31:16 +03:00
|
|
|
} else {
|
2019-09-05 21:15:40 +03:00
|
|
|
return isDefined(this.autofillHeight) ? this.autofillHeight : false;
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
openDashboardContextMenu($event: MouseEvent) {
|
|
|
|
|
if (this.callbacks && this.callbacks.prepareDashboardContextMenu) {
|
|
|
|
|
const items = this.callbacks.prepareDashboardContextMenu($event);
|
|
|
|
|
if (items && items.length) {
|
|
|
|
|
$event.preventDefault();
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
this.dashboardContextMenuEvent = $event;
|
|
|
|
|
this.dashboardMenuPosition.x = $event.clientX + 'px';
|
|
|
|
|
this.dashboardMenuPosition.y = $event.clientY + 'px';
|
|
|
|
|
this.dashboardMenuTrigger.menuData = { items };
|
|
|
|
|
this.dashboardMenuTrigger.openMenu();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
openWidgetContextMenu($event: MouseEvent, widget: DashboardWidget) {
|
|
|
|
|
if (this.callbacks && this.callbacks.prepareWidgetContextMenu) {
|
2019-10-31 10:06:57 +02:00
|
|
|
const items = this.callbacks.prepareWidgetContextMenu($event, widget.widget);
|
2019-09-25 19:37:29 +03:00
|
|
|
if (items && items.length) {
|
|
|
|
|
$event.preventDefault();
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
this.widgetContextMenuEvent = $event;
|
|
|
|
|
this.widgetMenuPosition.x = $event.clientX + 'px';
|
|
|
|
|
this.widgetMenuPosition.y = $event.clientY + 'px';
|
|
|
|
|
this.widgetMenuTrigger.menuData = { items, widget: widget.widget };
|
|
|
|
|
this.widgetMenuTrigger.openMenu();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onWidgetFullscreenChanged(expanded: boolean, widget: DashboardWidget) {
|
|
|
|
|
this.isWidgetExpanded = expanded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetMouseDown($event: Event, widget: DashboardWidget) {
|
2019-09-05 21:15:40 +03:00
|
|
|
if (this.callbacks && this.callbacks.onWidgetMouseDown) {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.callbacks.onWidgetMouseDown($event, widget.widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetClicked($event: Event, widget: DashboardWidget) {
|
2019-09-05 21:15:40 +03:00
|
|
|
if (this.callbacks && this.callbacks.onWidgetClicked) {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.callbacks.onWidgetClicked($event, widget.widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editWidget($event: Event, widget: DashboardWidget) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
2019-09-05 21:15:40 +03:00
|
|
|
if (this.isEditActionEnabled && this.callbacks && this.callbacks.onEditWidget) {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.callbacks.onEditWidget($event, widget.widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportWidget($event: Event, widget: DashboardWidget) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
2019-09-05 21:15:40 +03:00
|
|
|
if (this.isExportActionEnabled && this.callbacks && this.callbacks.onExportWidget) {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.callbacks.onExportWidget($event, widget.widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
removeWidget($event: Event, widget: DashboardWidget) {
|
|
|
|
|
if ($event) {
|
|
|
|
|
$event.stopPropagation();
|
|
|
|
|
}
|
2019-09-05 21:15:40 +03:00
|
|
|
if (this.isRemoveActionEnabled && this.callbacks && this.callbacks.onRemoveWidget) {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.callbacks.onRemoveWidget($event, widget.widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
highlightWidget(widgetId: string, delay?: number) {
|
|
|
|
|
const highlighted = this.dashboardWidgets.highlightWidget(widgetId);
|
2019-09-25 19:37:29 +03:00
|
|
|
if (highlighted) {
|
|
|
|
|
this.scrollToWidget(highlighted, delay);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
selectWidget(widgetId: string, delay?: number) {
|
|
|
|
|
const selected = this.dashboardWidgets.selectWidget(widgetId);
|
2019-09-25 19:37:29 +03:00
|
|
|
if (selected) {
|
|
|
|
|
this.scrollToWidget(selected, delay);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
getSelectedWidget(): Widget {
|
|
|
|
|
const dashboardWidget = this.dashboardWidgets.getSelectedWidget();
|
|
|
|
|
return dashboardWidget ? dashboardWidget.widget : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getEventGridPosition(event: Event): WidgetPosition {
|
|
|
|
|
const pos: WidgetPosition = {
|
|
|
|
|
row: 0,
|
|
|
|
|
column: 0
|
|
|
|
|
};
|
2019-10-31 10:06:57 +02:00
|
|
|
const parentElement = $(this.gridster.el);
|
2019-09-25 19:37:29 +03:00
|
|
|
let pageX = 0;
|
|
|
|
|
let pageY = 0;
|
|
|
|
|
if (event instanceof MouseEvent) {
|
|
|
|
|
pageX = event.pageX;
|
|
|
|
|
pageY = event.pageY;
|
|
|
|
|
}
|
2019-10-31 10:06:57 +02:00
|
|
|
const offset = parentElement.offset();
|
|
|
|
|
const x = pageX - offset.left + parentElement.scrollLeft();
|
|
|
|
|
const y = pageY - offset.top + parentElement.scrollTop();
|
2019-09-25 19:37:29 +03:00
|
|
|
pos.row = this.gridster.pixelsToPositionY(y, Math.floor);
|
|
|
|
|
pos.column = this.gridster.pixelsToPositionX(x, Math.floor);
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 19:31:16 +03:00
|
|
|
resetHighlight() {
|
2019-09-25 19:37:29 +03:00
|
|
|
const highlighted = this.dashboardWidgets.resetHighlight();
|
|
|
|
|
if (highlighted) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.scrollToWidget(highlighted, 0);
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isHighlighted(widget: DashboardWidget) {
|
2019-09-25 19:37:29 +03:00
|
|
|
return this.dashboardWidgets.isHighlighted(widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isNotHighlighted(widget: DashboardWidget) {
|
2019-09-25 19:37:29 +03:00
|
|
|
return this.dashboardWidgets.isNotHighlighted(widget);
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 19:37:29 +03:00
|
|
|
private scrollToWidget(widget: DashboardWidget, delay?: number) {
|
|
|
|
|
const parentElement = this.gridster.el as HTMLElement;
|
|
|
|
|
widget.gridsterItemComponent$().subscribe((gridsterItem) => {
|
|
|
|
|
const gridsterItemElement = gridsterItem.el as HTMLElement;
|
|
|
|
|
const offset = (parentElement.clientHeight - gridsterItemElement.clientHeight) / 2;
|
|
|
|
|
let scrollTop;
|
|
|
|
|
if (this.isMobileSize) {
|
|
|
|
|
scrollTop = gridsterItemElement.offsetTop;
|
|
|
|
|
} else {
|
|
|
|
|
scrollTop = scrollTop = gridsterItem.top;
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
if (offset > 0) {
|
|
|
|
|
scrollTop -= offset;
|
|
|
|
|
}
|
|
|
|
|
animatedScroll(parentElement, scrollTop, delay);
|
2019-09-25 19:37:29 +03:00
|
|
|
});
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
private updateMobileOpts(parentHeight?: number) {
|
2019-09-03 19:31:16 +03:00
|
|
|
this.isMobileSize = this.checkIsMobileSize();
|
2019-10-31 10:06:57 +02:00
|
|
|
const autofillHeight = this.isAutofillHeight();
|
|
|
|
|
if (autofillHeight) {
|
|
|
|
|
this.gridsterOpts.gridType = this.isMobileSize ? 'fixed' : 'fit';
|
|
|
|
|
} else {
|
|
|
|
|
this.gridsterOpts.gridType = this.isMobileSize ? 'fixed' : 'scrollVertical';
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
const mobileBreakPoint = this.isMobileSize ? 20000 : 0;
|
|
|
|
|
this.gridsterOpts.mobileBreakpoint = mobileBreakPoint;
|
2019-10-31 10:06:57 +02:00
|
|
|
const rowSize = this.detectRowSize(this.isMobileSize, autofillHeight, parentHeight);
|
2019-09-03 19:31:16 +03:00
|
|
|
if (this.gridsterOpts.fixedRowHeight !== rowSize) {
|
|
|
|
|
this.gridsterOpts.fixedRowHeight = rowSize;
|
|
|
|
|
}
|
2019-10-31 10:06:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onGridsterParentResize() {
|
|
|
|
|
const parentHeight = this.gridster.el.offsetHeight;
|
|
|
|
|
if (this.isMobileSize && this.mobileAutofillHeight && parentHeight) {
|
|
|
|
|
this.updateMobileOpts(parentHeight);
|
|
|
|
|
this.notifyGridsterOptionsChanged();
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
2019-09-05 21:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateLayoutOpts() {
|
2019-10-31 10:06:57 +02:00
|
|
|
this.gridsterOpts.minCols = this.columns ? this.columns : 24;
|
|
|
|
|
this.gridsterOpts.margin = this.margin ? this.margin : 10;
|
2019-09-05 21:15:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateEditingOpts() {
|
|
|
|
|
this.gridsterOpts.resizable.enabled = this.isEdit;
|
|
|
|
|
this.gridsterOpts.draggable.enabled = this.isEdit;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 19:52:19 +03:00
|
|
|
public notifyGridsterOptionsChanged() {
|
2019-10-31 10:06:57 +02:00
|
|
|
if (!this.optionsChangeNotificationsPaused) {
|
|
|
|
|
if (this.gridster && this.gridster.options) {
|
|
|
|
|
this.gridster.optionsChanged();
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 10:06:57 +02:00
|
|
|
public pauseChangeNotifications() {
|
|
|
|
|
this.optionsChangeNotificationsPaused = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public resumeChangeNotifications() {
|
|
|
|
|
this.optionsChangeNotificationsPaused = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public notifyLayoutUpdated() {
|
|
|
|
|
this.updateWidgetLayouts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private detectRowSize(isMobile: boolean, autofillHeight: boolean, parentHeight?: number): number | null {
|
2019-09-03 19:31:16 +03:00
|
|
|
let rowHeight = null;
|
2019-10-31 10:06:57 +02:00
|
|
|
if (!autofillHeight) {
|
2019-09-03 19:31:16 +03:00
|
|
|
if (isMobile) {
|
2019-09-05 21:15:40 +03:00
|
|
|
rowHeight = isDefined(this.mobileRowHeight) ? this.mobileRowHeight : 70;
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
2019-10-31 10:06:57 +02:00
|
|
|
} else if (autofillHeight && isMobile) {
|
|
|
|
|
if (!parentHeight) {
|
|
|
|
|
parentHeight = this.gridster.el.offsetHeight;
|
|
|
|
|
}
|
|
|
|
|
if (parentHeight) {
|
|
|
|
|
let totalRows = 0;
|
|
|
|
|
for (const widget of this.dashboardWidgets.dashboardWidgets) {
|
|
|
|
|
totalRows += widget.rows;
|
|
|
|
|
}
|
|
|
|
|
rowHeight = (parentHeight - this.gridsterOpts.margin * (this.dashboardWidgets.dashboardWidgets.length + 2)) / totalRows;
|
|
|
|
|
}
|
2019-09-03 19:31:16 +03:00
|
|
|
}
|
|
|
|
|
return rowHeight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private checkIsMobileSize(): boolean {
|
2019-09-05 21:15:40 +03:00
|
|
|
const isMobileDisabled = this.isMobileDisabled === true;
|
|
|
|
|
let isMobileSize = this.isMobile === true && !isMobileDisabled;
|
2019-09-03 19:31:16 +03:00
|
|
|
if (!isMobileSize && !isMobileDisabled) {
|
|
|
|
|
isMobileSize = !this.breakpointObserver.isMatched(MediaBreakpoints['gt-sm']);
|
|
|
|
|
}
|
|
|
|
|
return isMobileSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|