@stemy/ngx-utils 17.5.0 → 17.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1931,57 +1931,69 @@ class Initializer {
1931
1931
  }
1932
1932
 
1933
1933
  class LoaderUtils {
1934
- static { this.scriptPromises = {}; }
1935
- static { this.stylePromises = {}; }
1936
- static loadScript(src, async = false, type = "text/javascript") {
1937
- this.scriptPromises[src] = this.scriptPromises[src] || new Promise((resolve, reject) => {
1938
- // Load script
1934
+ static { this.promises = {}; }
1935
+ static loadScript(src, async = false, type = "text/javascript", parent, time = false) {
1936
+ return LoaderUtils.loadElement(src, parent, time, url => {
1939
1937
  const script = document.createElement("script");
1940
1938
  script.type = type;
1941
- script.src = src;
1939
+ script.src = url;
1942
1940
  script.async = async;
1943
- if (script.readyState) {
1944
- // Internet explorer
1945
- script.onreadystatechange = () => {
1946
- if (script.readyState === "loaded" || script.readyState === "complete") {
1947
- script.onreadystatechange = null;
1948
- resolve(script);
1949
- }
1950
- };
1951
- }
1952
- else {
1953
- // Other browsers
1954
- script.onload = () => resolve(script);
1955
- }
1956
- script.onerror = (error) => reject(error);
1957
- document.body.appendChild(script);
1941
+ return script;
1958
1942
  });
1959
- return this.scriptPromises[src];
1960
1943
  }
1961
- static loadStyle(src) {
1962
- this.stylePromises[src] = this.stylePromises[src] || new Promise((resolve, reject) => {
1963
- // Load script
1944
+ static loadStyle(src, parent, time = false) {
1945
+ return LoaderUtils.loadElement(src, parent, time, url => {
1964
1946
  const link = document.createElement("link");
1965
1947
  link.rel = "stylesheet";
1966
1948
  link.type = "text/css";
1967
- link.href = src;
1968
- if (link.readyState) {
1949
+ link.href = url;
1950
+ return link;
1951
+ });
1952
+ }
1953
+ static updateSrc(src, time) {
1954
+ const srcStr = String(src || "");
1955
+ if (srcStr.startsWith("data:") || !time) {
1956
+ return srcStr;
1957
+ }
1958
+ const url = new URL(src);
1959
+ url.searchParams.set("time", typeof time === "string" ? time : String(Date.now()));
1960
+ return url.toString();
1961
+ }
1962
+ static loadElement(url, parent, time, setup) {
1963
+ const promises = LoaderUtils.promises;
1964
+ const src = LoaderUtils.updateSrc(url, time);
1965
+ parent = parent || document;
1966
+ if (parent == document) {
1967
+ parent = document.body;
1968
+ }
1969
+ let { elem, promise } = promises[src] || {};
1970
+ if (elem) {
1971
+ if (parent === elem.parentElement)
1972
+ return promise;
1973
+ if (elem.parentElement) {
1974
+ elem.remove();
1975
+ }
1976
+ }
1977
+ elem = setup(src);
1978
+ promise = new Promise((resolve, reject) => {
1979
+ if (elem.readyState) {
1969
1980
  // Internet explorer
1970
- link.onreadystatechange = () => {
1971
- if (link.readyState === "loaded" || link.readyState === "complete") {
1972
- link.onreadystatechange = null;
1973
- resolve(link);
1981
+ elem.onreadystatechange = () => {
1982
+ if (elem.readyState === "loaded" || elem.readyState === "complete") {
1983
+ elem.onreadystatechange = null;
1984
+ resolve(elem);
1974
1985
  }
1975
1986
  };
1976
1987
  }
1977
1988
  else {
1978
1989
  // Other browsers
1979
- link.onload = () => resolve(link);
1990
+ elem.onload = () => resolve(elem);
1980
1991
  }
1981
- link.onerror = (error) => reject(error);
1982
- document.body.appendChild(link);
1992
+ elem.onerror = (error) => reject(error);
1983
1993
  });
1984
- return this.stylePromises[src];
1994
+ parent.appendChild(elem);
1995
+ promises[src] = { elem, promise };
1996
+ return promise;
1985
1997
  }
1986
1998
  }
1987
1999
 
@@ -3269,19 +3281,28 @@ class EventsService {
3269
3281
  get isSticky() {
3270
3282
  return this.sticky;
3271
3283
  }
3284
+ get isAuthenticated() {
3285
+ return !!this.user;
3286
+ }
3272
3287
  constructor() {
3273
- this.eventForwarded = new EventEmitter();
3274
- this.stickyUpdated = new EventEmitter();
3275
- this.languageChanged = new EventEmitter();
3276
- this.editLanguageChanged = new EventEmitter();
3288
+ this.eventForwarded = new Subject();
3289
+ this.stickyUpdated = new Subject();
3290
+ this.languageChanged = new Subject();
3291
+ this.editLanguageChanged = new Subject();
3292
+ this.translationsEnabled = new Subject();
3293
+ this.userChanged = new Subject();
3277
3294
  this.sticky = false;
3295
+ this.user = null;
3296
+ this.userChanged.subscribe(user => {
3297
+ this.user = user;
3298
+ });
3278
3299
  }
3279
3300
  event(e) {
3280
- this.eventForwarded.emit(e);
3301
+ this.eventForwarded.next(e);
3281
3302
  }
3282
3303
  updateSticky(sticky) {
3283
3304
  this.sticky = sticky;
3284
- this.stickyUpdated.emit(sticky);
3305
+ this.stickyUpdated.next(sticky);
3285
3306
  }
3286
3307
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: EventsService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
3287
3308
  static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "17.3.12", ngImport: i0, type: EventsService }); }
@@ -3392,16 +3413,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "17.3.12", ngImpo
3392
3413
  type: Injectable
3393
3414
  }], ctorParameters: () => [] });
3394
3415
 
3395
- const emptyDict = {};
3416
+ const EMPTY_DICT = {};
3396
3417
  class StaticLanguageService {
3397
3418
  get defaultLanguage() {
3398
3419
  return this.configs.getQueryParameter("lang") || this.storage.get("language", this.getDefaultLanguage());
3399
3420
  }
3400
3421
  get dictionary() {
3401
- return this.mergedTranslations[this.currentLanguage] || emptyDict;
3422
+ return this.mergedTranslations[this.currentLanguage] || EMPTY_DICT;
3402
3423
  }
3403
3424
  set dictionary(value) {
3404
- this.translations[this.currentLanguage] = value;
3425
+ this.setDictionary(this.currentLang, value);
3405
3426
  this.mergeTranslations();
3406
3427
  }
3407
3428
  get languages() {
@@ -3412,21 +3433,27 @@ class StaticLanguageService {
3412
3433
  }
3413
3434
  set currentLanguage(lang) {
3414
3435
  this.currentLang = lang;
3415
- this.events.languageChanged.emit(lang);
3436
+ this.events.languageChanged.next(lang);
3416
3437
  }
3417
3438
  get editLanguage() {
3418
3439
  return this.editLang || this.currentLanguage;
3419
3440
  }
3420
3441
  set editLanguage(lang) {
3421
3442
  this.editLang = lang || this.currentLanguage;
3422
- this.events.editLanguageChanged.emit(this.editLang);
3443
+ this.events.editLanguageChanged.next(this.editLang);
3444
+ }
3445
+ get enableTranslations() {
3446
+ return this.enableTrans;
3447
+ }
3448
+ set enableTranslations(value) {
3449
+ this.enableTrans = value;
3450
+ this.events.translationsEnabled.next(value);
3423
3451
  }
3424
3452
  get disableTranslations() {
3425
- return this.disableTrans;
3453
+ return !this.enableTranslations;
3426
3454
  }
3427
3455
  set disableTranslations(value) {
3428
- this.disableTrans = value;
3429
- this.events.languageChanged.emit(this.currentLang);
3456
+ this.enableTranslations = !value;
3430
3457
  }
3431
3458
  get httpClient() {
3432
3459
  return this.client;
@@ -3445,7 +3472,7 @@ class StaticLanguageService {
3445
3472
  this.client = client;
3446
3473
  this.editLang = null;
3447
3474
  this.currentLang = null;
3448
- this.disableTrans = false;
3475
+ this.enableTrans = true;
3449
3476
  this.languageList = [];
3450
3477
  this.translations = {
3451
3478
  none: {}
@@ -3462,7 +3489,7 @@ class StaticLanguageService {
3462
3489
  languages = Array.isArray(languages) && languages.length > 0 ? languages : this.languageList;
3463
3490
  this.languageList = Array.from(new Set(languages));
3464
3491
  this.languageList.forEach(lang => {
3465
- this.translations[lang] = this.translations[lang] || emptyDict;
3492
+ this.translations[lang] = this.translations[lang] || EMPTY_DICT;
3466
3493
  });
3467
3494
  }
3468
3495
  addLanguages(languages) {
@@ -3480,12 +3507,24 @@ class StaticLanguageService {
3480
3507
  this.mergedTranslations = this.translations;
3481
3508
  }
3482
3509
  getTranslationSync(key, params = null) {
3483
- key = String(key || "");
3484
- const lowerKey = key.toLocaleLowerCase();
3485
- const translation = !key ? "" : this.dictionary[lowerKey] || key;
3486
- return this.interpolate(translation, params);
3510
+ key = String(key ?? "");
3511
+ if (!key)
3512
+ return "";
3513
+ try {
3514
+ const lowerKey = key.toLocaleLowerCase();
3515
+ const dict = this.dictionary;
3516
+ if (lowerKey in dict && this.enableTranslations) {
3517
+ return this.interpolate(dict[lowerKey], params);
3518
+ }
3519
+ return this.interpolate(key, params);
3520
+ }
3521
+ catch (reason) {
3522
+ console.warn("ERROR IN TRANSLATIONS", reason);
3523
+ return key;
3524
+ }
3487
3525
  }
3488
- async getTranslation(key, params) {
3526
+ async getTranslation(key, params = null) {
3527
+ await this.loadDictionary();
3489
3528
  return this.getTranslationSync(key, params);
3490
3529
  }
3491
3530
  getTranslations(...keys) {
@@ -3507,9 +3546,20 @@ class StaticLanguageService {
3507
3546
  const translation = translations ? translations.find(t => t.lang == lang) : null;
3508
3547
  return this.interpolate(translation ? translation.translation : "", params);
3509
3548
  }
3549
+ async loadDictionary() {
3550
+ return this.dictionary;
3551
+ }
3552
+ setDictionary(lang, dictionary) {
3553
+ this.translations[lang] = Object.keys(dictionary || {}).reduce((res, key) => {
3554
+ res[key.toLocaleLowerCase()] = dictionary[key];
3555
+ return res;
3556
+ }, {});
3557
+ return this.translations[lang];
3558
+ }
3510
3559
  interpolate(expr, params) {
3511
3560
  if (typeof expr === "string") {
3512
- return this.interpolateString(expr, params);
3561
+ // Force single spaces to be empty strings, for labeling in forms.
3562
+ return expr === " " ? "" : this.interpolateString(expr, params);
3513
3563
  }
3514
3564
  if (typeof expr === "function") {
3515
3565
  return expr(params);
@@ -3520,7 +3570,7 @@ class StaticLanguageService {
3520
3570
  if (!expr || !params)
3521
3571
  return expr;
3522
3572
  return expr.replace(/{{\s?([^{}\s]*)\s?}}/g, (substring, b) => {
3523
- const r = ObjectUtils.getValue(params, b);
3573
+ const r = ObjectUtils.getValue(params, b, "");
3524
3574
  return ObjectUtils.isDefined(r) ? r : substring;
3525
3575
  });
3526
3576
  }
@@ -3546,8 +3596,8 @@ class StaticLanguageService {
3546
3596
  ]);
3547
3597
  this.mergedTranslations = Array.from(languages).reduce((merged, language) => {
3548
3598
  merged[language] = {
3549
- ...(this.translations[language] || emptyDict),
3550
- ...(this.overrideTranslations[language] || emptyDict),
3599
+ ...(this.translations[language] || EMPTY_DICT),
3600
+ ...(this.overrideTranslations[language] || EMPTY_DICT),
3551
3601
  };
3552
3602
  return merged;
3553
3603
  }, {});
@@ -3580,7 +3630,7 @@ class LanguageService extends StaticLanguageService {
3580
3630
  }
3581
3631
  set currentLanguage(lang) {
3582
3632
  this.useLanguage(lang).then(() => {
3583
- this.events.languageChanged.emit(lang);
3633
+ this.events.languageChanged.next(lang);
3584
3634
  });
3585
3635
  }
3586
3636
  get settings() {
@@ -3594,7 +3644,8 @@ class LanguageService extends StaticLanguageService {
3594
3644
  }));
3595
3645
  }
3596
3646
  initService() {
3597
- this.client.setExtraRequestParam("language", "de");
3647
+ super.initService();
3648
+ this.client.setExtraRequestParam("language", "en");
3598
3649
  this.translationRequests = {};
3599
3650
  this.languageSettings = new BehaviorSubject(null);
3600
3651
  if (this.universal.isServer)
@@ -3622,32 +3673,32 @@ class LanguageService extends StaticLanguageService {
3622
3673
  if (this.languageList.length === 0) {
3623
3674
  this.languageList = [defaultLanguage];
3624
3675
  }
3625
- const lang = this.languages.indexOf(defaultLanguage) < 0 ? settings.defaultLanguage || this.languageList[0] : defaultLanguage;
3676
+ const lang = this.selectLanguage(this.currentLang)
3677
+ ?? this.selectLanguage(defaultLanguage)
3678
+ ?? this.selectLanguage(settings.defaultLanguage || this.languageList[0]);
3626
3679
  await this.useLanguage(lang);
3627
- this.events.languageChanged.emit(lang);
3680
+ this.events.languageChanged.next(lang);
3628
3681
  }
3629
- async getTranslation(key, params = null) {
3630
- try {
3631
- await this.getDictionary();
3632
- return super.getTranslationSync(key, params);
3633
- }
3634
- catch (reason) {
3635
- console.log("ERROR IN TRANSLATIONS", reason);
3636
- return key;
3637
- }
3682
+ selectLanguage(lang) {
3683
+ if (!lang)
3684
+ return null;
3685
+ return this.languageList.length === 0 || this.languageList.includes(lang)
3686
+ ? lang : null;
3638
3687
  }
3639
3688
  async useLanguage(lang) {
3640
- lang = this.languages.indexOf(lang) < 0 ? this.languages[0] : lang;
3689
+ lang = this.selectLanguage(lang);
3641
3690
  this.client.setExtraRequestParam("language", lang);
3642
- if (lang == this.currentLang)
3691
+ if (lang === this.currentLang)
3643
3692
  return this.dictionary;
3644
3693
  this.storage.set("language", lang);
3645
3694
  this.currentLang = lang;
3646
- return this.getDictionary();
3695
+ return this.loadDictionary();
3647
3696
  }
3648
- getDictionary(lang) {
3649
- lang = this.languages.includes(lang) ? lang : this.currentLanguage;
3650
- this.translationRequests[lang] = this.translationRequests[lang] || firstValueFrom(this.client.get(`${this.config.translationUrl}${lang}`))
3697
+ async getDictionary(lang) {
3698
+ if (!lang)
3699
+ return EMPTY_DICT;
3700
+ const ext = this.config.translationExt || ``;
3701
+ this.translationRequests[lang] = this.translationRequests[lang] || firstValueFrom(this.client.get(`${this.config.translationUrl}${lang}${ext}`))
3651
3702
  .then(response => {
3652
3703
  response = response || {};
3653
3704
  const dictionary = Object.keys(response).reduce((result, key) => {
@@ -3659,12 +3710,16 @@ class LanguageService extends StaticLanguageService {
3659
3710
  return dictionary;
3660
3711
  }).catch(error => {
3661
3712
  console.warn("Translation dictionary problem:", error);
3662
- return {};
3713
+ return EMPTY_DICT;
3663
3714
  });
3664
3715
  return this.translationRequests[lang];
3665
3716
  }
3717
+ async loadDictionary() {
3718
+ return this.getDictionary(this.currentLang);
3719
+ }
3666
3720
  loadSettings() {
3667
- this.settingsPromise = this.settingsPromise || firstValueFrom(this.client.get(`${this.config.translationUrl}languageSettings`))
3721
+ const ext = this.config.translationExt || ``;
3722
+ this.settingsPromise = this.settingsPromise || firstValueFrom(this.client.get(`${this.config.translationUrl}languageSettings${ext}`))
3668
3723
  .catch(error => {
3669
3724
  console.warn("Translation settings problem:", error);
3670
3725
  return {