-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Working of theme selector (#2285)
- Loading branch information
1 parent
ee62384
commit daa2c1a
Showing
8 changed files
with
108 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,73 @@ | ||
/** Angular Imports */ | ||
import { Injectable, EventEmitter } from '@angular/core'; | ||
|
||
/** Custom Model */ | ||
import { Theme } from './theme.model'; | ||
|
||
/** Custom Services */ | ||
import { ThemeManagerService } from './theme-manager.service'; | ||
|
||
/** | ||
* Theme storage service. | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ThemeStorageService { | ||
/** Key to store current theme of application in local storage. */ | ||
private themeStorageKey = 'mifosXTheme'; | ||
/** Theme update event. */ | ||
onThemeUpdate: EventEmitter<Theme>; | ||
|
||
/** | ||
* Initializes theme update event. | ||
* @param {ThemeManagerService} themeManagerService | ||
*/ | ||
constructor(public themeManagerService: ThemeManagerService) { | ||
this.onThemeUpdate = new EventEmitter<Theme>(); | ||
} | ||
|
||
/** | ||
* Stores current theme in local storage and emits a theme update event. | ||
* @param mifosXTheme | ||
*/ | ||
storeTheme(mifosXTheme: Theme) { | ||
localStorage.setItem(this.themeStorageKey, JSON.stringify(mifosXTheme)); | ||
this.onThemeUpdate.emit(mifosXTheme); | ||
} | ||
|
||
/** | ||
* @returns current theme from local storage | ||
*/ | ||
getTheme(): Theme { | ||
return JSON.parse(localStorage.getItem(this.themeStorageKey)); | ||
} | ||
|
||
/** | ||
* Removes current theme from local storage. | ||
*/ | ||
clearTheme() { | ||
localStorage.removeItem(this.themeStorageKey); | ||
} | ||
|
||
/** | ||
* Sets a new theme for the application and stores in local storage. | ||
* Dynamically installs the theme by adding a class to the body. | ||
* @param {Theme} theme | ||
*/ | ||
installTheme(theme: Theme) { | ||
if (theme.isDefault) { | ||
this.themeManagerService.removeTheme(); | ||
} else { | ||
this.themeManagerService.setTheme(`theme/${theme.href}`); | ||
const body = document.body; | ||
|
||
// Remove any previously applied theme classes | ||
body.classList.remove( | ||
'pictonblue-yellowgreen-theme', | ||
'indigo-pink-theme', | ||
'deeppurple-amber-theme', | ||
'pink-bluegrey-theme', | ||
'purple-green-theme' | ||
); | ||
|
||
if (!theme.isDefault) { | ||
body.classList.add(this.getThemeClass(theme.href)); | ||
} | ||
|
||
this.storeTheme(theme); | ||
} | ||
|
||
/** | ||
* Maps theme file names to their respective CSS class. | ||
* @param themeHref The theme file name. | ||
* @returns The CSS class name for the theme. | ||
*/ | ||
private getThemeClass(themeHref: string): string { | ||
switch (themeHref) { | ||
case 'pictonblue-yellowgreen.css': | ||
return 'pictonblue-yellowgreen-theme'; | ||
case 'indigo-pink.css': | ||
return 'indigo-pink-theme'; | ||
case 'deeppurple-amber.css': | ||
return 'deeppurple-amber-theme'; | ||
case 'pink-bluegrey.css': | ||
return 'pink-bluegrey-theme'; | ||
case 'purple-green.css': | ||
return 'purple-green-theme'; | ||
default: | ||
return ''; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@use '@angular/material' as mat; | ||
|
||
$deeppurple-amber-primary: mat.define-palette(mat.$deep-purple-palette, 500, 300, 700); | ||
$deeppurple-amber-accent: mat.define-palette(mat.$amber-palette, 400, 200, 600); | ||
$deeppurple-amber-warn: mat.define-palette(mat.$red-palette); | ||
|
||
$deeppurple-amber-theme: mat.define-light-theme( | ||
$deeppurple-amber-primary, | ||
$deeppurple-amber-accent, | ||
$deeppurple-amber-warn | ||
); | ||
|
||
.deeppurple-amber-theme { | ||
@include mat.all-component-themes($deeppurple-amber-theme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@use '@angular/material' as mat; | ||
|
||
$indigo-pink-primary: mat.define-palette(mat.$indigo-palette, 500, 300, 700); | ||
$indigo-pink-accent: mat.define-palette(mat.$pink-palette, 400, 200, 600); | ||
$indigo-pink-warn: mat.define-palette(mat.$red-palette); | ||
|
||
$indigo-pink-theme: mat.define-light-theme($indigo-pink-primary, $indigo-pink-accent, $indigo-pink-warn); | ||
|
||
.indigo-pink-theme { | ||
@include mat.all-component-themes($indigo-pink-theme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@use '@angular/material' as mat; | ||
|
||
$pictonblue-yellowgreen-primary: mat.define-palette(mat.$blue-palette, 400, 200, 600); | ||
|
||
$pictonblue-yellowgreen-accent: mat.define-palette(mat.$light-green-palette, 400, 200, 600); | ||
|
||
$pictonblue-yellowgreen-warn: mat.define-palette(mat.$red-palette); | ||
|
||
$pictonblue-yellowgreen-theme: mat.define-light-theme( | ||
$pictonblue-yellowgreen-primary, | ||
$pictonblue-yellowgreen-accent, | ||
$pictonblue-yellowgreen-warn | ||
); | ||
|
||
.pictonblue-yellowgreen-theme { | ||
@include mat.all-component-themes($pictonblue-yellowgreen-theme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@use '@angular/material' as mat; | ||
|
||
$pink-bluegrey-primary: mat.define-palette(mat.$pink-palette, 500, 300, 700); | ||
$pink-bluegrey-accent: mat.define-palette(mat.$blue-grey-palette, 400, 200, 600); | ||
$pink-bluegrey-warn: mat.define-palette(mat.$red-palette); | ||
|
||
$pink-bluegrey-theme: mat.define-light-theme($pink-bluegrey-primary, $pink-bluegrey-accent, $pink-bluegrey-warn); | ||
|
||
.pink-bluegrey-theme { | ||
@include mat.all-component-themes($pink-bluegrey-theme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@use '@angular/material' as mat; | ||
|
||
$purple-green-primary: mat.define-palette(mat.$purple-palette, 500, 300, 700); | ||
$purple-green-accent: mat.define-palette(mat.$green-palette, 400, 200, 600); | ||
$purple-green-warn: mat.define-palette(mat.$red-palette); | ||
|
||
$purple-green-theme: mat.define-light-theme($purple-green-primary, $purple-green-accent, $purple-green-warn); | ||
|
||
.purple-green-theme { | ||
@include mat.all-component-themes($purple-green-theme); | ||
} |