Commit b29e208b95e7f1f33ceece344d2970cc237a7e99

Authored by Vladyslav_Prykhodko
1 parent d08e6dba

UI: Added global scope variables in javascript functions - refactoring

... ... @@ -44,7 +44,7 @@
44 44 (ngModelChange)="onActionUpdated()"
45 45 [fillHeight]="true"
46 46 [functionArgs]="['$event', 'widgetContext', 'entityId', 'entityName', 'htmlTemplate', 'additionalParams', 'entityLabel']"
47   - [globalVariables]="functionScopeVariables"
  47 + [disableUndefinedCheck]="true"
48 48 [validationArgs]="[]"
49 49 [editorCompleter]="customPrettyActionEditorCompleter">
50 50 </tb-js-func>
... ...
... ... @@ -36,7 +36,6 @@ 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";
40 39
41 40 @Component({
42 41 selector: 'tb-custom-action-pretty-editor',
... ... @@ -59,8 +58,6 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
59 58
60 59 fullscreen = false;
61 60
62   - functionScopeVariables: string[];
63   -
64 61 @ViewChildren('leftPanel')
65 62 leftPanelElmRef: QueryList<ElementRef<HTMLElement>>;
66 63
... ... @@ -71,10 +68,8 @@ export class CustomActionPrettyEditorComponent extends PageComponent implements
71 68
72 69 private propagateChange = (_: any) => {};
73 70
74   - constructor(protected store: Store<AppState>,
75   - private widgetService: WidgetService) {
  71 + constructor(protected store: Store<AppState>) {
76 72 super(store);
77   - this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
78 73 }
79 74
80 75 ngOnInit(): void {
... ...
... ... @@ -94,7 +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 + [disableUndefinedCheck]="true"
98 98 [validationArgs]="[]"
99 99 [editorCompleter]="customPrettyActionEditorCompleter">
100 100 </tb-js-func>
... ...
... ... @@ -41,7 +41,6 @@ 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";
45 44
46 45 @Component({
47 46 selector: 'tb-custom-action-pretty-resources-tabs',
... ... @@ -76,16 +75,12 @@ export class CustomActionPrettyResourcesTabsComponent extends PageComponent impl
76 75 cssEditor: Ace.Editor;
77 76 setValuesPending = false;
78 77
79   - functionScopeVariables: string[];
80   -
81 78 customPrettyActionEditorCompleter = CustomPrettyActionEditorCompleter;
82 79
83 80 constructor(protected store: Store<AppState>,
84 81 private translate: TranslateService,
85   - private widgetService: WidgetService,
86 82 private raf: RafService) {
87 83 super(store);
88   - this.functionScopeVariables = this.widgetService.getWidgetScopeVariables();
89 84 }
90 85
91 86 ngOnInit(): void {
... ...
... ... @@ -36,7 +36,7 @@ import {
36 36 getDefaultProcessLocationFunction,
37 37 getDefaultProcessQrCodeFunction
38 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 41 @Component({
42 42 selector: 'tb-mobile-action-editor',
... ...
... ... @@ -47,7 +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 +import { WidgetService } from '@core/http/widget.service';
51 51
52 52 export interface WidgetActionDialogData {
53 53 isAdd: boolean;
... ...
... ... @@ -40,7 +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 +import { WidgetService } from '@core/http/widget.service';
44 44
45 45 @Component({
46 46 selector: 'tb-data-key-config',
... ...
... ... @@ -85,6 +85,8 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
85 85
86 86 @Input() globalVariables: Array<string>;
87 87
  88 + @Input() disableUndefinedCheck = false;
  89 +
88 90 private noValidateValue: boolean;
89 91 get noValidate(): boolean {
90 92 return this.noValidateValue;
... ... @@ -167,32 +169,34 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
167 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 187 // @ts-ignore
184 188 if (!!this.jsEditor.session.$worker) {
185 189 const jsWorkerOptions = {
186   - undef: true,
  190 + undef: !this.disableUndefinedCheck,
187 191 unused: true,
188 192 globals: {}
189 193 };
190   - if (this.functionArgs) {
  194 + if (!this.disableUndefinedCheck && this.functionArgs) {
191 195 this.functionArgs.forEach(arg => {
192 196 jsWorkerOptions.globals[arg] = false;
193 197 });
194 198 }
195   - if (this.globalVariables) {
  199 + if (!this.disableUndefinedCheck && this.globalVariables) {
196 200 this.globalVariables.forEach(arg => {
197 201 jsWorkerOptions.globals[arg] = false;
198 202 });
... ...