15 changed files with 1109 additions and 177 deletions
@ -0,0 +1,109 @@ |
|||
<!-- |
|||
|
|||
SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
SPDX-License-Identifier: BUSL-1.1 |
|||
|
|||
--> |
|||
<tb-home-card [card]="card" [count]="total()"> |
|||
<ng-container homeCardActions> |
|||
@if (total()) { |
|||
<tb-home-toggle [options]="tabOptions" [value]="tab()" [label]="'home.dashboards.view' | translate" (valueChange)="setTab($event)"></tb-home-toggle> |
|||
} |
|||
@if (canAdd) { |
|||
<a mat-button color="primary" class="tb-dashboards-add" routerLink="/dashboards/all" [queryParams]="{action: 'add'}"> |
|||
<mat-icon>add</mat-icon> |
|||
<span class="tb-dashboards-add-label">{{ 'home.dashboards.add' | translate }}</span> |
|||
</a> |
|||
} |
|||
</ng-container> |
|||
@if (loaded()) { |
|||
@if (!total()) { |
|||
<div class="tb-dashboards-empty"> |
|||
<span class="tb-dashboards-empty-glyph" aria-hidden="true"><mat-icon>{{ tenantAdmin ? 'dashboard_customize' : 'dashboard' }}</mat-icon></span> |
|||
@if (tenantAdmin) { |
|||
<div class="tb-dashboards-empty-text"> |
|||
<p>{{ 'home.dashboards.none' | translate }}</p> |
|||
<div class="tb-dashboards-empty-actions"> |
|||
<a routerLink="/iot-hub/solution-templates">{{ 'home.dashboards.from-template' | translate }}</a> |
|||
<a [href]="howToLink" target="_blank" rel="noopener">{{ 'home.dashboards.how-to' | translate }}<mat-icon class="tb-dashboards-ext" aria-hidden="true">north_east</mat-icon><span class="tb-dashboards-sr">{{ 'home.dashboards.opens-new-tab' | translate }}</span></a> |
|||
</div> |
|||
</div> |
|||
} @else { |
|||
<div class="tb-dashboards-empty-title">{{ 'home.dashboards.none-assigned' | translate }}</div> |
|||
} |
|||
</div> |
|||
} @else { |
|||
@if (rows().length) { |
|||
<table class="tb-dashboards-table"> |
|||
<thead> |
|||
<tr> |
|||
<th class="col-star"></th> |
|||
<th class="col-title">{{ 'home.dashboards.name' | translate }}</th> |
|||
<th class="col-time">{{ 'home.dashboards.last-viewed' | translate }}</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
@for (row of rows(); track row.id) { |
|||
<tr (click)="open(row)"> |
|||
<td class="col-star"> |
|||
<button type="button" class="tb-dashboards-star" [class.starred]="row.starred" |
|||
[matTooltip]="(row.starred ? 'home.dashboards.unstar-tip' : 'home.dashboards.star-tip') | translate" |
|||
[attr.aria-label]="(row.starred ? 'home.dashboards.unstar' : 'home.dashboards.star') | translate: {title: row.title}" |
|||
(click)="toggleStar(row, $event)"> |
|||
<mat-icon>{{ row.starred ? 'star' : 'star_border' }}</mat-icon> |
|||
</button> |
|||
</td> |
|||
<td class="col-title"><a [routerLink]="row.url" (click)="$event.stopPropagation()">{{ row.title | customTranslate }}</a></td> |
|||
<td class="col-time">{{ row.ago }}</td> |
|||
</tr> |
|||
} |
|||
</tbody> |
|||
</table> |
|||
} @else { |
|||
<div class="tb-dashboards-empty-note">{{ (tab() === 'last' ? 'home.dashboards.none-viewed' : 'home.dashboards.none-starred') | translate }}</div> |
|||
} |
|||
@if (tab() === 'starred') { |
|||
<div class="tb-dashboards-picker"> |
|||
@if (picking()) { |
|||
<label class="tb-dashboards-search"> |
|||
<mat-icon aria-hidden="true">search</mat-icon> |
|||
<input #searchInput type="text" autocomplete="off" spellcheck="false" role="combobox" aria-expanded="true" |
|||
aria-controls="tb-dashboards-star-options" aria-autocomplete="list" |
|||
[attr.aria-activedescendant]="candidates().length ? 'tb-dashboards-star-option-' + active() : null" |
|||
[placeholder]="'home.dashboards.search-to-star' | translate" [attr.aria-label]="'home.dashboards.search-to-star' | translate" |
|||
[value]="query()" (input)="onQuery($any($event.target).value)" (keydown)="onPickerKeydown($event)" (blur)="onPickerBlur()"> |
|||
<button mat-icon-button type="button" class="tb-dashboards-search-close" [attr.aria-label]="'action.close' | translate" |
|||
(mousedown)="$event.preventDefault()" (click)="closePicker()"> |
|||
<mat-icon>close</mat-icon> |
|||
</button> |
|||
</label> |
|||
<div class="tb-dashboards-options" id="tb-dashboards-star-options" role="listbox"> |
|||
@for (candidate of candidates(); track candidate.id; let i = $index) { |
|||
<div class="tb-dashboards-option" role="option" [id]="'tb-dashboards-star-option-' + i" [class.active]="i === active()" |
|||
[attr.aria-selected]="i === active()" tabindex="-1" (mousedown)="$event.preventDefault()" (mousemove)="active.set(i)" |
|||
(click)="pick(candidate)" (keydown.enter)="pick(candidate)"> |
|||
<mat-icon>star_border</mat-icon> |
|||
<span class="tb-dashboards-option-label">{{ candidate.title | customTranslate }}</span> |
|||
@if (candidate.ago) { |
|||
<small>{{ candidate.ago }}</small> |
|||
} |
|||
</div> |
|||
} @empty { |
|||
@if (query().trim()) { |
|||
<div class="tb-dashboards-options-empty">{{ 'home.dashboards.no-match' | translate: {query: query().trim()} }}</div> |
|||
} |
|||
} |
|||
</div> |
|||
} @else { |
|||
<button type="button" class="tb-dashboards-picker-add" [disabled]="allStarred()" (click)="openPicker()"> |
|||
<mat-icon>{{ allStarred() ? 'star' : 'star_border' }}</mat-icon> |
|||
{{ (allStarred() ? 'home.dashboards.all-starred' : 'home.dashboards.star-a-dashboard') | translate }} |
|||
</button> |
|||
} |
|||
</div> |
|||
} |
|||
<tb-home-list-footer [total]="listed()" [expanded]="expanded()" [link]="dashboardsLink" |
|||
(expandedChange)="expanded.set($event)"></tb-home-list-footer> |
|||
} |
|||
} |
|||
</tb-home-card> |
|||
@ -0,0 +1,439 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
:host { |
|||
display: block; |
|||
min-width: 0; |
|||
container: tb-dashboards-home / inline-size; |
|||
} |
|||
|
|||
.tb-dashboards-add.mat-mdc-button { |
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
.mat-icon { |
|||
margin-right: 6px; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-table { |
|||
width: 100%; |
|||
border-collapse: collapse; |
|||
table-layout: auto; |
|||
|
|||
th { |
|||
height: 28px; |
|||
padding: 0 12px 0 0; |
|||
font: 400 12px/16px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.25px; |
|||
text-align: left; |
|||
white-space: nowrap; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
} |
|||
|
|||
td { |
|||
height: 36px; |
|||
padding: 0 12px 0 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
vertical-align: middle; |
|||
font-variant-numeric: tabular-nums; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
|
|||
tbody tr { |
|||
cursor: pointer; |
|||
|
|||
&:hover td { |
|||
background: rgba(0, 0, 0, 0.02); |
|||
} |
|||
|
|||
&:hover .col-title a { |
|||
text-decoration: underline; |
|||
} |
|||
} |
|||
|
|||
.col-star { |
|||
width: 40px; |
|||
min-width: 40px; |
|||
} |
|||
|
|||
td.col-title { |
|||
max-width: 0; |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
white-space: nowrap; |
|||
|
|||
a { |
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
text-decoration: none; |
|||
|
|||
&:hover { |
|||
text-decoration: underline; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.col-time { |
|||
width: 96px; |
|||
text-align: right; |
|||
white-space: nowrap; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-star { |
|||
position: relative; |
|||
display: inline-flex; |
|||
margin: 0; |
|||
padding: 0; |
|||
border: 0; |
|||
background: none; |
|||
vertical-align: middle; |
|||
color: var(--tb-ink-12, rgba(0, 0, 0, 0.12)); |
|||
cursor: pointer; |
|||
|
|||
&::after { |
|||
content: ''; |
|||
position: absolute; |
|||
inset: -4px; |
|||
border-radius: 50%; |
|||
} |
|||
|
|||
&:hover { |
|||
color: rgba(0, 0, 0, 0.18); |
|||
} |
|||
|
|||
&.starred { |
|||
color: var(--tb-primary-500); |
|||
|
|||
&:hover { |
|||
color: var(--tb-primary-700); |
|||
} |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
border-radius: 50%; |
|||
} |
|||
|
|||
.mat-icon { |
|||
color: inherit; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-empty-note { |
|||
padding: 4px 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-dashboards-picker { |
|||
position: relative; |
|||
} |
|||
|
|||
.tb-dashboards-picker-add { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 8px; |
|||
box-sizing: border-box; |
|||
width: 100%; |
|||
height: 40px; |
|||
margin: 0; |
|||
padding: 0 12px; |
|||
border: 1px dashed rgba(0, 0, 0, 0.2); |
|||
border-radius: 8px; |
|||
background: none; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
text-align: left; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
cursor: pointer; |
|||
transition: border-color .15s ease, color .15s ease, background-color .15s ease; |
|||
|
|||
.mat-icon { |
|||
width: 18px; |
|||
height: 18px; |
|||
font-size: 18px; |
|||
color: inherit; |
|||
} |
|||
|
|||
&:hover { |
|||
border-color: var(--tb-primary-500); |
|||
background: color-mix(in srgb, var(--tb-primary-500) 4%, #fff); |
|||
color: var(--tb-primary-500); |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
} |
|||
|
|||
&:disabled { |
|||
border-style: solid; |
|||
border-color: rgba(0, 0, 0, 0.06); |
|||
background: none; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
cursor: default; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-search { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 8px; |
|||
box-sizing: border-box; |
|||
height: 40px; |
|||
padding: 0 4px 0 12px; |
|||
border-radius: 8px; |
|||
background: rgba(0, 0, 0, 0.04); |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
cursor: text; |
|||
|
|||
&:focus-within { |
|||
background: #fff; |
|||
box-shadow: 0 0 0 2px var(--tb-primary-500), 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 4px rgba(16, 24, 40, 0.04); |
|||
color: var(--tb-primary-500); |
|||
} |
|||
|
|||
> .mat-icon { |
|||
flex: none; |
|||
width: 20px; |
|||
height: 20px; |
|||
font-size: 20px; |
|||
color: inherit; |
|||
} |
|||
|
|||
input { |
|||
flex: 1; |
|||
min-width: 0; |
|||
border: 0; |
|||
outline: 0; |
|||
background: transparent; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
caret-color: var(--tb-primary-500); |
|||
|
|||
&::placeholder { |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-search-close.mat-mdc-icon-button { |
|||
--mdc-icon-button-state-layer-size: 32px; |
|||
--mat-icon-button-state-layer-size: 32px; |
|||
display: inline-flex; |
|||
align-items: center; |
|||
justify-content: center; |
|||
width: 32px; |
|||
height: 32px; |
|||
padding: 0; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
|
|||
.mat-icon { |
|||
width: 20px; |
|||
height: 20px; |
|||
font-size: 20px; |
|||
} |
|||
|
|||
.mat-mdc-button-touch-target { |
|||
width: 32px; |
|||
height: 32px; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-options { |
|||
position: absolute; |
|||
top: calc(100% + 4px); |
|||
left: 0; |
|||
right: 0; |
|||
z-index: 5; |
|||
display: flex; |
|||
flex-direction: column; |
|||
max-height: 248px; |
|||
overflow-y: auto; |
|||
padding: 4px; |
|||
border-radius: 8px; |
|||
background: #fff; |
|||
box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14), 0 1px 3px rgba(0, 0, 0, 0.08); |
|||
|
|||
&:empty { |
|||
display: none; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-option { |
|||
display: flex; |
|||
align-items: center; |
|||
gap: 10px; |
|||
min-height: 40px; |
|||
padding: 0 10px; |
|||
border-radius: 6px; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
cursor: pointer; |
|||
|
|||
.mat-icon { |
|||
flex: none; |
|||
width: 18px; |
|||
height: 18px; |
|||
font-size: 18px; |
|||
color: var(--tb-ink-38, rgba(0, 0, 0, 0.38)); |
|||
} |
|||
|
|||
small { |
|||
font-size: 12px; |
|||
white-space: nowrap; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
} |
|||
|
|||
&.active { |
|||
background: rgba(0, 0, 0, 0.04); |
|||
|
|||
.mat-icon { |
|||
color: var(--tb-primary-500); |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-option-label { |
|||
flex: 1; |
|||
min-width: 0; |
|||
overflow: hidden; |
|||
white-space: nowrap; |
|||
text-overflow: ellipsis; |
|||
} |
|||
|
|||
.tb-dashboards-options-empty { |
|||
padding: 10px; |
|||
font-size: 13px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-dashboards-empty { |
|||
display: flex; |
|||
flex-direction: column; |
|||
align-items: center; |
|||
justify-content: center; |
|||
gap: 8px; |
|||
padding: 8px 16px 12px; |
|||
text-align: center; |
|||
} |
|||
|
|||
.tb-dashboards-empty-glyph { |
|||
display: inline-flex; |
|||
flex: none; |
|||
align-items: center; |
|||
justify-content: center; |
|||
width: 44px; |
|||
height: 44px; |
|||
border-radius: 50%; |
|||
background: color-mix(in srgb, var(--tb-primary-500) 10%, transparent); |
|||
color: var(--tb-primary-500); |
|||
|
|||
.mat-icon { |
|||
width: 20px; |
|||
height: 20px; |
|||
font-size: 20px; |
|||
color: inherit; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-empty-text { |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 4px; |
|||
min-width: 0; |
|||
|
|||
p { |
|||
max-width: 36em; |
|||
margin: 0; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-empty-title { |
|||
font: 500 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.25px; |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-dashboards-empty-actions { |
|||
display: flex; |
|||
flex-wrap: wrap; |
|||
align-items: center; |
|||
justify-content: center; |
|||
gap: 4px 16px; |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
|
|||
a { |
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
border-radius: 4px; |
|||
text-decoration: underline; |
|||
text-decoration-thickness: 1px; |
|||
text-underline-offset: 3px; |
|||
text-decoration-color: transparent; |
|||
color: var(--tb-primary-500); |
|||
transition: text-decoration-color .15s ease; |
|||
|
|||
&:hover { |
|||
text-decoration-color: currentColor; |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: 2px; |
|||
} |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-ext { |
|||
width: 14px; |
|||
height: 14px; |
|||
margin-left: 2px; |
|||
font-size: 14px; |
|||
line-height: 14px; |
|||
vertical-align: -2px; |
|||
opacity: .7; |
|||
} |
|||
|
|||
.tb-dashboards-sr { |
|||
position: absolute; |
|||
width: 1px; |
|||
height: 1px; |
|||
overflow: hidden; |
|||
clip: rect(0 0 0 0); |
|||
white-space: nowrap; |
|||
} |
|||
|
|||
@container tb-dashboards-home (max-width: 459px) { |
|||
.tb-dashboards-add.mat-mdc-button { |
|||
min-width: 40px; |
|||
padding: 0 8px; |
|||
|
|||
.mat-icon { |
|||
margin: 0; |
|||
} |
|||
} |
|||
|
|||
.tb-dashboards-add-label { |
|||
position: absolute; |
|||
width: 1px; |
|||
height: 1px; |
|||
overflow: hidden; |
|||
clip: rect(0 0 0 0); |
|||
white-space: nowrap; |
|||
} |
|||
} |
|||
@ -1,16 +1,283 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component } from '@angular/core'; |
|||
import { HomeCardId } from '@shared/models/user-settings.models'; |
|||
import { ChangeDetectionStrategy, Component, computed, DestroyRef, ElementRef, inject, signal, viewChild } from '@angular/core'; |
|||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|||
import { Router } from '@angular/router'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
import { of, Subject } from 'rxjs'; |
|||
import { catchError, debounceTime, map, switchMap } from 'rxjs/operators'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { getCurrentAuthState, getCurrentAuthUser } from '@core/auth/auth.selectors'; |
|||
import { UserSettingsService } from '@core/http/user-settings.service'; |
|||
import { DashboardService } from '@core/http/dashboard.service'; |
|||
import { UserPermissionsService } from '@core/http/user-permissions.service'; |
|||
import { ToastNotificationService } from '@core/services/toast-notification.service'; |
|||
import { Authority } from '@shared/models/authority.enum'; |
|||
import { EntityType } from '@shared/models/entity-type.models'; |
|||
import { Operation, Resource } from '@shared/models/security.models'; |
|||
import { HelpLinks } from '@shared/models/constants'; |
|||
import { PageLink } from '@shared/models/page/page-link'; |
|||
import { Direction } from '@shared/models/page/sort-order'; |
|||
import { HomeCardId, UserDashboardAction, UserDashboardsInfo } from '@shared/models/user-settings.models'; |
|||
import { HOME_CARDS } from '@home/pages/home-links/home/home-cards.models'; |
|||
import { homeAgo, homeListShown } from '@home/pages/home-links/home/home-list.models'; |
|||
import { HomeToggleOption } from '@home/pages/home-links/home/components/home-toggle.component'; |
|||
|
|||
type DashboardsTab = 'last' | 'starred'; |
|||
|
|||
const MINUTE = 60000; |
|||
const SEARCH_SIZE = 10; |
|||
const FILL_SIZE = 20; |
|||
|
|||
export interface DashboardHomeRow { |
|||
id: string; |
|||
title: string; |
|||
starred: boolean; |
|||
lastVisited: number | null; |
|||
ago: string; |
|||
url: string; |
|||
} |
|||
|
|||
interface StarCandidate { |
|||
id: string; |
|||
title: string; |
|||
ago: string | null; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-dashboards-home', |
|||
template: `<tb-home-card [card]="card"></tb-home-card>`, |
|||
templateUrl: './dashboards-home.component.html', |
|||
styleUrls: ['./dashboards-home.component.scss'], |
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class DashboardsHomeComponent { |
|||
|
|||
readonly card = HOME_CARDS[HomeCardId.DASHBOARDS]; |
|||
|
|||
readonly tabOptions: HomeToggleOption<DashboardsTab>[] = [ |
|||
{value: 'last', label: 'home.dashboards.last-viewed'}, |
|||
{value: 'starred', label: 'home.dashboards.starred'} |
|||
]; |
|||
|
|||
readonly howToLink = HelpLinks.linksMap.dashboards; |
|||
|
|||
private store = inject(Store<AppState>); |
|||
private router = inject(Router); |
|||
private translate = inject(TranslateService); |
|||
private userSettingsService = inject(UserSettingsService); |
|||
private dashboardService = inject(DashboardService); |
|||
private userPermissions = inject(UserPermissionsService); |
|||
private toast = inject(ToastNotificationService); |
|||
|
|||
private readonly authUser = getCurrentAuthUser(this.store); |
|||
private readonly fullscreen = getCurrentAuthState(this.store).forceFullscreen; |
|||
|
|||
readonly tenantAdmin = this.authUser?.authority === Authority.TENANT_ADMIN; |
|||
readonly canAdd = this.tenantAdmin && this.userPermissions.hasGenericPermission(Resource.DASHBOARD, Operation.CREATE); |
|||
readonly dashboardsLink = this.userPermissions.hasReadGenericPermission(Resource.DASHBOARD) ? '/dashboards/all' |
|||
: this.userPermissions.hasSharedReadGroupsPermission(EntityType.DASHBOARD) ? '/dashboards/shared' : null; |
|||
|
|||
readonly tab = signal<DashboardsTab>('last'); |
|||
readonly expanded = signal(false); |
|||
readonly total = signal<number | null>(null); |
|||
private readonly info = signal<UserDashboardsInfo | null>(null); |
|||
private readonly others = signal<{id: string; title: string}[]>([]); |
|||
private readonly now = signal(Date.now()); |
|||
|
|||
readonly picking = signal(false); |
|||
readonly query = signal(''); |
|||
readonly active = signal(0); |
|||
private readonly searchResults = signal<StarCandidate[]>([]); |
|||
private readonly search$ = new Subject<string>(); |
|||
|
|||
private readonly searchInput = viewChild<ElementRef<HTMLInputElement>>('searchInput'); |
|||
|
|||
readonly loaded = computed(() => this.total() !== null && this.info() !== null); |
|||
|
|||
private readonly starredIds = computed(() => new Set((this.info()?.starred ?? []).map(dashboard => dashboard.id))); |
|||
|
|||
private readonly visits = computed(() => new Map((this.info()?.last ?? []).map(dashboard => [dashboard.id, dashboard.lastVisited]))); |
|||
|
|||
private readonly allRows = computed<DashboardHomeRow[]>(() => { |
|||
const info = this.info(); |
|||
if (!info) { |
|||
return []; |
|||
} |
|||
const now = this.now(); |
|||
const starred = this.starredIds(); |
|||
const visits = this.visits(); |
|||
if (this.tab() === 'last') { |
|||
const viewed = [...info.last] |
|||
.sort((a, b) => b.lastVisited - a.lastVisited) |
|||
.map(dashboard => this.toRow(dashboard.id, dashboard.title, starred.has(dashboard.id), dashboard.lastVisited, now)); |
|||
const rest = this.others() |
|||
.filter(dashboard => !visits.has(dashboard.id)) |
|||
.map(dashboard => this.toRow(dashboard.id, dashboard.title, starred.has(dashboard.id), null, now)); |
|||
return [...viewed, ...rest]; |
|||
} |
|||
return [...info.starred] |
|||
.sort((a, b) => (visits.get(b.id) ?? 0) - (visits.get(a.id) ?? 0) || b.starredAt - a.starredAt) |
|||
.map(dashboard => this.toRow(dashboard.id, dashboard.title, true, visits.get(dashboard.id) ?? null, now)); |
|||
}); |
|||
|
|||
readonly listed = computed(() => this.tab() === 'last' ? Math.max(this.total() ?? 0, this.allRows().length) : this.allRows().length); |
|||
|
|||
readonly rows = computed(() => this.allRows().slice(0, homeListShown(this.listed(), this.expanded()))); |
|||
|
|||
readonly allStarred = computed(() => { |
|||
const total = this.total(); |
|||
return total !== null && total > 0 && this.starredIds().size >= total; |
|||
}); |
|||
|
|||
readonly candidates = computed<StarCandidate[]>(() => { |
|||
const starred = this.starredIds(); |
|||
if (this.query().trim()) { |
|||
return this.searchResults().filter(candidate => !starred.has(candidate.id)); |
|||
} |
|||
const now = this.now(); |
|||
return [...(this.info()?.last ?? [])] |
|||
.filter(dashboard => !starred.has(dashboard.id)) |
|||
.sort((a, b) => b.lastVisited - a.lastVisited) |
|||
.slice(0, SEARCH_SIZE) |
|||
.map(dashboard => ({id: dashboard.id, title: dashboard.title, ago: homeAgo(this.translate, now - dashboard.lastVisited)})); |
|||
}); |
|||
|
|||
constructor() { |
|||
if (this.dashboardsLink && this.authUser?.authority !== Authority.SYS_ADMIN) { |
|||
this.userSettingsService.getUserDashboardsInfo({ignoreLoading: true}).subscribe(info => { |
|||
this.info.set({last: info?.last ?? [], starred: info?.starred ?? []}); |
|||
}); |
|||
this.dashboardService.getUserDashboards(this.authUser.userId, null, |
|||
new PageLink(FILL_SIZE, 0, null, {property: 'createdTime', direction: Direction.DESC}), {ignoreLoading: true}).pipe( |
|||
catchError(() => of(null)) |
|||
).subscribe(page => { |
|||
this.others.set((page?.data ?? []).map(dashboard => ({id: dashboard.id.id, title: dashboard.title}))); |
|||
this.total.set(page?.totalElements ?? 0); |
|||
}); |
|||
} else { |
|||
this.info.set({last: [], starred: []}); |
|||
this.total.set(0); |
|||
} |
|||
this.search$.pipe( |
|||
debounceTime(200), |
|||
switchMap(query => query |
|||
? this.dashboardService.getUserDashboards(this.authUser.userId, null, |
|||
new PageLink(SEARCH_SIZE + this.starredIds().size, 0, query, {property: 'title', direction: Direction.ASC}), {ignoreLoading: true}).pipe( |
|||
map(page => page.data.map(dashboard => { |
|||
const visited = this.visits().get(dashboard.id.id); |
|||
return {id: dashboard.id.id, title: dashboard.title, ago: visited ? homeAgo(this.translate, Date.now() - visited) : null}; |
|||
})), |
|||
catchError(() => of([])) |
|||
) |
|||
: of([])), |
|||
takeUntilDestroyed() |
|||
).subscribe(results => { |
|||
this.searchResults.set(results); |
|||
this.active.set(0); |
|||
}); |
|||
const tick = setInterval(() => this.now.set(Date.now()), MINUTE); |
|||
inject(DestroyRef).onDestroy(() => clearInterval(tick)); |
|||
} |
|||
|
|||
setTab(tab: DashboardsTab): void { |
|||
this.tab.set(tab); |
|||
this.closePicker(); |
|||
} |
|||
|
|||
open(row: DashboardHomeRow): void { |
|||
this.router.navigateByUrl(row.url); |
|||
} |
|||
|
|||
toggleStar(row: DashboardHomeRow, $event: Event): void { |
|||
$event.stopPropagation(); |
|||
const info = this.info(); |
|||
if (!info) { |
|||
return; |
|||
} |
|||
if (row.starred) { |
|||
this.info.set({...info, starred: info.starred.filter(dashboard => dashboard.id !== row.id)}); |
|||
this.userSettingsService.reportUserDashboardAction(row.id, UserDashboardAction.UNSTAR, {ignoreLoading: true}).subscribe(); |
|||
} else { |
|||
this.star(row.id, row.title); |
|||
} |
|||
} |
|||
|
|||
openPicker(): void { |
|||
this.query.set(''); |
|||
this.searchResults.set([]); |
|||
this.active.set(0); |
|||
this.picking.set(true); |
|||
setTimeout(() => this.searchInput()?.nativeElement.focus()); |
|||
} |
|||
|
|||
closePicker(): void { |
|||
this.picking.set(false); |
|||
} |
|||
|
|||
onQuery(value: string): void { |
|||
this.query.set(value); |
|||
this.active.set(0); |
|||
this.search$.next(value.trim()); |
|||
} |
|||
|
|||
onPickerKeydown(event: KeyboardEvent): void { |
|||
const count = this.candidates().length; |
|||
if (event.key === 'ArrowDown' && count) { |
|||
event.preventDefault(); |
|||
this.active.set((this.active() + 1) % count); |
|||
} else if (event.key === 'ArrowUp' && count) { |
|||
event.preventDefault(); |
|||
this.active.set((this.active() - 1 + count) % count); |
|||
} else if (event.key === 'Enter') { |
|||
event.preventDefault(); |
|||
const candidate = this.candidates()[this.active()]; |
|||
if (candidate) { |
|||
this.pick(candidate); |
|||
} |
|||
} else if (event.key === 'Escape') { |
|||
event.preventDefault(); |
|||
this.closePicker(); |
|||
} |
|||
} |
|||
|
|||
onPickerBlur(): void { |
|||
if (document.hasFocus()) { |
|||
this.closePicker(); |
|||
} |
|||
} |
|||
|
|||
pick(candidate: StarCandidate): void { |
|||
this.closePicker(); |
|||
this.star(candidate.id, candidate.title); |
|||
this.toast.dispatchNotification({ |
|||
message: this.translate.instant('home.dashboards.starred-toast', {title: candidate.title}), |
|||
type: 'info', |
|||
duration: 4000, |
|||
horizontalPosition: 'right', |
|||
verticalPosition: 'bottom', |
|||
panelClass: 'tb-home-toast' |
|||
}); |
|||
} |
|||
|
|||
private star(id: string, title: string): void { |
|||
const info = this.info(); |
|||
if (!info || this.starredIds().has(id)) { |
|||
return; |
|||
} |
|||
this.info.set({...info, starred: [...info.starred, {id, title, starred: true, starredAt: Date.now()}]}); |
|||
this.userSettingsService.reportUserDashboardAction(id, UserDashboardAction.STAR, {ignoreLoading: true}).subscribe(); |
|||
} |
|||
|
|||
private toRow(id: string, title: string, starred: boolean, lastVisited: number | null, now: number): DashboardHomeRow { |
|||
return { |
|||
id, |
|||
title, |
|||
starred, |
|||
lastVisited, |
|||
ago: lastVisited ? homeAgo(this.translate, now - lastVisited) : '', |
|||
url: (this.fullscreen ? '/dashboard/' : '/dashboards/all/') + id |
|||
}; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,66 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
:host { |
|||
display: block; |
|||
} |
|||
|
|||
.tb-home-list-footer { |
|||
display: flex; |
|||
flex-wrap: wrap; |
|||
align-items: center; |
|||
gap: 8px; |
|||
min-height: 36px; |
|||
padding-top: 8px; |
|||
border-top: 1px solid rgba(0, 0, 0, 0.05); |
|||
font: 400 14px/20px var(--tb-font, Roboto, sans-serif); |
|||
color: var(--tb-text-secondary, rgba(0, 0, 0, 0.6)); |
|||
} |
|||
|
|||
.tb-home-list-toggle.mat-mdc-button { |
|||
margin-left: -8px; |
|||
color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); |
|||
} |
|||
|
|||
.tb-home-list-spacer { |
|||
flex: 1 1 auto; |
|||
} |
|||
|
|||
.tb-home-list-count { |
|||
font-variant-numeric: tabular-nums; |
|||
} |
|||
|
|||
.tb-home-list-view-all { |
|||
&, &:hover, &:focus, &:active { |
|||
border-bottom: none; |
|||
} |
|||
|
|||
display: inline-flex; |
|||
align-items: center; |
|||
height: 36px; |
|||
margin-right: -8px; |
|||
padding: 0 4px 0 8px; |
|||
border-radius: 4px; |
|||
font-weight: 500; |
|||
font-size: 14px; |
|||
letter-spacing: 0.03125em; |
|||
white-space: nowrap; |
|||
text-decoration: none; |
|||
color: var(--tb-ink-76, rgba(0, 0, 0, 0.76)); |
|||
transition: background-color .15s ease; |
|||
|
|||
.mat-icon { |
|||
width: 18px; |
|||
height: 18px; |
|||
font-size: 18px; |
|||
} |
|||
|
|||
&:hover { |
|||
background: rgba(0, 0, 0, 0.04); |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: -2px; |
|||
} |
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component, computed, input, output } from '@angular/core'; |
|||
import { Params } from '@angular/router'; |
|||
import { HOME_LIST_LIMIT, homeListFew, homeListShown } from '@home/pages/home-links/home/home-list.models'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-home-list-footer', |
|||
template: ` |
|||
@if (!few() || (total() && link())) { |
|||
<div class="tb-home-list-footer"> |
|||
@if (!few()) { |
|||
<button mat-button type="button" class="tb-home-list-toggle" (click)="expandedChange.emit(!expanded())"> |
|||
<mat-icon>{{ expanded() ? 'expand_less' : 'expand_more' }}</mat-icon> |
|||
{{ toggleLabel() | translate }} |
|||
</button> |
|||
} |
|||
<span class="tb-home-list-spacer"></span> |
|||
@if (shown() < total()) { |
|||
<span class="tb-home-list-count">{{ 'home.list.shown-of' | translate: {shown: shown(), total: (total() | shortNumber)} }}</span> |
|||
} |
|||
@if (total() && link()) { |
|||
<a class="tb-home-list-view-all" [routerLink]="link()" [queryParams]="params()">{{ 'home.list.view-all' | translate }}<mat-icon>chevron_right</mat-icon></a> |
|||
} |
|||
</div> |
|||
} |
|||
`,
|
|||
styleUrls: ['./home-list-footer.component.scss'], |
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class HomeListFooterComponent { |
|||
|
|||
total = input(0); |
|||
|
|||
expanded = input(false); |
|||
|
|||
link = input<string | null>(null); |
|||
|
|||
params = input<Params | null>(null); |
|||
|
|||
expandedChange = output<boolean>(); |
|||
|
|||
readonly few = computed(() => homeListFew(this.total())); |
|||
|
|||
readonly shown = computed(() => homeListShown(this.total(), this.expanded())); |
|||
|
|||
readonly toggleLabel = computed(() => { |
|||
if (this.expanded()) { |
|||
return 'home.list.show-less'; |
|||
} |
|||
return this.total() <= HOME_LIST_LIMIT ? 'home.list.show-all' : 'home.list.show-more'; |
|||
}); |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc. |
|||
// SPDX-License-Identifier: BUSL-1.1 |
|||
:host { |
|||
display: inline-flex; |
|||
max-width: 100%; |
|||
} |
|||
|
|||
.tb-home-toggle { |
|||
display: inline-flex; |
|||
box-sizing: border-box; |
|||
height: 32px; |
|||
max-width: 100%; |
|||
padding: 2px; |
|||
border-radius: 100px; |
|||
background: rgba(0, 0, 0, 0.06); |
|||
} |
|||
|
|||
.tb-home-toggle-option { |
|||
flex: 1 1 auto; |
|||
height: 28px; |
|||
margin: 0; |
|||
padding: 0 12px; |
|||
border: 1px solid transparent; |
|||
border-radius: 20px; |
|||
background: none; |
|||
font: 400 14px/26px var(--tb-font, Roboto, sans-serif); |
|||
letter-spacing: 0.2px; |
|||
white-space: nowrap; |
|||
color: var(--tb-text-muted, rgba(0, 0, 0, 0.56)); |
|||
cursor: pointer; |
|||
|
|||
&[aria-pressed="true"] { |
|||
background: #fff; |
|||
box-shadow: 0 1px 2px rgba(16, 24, 40, 0.10), 0 1px 4px rgba(16, 24, 40, 0.06); |
|||
font-weight: 500; |
|||
line-height: 24px; |
|||
color: var(--tb-ink, rgba(0, 0, 0, 0.87)); |
|||
} |
|||
|
|||
&:focus-visible { |
|||
outline: 2px solid var(--tb-primary-500); |
|||
outline-offset: -2px; |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { ChangeDetectionStrategy, Component, input, output } from '@angular/core'; |
|||
|
|||
export interface HomeToggleOption<T> { |
|||
value: T; |
|||
label: string; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-home-toggle', |
|||
template: ` |
|||
<div class="tb-home-toggle" role="group" [attr.aria-label]="label()"> |
|||
@for (option of options(); track option.value) { |
|||
<button type="button" class="tb-home-toggle-option" [attr.aria-pressed]="option.value === value()" |
|||
(click)="option.value !== value() && valueChange.emit(option.value)">{{ option.label | translate }}</button> |
|||
} |
|||
</div> |
|||
`,
|
|||
styleUrls: ['./home-toggle.component.scss'], |
|||
standalone: false, |
|||
changeDetection: ChangeDetectionStrategy.OnPush |
|||
}) |
|||
export class HomeToggleComponent<T = string> { |
|||
|
|||
options = input.required<HomeToggleOption<T>[]>(); |
|||
|
|||
value = input.required<T>(); |
|||
|
|||
label = input<string>(); |
|||
|
|||
valueChange = output<T>(); |
|||
} |
|||
@ -0,0 +1,37 @@ |
|||
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
|
|||
// SPDX-License-Identifier: BUSL-1.1
|
|||
import { TranslateService } from '@ngx-translate/core'; |
|||
|
|||
export const HOME_LIST_PREVIEW = 5; |
|||
export const HOME_LIST_SLACK = 2; |
|||
export const HOME_LIST_LIMIT = 10; |
|||
|
|||
const MINUTE = 60000; |
|||
|
|||
export const homeListPageSize = (expanded: boolean): number => expanded ? HOME_LIST_LIMIT : HOME_LIST_PREVIEW + HOME_LIST_SLACK; |
|||
|
|||
export const homeListFew = (total: number): boolean => total <= HOME_LIST_PREVIEW + HOME_LIST_SLACK; |
|||
|
|||
export const homeListShown = (total: number, expanded: boolean): number => { |
|||
if (homeListFew(total)) { |
|||
return total; |
|||
} |
|||
return expanded ? Math.min(total, HOME_LIST_LIMIT) : HOME_LIST_PREVIEW; |
|||
}; |
|||
|
|||
export const homeAgo = (translate: TranslateService, ms: number): string => { |
|||
const min = ms / MINUTE; |
|||
if (min < 1) { |
|||
return translate.instant('home.ago.now'); |
|||
} |
|||
if (min < 60) { |
|||
return translate.instant('home.ago.min', {count: Math.round(min)}); |
|||
} |
|||
if (min < 24 * 60) { |
|||
return translate.instant('home.ago.hour', {count: Math.floor(min / 60)}); |
|||
} |
|||
if (min < 7 * 24 * 60) { |
|||
return translate.instant('home.ago.day', {count: Math.floor(min / (24 * 60))}); |
|||
} |
|||
return translate.instant('home.ago.week', {count: Math.floor(min / (7 * 24 * 60))}); |
|||
}; |
|||
Loading…
Reference in new issue