Commit 965870a980ec613076e0ec1aa311d5bff85f1c40

Authored by Vladyslav_Prykhodko
1 parent 9a7d5a43

UI: Fixed clear content for ace editor and show toast error

... ... @@ -36,7 +36,6 @@ import { TranslateService } from '@ngx-translate/core';
36 36 import { CancelAnimationFrame, RafService } from '@core/services/raf.service';
37 37 import { ResizeObserver } from '@juggle/resize-observer';
38 38 import { TbEditorCompleter } from '@shared/models/ace/completion.models';
39   -import { widgetEditorCompleter } from '@home/pages/widget/widget-editor.models';
40 39
41 40 @Component({
42 41 selector: 'tb-js-func',
... ... @@ -64,6 +63,7 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
64 63 private jsEditor: ace.Ace.Editor;
65 64 private editorsResizeCaf: CancelAnimationFrame;
66 65 private editorResize$: ResizeObserver;
  66 + private ignoreChange = false;
67 67
68 68 toastTargetId = `jsFuncEditor-${guid()}`;
69 69
... ... @@ -154,8 +154,10 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
154 154 this.jsEditor.session.setUseWrapMode(true);
155 155 this.jsEditor.setValue(this.modelValue ? this.modelValue : '', -1);
156 156 this.jsEditor.on('change', () => {
157   - this.cleanupJsErrors();
158   - this.updateView();
  157 + if (!this.ignoreChange) {
  158 + this.cleanupJsErrors();
  159 + this.updateView();
  160 + }
159 161 });
160 162 if (this.editorCompleter) {
161 163 this.jsEditor.completers = [this.editorCompleter, ...(this.jsEditor.completers || [])];
... ... @@ -332,7 +334,9 @@ export class JsFuncComponent implements OnInit, OnDestroy, ControlValueAccessor,
332 334 writeValue(value: string): void {
333 335 this.modelValue = value;
334 336 if (this.jsEditor) {
  337 + this.ignoreChange = true;
335 338 this.jsEditor.setValue(this.modelValue ? this.modelValue : '', -1);
  339 + this.ignoreChange = false;
336 340 }
337 341 }
338 342
... ...
... ... @@ -61,6 +61,7 @@ export class JsonContentComponent implements OnInit, ControlValueAccessor, Valid
61 61 private jsonEditor: ace.Ace.Editor;
62 62 private editorsResizeCaf: CancelAnimationFrame;
63 63 private editorResize$: ResizeObserver;
  64 + private ignoreChange = false;
64 65
65 66 toastTargetId = `jsonContentEditor-${guid()}`;
66 67
... ... @@ -140,8 +141,13 @@ export class JsonContentComponent implements OnInit, ControlValueAccessor, Valid
140 141 this.jsonEditor.session.setUseWrapMode(true);
141 142 this.jsonEditor.setValue(this.contentBody ? this.contentBody : '', -1);
142 143 this.jsonEditor.on('change', () => {
143   - this.cleanupJsonErrors();
144   - this.updateView();
  144 + if (!this.ignoreChange) {
  145 + this.cleanupJsonErrors();
  146 + this.updateView();
  147 + }
  148 + });
  149 + this.jsonEditor.on('blur', () => {
  150 + this.contentValid = !this.validateContent || this.doValidate(true);
145 151 });
146 152 this.editorResize$ = new ResizeObserver(() => {
147 153 this.onAceEditorResize();
... ... @@ -210,34 +216,36 @@ export class JsonContentComponent implements OnInit, ControlValueAccessor, Valid
210 216 this.cleanupJsonErrors();
211 217 this.contentValid = true;
212 218 this.propagateChange(this.contentBody);
213   - this.contentValid = this.doValidate();
  219 + this.contentValid = this.doValidate(true);
214 220 this.propagateChange(this.contentBody);
215 221 }
216 222 }
217 223
218   - private doValidate(): boolean {
  224 + private doValidate(showErrorToast = false): boolean {
219 225 try {
220 226 if (this.validateContent && this.contentType === ContentType.JSON) {
221 227 JSON.parse(this.contentBody);
222 228 }
223 229 return true;
224 230 } catch (ex) {
225   - let errorInfo = 'Error:';
226   - if (ex.name) {
227   - errorInfo += ' ' + ex.name + ':';
228   - }
229   - if (ex.message) {
230   - errorInfo += ' ' + ex.message;
  231 + if (showErrorToast) {
  232 + let errorInfo = 'Error:';
  233 + if (ex.name) {
  234 + errorInfo += ' ' + ex.name + ':';
  235 + }
  236 + if (ex.message) {
  237 + errorInfo += ' ' + ex.message;
  238 + }
  239 + this.store.dispatch(new ActionNotificationShow(
  240 + {
  241 + message: errorInfo,
  242 + type: 'error',
  243 + target: this.toastTargetId,
  244 + verticalPosition: 'bottom',
  245 + horizontalPosition: 'left'
  246 + }));
  247 + this.errorShowed = true;
231 248 }
232   - this.store.dispatch(new ActionNotificationShow(
233   - {
234   - message: errorInfo,
235   - type: 'error',
236   - target: this.toastTargetId,
237   - verticalPosition: 'bottom',
238   - horizontalPosition: 'left'
239   - }));
240   - this.errorShowed = true;
241 249 return false;
242 250 }
243 251 }
... ... @@ -256,8 +264,9 @@ export class JsonContentComponent implements OnInit, ControlValueAccessor, Valid
256 264 this.contentBody = value;
257 265 this.contentValid = true;
258 266 if (this.jsonEditor) {
  267 + this.ignoreChange = true;
259 268 this.jsonEditor.setValue(this.contentBody ? this.contentBody : '', -1);
260   - // this.jsonEditor.
  269 + this.ignoreChange = false;
261 270 }
262 271 }
263 272
... ...