Skip to content

Commit 080ea9f

Browse files
committed
Change theme on the server.
1 parent 2290af6 commit 080ea9f

File tree

3 files changed

+117
-19
lines changed

3 files changed

+117
-19
lines changed

angular/src/app/layout/right-sidebar.component.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
import { Component, Injector, ViewEncapsulation } from '@angular/core';
1+
import { Component, Injector, ViewEncapsulation, OnInit } from '@angular/core';
22
import { AppComponentBase } from '@shared/app-component-base';
3-
4-
class UiThemeInfo {
5-
constructor(
6-
public name: string,
7-
public cssClass: string
8-
) {
9-
10-
}
11-
}
3+
import { ConfigurationServiceProxy, ChangeUiThemeInput } from '@shared/service-proxies/service-proxies';
124

135
@Component({
146
templateUrl: './right-sidebar.component.html',
157
selector: 'right-sidebar',
168
encapsulation: ViewEncapsulation.None
179
})
18-
export class RightSideBarComponent extends AppComponentBase {
10+
export class RightSideBarComponent extends AppComponentBase implements OnInit {
1911

2012
themes: UiThemeInfo[] = [
2113
new UiThemeInfo("Red", "red"),
@@ -43,19 +35,36 @@ export class RightSideBarComponent extends AppComponentBase {
4335
selectedThemeCssClass: string = "red";
4436

4537
constructor(
46-
injector: Injector
38+
injector: Injector,
39+
private _configurationService: ConfigurationServiceProxy
4740
) {
4841
super(injector);
4942
}
5043

44+
ngOnInit(): void {
45+
this.selectedThemeCssClass = this.setting.get('App.UiTheme');
46+
$('body').addClass('theme-' + this.selectedThemeCssClass);
47+
}
48+
5149
setTheme(theme: UiThemeInfo): void {
52-
const $body = $('body');
50+
const input = new ChangeUiThemeInput();
51+
input.theme = theme.cssClass;
52+
this._configurationService.changeUiTheme(input).subscribe(() => {
53+
const $body = $('body');
5354

54-
$('.right-sidebar .demo-choose-skin li').removeClass('active');
55-
$body.removeClass('theme-' + this.selectedThemeCssClass);
56-
$('.right-sidebar .demo-choose-skin li div.' + theme.cssClass).closest('li').addClass('active');
57-
$body.addClass('theme-' + theme.cssClass);
55+
$('.right-sidebar .demo-choose-skin li').removeClass('active');
56+
$body.removeClass('theme-' + this.selectedThemeCssClass);
57+
$('.right-sidebar .demo-choose-skin li div.' + theme.cssClass).closest('li').addClass('active');
58+
$body.addClass('theme-' + theme.cssClass);
5859

59-
this.selectedThemeCssClass = theme.cssClass;
60+
this.selectedThemeCssClass = theme.cssClass;
61+
});
6062
}
63+
}
64+
65+
class UiThemeInfo {
66+
constructor(
67+
public name: string,
68+
public cssClass: string
69+
) { }
6170
}

angular/src/shared/service-proxies/service-proxies.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,66 @@ export class AccountServiceProxy {
121121
}
122122
}
123123

124+
@Injectable()
125+
export class ConfigurationServiceProxy {
126+
private http: Http = null;
127+
private baseUrl: string = undefined;
128+
protected jsonParseReviver: (key: string, value: any) => any = undefined;
129+
130+
constructor(@Inject(Http) http: Http, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
131+
this.http = http;
132+
this.baseUrl = baseUrl ? baseUrl : "";
133+
}
134+
135+
/**
136+
* @return Success
137+
*/
138+
changeUiTheme(input: ChangeUiThemeInput): Observable<void> {
139+
let url_ = this.baseUrl + "/api/services/app/Configuration/ChangeUiTheme";
140+
141+
const content_ = JSON.stringify(input ? input.toJS() : null);
142+
143+
return this.http.request(url_, {
144+
body: content_,
145+
method: "post",
146+
headers: new Headers({
147+
"Content-Type": "application/json; charset=UTF-8",
148+
"Accept": "application/json; charset=UTF-8"
149+
})
150+
}).map((response) => {
151+
return this.processChangeUiTheme(response);
152+
}).catch((response: any, caught: any) => {
153+
if (response instanceof Response) {
154+
try {
155+
return Observable.of(this.processChangeUiTheme(response));
156+
} catch (e) {
157+
return <Observable<void>><any>Observable.throw(e);
158+
}
159+
} else
160+
return <Observable<void>><any>Observable.throw(response);
161+
});
162+
}
163+
164+
protected processChangeUiTheme(response: Response): void {
165+
const responseText = response.text();
166+
const status = response.status;
167+
168+
if (status === 200) {
169+
return null;
170+
} else if (status !== 200 && status !== 204) {
171+
this.throwException("An unexpected server error occurred.", status, responseText);
172+
}
173+
return null;
174+
}
175+
176+
protected throwException(message: string, status: number, response: string, result?: any): any {
177+
if(result !== null && result !== undefined)
178+
throw result;
179+
else
180+
throw new SwaggerException(message, status, response);
181+
}
182+
}
183+
124184
@Injectable()
125185
export class RoleServiceProxy {
126186
private http: Http = null;
@@ -826,6 +886,34 @@ export class RegisterOutput {
826886
}
827887
}
828888

889+
export class ChangeUiThemeInput {
890+
theme: string;
891+
constructor(data?: any) {
892+
if (data !== undefined) {
893+
this.theme = data["theme"] !== undefined ? data["theme"] : null;
894+
}
895+
}
896+
897+
static fromJS(data: any): ChangeUiThemeInput {
898+
return new ChangeUiThemeInput(data);
899+
}
900+
901+
toJS(data?: any) {
902+
data = data === undefined ? {} : data;
903+
data["theme"] = this.theme !== undefined ? this.theme : null;
904+
return data;
905+
}
906+
907+
toJSON() {
908+
return JSON.stringify(this.toJS());
909+
}
910+
911+
clone() {
912+
const json = this.toJSON();
913+
return new ChangeUiThemeInput(JSON.parse(json));
914+
}
915+
}
916+
829917
export class UpdateRolePermissionsInput {
830918
roleId: number;
831919
grantedPermissionNames: string[] = [];

angular/src/shared/service-proxies/service-proxy.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import * as ApiServiceProxies from './service-proxies';
99
ApiServiceProxies.TenantServiceProxy,
1010
ApiServiceProxies.UserServiceProxy,
1111
ApiServiceProxies.TokenAuthServiceProxy,
12-
ApiServiceProxies.AccountServiceProxy
12+
ApiServiceProxies.AccountServiceProxy,
13+
ApiServiceProxies.ConfigurationServiceProxy
1314
]
1415
})
1516
export class ServiceProxyModule { }

0 commit comments

Comments
 (0)