| |
|
|
| const Config = require( '../config' ); |
| const constants = require( './constants' ); |
| const settingsFile = require( './settings-file' ); |
|
|
| |
| |
| |
| let settings = false; |
|
|
| function Settings() { |
| this.settings = false; |
| } |
|
|
| Settings.prototype._getAll = function () { |
| if ( this.settings === false ) { |
| this.settings = settingsFile.load(); |
| } |
|
|
| return this.settings; |
| }; |
|
|
| Settings.prototype.isDebug = function () { |
| if ( typeof this._getAll().debug !== 'undefined' ) { |
| return this._getAll().debug; |
| } |
| return Config.debug.enabled_by_default; |
| }; |
|
|
| |
| |
| |
| |
| |
| Settings.prototype.getSetting = function ( setting ) { |
| if ( process.env.WP_DESKTOP_BASE_URL !== undefined && setting === constants.LAST_LOCATION ) { |
| return process.env.WP_DESKTOP_BASE_URL; |
| } |
|
|
| const value = this._getAll()[ setting ]; |
|
|
| if ( typeof value === 'undefined' ) { |
| if ( typeof Config.default_settings[ setting ] !== 'undefined' ) { |
| return Config.default_settings[ setting ]; |
| } |
| return false; |
| } |
|
|
| return value; |
| }; |
|
|
| |
| |
| |
| Settings.prototype.getSettingGroup = function ( existing, group, values ) { |
| const settingsGroup = this._getAll()[ group ]; |
|
|
| if ( typeof settingsGroup !== 'undefined' ) { |
| if ( values instanceof Array ) { |
| const updated = {}; |
| for ( let x = 0; x < values.length; x++ ) { |
| const value = values[ x ]; |
| existing[ value ] = settingsGroup[ value ]; |
| updated[ value ] = settingsGroup[ value ]; |
| } |
| } else { |
| return settingsGroup; |
| } |
| } |
|
|
| return existing; |
| }; |
|
|
| Settings.prototype.saveSetting = function ( group, groupData ) { |
| if ( process.env.WP_DESKTOP_DEBUG !== undefined && group === constants.LAST_LOCATION ) { |
| return; |
| } |
| this.settings = settingsFile.save( group, groupData ); |
| }; |
|
|
| |
| Settings.prototype.toRenderer = function () { |
| const all = this._getAll(); |
| const exported = {}; |
| if ( all ) { |
| for ( const [ key ] of Object.entries( all ) ) { |
| exported[ key ] = this.getSetting( key ); |
| } |
| } |
| return exported; |
| }; |
|
|
| if ( ! settings ) { |
| settings = new Settings(); |
| } |
|
|
| module.exports = settings; |
|
|