|
| 1 | +import { Injectable, Injector } from '@angular/core'; |
| 2 | +import { PlatformLocation, registerLocaleData } from '@angular/common'; |
| 3 | +import { HttpClient } from '@angular/common/http'; |
| 4 | +import * as moment from 'moment'; |
| 5 | +import * as _ from 'lodash'; |
| 6 | +import { AppConsts } from '@shared/AppConsts'; |
| 7 | +import { AppSessionService } from '@shared/session/app-session.service'; |
| 8 | +import { environment } from './environments/environment'; |
| 9 | + |
| 10 | +@Injectable({ |
| 11 | + providedIn: 'root', |
| 12 | +}) |
| 13 | +export class AppInitializer { |
| 14 | + constructor( |
| 15 | + private _injector: Injector, |
| 16 | + private _platformLocation: PlatformLocation, |
| 17 | + private _httpClient: HttpClient |
| 18 | + ) { } |
| 19 | + |
| 20 | + init(): () => Promise<boolean> { |
| 21 | + return () => { |
| 22 | + abp.ui.setBusy(); |
| 23 | + return new Promise<boolean>((resolve, reject) => { |
| 24 | + AppConsts.appBaseHref = this.getBaseHref(); |
| 25 | + const appBaseUrl = this.getDocumentOrigin() + AppConsts.appBaseHref; |
| 26 | + this.getApplicationConfig(appBaseUrl, () => { |
| 27 | + this.getUserConfiguration(() => { |
| 28 | + abp.event.trigger('abp.dynamicScriptsInitialized'); |
| 29 | + // do not use constructor injection for AppSessionService |
| 30 | + const appSessionService = this._injector.get(AppSessionService); |
| 31 | + appSessionService.init().then( |
| 32 | + (result) => { |
| 33 | + abp.ui.clearBusy(); |
| 34 | + if (this.shouldLoadLocale()) { |
| 35 | + const angularLocale = this.convertAbpLocaleToAngularLocale( |
| 36 | + abp.localization.currentLanguage.name |
| 37 | + ); |
| 38 | + import(`@angular/common/locales/${angularLocale}.js`).then( |
| 39 | + (module) => { |
| 40 | + registerLocaleData(module.default); |
| 41 | + resolve(result); |
| 42 | + }, |
| 43 | + reject |
| 44 | + ); |
| 45 | + } else { |
| 46 | + resolve(result); |
| 47 | + } |
| 48 | + }, |
| 49 | + (err) => { |
| 50 | + abp.ui.clearBusy(); |
| 51 | + reject(err); |
| 52 | + } |
| 53 | + ); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + private getBaseHref(): string { |
| 61 | + const baseUrl = this._platformLocation.getBaseHrefFromDOM(); |
| 62 | + if (baseUrl) { |
| 63 | + return baseUrl; |
| 64 | + } |
| 65 | + |
| 66 | + return '/'; |
| 67 | + } |
| 68 | + |
| 69 | + private getDocumentOrigin(): string { |
| 70 | + if (!document.location.origin) { |
| 71 | + const port = document.location.port ? ':' + document.location.port : ''; |
| 72 | + return ( |
| 73 | + document.location.protocol + '//' + document.location.hostname + port |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return document.location.origin; |
| 78 | + } |
| 79 | + |
| 80 | + private shouldLoadLocale(): boolean { |
| 81 | + return ( |
| 82 | + abp.localization.currentLanguage.name && |
| 83 | + abp.localization.currentLanguage.name !== 'en-US' |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + private convertAbpLocaleToAngularLocale(locale: string): string { |
| 88 | + if (!AppConsts.localeMappings) { |
| 89 | + return locale; |
| 90 | + } |
| 91 | + |
| 92 | + const localeMapings = _.filter(AppConsts.localeMappings, { from: locale }); |
| 93 | + if (localeMapings && localeMapings.length) { |
| 94 | + return localeMapings[0]['to']; |
| 95 | + } |
| 96 | + |
| 97 | + return locale; |
| 98 | + } |
| 99 | + |
| 100 | + private getCurrentClockProvider( |
| 101 | + currentProviderName: string |
| 102 | + ): abp.timing.IClockProvider { |
| 103 | + if (currentProviderName === 'unspecifiedClockProvider') { |
| 104 | + return abp.timing.unspecifiedClockProvider; |
| 105 | + } |
| 106 | + |
| 107 | + if (currentProviderName === 'utcClockProvider') { |
| 108 | + return abp.timing.utcClockProvider; |
| 109 | + } |
| 110 | + |
| 111 | + return abp.timing.localClockProvider; |
| 112 | + } |
| 113 | + |
| 114 | + private getUserConfiguration(callback: () => void): void { |
| 115 | + const cookieLangValue = abp.utils.getCookieValue( |
| 116 | + 'Abp.Localization.CultureName' |
| 117 | + ); |
| 118 | + const token = abp.auth.getToken(); |
| 119 | + |
| 120 | + const requestHeaders = { |
| 121 | + 'Abp.TenantId': `${abp.multiTenancy.getTenantIdCookie()}`, |
| 122 | + '.AspNetCore.Culture': `c=${cookieLangValue}|uic=${cookieLangValue}`, |
| 123 | + }; |
| 124 | + |
| 125 | + if (token) { |
| 126 | + requestHeaders['Authorization'] = `Bearer ${token}`; |
| 127 | + } |
| 128 | + |
| 129 | + this._httpClient |
| 130 | + .get<any>( |
| 131 | + `${AppConsts.remoteServiceBaseUrl}/AbpUserConfiguration/GetAll`, |
| 132 | + { |
| 133 | + headers: requestHeaders, |
| 134 | + } |
| 135 | + ) |
| 136 | + .subscribe((response) => { |
| 137 | + const result = response.result; |
| 138 | + |
| 139 | + _.merge(abp, result); |
| 140 | + |
| 141 | + abp.clock.provider = this.getCurrentClockProvider( |
| 142 | + result.clock.provider |
| 143 | + ); |
| 144 | + |
| 145 | + moment.locale(abp.localization.currentLanguage.name); |
| 146 | + |
| 147 | + if (abp.clock.provider.supportsMultipleTimezone) { |
| 148 | + moment.tz.setDefault(abp.timing.timeZoneInfo.iana.timeZoneId); |
| 149 | + } |
| 150 | + |
| 151 | + callback(); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + private getApplicationConfig(appRootUrl: string, callback: () => void) { |
| 156 | + this._httpClient |
| 157 | + .get<any>(`${appRootUrl}assets/${environment.appConfig}`, { |
| 158 | + headers: { |
| 159 | + 'Abp.TenantId': `${abp.multiTenancy.getTenantIdCookie()}`, |
| 160 | + }, |
| 161 | + }) |
| 162 | + .subscribe((response) => { |
| 163 | + AppConsts.appBaseUrl = response.appBaseUrl; |
| 164 | + AppConsts.remoteServiceBaseUrl = response.remoteServiceBaseUrl; |
| 165 | + AppConsts.localeMappings = response.localeMappings; |
| 166 | + |
| 167 | + callback(); |
| 168 | + }); |
| 169 | + } |
| 170 | +} |
0 commit comments