/// /// 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 { Inject, Injectable } from '@angular/core'; import { WINDOW } from '@core/services/window.service'; import { WidgetInfo } from '@shared/models/widget.models'; import { ExceptionData } from '@app/shared/models/error.models'; import { isUndefined } from '@core/utils'; import { WindowMessage } from '@shared/models/window-message.model'; import { TranslateService } from '@ngx-translate/core'; import { customTranslationsPrefix } from '@app/shared/models/constants'; @Injectable({ providedIn: 'root' }) export class UtilsService { iframeMode = false; widgetEditMode = false; editWidgetInfo: WidgetInfo = null; constructor(@Inject(WINDOW) private window: Window, private translate: TranslateService) { let frame: Element = null; try { frame = window.frameElement; } catch (e) { // ie11 fix } if (frame) { this.iframeMode = true; const dataWidgetAttr = frame.getAttribute('data-widget'); if (dataWidgetAttr && dataWidgetAttr.length) { this.editWidgetInfo = JSON.parse(dataWidgetAttr); this.widgetEditMode = true; } } } public processWidgetException(exception: any): ExceptionData { const data = this.parseException(exception, -5); if (this.widgetEditMode) { const message: WindowMessage = { type: 'widgetException', data }; this.window.parent.postMessage(message, '*'); } return data; } public parseException(exception: any, lineOffset?: number): ExceptionData { const data: ExceptionData = {}; if (exception) { if (typeof exception === 'string') { data.message = exception; } else if (exception instanceof String) { data.message = exception.toString(); } else { if (exception.name) { data.name = exception.name; } else { data.name = 'UnknownError'; } if (exception.message) { data.message = exception.message; } if (exception.lineNumber) { data.lineNumber = exception.lineNumber; if (exception.columnNumber) { data.columnNumber = exception.columnNumber; } } else if (exception.stack) { const lineInfoRegexp = /(.*):(\d*)(:)?(\d*)?/g; const lineInfoGroups = lineInfoRegexp.exec(exception.stack); if (lineInfoGroups != null && lineInfoGroups.length >= 3) { if (isUndefined(lineOffset)) { lineOffset = -2; } data.lineNumber = Number(lineInfoGroups[2]) + lineOffset; if (lineInfoGroups.length >= 5) { data.columnNumber = Number(lineInfoGroups[4]); } } } } } return data; } public customTranslation(translationValue: string, defaultValue: string): string { let result = ''; const translationId = customTranslationsPrefix + translationValue; const translation = this.translate.instant(translationId); if (translation !== translationId) { result = translation + ''; } else { result = defaultValue; } return result; } }