thingsboard/ui-ngx/src/app/modules/home/pages/dashboard/dashboard-page.models.ts

152 lines
4.8 KiB
TypeScript
Raw Normal View History

///
/// 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.
///
import { DashboardLayoutId, GridSettings, WidgetLayout, Dashboard, WidgetLayouts } from '@app/shared/models/dashboard.models';
import { Widget, WidgetPosition } from '@app/shared/models/widget.models';
import { Timewindow } from '@shared/models/time/time.models';
import { IAliasController, IStateController } from '@core/api/widget-api.models';
import { ILayoutController } from './layout/layout.models';
2019-09-25 19:37:29 +03:00
import {
DashboardContextMenuItem,
WidgetContextMenuItem
2019-09-25 19:37:29 +03:00
} from '@home/models/dashboard-component.models';
import { Observable } from 'rxjs';
2019-10-24 19:52:19 +03:00
import { ChangeDetectorRef } from '@angular/core';
export declare type DashboardPageScope = 'tenant' | 'customer';
export interface DashboardContext {
state: string;
2019-11-04 15:47:36 +02:00
getDashboard: () => Dashboard;
dashboardTimewindow: Timewindow;
aliasController: IAliasController;
stateController: IStateController;
2019-10-24 19:52:19 +03:00
runChangeDetection: () => void;
}
export interface IDashboardController {
dashboardCtx: DashboardContext;
openRightLayout();
openDashboardState(stateId: string, openRightLayout: boolean);
2019-09-25 19:37:29 +03:00
addWidget($event: Event, layoutCtx: DashboardPageLayoutContext);
editWidget($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
exportWidget($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
2019-09-25 19:37:29 +03:00
removeWidget($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
widgetMouseDown($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
widgetClicked($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
2019-09-25 19:37:29 +03:00
prepareDashboardContextMenu(layoutCtx: DashboardPageLayoutContext): Array<DashboardContextMenuItem>;
prepareWidgetContextMenu(layoutCtx: DashboardPageLayoutContext, widget: Widget): Array<WidgetContextMenuItem>;
2019-09-25 19:37:29 +03:00
copyWidget($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
copyWidgetReference($event: Event, layoutCtx: DashboardPageLayoutContext, widget: Widget);
pasteWidget($event: Event, layoutCtx: DashboardPageLayoutContext, pos: WidgetPosition);
pasteWidgetReference($event: Event, layoutCtx: DashboardPageLayoutContext, pos: WidgetPosition);
}
export interface DashboardPageLayoutContext {
id: DashboardLayoutId;
widgets: LayoutWidgetsArray;
widgetLayouts: WidgetLayouts;
gridSettings: GridSettings;
ctrl: ILayoutController;
2019-09-25 19:37:29 +03:00
dashboardCtrl: IDashboardController;
ignoreLoading: boolean;
}
export interface DashboardPageLayout {
show: boolean;
layoutCtx: DashboardPageLayoutContext;
}
export declare type DashboardPageLayouts = {[key in DashboardLayoutId]: DashboardPageLayout};
export class LayoutWidgetsArray implements Iterable<Widget> {
private widgetIds: string[] = [];
private pointer = 0;
2019-10-31 18:33:51 +02:00
private loaded = false;
2019-11-04 15:47:36 +02:00
constructor(private dashboardCtx: DashboardContext) {
}
size() {
return this.widgetIds.length;
}
2019-10-31 18:33:51 +02:00
isLoading() {
return !this.loaded;
}
isEmpty() {
return this.loaded && this.widgetIds.length === 0;
}
setWidgetIds(widgetIds: string[]) {
this.widgetIds = widgetIds;
2019-10-31 18:33:51 +02:00
this.loaded = true;
}
addWidgetId(widgetId: string) {
this.widgetIds.push(widgetId);
}
removeWidgetId(widgetId: string): boolean {
const index = this.widgetIds.indexOf(widgetId);
if (index > -1) {
this.widgetIds.splice(index, 1);
return true;
}
return false;
}
[Symbol.iterator](): Iterator<Widget> {
let pointer = 0;
const widgetIds = this.widgetIds;
2019-11-04 15:47:36 +02:00
const dashboard = this.dashboardCtx.getDashboard();
return {
next(value?: any): IteratorResult<Widget> {
if (pointer < widgetIds.length) {
const widgetId = widgetIds[pointer++];
const widget = dashboard.configuration.widgets[widgetId];
return {
done: false,
value: widget
};
} else {
return {
done: true,
value: null
};
}
}
};
}
public widgetByIndex(index: number): Widget {
const widgetId = this.widgetIds[index];
if (widgetId) {
return this.widgetById(widgetId);
} else {
return null;
}
}
private widgetById(widgetId: string): Widget {
2019-11-04 15:47:36 +02:00
return this.dashboardCtx.getDashboard().configuration.widgets[widgetId];
}
}