X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=src%2Fservices%2FSharedService.ts;h=8fb51bff646c058364b1569cb4b9c4acb43f8912;hb=refs%2Fheads%2Fmaster;hp=53078b52e6e3cbafaf2b265097625b313b731b65;hpb=0a34dfa32165036b380ec6ac493469b34007df0a;p=osm%2FNG-UI.git diff --git a/src/services/SharedService.ts b/src/services/SharedService.ts index 53078b5..61c3367 100644 --- a/src/services/SharedService.ts +++ b/src/services/SharedService.ts @@ -18,7 +18,6 @@ /** * @file Provider for Shared Service */ -import { isNullOrUndefined } from 'util'; import { HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { EventEmitter, Injectable, Output } from '@angular/core'; import { FormArray, FormGroup } from '@angular/forms'; @@ -39,6 +38,7 @@ import { import { environment } from 'environment'; import * as HttpStatus from 'http-status-codes'; import * as untar from 'js-untar'; +import { ActiveToast, ToastrService } from 'ngx-toastr'; import * as pako from 'pako'; import { RestService } from 'RestService'; import { Observable } from 'rxjs'; @@ -81,6 +81,9 @@ export class SharedService { /** Variables to hold regexp pattern for Longitude */ public REGX_LONG_PATTERN: RegExp = new RegExp(/^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,15})?))$/); + /** Variable to hold regexp pattern for EMAIL */ + public REGX_EMAIL_PATTERN: RegExp = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; + /** Variables to hold maxlength for the description @public */ // eslint-disable-next-line @typescript-eslint/no-magic-numbers public MAX_LENGTH_DESCRIPTION: number = 500; @@ -98,6 +101,15 @@ export class SharedService { /** Holds OSM Version value @public */ public osmVersion: string; + /** Holds Last Login Toaster Message @public */ + public lastLoginMessage: string; + + /** Holds Failed Attempts Toaster Message @public */ + public failedAttemptsMessage: string; + + /** Holds No Of Days Toaster Message @public */ + public daysMessage: string; + /** express number for time manupulation -2 */ // eslint-disable-next-line @typescript-eslint/no-magic-numbers private epochTimeMinus2: number = -2; @@ -106,31 +118,54 @@ export class SharedService { // eslint-disable-next-line @typescript-eslint/no-magic-numbers private epochTime1000: number = 1000; + /** express number for time manupulation 60 */ + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + private epochTime60: number = 60; + + /** express number for time manupulation 24 */ + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + private epochTime24: number = 24; + /** Random string generator length */ // eslint-disable-next-line @typescript-eslint/no-magic-numbers private randomStringLength: number = 4; + /** express number for rgb manipulation */ + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + private colourHour: number = 10; + + /** express number for rgb manipulation*/ + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + private colourMin: number = 5; + /** Instance of the rest service @private */ private restService: RestService; /** Service holds the router information @private */ private router: Router; - /** Random color string generator length @private */ - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - private colorStringLength: number = 256; - /** Check for the root directory @private */ // eslint-disable-next-line @typescript-eslint/no-magic-numbers private directoryCount: number = 2; + /** express number for time manupulation 1000 */ + private toasterSettings: {} = { + enableHtml: true, + closeButton: true, + timeOut: 2000 + }; + /** Contains tranlsate instance @private */ private translateService: TranslateService; - constructor(restService: RestService, router: Router, translateService: TranslateService) { + /** Contains toaster instance @private */ + private toaster: ToastrService; + + constructor(restService: RestService, router: Router, translateService: TranslateService, toaster: ToastrService) { this.restService = restService; this.router = router; this.translateService = translateService; + this.toaster = toaster; } /** convert epoch time function @public */ @@ -153,6 +188,56 @@ export class SharedService { return this.translateService.instant('NODATE'); } + /** convert epoch time function to No of days @public */ + public converEpochToDays(date: string): number { + if (!isNullOrUndefined(date)) { + const today: Date = new Date(); + const accountDate: Date = new Date(date); + const toasterDate: number = (accountDate.getTime() - + today.getTime()) / this.epochTime1000 / this.epochTime60 / this.epochTime60 / this.epochTime24; + if (toasterDate >= 0 || toasterDate < 1) { + return Math.round(toasterDate); + } + return Math.floor(toasterDate); + } + return this.translateService.instant('N/A'); + } + + /** show toaster for password & account expiry @public */ + public showToaster(lastLogin: string, failedAttempts: string, passwordNoOfDays: string, + accountNoOfDays: string, passwordExpireMessage: string, accountExpireMessage: string, + passwordMessage: string, accountMessage: string): ActiveToast { + this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); + this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); + return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + + '
' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + + '
' + passwordExpireMessage + ' ' + passwordNoOfDays + ' ' + passwordMessage + + '
' + accountExpireMessage + ' ' + accountNoOfDays + ' ' + accountMessage, + this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); + } + + /** show toaster for password expiry @public */ + public passwordToaster(lastLogin: string, failedAttempts: string, passwordNoOfDays: string, + passwordExpireMessage: string, passwordMessage: string): ActiveToast { + this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); + this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); + return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + + '
' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + + '
' + passwordExpireMessage + ' ' + passwordNoOfDays + ' ' + passwordMessage, + this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); + } + + /** show toaster for account expiry @public */ + public accountToaster(lastLogin: string, failedAttempts: string, + accountNoOfDays: string, accountExpireMessage: string, accountMessage: string): ActiveToast { + this.lastLoginMessage = this.translateService.instant('PAGE.LOGIN.LASTACCESS'); + this.failedAttemptsMessage = this.translateService.instant('PAGE.LOGIN.FAILED'); + return this.toaster.info(this.lastLoginMessage + ':' + ' ' + lastLogin + + '
' + this.failedAttemptsMessage + ':' + ' ' + failedAttempts + + '
' + accountExpireMessage + ' ' + accountNoOfDays + ' ' + accountMessage, + this.translateService.instant('PAGE.LOGIN.LOGINHISTORY'), this.toasterSettings); + } + /** Download Files function @public */ public downloadFiles(name: string, binaryData: Blob[], filetype: string): void { const downloadLink: HTMLAnchorElement = document.createElement('a'); @@ -177,10 +262,9 @@ export class SharedService { /** Generate random string @public */ public randomString(): string { - const chars: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; let result: string = ''; for (let randomStringRef: number = this.randomStringLength; randomStringRef > 0; --randomStringRef) { - result += chars[Math.floor(Math.random() * chars.length)]; + result += new Date().getSeconds(); } return result; } @@ -331,6 +415,7 @@ export class SharedService { const version: string[] = res.version.split('+'); if (!isNullOrUndefined(version[0])) { this.osmVersion = version[0]; + sessionStorage.setItem('version', version[0]); } else { this.osmVersion = null; } @@ -342,9 +427,9 @@ export class SharedService { /** Random RGB color code generator @public */ public generateColor(): string { - const x: number = Math.floor(Math.random() * this.colorStringLength); - const y: number = Math.floor(Math.random() * this.colorStringLength); - const z: number = Math.floor(Math.random() * this.colorStringLength); + const x: number = Math.floor((new Date().getHours()) * this.colourHour); + const y: number = Math.floor((new Date().getMinutes()) * this.colourMin); + const z: number = Math.floor((new Date().getSeconds()) * this.colourMin); return 'rgb(' + x + ',' + y + ',' + z + ')'; } @@ -385,6 +470,19 @@ export class SharedService { })); } + /** Sorting the list based on date @public */ + public compareFunction = (dir: number, a: string, b: string): number => { + const first: number = new Date(a).getTime(); + const second: number = new Date(b).getTime(); + if (first < second) { + return -1 * dir; + } + if (first > second) { + return dir; + } + return 0; + }; + /** Method to validate file extension and size @private */ private vaildataFileInfo(fileInfo: File, fileType: string): boolean { const extension: string = fileInfo.name.substring(fileInfo.name.lastIndexOf('.') + 1); @@ -418,3 +516,8 @@ export class SharedService { }; } } + +/** Method to handle null or undefined @public */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types +export const isNullOrUndefined = (data: any): boolean => data === null || data === undefined; +