import { Injectable } from '@angular/core'; import { ConfigService } from '@arrow/bom/config'; import { TranslateService } from '@ngx-translate/core'; @Injectable() export class Utils { private _domain: string = ''; // The top-level domain (.com or .de) readonly emailRegex: RegExp = /^\w+([.-]\w+)*@\w+([.-]\w+)*\.\w{2,25}$/; constructor(private config: ConfigService, private translate: TranslateService) { const hostname = location.hostname; const index = hostname.search(/\.\w/); this._domain = hostname.slice(index); } public getAllEnumValuesAsList(enumToMap: any): T[] { if (!enumToMap) throw Error('value can not be undefined'); return Object.keys(enumToMap).map(key => enumToMap[key as any]); } debounce(func: any, wait: number, immediate: boolean) { let timeout: any; return function(data?: any) { const context = this, args = arguments; const later = function() { timeout = null; if (!immediate) func.apply(context, args); }; const callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; } throttle(func: any, threshhold: number, scope: any) { threshhold || (threshhold = 250); let last: number, deferTimer: any; return function($event?: Event) { const context = scope || this; const now = +new Date(), args = arguments; if (last && now < last + threshhold) { // hold on to it clearTimeout(deferTimer); deferTimer = setTimeout(function() { last = now; func.apply(context, args); }, threshhold); } else { last = now; func.apply(context, args); } }; } getCoords(elem: HTMLElement) { const box = elem.getBoundingClientRect(); const body = document.body; const docEl = document.documentElement; const scrollTop = window.pageYOffset || docEl.scrollTop || body.scrollTop; const scrollLeft = window.pageXOffset || docEl.scrollLeft || body.scrollLeft; const clientTop = docEl.clientTop || body.clientTop || 0; const clientLeft = docEl.clientLeft || body.clientLeft || 0; const top = box.top + scrollTop - clientTop; const left = box.left + scrollLeft - clientLeft; const bottom = top + box.height - 38; return { top: Math.round(top), left: Math.round(left), bottom: Math.round(bottom) }; } /** * Returns relevant currency symbol based on user selected currency * @param code Currency code * @returns Currency symbol */ getCurrencySymbol(code: string): string { let symbol: string; switch (code) { case 'USD': case 'CAD': case 'AUD': case 'SGD': case 'MXN': symbol = '$'; break; case 'HKD': symbol = 'HK$'; break; case 'BRL': symbol = 'R$'; break; case 'TWD': symbol = 'NT$'; break; case 'JPY': case 'CNY': symbol = '¥'; break; case 'GBP': symbol = '£'; break; case 'MYR': symbol = 'RM'; break; case 'KRW': symbol = '₩'; break; case 'EUR': symbol = '€'; break; case 'INR': symbol = '₹'; break; default: symbol = code; break; } return symbol; } isNullOrWhitespace(input: string): boolean { if (typeof input === 'undefined' || input == null) return true; return input.replace(/\s/g, '').length < 1; } isValidEmail(input: string): boolean { return this.emailRegex.test(input); } stringifyURLParams(params: any): string { let paramString = '?'; Object.keys(params).forEach( key => (paramString += `${key}=${params[key]}&`) ); return paramString.slice(0, -1); } /** * @Deprecated Use getTariffsUrl */ public openTariffsUrl() { const target = this.config.getTarget(); const websiteLang = this.config.websiteLang; const baseUrl = 'https://www.arrow.'; const relPath = 'support/tariff-support/china-tariffs'; const topDomain = this.config.topDomain; let finalUrl = ''; if (target === 'arrowcom') { if (topDomain !== 'com') { finalUrl = `${baseUrl}${topDomain}/${relPath}`; } else { finalUrl = `${baseUrl}com/${websiteLang}/${relPath}`; } } else if (target === 'myarrow') { finalUrl = `${baseUrl}com/en/${relPath}`; } window.open(finalUrl, '_blank'); } /** * Return the tariff url */ public getTariffsUrl() { const target = this.config.getTarget(); const websiteLang = this.config.websiteLang; const baseUrl = 'https://www.arrow.'; const relPath = 'support/tariff-support/china-tariffs'; const topDomain = this.config.topDomain; if (target === 'arrowcom') { if (topDomain !== 'com') { return `${baseUrl}${topDomain}/${relPath}`; } else { return `${baseUrl}com/${websiteLang}/${relPath}`; } } else if (target === 'myarrow') { return `${baseUrl}com/en/${relPath}`; } } public translateByKeyWithFallback(translationKey: string, fallbackStr: string = null, interpolateParams: any = {}) : string { if (this.isNullOrWhitespace(translationKey) && this.isNullOrWhitespace(fallbackStr)) { return ""; } if (this.isNullOrWhitespace(translationKey) && !this.isNullOrWhitespace(fallbackStr)) { return fallbackStr; } let translation = this.translate.instant(translationKey, interpolateParams); // Also covers the case when there is no translation provided for a key in messages..json return (translation === translationKey && !this.isNullOrWhitespace(fallbackStr)) ? fallbackStr : translation } }