Commit d08e6dba5d42913fccd4ca67b2e00eea3d5ecfd6

Authored by Vladyslav Prykhodko
1 parent 894a1df1

UI: Added global scope variables in javascript functions

... ... @@ -68,6 +68,10 @@ export class WidgetService {
68 68 );
69 69 }
70 70
  71 + public getWidgetScopeVariables(): string[] {
  72 + return ['tinycolor', 'cssjs', 'moment', '$', 'jQuery'];
  73 + }
  74 +
71 75 public getAllWidgetsBundles(config?: RequestConfig): Observable<Array<WidgetsBundle>> {
72 76 return this.loadWidgetsBundleCache(config).pipe(
73 77 map(() => this.allWidgetsBundles)
... ... @@ -311,5 +315,4 @@ export class WidgetService {
311 315 this.loadWidgetsBundleCacheSubject = undefined;
312 316 this.widgetTypeInfosCache.clear();
313 317 }
314   -
315 318 }
... ...
... ... @@ -44,6 +44,7 @@
44 44 (ngModelChange)="onActionUpdated()"
45 45 [fillHeight]="true"
46 46 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']"
  47 + [globalVariables]="functionScopeVariables"
47 48 [validationArgs]="[]"
48 49 [editorCompleter]="customPrettyActionEditorCompleter">
49 50 </tb-js-func>
... ...
... ... @@ -36,6 +36,7 @@ import { AppState } from '@core/core.state';
36 36 import { combineLatest } from 'rxjs';
37 37 import { CustomActionDescriptor } from '@shared/models/widget.models';
38 38 import { CustomPrettyActionEditorCompleter } from '@home/components/widget/action/custom-action.models';
  39 +import { WidgetService } from "@core/http/widget.service";
39 40
40 41 @Component({
41 42 selector: 'tb-custom-action-pretty-editor',
... ... @@ -58,6 +59,8 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
58 59
59 60 fullscreen = false;
60 61
  62 + functionScopeVariables: string[];
  63 +
61 64 @ViewChildren('leftPanel')
62 65 leftPanelElmRef: QueryList<ElementRef<HTMLElement>>;
63 66
... ... @@ -68,8 +71,10 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
68 71
69 72 private propagateChange = (_: any) => {};
70 73
71   - constructor(protected store: Store<AppState>) {
  74 + constructor(protected store: Store<AppState>,
  75 + private widgetService: WidgetService) {
72 76 super(store);
  77 + this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
73 78 }
74 79
75 80 ngOnInit(): void {
... ...
... ... @@ -94,6 +94,7 @@
94 94 [(ngModel)]="action.customFunction"
95 95 (ngModelChange)="notifyActionUpdated()"
96 96 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']"
  97 + [globalVariables]="functionScopeVariables"
97 98 [validationArgs]="[]"
98 99 [editorCompleter]="customPrettyActionEditorCompleter">
99 100 </tb-js-func>
... ...
... ... @@ -41,6 +41,7 @@ import { forkJoin, from } from 'rxjs';
41 41 import { map, tap } from 'rxjs/operators';
42 42 import { getAce } from '@shared/models/ace/ace.models';
43 43 import { beautifyCss, beautifyHtml } from '@shared/models/beautify.models';
  44 +import { WidgetService } from "@core/http/widget.service";
44 45
45 46 @Component({
46 47 selector: 'tb-custom-action-pretty-resources-tabs',
... ... @@ -75,12 +76,16 @@ export class CustomActionPrettyResourcesTabsComponent extends PageComponent impl
75 76 cssEditor: Ace.Editor;
76 77 setValuesPending = false;
77 78
  79 + functionScopeVariables: string[];
  80 +
78 81 customPrettyActionEditorCompleter = CustomPrettyActionEditorCompleter;
79 82
80 83 constructor(protected store: Store<AppState>,
81 84 private translate: TranslateService,
  85 + private widgetService: WidgetService,
82 86 private raf: RafService) {
83 87 super(store);
  88 + this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
84 89 }
85 90
86 91 ngOnInit(): void {
... ...
... ... @@ -35,6 +35,7 @@
35 35 formControlName="getLocationFunction"
36 36 functionName="getLocation"
37 37 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  38 + [globalVariables]="functionScopeVariables"
38 39 [editorCompleter]="customActionEditorCompleter"
39 40 ></tb-js-func>
40 41 </ng-template>
... ... @@ -43,6 +44,7 @@
43 44 formControlName="getPhoneNumberFunction"
44 45 functionName="getPhoneNumber"
45 46 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  47 + [globalVariables]="functionScopeVariables"
46 48 [editorCompleter]="customActionEditorCompleter"
47 49 ></tb-js-func>
48 50 </ng-template>
... ... @@ -54,6 +56,7 @@
54 56 formControlName="processImageFunction"
55 57 functionName="processImage"
56 58 [functionArgs]="['imageUrl', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  59 + [globalVariables]="functionScopeVariables"
57 60 [editorCompleter]="customActionEditorCompleter"
58 61 ></tb-js-func>
59 62 </ng-template>
... ... @@ -62,6 +65,7 @@
62 65 formControlName="processQrCodeFunction"
63 66 functionName="processQrCode"
64 67 [functionArgs]="['code', 'format', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  68 + [globalVariables]="functionScopeVariables"
65 69 [editorCompleter]="customActionEditorCompleter"
66 70 ></tb-js-func>
67 71 </ng-template>
... ... @@ -70,6 +74,7 @@
70 74 formControlName="processLocationFunction"
71 75 functionName="processLocation"
72 76 [functionArgs]="['latitude', 'longitude', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  77 + [globalVariables]="functionScopeVariables"
73 78 [editorCompleter]="customActionEditorCompleter"
74 79 ></tb-js-func>
75 80 </ng-template>
... ... @@ -81,6 +86,7 @@
81 86 formControlName="processLaunchResultFunction"
82 87 functionName="processLaunchResult"
83 88 [functionArgs]="['launched', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  89 + [globalVariables]="functionScopeVariables"
84 90 [editorCompleter]="customActionEditorCompleter"
85 91 ></tb-js-func>
86 92 </ng-template>
... ... @@ -89,12 +95,14 @@
89 95 formControlName="handleEmptyResultFunction"
90 96 functionName="handleEmptyResult"
91 97 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  98 + [globalVariables]="functionScopeVariables"
92 99 [editorCompleter]="customActionEditorCompleter"
93 100 ></tb-js-func>
94 101 <tb-js-func *ngIf="mobileActionFormGroup.get('type').value"
95 102 formControlName="handleErrorFunction"
96 103 functionName="handleError"
97 104 [functionArgs]="['error', '$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  105 + [globalVariables]="functionScopeVariables"
98 106 [editorCompleter]="customActionEditorCompleter"
99 107 ></tb-js-func>
100 108 </div>
... ...
... ... @@ -14,26 +14,29 @@
14 14 /// limitations under the License.
15 15 ///
16 16
17   -import { Component, forwardRef, Input, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
  17 +import { Component, forwardRef, Input, OnInit, QueryList, ViewChildren } from '@angular/core';
18 18 import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
19 19 import { Store } from '@ngrx/store';
20 20 import { AppState } from '@app/core/core.state';
21 21 import { coerceBooleanProperty } from '@angular/cdk/coercion';
22   -import { deepClone } from '@core/utils';
23 22 import {
24 23 WidgetMobileActionDescriptor,
25   - WidgetMobileActionType, widgetMobileActionTypeTranslationMap
  24 + WidgetMobileActionType,
  25 + widgetMobileActionTypeTranslationMap
26 26 } from '@shared/models/widget.models';
27 27 import { CustomActionEditorCompleter } from '@home/components/widget/action/custom-action.models';
28 28 import { JsFuncComponent } from '@shared/components/js-func.component';
29 29 import {
30   - getDefaultGetLocationFunction, getDefaultGetPhoneNumberFunction,
  30 + getDefaultGetLocationFunction,
  31 + getDefaultGetPhoneNumberFunction,
31 32 getDefaultHandleEmptyResultFunction,
32 33 getDefaultHandleErrorFunction,
33 34 getDefaultProcessImageFunction,
34   - getDefaultProcessLaunchResultFunction, getDefaultProcessLocationFunction,
  35 + getDefaultProcessLaunchResultFunction,
  36 + getDefaultProcessLocationFunction,
35 37 getDefaultProcessQrCodeFunction
36 38 } from '@home/components/widget/action/mobile-action-editor.models';
  39 +import { WidgetService } from "@core/http/widget.service";
37 40
38 41 @Component({
39 42 selector: 'tb-mobile-action-editor',
... ... @@ -58,6 +61,8 @@ export class MobileActionEditorComponent implements ControlValueAccessor, OnInit
58 61 mobileActionFormGroup: FormGroup;
59 62 mobileActionTypeFormGroup: FormGroup;
60 63
  64 + functionScopeVariables: string[];
  65 +
61 66 private requiredValue: boolean;
62 67 get required(): boolean {
63 68 return this.requiredValue;
... ... @@ -73,7 +78,9 @@ export class MobileActionEditorComponent implements ControlValueAccessor, OnInit
73 78 private propagateChange = (v: any) => { };
74 79
75 80 constructor(private store: Store<AppState>,
76   - private fb: FormBuilder) {
  81 + private fb: FormBuilder,
  82 + private widgetService: WidgetService) {
  83 + this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
77 84 }
78 85
79 86 registerOnChange(fn: any): void {
... ...
... ... @@ -171,6 +171,7 @@
171 171 <tb-js-func
172 172 formControlName="customFunction"
173 173 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'additionalParams', 'entityLabel']"
  174 + [globalVariables]="functionScopeVariables"
174 175 [validationArgs]="[]"
175 176 [editorCompleter]="customActionEditorCompleter"
176 177 ></tb-js-func>
... ...
... ... @@ -47,6 +47,7 @@ import { CustomActionEditorCompleter } from '@home/components/widget/action/cust
47 47 import { isDefinedAndNotNull } from '@core/utils';
48 48 import { MobileActionEditorComponent } from '@home/components/widget/action/mobile-action-editor.component';
49 49 import { widgetType } from '@shared/models/widget.models';
  50 +import { WidgetService } from "@core/http/widget.service";
50 51
51 52 export interface WidgetActionDialogData {
52 53 isAdd: boolean;
... ... @@ -88,11 +89,14 @@ export class WidgetActionDialogComponent extends DialogComponent<WidgetActionDia
88 89 submitted = false;
89 90 widgetType = widgetType;
90 91
  92 + functionScopeVariables: string[];
  93 +
91 94 constructor(protected store: Store<AppState>,
92 95 protected router: Router,
93 96 private utils: UtilsService,
94 97 private dashboardService: DashboardService,
95 98 private dashboardUtils: DashboardUtilsService,
  99 + private widgetService: WidgetService,
96 100 @Inject(MAT_DIALOG_DATA) public data: WidgetActionDialogData,
97 101 @SkipSelf() private errorStateMatcher: ErrorStateMatcher,
98 102 public dialogRef: MatDialogRef<WidgetActionDialogComponent, WidgetActionDescriptorInfo>,
... ... @@ -109,6 +113,7 @@ export class WidgetActionDialogComponent extends DialogComponent<WidgetActionDia
109 113 } else {
110 114 this.action = this.data.action;
111 115 }
  116 + this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
112 117 }
113 118
114 119 ngOnInit(): void {
... ...
... ... @@ -67,6 +67,7 @@
67 67 <br/>
68 68 <tb-js-func #funcBodyEdit
69 69 [functionArgs]="['time', 'prevValue']"
  70 + [globalVariables]="functionScopeVariables"
70 71 [validationArgs]="[[1, 1],[1, '1']]"
71 72 resultType="any"
72 73 formControlName="funcBody">
... ... @@ -78,6 +79,7 @@
78 79 </mat-checkbox>
79 80 <tb-js-func *ngIf="dataKeyFormGroup.get('usePostProcessing').value" #postFuncBodyEdit
80 81 [functionArgs]="['time', 'value', 'prevValue', 'timePrev', 'prevOrigValue']"
  82 + [globalVariables]="functionScopeVariables"
81 83 [validationArgs]="[[1, 1, 1, 1, 1],[1, '1', '1', 1, '1']]"
82 84 resultType="any"
83 85 formControlName="postFuncBody">
... ...
... ... @@ -40,6 +40,7 @@ import { map, mergeMap, publishReplay, refCount, tap } from 'rxjs/operators';
40 40 import { alarmFields } from '@shared/models/alarm.models';
41 41 import { JsFuncComponent } from '@shared/components/js-func.component';
42 42 import { JsonFormComponentData } from '@shared/components/json-form/json-form-component.models';
  43 +import { WidgetService } from "@core/http/widget.service";
43 44
44 45 @Component({
45 46 selector: 'tb-data-key-config',
... ... @@ -99,13 +100,17 @@ export class DataKeyConfigComponent extends PageComponent implements OnInit, Con
99 100
100 101 keySearchText = '';
101 102
  103 + functionScopeVariables: string[];
  104 +
102 105 constructor(protected store: Store<AppState>,
103 106 private utils: UtilsService,
104 107 private entityService: EntityService,
105 108 private dialog: MatDialog,
106 109 private translate: TranslateService,
  110 + private widgetService: WidgetService,
107 111 private fb: FormBuilder) {
108 112 super(store);
  113 + this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
109 114 }
110 115
111 116 ngOnInit(): void {
... ...
... ... @@ -83,6 +83,8 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
83 83
84 84 @Input() editorCompleter: TbEditorCompleter;
85 85
  86 + @Input() globalVariables: Array<string>;
  87 +
86 88 private noValidateValue: boolean;
87 89 get noValidate(): boolean {
88 90 return this.noValidateValue;
... ... @@ -190,6 +192,11 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
190 192 jsWorkerOptions.globals[arg] = false;
191 193 });
192 194 }
  195 + if (this.globalVariables) {
  196 + this.globalVariables.forEach(arg => {
  197 + jsWorkerOptions.globals[arg] = false;
  198 + });
  199 + }
193 200 // @ts-ignore
194 201 this.jsEditor.session.$worker.send('changeOptions', [jsWorkerOptions]);
195 202 }
... ...