fix: Working of theme selector (#2285)
This commit is contained in:
@@ -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 '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
|
||||
// Import Default Theme for MifosX
|
||||
@use 'theme/mifosx-theme';
|
||||
@use 'theme/pictonblue-yellowgreen' as *;
|
||||
@use 'theme/indigo-pink' as *;
|
||||
@use 'theme/pink-bluegrey' as *;
|
||||
@use 'theme/deeppurple-amber' as *;
|
||||
@use 'theme/purple-green.scss' as *;
|
||||
|
||||
// Global Imports
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
@use '../app/shares/shares-account-view/shares-account-view.component-theme.scss' as *;
|
||||
@use 'content' as *;
|
||||
@use 'dark_content' as *;
|
||||
@use 'pictonblue-yellowgreen' as *;
|
||||
|
||||
// Include the common styles for Angular Material. We include this here so that you only
|
||||
// have to load a single css file for Angular Material in your app.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user