Commit b29e208b95e7f1f33ceece344d2970cc237a7e99

Authored by Vladyslav_Prykhodko
1 parent d08e6dba

UI: Added global scope variables in javascript functions - refactoring

@@ -44,7 +44,7 @@ @@ -44,7 +44,7 @@
44 (ngModelChange)="onActionUpdated()" 44 (ngModelChange)="onActionUpdated()"
45 [fillHeight]="true" 45 [fillHeight]="true"
46 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']" 46 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']"
47 - [globalVariables]="functionScopeVariables" 47 + [disableUndefinedCheck]="true"
48 [validationArgs]="[]" 48 [validationArgs]="[]"
49 [editorCompleter]="customPrettyActionEditorCompleter"> 49 [editorCompleter]="customPrettyActionEditorCompleter">
50 </tb-js-func> 50 </tb-js-func>
@@ -36,7 +36,6 @@ import { AppState } from '@core/core.state'; @@ -36,7 +36,6 @@ import { AppState } from '@core/core.state';
36 import { combineLatest } from 'rxjs'; 36 import { combineLatest } from 'rxjs';
37 import { CustomActionDescriptor } from '@shared/models/widget.models'; 37 import { CustomActionDescriptor } from '@shared/models/widget.models';
38 import { CustomPrettyActionEditorCompleter } from '@home/components/widget/action/custom-action.models'; 38 import { CustomPrettyActionEditorCompleter } from '@home/components/widget/action/custom-action.models';
39 -import { WidgetService } from "@core/http/widget.service";  
40 39
41 @Component({ 40 @Component({
42 selector: 'tb-custom-action-pretty-editor', 41 selector: 'tb-custom-action-pretty-editor',
@@ -59,8 +58,6 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements @@ -59,8 +58,6 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
59 58
60 fullscreen = false; 59 fullscreen = false;
61 60
62 - functionScopeVariables: string[];  
63 -  
64 @ViewChildren('leftPanel') 61 @ViewChildren('leftPanel')
65 leftPanelElmRef: QueryList<ElementRef<HTMLElement>>; 62 leftPanelElmRef: QueryList<ElementRef<HTMLElement>>;
66 63
@@ -71,10 +68,8 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements @@ -71,10 +68,8 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
71 68
72 private propagateChange = (_: any) => {}; 69 private propagateChange = (_: any) => {};
73 70
74 - constructor(protected store: Store<AppState>,  
75 - private widgetService: WidgetService) { 71 + constructor(protected store: Store<AppState>) {
76 super(store); 72 super(store);
77 - this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();  
78 } 73 }
79 74
80 ngOnInit(): void { 75 ngOnInit(): void {
@@ -94,7 +94,7 @@ @@ -94,7 +94,7 @@
94 [(ngModel)]="action.customFunction" 94 [(ngModel)]="action.customFunction"
95 (ngModelChange)="notifyActionUpdated()" 95 (ngModelChange)="notifyActionUpdated()"
96 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']" 96 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']"
97 - [globalVariables]="functionScopeVariables" 97 + [disableUndefinedCheck]="true"
98 [validationArgs]="[]" 98 [validationArgs]="[]"
99 [editorCompleter]="customPrettyActionEditorCompleter"> 99 [editorCompleter]="customPrettyActionEditorCompleter">
100 </tb-js-func> 100 </tb-js-func>
@@ -41,7 +41,6 @@ import { forkJoin, from } from 'rxjs'; @@ -41,7 +41,6 @@ import { forkJoin, from } from 'rxjs';
41 import { map, tap } from 'rxjs/operators'; 41 import { map, tap } from 'rxjs/operators';
42 import { getAce } from '@shared/models/ace/ace.models'; 42 import { getAce } from '@shared/models/ace/ace.models';
43 import { beautifyCss, beautifyHtml } from '@shared/models/beautify.models'; 43 import { beautifyCss, beautifyHtml } from '@shared/models/beautify.models';
44 -import { WidgetService } from "@core/http/widget.service";  
45 44
46 @Component({ 45 @Component({
47 selector: 'tb-custom-action-pretty-resources-tabs', 46 selector: 'tb-custom-action-pretty-resources-tabs',
@@ -76,16 +75,12 @@ export class CustomActionPrettyResourcesTabsComponent extends PageComponent impl @@ -76,16 +75,12 @@ export class CustomActionPrettyResourcesTabsComponent extends PageComponent impl
76 cssEditor: Ace.Editor; 75 cssEditor: Ace.Editor;
77 setValuesPending = false; 76 setValuesPending = false;
78 77
79 - functionScopeVariables: string[];  
80 -  
81 customPrettyActionEditorCompleter = CustomPrettyActionEditorCompleter; 78 customPrettyActionEditorCompleter = CustomPrettyActionEditorCompleter;
82 79
83 constructor(protected store: Store<AppState>, 80 constructor(protected store: Store<AppState>,
84 private translate: TranslateService, 81 private translate: TranslateService,
85 - private widgetService: WidgetService,  
86 private raf: RafService) { 82 private raf: RafService) {
87 super(store); 83 super(store);
88 - this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();  
89 } 84 }
90 85
91 ngOnInit(): void { 86 ngOnInit(): void {
@@ -36,7 +36,7 @@ import { @@ -36,7 +36,7 @@ import {
36 getDefaultProcessLocationFunction, 36 getDefaultProcessLocationFunction,
37 getDefaultProcessQrCodeFunction 37 getDefaultProcessQrCodeFunction
38 } from '@home/components/widget/action/mobile-action-editor.models'; 38 } from '@home/components/widget/action/mobile-action-editor.models';
39 -import { WidgetService } from "@core/http/widget.service"; 39 +import { WidgetService } from '@core/http/widget.service';
40 40
41 @Component({ 41 @Component({
42 selector: 'tb-mobile-action-editor', 42 selector: 'tb-mobile-action-editor',
@@ -47,7 +47,7 @@ import { CustomActionEditorCompleter } from '@home/components/widget/action/cust @@ -47,7 +47,7 @@ import { CustomActionEditorCompleter } from '@home/components/widget/action/cust
47 import { isDefinedAndNotNull } from '@core/utils'; 47 import { isDefinedAndNotNull } from '@core/utils';
48 import { MobileActionEditorComponent } from '@home/components/widget/action/mobile-action-editor.component'; 48 import { MobileActionEditorComponent } from '@home/components/widget/action/mobile-action-editor.component';
49 import { widgetType } from '@shared/models/widget.models'; 49 import { widgetType } from '@shared/models/widget.models';
50 -import { WidgetService } from "@core/http/widget.service"; 50 +import { WidgetService } from '@core/http/widget.service';
51 51
52 export interface WidgetActionDialogData { 52 export interface WidgetActionDialogData {
53 isAdd: boolean; 53 isAdd: boolean;
@@ -40,7 +40,7 @@ import { map, mergeMap, publishReplay, refCount, tap } from 'rxjs/operators'; @@ -40,7 +40,7 @@ import { map, mergeMap, publishReplay, refCount, tap } from 'rxjs/operators';
40 import { alarmFields } from '@shared/models/alarm.models'; 40 import { alarmFields } from '@shared/models/alarm.models';
41 import { JsFuncComponent } from '@shared/components/js-func.component'; 41 import { JsFuncComponent } from '@shared/components/js-func.component';
42 import { JsonFormComponentData } from '@shared/components/json-form/json-form-component.models'; 42 import { JsonFormComponentData } from '@shared/components/json-form/json-form-component.models';
43 -import { WidgetService } from "@core/http/widget.service"; 43 +import { WidgetService } from '@core/http/widget.service';
44 44
45 @Component({ 45 @Component({
46 selector: 'tb-data-key-config', 46 selector: 'tb-data-key-config',
@@ -85,6 +85,8 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor, @@ -85,6 +85,8 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
85 85
86 @Input() globalVariables: Array<string>; 86 @Input() globalVariables: Array<string>;
87 87
  88 + @Input() disableUndefinedCheck = false;
  89 +
88 private noValidateValue: boolean; 90 private noValidateValue: boolean;
89 get noValidate(): boolean { 91 get noValidate(): boolean {
90 return this.noValidateValue; 92 return this.noValidateValue;
@@ -167,32 +169,34 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor, @@ -167,32 +169,34 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
167 this.updateView(); 169 this.updateView();
168 } 170 }
169 }); 171 });
170 - // @ts-ignore  
171 - this.jsEditor.session.on('changeAnnotation', () => {  
172 - const annotations = this.jsEditor.session.getAnnotations();  
173 - annotations.filter(annotation => annotation.text.includes('is not defined')).forEach(annotation => {  
174 - annotation.type = 'error'; 172 + if (!this.disableUndefinedCheck) {
  173 + // @ts-ignore
  174 + this.jsEditor.session.on('changeAnnotation', () => {
  175 + const annotations = this.jsEditor.session.getAnnotations();
  176 + annotations.filter(annotation => annotation.text.includes('is not defined')).forEach(annotation => {
  177 + annotation.type = 'error';
  178 + });
  179 + this.jsEditor.renderer.setAnnotations(annotations);
  180 + const hasErrors = annotations.filter(annotation => annotation.type === 'error').length > 0;
  181 + if (this.hasErrors !== hasErrors) {
  182 + this.hasErrors = hasErrors;
  183 + this.propagateChange(this.modelValue);
  184 + }
175 }); 185 });
176 - this.jsEditor.renderer.setAnnotations(annotations);  
177 - const hasErrors = annotations.filter(annotation => annotation.type === 'error').length > 0;  
178 - if (this.hasErrors !== hasErrors) {  
179 - this.hasErrors = hasErrors;  
180 - this.propagateChange(this.modelValue);  
181 - }  
182 - }); 186 + }
183 // @ts-ignore 187 // @ts-ignore
184 if (!!this.jsEditor.session.$worker) { 188 if (!!this.jsEditor.session.$worker) {
185 const jsWorkerOptions = { 189 const jsWorkerOptions = {
186 - undef: true, 190 + undef: !this.disableUndefinedCheck,
187 unused: true, 191 unused: true,
188 globals: {} 192 globals: {}
189 }; 193 };
190 - if (this.functionArgs) { 194 + if (!this.disableUndefinedCheck && this.functionArgs) {
191 this.functionArgs.forEach(arg => { 195 this.functionArgs.forEach(arg => {
192 jsWorkerOptions.globals[arg] = false; 196 jsWorkerOptions.globals[arg] = false;
193 }); 197 });
194 } 198 }
195 - if (this.globalVariables) { 199 + if (!this.disableUndefinedCheck && this.globalVariables) {
196 this.globalVariables.forEach(arg => { 200 this.globalVariables.forEach(arg => {
197 jsWorkerOptions.globals[arg] = false; 201 jsWorkerOptions.globals[arg] = false;
198 }); 202 });