### File

`projects/app-base-library/src/lib/shared/config.ts`

### Index

###### Properties

- _config
- _defaults
- _locked

###### Methods

- Public add
- Public all
- Public extend
- Public Async fetch
- Public get
- Public getLocalSettings
- initXdomain
- Public islocked
- Public lock
- Public restoreDefaults
- Public set
- Public setDefaults
- Public setLocalSettings
- Public unLock

### Constructor

`constructor()`
Defined in projects/app-base-library/src/lib/shared/config.ts:8

### Properties

_config
`_config: `any``
Type : `any`
Default value : `{}`
Defined in projects/app-base-library/src/lib/shared/config.ts:6

_defaults
`_defaults: `any``
Type : `any`
Defined in projects/app-base-library/src/lib/shared/config.ts:7

_locked
`_locked: `[]``
Type : `[]`
Default value : `['_config', '_defaults']`
Defined in projects/app-base-library/src/lib/shared/config.ts:8

### Methods

Public add
`add(object: any, extend: , castToType: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:27

Parameters :

Name | Type | Optional | Default value
object | `any` | no |
extend |  | no | `false`
castToType |  | no | `false`

Returns : `void`

Public all
`all()`
Defined in projects/app-base-library/src/lib/shared/config.ts:66
Returns : `any`

Public extend
`extend(object: any, target: any)`
Defined in projects/app-base-library/src/lib/shared/config.ts:62

Parameters :

Name | Type | Optional | Default value
object | `any` | no |
target | `any` | no | `this._config`

Returns : `void`

Public Async fetch
`fetch(url: , extend: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:38

Parameters :

Name | Optional | Default value
url | no |
extend | no | `false`

Returns : `{}`

Public get
`get(key: any)`
Defined in projects/app-base-library/src/lib/shared/config.ts:12

Parameters :

Name | Type | Optional
key | `any` | no

Returns : `any`

Public getLocalSettings
`getLocalSettings(id: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:70

Parameters :

Name | Optional
id | no

Returns : `void`

initXdomain
`initXdomain(object: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:125

Parameters :

Name | Optional
object | no

Returns : `void`

Public islocked
`islocked(key: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:96

Parameters :

Name | Optional
key | no

Returns : `void`

Public lock
`lock(key: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:110

Parameters :

Name | Optional
key | no

Returns : `boolean`

Public restoreDefaults
`restoreDefaults()`
Defined in projects/app-base-library/src/lib/shared/config.ts:89
Returns : `void`

Public set
`set(key: any, value: any, castToType: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:16

Parameters :

Name | Type | Optional | Default value
key | `any` | no |
value | `any` | no |
castToType |  | no | `false`

Returns : `any`

Public setDefaults
`setDefaults(configDefaults: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:81

Parameters :

Name | Optional
configDefaults | no

Returns : `void`

Public setLocalSettings
`setLocalSettings(id: , settings: any)`
Defined in projects/app-base-library/src/lib/shared/config.ts:75

Parameters :

Name | Type | Optional | Default value
id |  | no |
settings | `any` | no | `this._config`

Returns : `void`

Public unLock
`unLock(key: )`
Defined in projects/app-base-library/src/lib/shared/config.ts:117

Parameters :

Name | Optional
key | no

Returns : `void`

    import { Util } from './util/util';
    import { Storage } from './util/storage';

    export class Config {

        _config: any = {};
        _defaults: any;
        _locked = ['_config', '_defaults'];

        constructor() { }

        public get(key: any) {
            return this._config[key];
        }

        public set(key: any, value: any, castToType = false) {
            if (!this.islocked(key)) {
                if (castToType) {
                    value = Util.castToType(value);
                }
                return this._config[key] = value;
            } else {
                console.error('Config: property "'+key+'" can not be changed.');
            }
        }

        public add(object: any, extend = false, castToType = false) {
            const self = this;
            if(!extend) {
                Object.keys(object).forEach((key) => {
                    self.set(key, object[key] ,castToType);
                });
            }else{
                this.extend(object);
            }
        }

        public async fetch(url, extend = false) {
            const self = this;
            // if(self.get('api_headers')){
            //
            // }
            // let config =  await fetch(url, {headers: self.get('api_headers')})
            let config =  await fetch(url,)
                .then(function(response) {
                    return response.json();
                })
                .catch(function(error) {
                    console.log('config fetch error:', error.message);
                });

            if (extend) {
                this.extend(config);
            }else {
                Object.keys(config).forEach((k, i) => {
                    self.set(k, config[k]);
                });
            }
            return config;
        }

        public extend(object: any, target: any = this._config) {
            Util.deepAssign(target, object);
        }

        public all() {
            return this._config;
        }

        public getLocalSettings(id) {
            if (window.localStorage && window.localStorage.getItem(id)) {
                this.add(Storage.restoreLocalModel(id));
            }
        }
        public setLocalSettings(id, settings:any = this._config) {
            if (window.localStorage) {
                Storage.saveLocalModel(settings, id);
            }
        }

        public setDefaults(configDefaults) {
            const self = this;
            self._defaults = configDefaults;
            Object.keys(configDefaults).forEach((k) => {
                self.set(k, Util.copy(configDefaults[k]));
            });
        }

        public restoreDefaults() {
            const self = this;
            Object.keys(self._defaults).forEach((k) => {
                self.set(k, self._defaults[k]);
            });
        }

        public islocked(key) {
            // todo:
            this._locked.forEach((k) => {
                if(k===key){
                    console.log('Config error: property "'+k+'" is protected');
                    return true;
                }
            })
        }

        // todo
        public lock(key) {
            // todo:
            return false;
            // this._locked.forEach((k) => {
            //
            // })
        }
        public unLock(key) {
            // todo:
            // this._locked.forEach((k) => {
            //
            // })
        }

        initXdomain(object) {
            if (typeof window !== 'undefined' && window['xdomain']) {
                window['xdomain'].masters(object.masters);
                window['xdomain'].slaves(object.slaves);
            }
        };

    }

# results matching ""

# No results matching ""