24 changed files with 821 additions and 53 deletions
@ -0,0 +1,65 @@ |
|||
<!-- |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<form #saveWidgetTypeAsForm="ngForm" |
|||
[formGroup]="saveWidgetTypeAsFormGroup"(ngSubmit)="saveAs()"> |
|||
<mat-toolbar fxLayout="row" color="primary"> |
|||
<h2 translate>widget.save-widget-type-as</h2> |
|||
<span fxFlex></span> |
|||
<button mat-button mat-icon-button |
|||
(click)="cancel()" |
|||
type="button"> |
|||
<mat-icon class="material-icons">close</mat-icon> |
|||
</button> |
|||
</mat-toolbar> |
|||
<mat-progress-bar color="warn" mode="indeterminate" *ngIf="isLoading$ | async"> |
|||
</mat-progress-bar> |
|||
<div style="height: 4px;" *ngIf="!(isLoading$ | async)"></div> |
|||
<div mat-dialog-content> |
|||
<fieldset> |
|||
<span translate>widget.save-widget-type-as-text</span> |
|||
<mat-form-field class="mat-block"> |
|||
<mat-label translate>widget.title</mat-label> |
|||
<input matInput formControlName="title" required> |
|||
<mat-error *ngIf="saveWidgetTypeAsFormGroup.get('title').hasError('required')"> |
|||
{{ 'widget.title-required' | translate }} |
|||
</mat-error> |
|||
</mat-form-field> |
|||
<tb-widgets-bundle-select fxFlex |
|||
formControlName="widgetsBundle" |
|||
required |
|||
bundlesScope="{{bundlesScope}}"> |
|||
</tb-widgets-bundle-select> |
|||
</fieldset> |
|||
</div> |
|||
<div mat-dialog-actions fxLayout="row"> |
|||
<span fxFlex></span> |
|||
<button mat-button mat-raised-button color="primary" |
|||
type="submit" |
|||
[disabled]="(isLoading$ | async) || saveWidgetTypeAsForm.invalid |
|||
|| !saveWidgetTypeAsForm.dirty"> |
|||
{{ 'action.saveAs' | translate }} |
|||
</button> |
|||
<button mat-button color="primary" |
|||
style="margin-right: 20px;" |
|||
type="button" |
|||
[disabled]="(isLoading$ | async)" |
|||
(click)="cancel()" cdkFocusInitial> |
|||
{{ 'action.cancel' | translate }} |
|||
</button> |
|||
</div> |
|||
</form> |
|||
@ -0,0 +1,81 @@ |
|||
///
|
|||
/// 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 { Component, OnInit } from '@angular/core'; |
|||
import { MatDialogRef } from '@angular/material'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@core/core.state'; |
|||
import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
|||
import { DialogComponent } from '@shared/components/dialog.component'; |
|||
import { Router } from '@angular/router'; |
|||
import { WidgetsBundle } from '@shared/models/widgets-bundle.model'; |
|||
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; |
|||
import { Authority } from '@shared/models/authority.enum'; |
|||
|
|||
export interface SaveWidgetTypeAsDialogResult { |
|||
widgetName: string; |
|||
bundleId: string; |
|||
bundleAlias: string; |
|||
} |
|||
|
|||
@Component({ |
|||
selector: 'tb-save-widget-type-as-dialog', |
|||
templateUrl: './save-widget-type-as-dialog.component.html', |
|||
styleUrls: [] |
|||
}) |
|||
export class SaveWidgetTypeAsDialogComponent extends |
|||
DialogComponent<SaveWidgetTypeAsDialogComponent, SaveWidgetTypeAsDialogResult> implements OnInit { |
|||
|
|||
saveWidgetTypeAsFormGroup: FormGroup; |
|||
|
|||
bundlesScope: string; |
|||
|
|||
constructor(protected store: Store<AppState>, |
|||
protected router: Router, |
|||
public dialogRef: MatDialogRef<SaveWidgetTypeAsDialogComponent, SaveWidgetTypeAsDialogResult>, |
|||
public fb: FormBuilder) { |
|||
super(store, router, dialogRef); |
|||
|
|||
const authUser = getCurrentAuthUser(store); |
|||
if (authUser.authority === Authority.TENANT_ADMIN) { |
|||
this.bundlesScope = 'tenant'; |
|||
} else { |
|||
this.bundlesScope = 'system'; |
|||
} |
|||
} |
|||
|
|||
ngOnInit(): void { |
|||
this.saveWidgetTypeAsFormGroup = this.fb.group({ |
|||
title: [null, [Validators.required]], |
|||
widgetsBundle: [null, [Validators.required]] |
|||
}); |
|||
} |
|||
|
|||
cancel(): void { |
|||
this.dialogRef.close(null); |
|||
} |
|||
|
|||
saveAs(): void { |
|||
const widgetName: string = this.saveWidgetTypeAsFormGroup.get('title').value; |
|||
const widgetsBundle: WidgetsBundle = this.saveWidgetTypeAsFormGroup.get('widgetsBundle').value; |
|||
const result: SaveWidgetTypeAsDialogResult = { |
|||
widgetName, |
|||
bundleId: widgetsBundle.id.id, |
|||
bundleAlias: widgetsBundle.alias |
|||
}; |
|||
this.dialogRef.close(result); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
<!-- |
|||
|
|||
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. |
|||
|
|||
--> |
|||
<mat-form-field floatLabel="always" hideRequiredMarker class="mat-block"> |
|||
<mat-label></mat-label> |
|||
<mat-select [required]="required" |
|||
[disabled]="disabled" |
|||
[(ngModel)]="widgetsBundle" |
|||
matInput |
|||
panelClass="tb-widgets-bundle-select" |
|||
placeholder="{{ 'widget.select-widgets-bundle' | translate }}" |
|||
(ngModelChange)="widgetsBundleChanged()"> |
|||
<mat-option *ngFor="let widgetsBundle of widgetsBundles$ | async" [value]="widgetsBundle"> |
|||
<div class="tb-bundle-item"> |
|||
<span>{{widgetsBundle.title}}</span> |
|||
<span translate class="tb-bundle-system" *ngIf="isSystem(item)">widgets-bundle.system</span> |
|||
</div> |
|||
</mat-option> |
|||
</mat-select> |
|||
</mat-form-field> |
|||
@ -0,0 +1,91 @@ |
|||
/** |
|||
* 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. |
|||
*/ |
|||
|
|||
tb-widgets-bundle-select { |
|||
mat-select { |
|||
margin: 0; |
|||
} |
|||
|
|||
.tb-bundle-item { |
|||
height: 24px; |
|||
line-height: 24px; |
|||
} |
|||
} |
|||
|
|||
.tb-widgets-bundle-select { |
|||
.tb-bundle-item { |
|||
height: 48px; |
|||
line-height: 48px; |
|||
} |
|||
} |
|||
|
|||
tb-widgets-bundle-select, |
|||
.tb-widgets-bundle-select { |
|||
.mat-select-value-text { |
|||
display: block; |
|||
width: 100%; |
|||
} |
|||
|
|||
.tb-bundle-item { |
|||
display: inline-block; |
|||
width: 100%; |
|||
|
|||
span { |
|||
display: inline-block; |
|||
vertical-align: middle; |
|||
} |
|||
|
|||
.tb-bundle-system { |
|||
float: right; |
|||
font-size: .8rem; |
|||
opacity: .8; |
|||
} |
|||
} |
|||
|
|||
mat-option { |
|||
height: auto !important; |
|||
white-space: normal !important; |
|||
} |
|||
} |
|||
|
|||
mat-toolbar { |
|||
tb-widgets-bundle-select { |
|||
mat-select { |
|||
background: rgba(255, 255, 255, .2); |
|||
padding: 5px 20px; |
|||
|
|||
.mat-select-value-text { |
|||
font-size: 1.2rem; |
|||
color: #fff; |
|||
|
|||
span:first-child::after { |
|||
color: #fff; |
|||
} |
|||
} |
|||
|
|||
.mat-select-value.mat-select-placeholder { |
|||
color: #fff; |
|||
opacity: .8; |
|||
} |
|||
} |
|||
|
|||
mat-select.ng-invalid.ng-touched { |
|||
.mat-select-value-text { |
|||
color: #fff !important; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,156 @@ |
|||
///
|
|||
/// 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 { Component, forwardRef, Input, OnChanges, OnInit, ViewEncapsulation, SimpleChanges } from '@angular/core'; |
|||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|||
import { Observable } from 'rxjs'; |
|||
import { share, tap } from 'rxjs/operators'; |
|||
import { Store } from '@ngrx/store'; |
|||
import { AppState } from '@app/core/core.state'; |
|||
import { coerceBooleanProperty } from '@angular/cdk/coercion'; |
|||
import { WidgetsBundle } from '@shared/models/widgets-bundle.model'; |
|||
import { WidgetService } from '@core/http/widget.service'; |
|||
import { isDefined } from '@core/utils'; |
|||
import { NULL_UUID } from '@shared/models/id/has-uuid'; |
|||
|
|||
@Component({ |
|||
selector: 'tb-widgets-bundle-select', |
|||
templateUrl: './widgets-bundle-select.component.html', |
|||
styleUrls: ['./widgets-bundle-select.component.scss'], |
|||
providers: [{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => WidgetsBundleSelectComponent), |
|||
multi: true |
|||
}], |
|||
encapsulation: ViewEncapsulation.None |
|||
}) |
|||
export class WidgetsBundleSelectComponent implements ControlValueAccessor, OnInit, OnChanges { |
|||
|
|||
@Input() |
|||
bundlesScope: 'system' | 'tenant'; |
|||
|
|||
@Input() |
|||
selectFirstBundle: boolean; |
|||
|
|||
@Input() |
|||
selectBundleAlias: string; |
|||
|
|||
private requiredValue: boolean; |
|||
get required(): boolean { |
|||
return this.requiredValue; |
|||
} |
|||
@Input() |
|||
set required(value: boolean) { |
|||
this.requiredValue = coerceBooleanProperty(value); |
|||
} |
|||
|
|||
@Input() |
|||
disabled: boolean; |
|||
|
|||
widgetsBundles$: Observable<Array<WidgetsBundle>>; |
|||
|
|||
widgetsBundles: Array<WidgetsBundle>; |
|||
|
|||
widgetsBundle: WidgetsBundle | null; |
|||
|
|||
private propagateChange = (v: any) => { }; |
|||
|
|||
constructor(private store: Store<AppState>, |
|||
private widgetService: WidgetService) { |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void { |
|||
} |
|||
|
|||
ngOnInit() { |
|||
this.widgetsBundles$ = this.getWidgetsBundles().pipe( |
|||
tap((widgetsBundles) => { |
|||
this.widgetsBundles = widgetsBundles; |
|||
if (this.selectFirstBundle) { |
|||
if (widgetsBundles.length > 0) { |
|||
if (this.widgetsBundle !== widgetsBundles[0]) { |
|||
this.widgetsBundle = widgetsBundles[0]; |
|||
this.updateView(); |
|||
} else if (isDefined(this.selectBundleAlias)) { |
|||
this.selectWidgetsBundleByAlias(this.selectBundleAlias); |
|||
} |
|||
} |
|||
} |
|||
}), |
|||
share() |
|||
); |
|||
} |
|||
|
|||
ngOnChanges(changes: SimpleChanges): void { |
|||
for (const propName of Object.keys(changes)) { |
|||
const change = changes[propName]; |
|||
if (!change.firstChange && change.currentValue !== change.previousValue) { |
|||
if (propName === 'selectBundleAlias') { |
|||
this.selectWidgetsBundleByAlias(this.selectBundleAlias); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
setDisabledState(isDisabled: boolean): void { |
|||
this.disabled = isDisabled; |
|||
} |
|||
|
|||
writeValue(value: WidgetsBundle | null): void { |
|||
this.widgetsBundle = value; |
|||
} |
|||
|
|||
widgetsBundleChanged() { |
|||
this.updateView(); |
|||
} |
|||
|
|||
isSystem(item: WidgetsBundle) { |
|||
return item && item.tenantId.id === NULL_UUID; |
|||
} |
|||
|
|||
private selectWidgetsBundleByAlias(alias: string) { |
|||
if (this.widgetsBundles && alias) { |
|||
const found = this.widgetsBundles.find((widgetsBundle) => widgetsBundle.alias === alias); |
|||
if (found && this.widgetsBundle !== found) { |
|||
this.widgetsBundle = found; |
|||
this.updateView(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private updateView() { |
|||
this.propagateChange(this.widgetsBundle); |
|||
} |
|||
|
|||
private getWidgetsBundles(): Observable<Array<WidgetsBundle>> { |
|||
let widgetsBundlesObservable: Observable<Array<WidgetsBundle>>; |
|||
if (this.bundlesScope) { |
|||
if (this.bundlesScope === 'system') { |
|||
widgetsBundlesObservable = this.widgetService.getSystemWidgetsBundles(); |
|||
} else if (this.bundlesScope === 'tenant') { |
|||
widgetsBundlesObservable = this.widgetService.getTenantWidgetsBundles(); |
|||
} |
|||
} else { |
|||
widgetsBundlesObservable = this.widgetService.getAllWidgetsBundles(); |
|||
} |
|||
return widgetsBundlesObservable; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue