๐Ÿ“ฆ ionic-team / ionic-framework

๐Ÿ“„ config.ts ยท 46 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46import { Injectable, InjectionToken } from '@angular/core';
import type { Config as CoreConfig, IonicConfig } from '@ionic/core/components';

import { IonicWindow } from '../types/interfaces';

@Injectable({
  providedIn: 'root',
})
export class Config {
  get(key: keyof IonicConfig, fallback?: any): any {
    const c = getConfig();
    if (c) {
      return c.get(key, fallback);
    }
    return null;
  }

  getBoolean(key: keyof IonicConfig, fallback?: boolean): boolean {
    const c = getConfig();
    if (c) {
      return c.getBoolean(key, fallback);
    }
    return false;
  }

  getNumber(key: keyof IonicConfig, fallback?: number): number {
    const c = getConfig();
    if (c) {
      return c.getNumber(key, fallback);
    }
    return 0;
  }
}

export const ConfigToken = new InjectionToken<any>('USERCONFIG');

const getConfig = (): CoreConfig | null => {
  if (typeof (window as any) !== 'undefined') {
    const Ionic = (window as any as IonicWindow).Ionic;
    if (Ionic?.config) {
      return Ionic.config;
    }
  }
  return null;
};