1
|
import {Component, ElementRef, OnInit} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
|
4
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
5
|
|
6
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
7
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
8
|
import {DomSanitizer, Title} from '@angular/platform-browser';
|
9
|
import {CustomizationOptions, Layout} from '../../openaireLibrary/connect/community/CustomizationOptions';
|
10
|
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
|
11
|
import {LayoutService} from '../../openaireLibrary/services/layout.service';
|
12
|
import {properties} from '../../../environments/environment';
|
13
|
import {UtilitiesService} from '../../openaireLibrary/services/utilities.service';
|
14
|
import {Subscription} from 'rxjs';
|
15
|
|
16
|
declare var UIkit;
|
17
|
|
18
|
@Component({
|
19
|
selector: 'customization',
|
20
|
templateUrl: './customization.component.html',
|
21
|
styles:[`
|
22
|
.refresh-indicator {
|
23
|
background-color: rgba(0, 0, 0, 0.50);
|
24
|
border-radius: 4px;
|
25
|
position: absolute;
|
26
|
color: white;
|
27
|
}
|
28
|
iframe, .refresh-indicator{
|
29
|
height:900px;
|
30
|
}
|
31
|
`]
|
32
|
})
|
33
|
|
34
|
export class CustomizationComponent implements OnInit {
|
35
|
|
36
|
menuSelected = 'identity';
|
37
|
color = 'white';
|
38
|
defaultCustomizationOptions:CustomizationOptions = new CustomizationOptions();
|
39
|
publishedLayout: Layout = null;
|
40
|
publishedCustomizationOptions: CustomizationOptions = null;
|
41
|
draftCustomizationOptions: CustomizationOptions = null;
|
42
|
appliedCustomizationOptions: CustomizationOptions = null;
|
43
|
previewUrl = null;
|
44
|
previewCustomization = null;
|
45
|
|
46
|
|
47
|
public showLoading = true;
|
48
|
|
49
|
|
50
|
public communityId = null;
|
51
|
|
52
|
public properties: EnvProperties = null;
|
53
|
private subscriptions: any[] = [];
|
54
|
|
55
|
public enabled = true;
|
56
|
|
57
|
constructor(private element: ElementRef,
|
58
|
private route: ActivatedRoute,
|
59
|
private _router: Router,
|
60
|
private title: Title,
|
61
|
private sanitizer: DomSanitizer,
|
62
|
private layoutService: LayoutService,
|
63
|
private utilsService: UtilitiesService) {
|
64
|
}
|
65
|
|
66
|
ngOnDestroy() {
|
67
|
this.deleteDraftImages();
|
68
|
|
69
|
}
|
70
|
cleanUp(){
|
71
|
this.subscriptions.forEach(subscription => {
|
72
|
if (subscription instanceof Subscription) {
|
73
|
subscription.unsubscribe();
|
74
|
}
|
75
|
});
|
76
|
}
|
77
|
ngOnInit() {
|
78
|
this.properties = properties;
|
79
|
if (!Session.isLoggedIn()) {
|
80
|
this._router.navigate(['/user-info'], {
|
81
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
82
|
});
|
83
|
} else {
|
84
|
this.subscriptions.push(this.route.params.subscribe((params) => {
|
85
|
this.communityId = params['community'];
|
86
|
if(this.communityId == "covid-19"){
|
87
|
this.defaultCustomizationOptions= new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor,CustomizationOptions.getIdentity(this.communityId).secondaryColor);
|
88
|
}
|
89
|
this.title.setTitle('Administration Dashboard | Customization');
|
90
|
this.showLoading = true;
|
91
|
this.subscriptions.push(this.layoutService.getLayout(this.properties, this.communityId).subscribe(layout => {
|
92
|
this.publishedLayout = (layout?layout:new Layout(this.communityId,this.defaultCustomizationOptions));
|
93
|
this.publishedCustomizationOptions = (layout?CustomizationOptions.checkForObsoleteVersion(layout.layoutOptions,this.communityId):Object.assign({},this.defaultCustomizationOptions));
|
94
|
this.initializeCustomizationOptions(true);
|
95
|
}, error => {
|
96
|
this.publishedCustomizationOptions = new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor,CustomizationOptions.getIdentity(this.communityId).secondaryColor);
|
97
|
this.initializeCustomizationOptions(true);
|
98
|
UIkit.notification("An error occured fetching customizations options", {
|
99
|
status: 'danger',
|
100
|
timeout: 6000,
|
101
|
pos: 'bottom-right'
|
102
|
});
|
103
|
}));
|
104
|
}));
|
105
|
}
|
106
|
|
107
|
|
108
|
}
|
109
|
hasChanges(object1,object2):boolean{
|
110
|
return JSON.stringify(object1) != JSON.stringify(object2);
|
111
|
}
|
112
|
saveLayout() {
|
113
|
if (!Session.isLoggedIn()) {
|
114
|
this._router.navigate(['/user-info'], {
|
115
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
116
|
});
|
117
|
}
|
118
|
this.publishedLayout.layoutOptions = this.draftCustomizationOptions;
|
119
|
this.subscriptions.push(this.layoutService.saveLayout(this.properties, this.communityId, this.publishedLayout).subscribe(layout => {
|
120
|
this.deleteOldImages();
|
121
|
this.publishedLayout = layout;
|
122
|
this.publishedCustomizationOptions = layout.layoutOptions;
|
123
|
this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
|
124
|
UIkit.notification("Customizations was succesfully saved!", {
|
125
|
status: 'success',
|
126
|
timeout: 6000,
|
127
|
pos: 'bottom-right'
|
128
|
});
|
129
|
}, error => {
|
130
|
|
131
|
UIkit.notification("An error occured on save", {
|
132
|
status: 'danger',
|
133
|
timeout: 6000,
|
134
|
pos: 'bottom-right'
|
135
|
});
|
136
|
}));
|
137
|
}
|
138
|
private deleteOldImages( ) {
|
139
|
if( this.publishedCustomizationOptions.backgrounds.form.imageFile && this.draftCustomizationOptions.backgrounds.form.imageFile !=this.publishedCustomizationOptions.backgrounds.form.imageFile){
|
140
|
this.deleteImage(this.publishedCustomizationOptions.backgrounds.form.imageFile)
|
141
|
}
|
142
|
}
|
143
|
private deleteDraftImages( ) {
|
144
|
console.log("delete drafr images")
|
145
|
if( this.draftCustomizationOptions.backgrounds.form.imageFile && this.draftCustomizationOptions.backgrounds.form.imageFile !=this.publishedCustomizationOptions.backgrounds.form.imageFile){
|
146
|
this.subscriptions.push(this.utilsService.deletePhoto(this.properties.utilsService + '/delete/stakeholder/' + this.draftCustomizationOptions.backgrounds.form.imageFile).subscribe(
|
147
|
re => {
|
148
|
console.log("delete!!!!!!!!")
|
149
|
this.cleanUp()
|
150
|
}
|
151
|
));
|
152
|
}else{
|
153
|
this.cleanUp()
|
154
|
}
|
155
|
}
|
156
|
private deleteImage( filename:string) {
|
157
|
this.subscriptions.push(this.utilsService.deletePhoto(this.properties.utilsService + '/delete/stakeholder/' + filename).subscribe());
|
158
|
}
|
159
|
initializeCustomizationOptions(updatePreviewUrl) {
|
160
|
this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
161
|
this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
162
|
if(updatePreviewUrl){
|
163
|
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
|
164
|
}
|
165
|
|
166
|
}
|
167
|
|
168
|
applyLayout() {
|
169
|
this.appliedCustomizationOptions = this.copyObject(this.draftCustomizationOptions);
|
170
|
if(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization){
|
171
|
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
|
172
|
}
|
173
|
}
|
174
|
|
175
|
resetLayout() {
|
176
|
this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
|
177
|
}
|
178
|
resetBackgroundsTo(backgrounds) {
|
179
|
this.draftCustomizationOptions.backgrounds.dark.color = backgrounds.dark.color;
|
180
|
this.draftCustomizationOptions.backgrounds.light.color = backgrounds.light.color;
|
181
|
this.draftCustomizationOptions.backgrounds.form.color = backgrounds.form.color;
|
182
|
|
183
|
}
|
184
|
|
185
|
resetBackgroundsAndButtonsTo(c:CustomizationOptions){
|
186
|
this.resetBackgroundsTo(c.backgrounds);
|
187
|
this.resetButtonsTo(c.buttons);
|
188
|
}
|
189
|
resetBackgroundsAndButtonsToPublished(){
|
190
|
this.resetBackgroundsAndButtonsTo(this.publishedCustomizationOptions);
|
191
|
}
|
192
|
resetBackgroundsAndButtonsToDefault(){
|
193
|
this.resetBackgroundsAndButtonsTo(this.publishedCustomizationOptions);
|
194
|
this.updateBackgroundsAndButtonsBasedOnIdentity();
|
195
|
}
|
196
|
|
197
|
resetButtonsTo(buttonsToRevert) {
|
198
|
this.draftCustomizationOptions.buttons= this.copyObject(buttonsToRevert);
|
199
|
}
|
200
|
|
201
|
resetIdentityTo( original:CustomizationOptions) {
|
202
|
this.draftCustomizationOptions.identity.mainColor = this.copyObject(original.identity.mainColor);
|
203
|
this.draftCustomizationOptions.identity.secondaryColor = this.copyObject(original.identity.secondaryColor);
|
204
|
}
|
205
|
resetIdentityToPublished() {
|
206
|
this.resetIdentityTo( this.publishedCustomizationOptions);
|
207
|
this.updateBackgroundsAndButtonsBasedOnIdentity()
|
208
|
}
|
209
|
resetIdentityToDefault() {
|
210
|
this.resetIdentityTo( this.defaultCustomizationOptions);
|
211
|
this.draftCustomizationOptions.identityIsCustom = false;
|
212
|
this.updateBackgroundsAndButtonsBasedOnIdentity()
|
213
|
|
214
|
}
|
215
|
updateBackgroundsAndButtonsBasedOnIdentity(){
|
216
|
if(!this.draftCustomizationOptions.backgroundsAndButtonsIsCustom){
|
217
|
let tmp = new CustomizationOptions(this.draftCustomizationOptions.identity.mainColor, this.draftCustomizationOptions.identity.secondaryColor);
|
218
|
this.resetBackgroundsAndButtonsTo(tmp);
|
219
|
}
|
220
|
}
|
221
|
getCommunityUrlSatinized(layout: string) {
|
222
|
return this.sanitizer.bypassSecurityTrustResourceUrl(this.getCommunityUrlNewLayout(layout));
|
223
|
}
|
224
|
getCommunityUrl() {
|
225
|
/* if(this.properties.environment == "development") {
|
226
|
return 'http://spitoo.di.uoa.gr:4200';
|
227
|
}*/
|
228
|
return 'https://'+ (this.properties.environment == 'production'?'':'beta.')+this.communityId+'.openaire.eu';
|
229
|
}
|
230
|
|
231
|
getCommunityUrlNewLayout(layout: string) {
|
232
|
this.previewCustomization = layout;
|
233
|
return this.getCommunityUrl()+'/preview/?' + 'layout=' + StringUtils.URIEncode(layout);
|
234
|
}
|
235
|
|
236
|
copyObject(obj) {
|
237
|
return JSON.parse(JSON.stringify(obj));
|
238
|
}
|
239
|
|
240
|
}
|