DCSOlympus/client/src/FeatureSwitches.ts
PeekabooSteam 1c1e60146d
Pax1601 main (#52)
* GA initial data

* First commit of crude functionality.

* More AIC work so I don't lose it. (Best commit message ever.)

* Restructured to use 'phrases'.

* Set to a working state.

* Committing so I don't lose work.

* Added ai-formation feature swtich and UI kit stuff.

* Added plane units to UI kit.
2023-02-25 18:03:03 +01:00

134 lines
2.9 KiB
TypeScript

export interface FeatureSwitchInterface {
"defaultEnabled": boolean, // default on/off state (if allowed by masterSwitch)
"label": string,
"masterSwitch": boolean, // on/off regardless of user preference
"name": string,
"options"?: object,
"removeArtifactsIfDisabled"?: boolean
}
class FeatureSwitch {
// From config param
defaultEnabled;
label;
masterSwitch;
name;
removeArtifactsIfDisabled = true;
// Self-set
userPreference;
constructor( config:FeatureSwitchInterface ) {
this.defaultEnabled = config.defaultEnabled;
this.label = config.label;
this.masterSwitch = config.masterSwitch;
this.name = config.name;
this.userPreference = this.getUserPreference();
}
getUserPreference() {
let preferences = JSON.parse( localStorage.getItem( "featureSwitches" ) || "{}" );
return ( preferences.hasOwnProperty( this.name ) ) ? preferences[ this.name ] : this.defaultEnabled;
}
isEnabled() {
if ( !this.masterSwitch ) {
return false;
}
return this.userPreference;
}
}
export class FeatureSwitches {
#featureSwitches:FeatureSwitch[] = [
new FeatureSwitch({
"defaultEnabled": false,
"label": "AIC",
"masterSwitch": true,
"name": "aic"
}),
new FeatureSwitch({
"defaultEnabled": false,
"label": "AI Formations",
"masterSwitch": true,
"name": "ai-formations",
"removeArtifactsIfDisabled": false
}),
new FeatureSwitch({
"defaultEnabled": false,
"label": "ATC",
"masterSwitch": true,
"name": "atc"
})
];
constructor() {
this.#removeArtifacts();
this.savePreferences();
}
getSwitch( switchName:string ) {
return this.#featureSwitches.find( featureSwitch => featureSwitch.name === switchName );
}
#removeArtifacts() {
for ( const featureSwitch of this.#featureSwitches ) {
if ( !featureSwitch.isEnabled() ) {
document.querySelectorAll( "[data-feature-switch='" + featureSwitch.name + "']" ).forEach( el => {
if ( featureSwitch.removeArtifactsIfDisabled === false ) {
el.remove();
} else {
el.classList.add( "hide" );
}
});
}
}
}
savePreferences() {
let preferences:any = {};
for ( const featureSwitch of this.#featureSwitches ) {
preferences[ featureSwitch.name ] = featureSwitch.isEnabled();
}
localStorage.setItem( "featureSwitches", JSON.stringify( preferences ) );
}
}