1
|
import {Component, ElementRef, OnInit, ViewChild} 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} from '../../openaireLibrary/connect/community/CustomizationOptions';
|
10
|
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
|
11
|
import {LayoutService} from '../../openaireLibrary/services/layout.service';
|
12
|
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
|
13
|
import {properties} from '../../../environments/environment';
|
14
|
|
15
|
@Component({
|
16
|
selector: 'customization',
|
17
|
templateUrl: './customization.component.html',
|
18
|
})
|
19
|
|
20
|
export class CustomizationComponent implements OnInit {
|
21
|
|
22
|
menuSelected = 'identity';
|
23
|
color = 'white';
|
24
|
defaultCustomizationOptions:CustomizationOptions = new CustomizationOptions();
|
25
|
publishedCustomizationOptions: CustomizationOptions = null;
|
26
|
draftCustomizationOptions: CustomizationOptions = null;
|
27
|
appliedCustomizationOptions: CustomizationOptions = null;
|
28
|
previewUrl = null;
|
29
|
previewCustomization = null;
|
30
|
buttonDarkBackgroundPreview;
|
31
|
buttonLightBackgroundPreview;
|
32
|
linkDarkBackgroundPreview;
|
33
|
linkLightBackgroundPreview;
|
34
|
hoveredText;
|
35
|
public showLoading = true;
|
36
|
public errorMessage = '';
|
37
|
public successfulSaveMessage = '';
|
38
|
|
39
|
|
40
|
public communityId = null;
|
41
|
|
42
|
public properties: EnvProperties = null;
|
43
|
|
44
|
public enabled = true;
|
45
|
@ViewChild('backAlert') backAlert: AlertModal;
|
46
|
@ViewChild('exitAlert') exitAlert: AlertModal;
|
47
|
|
48
|
constructor(private element: ElementRef,
|
49
|
private route: ActivatedRoute,
|
50
|
private _router: Router,
|
51
|
private title: Title,
|
52
|
private sanitizer: DomSanitizer,
|
53
|
private layoutService: LayoutService) {
|
54
|
}
|
55
|
|
56
|
|
57
|
ngOnInit() {
|
58
|
this.properties = properties;
|
59
|
if (!Session.isLoggedIn()) {
|
60
|
this._router.navigate(['/user-info'], {
|
61
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
62
|
});
|
63
|
} else {
|
64
|
this.route.params.subscribe((params) => {
|
65
|
this.communityId = params['community'];
|
66
|
if(this.communityId == "covid-19"){
|
67
|
this.defaultCustomizationOptions= new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor,CustomizationOptions.getIdentity(this.communityId).secondaryColor);
|
68
|
}
|
69
|
this.title.setTitle('Administration Dashboard | Customization');
|
70
|
this.showLoading = true;
|
71
|
this.errorMessage = '';
|
72
|
this.successfulSaveMessage = '';
|
73
|
this.layoutService.getLayout(this.properties, this.communityId).subscribe(layout => {
|
74
|
this.publishedCustomizationOptions = (layout?layout:Object.assign({},this.defaultCustomizationOptions));
|
75
|
this.initializeCustomizationOptions(true);
|
76
|
}, error => {
|
77
|
this.publishedCustomizationOptions = new CustomizationOptions();
|
78
|
this.initializeCustomizationOptions(true);
|
79
|
this.errorMessage = "An error occured fetching customizations options"
|
80
|
});
|
81
|
});
|
82
|
}
|
83
|
|
84
|
|
85
|
}
|
86
|
hasChanges(object1,object2):boolean{
|
87
|
return JSON.stringify(object1) != JSON.stringify(object2);
|
88
|
}
|
89
|
saveLayout() {
|
90
|
if (!Session.isLoggedIn()) {
|
91
|
this._router.navigate(['/user-info'], {
|
92
|
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
|
93
|
});
|
94
|
}
|
95
|
this.layoutService.saveLayout(this.properties, this.communityId, this.draftCustomizationOptions).subscribe(layout => {
|
96
|
this.publishedCustomizationOptions = layout;
|
97
|
this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
|
98
|
this.successfulSaveMessage = "Customization Options saved!"
|
99
|
}, error => {
|
100
|
this.errorMessage = "An error occured on save"
|
101
|
});
|
102
|
}
|
103
|
|
104
|
initializeCustomizationOptions(updatePreviewUrl) {
|
105
|
this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
106
|
this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
107
|
|
108
|
if(updatePreviewUrl){
|
109
|
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
|
110
|
}
|
111
|
|
112
|
|
113
|
this.buttonDarkBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.darkBackground);
|
114
|
this.buttonLightBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.lightBackground);
|
115
|
}
|
116
|
|
117
|
applyLayout() {
|
118
|
this.appliedCustomizationOptions = this.copyObject(this.draftCustomizationOptions);
|
119
|
if(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization){
|
120
|
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
|
121
|
}
|
122
|
}
|
123
|
|
124
|
resetLayout() {
|
125
|
|
126
|
if (this.menuSelected == 'backgrounds') {
|
127
|
this.resetBackgrounds();
|
128
|
// } else if (this.menuSelected == 'fonts') {
|
129
|
// this.resetFonts();
|
130
|
} else if (this.menuSelected == 'buttons') {
|
131
|
this.resetButtons();
|
132
|
} else if (this.menuSelected == 'identity') {
|
133
|
this.resetIdentity();
|
134
|
} else {
|
135
|
this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
136
|
|
137
|
this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
|
138
|
}
|
139
|
// console.log(JSON.stringify(this.appliedCustomizationOptions))
|
140
|
// console.log(this.previewCustomization)
|
141
|
this.initializeCustomizationOptions(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization);
|
142
|
|
143
|
}
|
144
|
resetBackgroundsTo(backgrounds) {
|
145
|
this.draftCustomizationOptions.backgrounds.dark.color = backgrounds.dark.color;
|
146
|
this.draftCustomizationOptions.backgrounds.light.color = backgrounds.light.color;
|
147
|
|
148
|
this.appliedCustomizationOptions.backgrounds.dark.color = backgrounds.dark.color;
|
149
|
this.appliedCustomizationOptions.backgrounds.light.color = backgrounds.light.color;
|
150
|
}
|
151
|
resetBackgrounds() {
|
152
|
this.resetBackgroundsTo(this.publishedCustomizationOptions.backgrounds)
|
153
|
}
|
154
|
resetBackgroundsToDefault() {
|
155
|
this.resetBackgroundsTo(this.defaultCustomizationOptions.backgrounds)
|
156
|
}
|
157
|
|
158
|
|
159
|
resetButtons() {
|
160
|
this.resetButtonsTo(this.draftCustomizationOptions.buttons.lightBackground,this.publishedCustomizationOptions.buttons.lightBackground);
|
161
|
this.resetButtonsTo(this.draftCustomizationOptions.buttons.darkBackground,this.publishedCustomizationOptions.buttons.darkBackground);
|
162
|
}
|
163
|
|
164
|
resetButtonsTo(buttons, buttonsToRevert) {
|
165
|
buttons = this.copyObject(buttonsToRevert);
|
166
|
|
167
|
buttons= this.copyObject(buttonsToRevert);
|
168
|
|
169
|
}
|
170
|
|
171
|
resetButtonsToDefaul() {
|
172
|
this.resetButtonsTo(this.draftCustomizationOptions.buttons.lightBackground,this.defaultCustomizationOptions.buttons.lightBackground);
|
173
|
this.resetButtonsTo(this.draftCustomizationOptions.buttons.darkBackground,this.defaultCustomizationOptions.buttons.darkBackground);
|
174
|
}
|
175
|
resetIdentityTo( original:CustomizationOptions) {
|
176
|
// this.draftCustomizationOptions.identity.isDefault = original.identity.isDefault;
|
177
|
this.draftCustomizationOptions.identity.mainColor = this.copyObject(original.identity.mainColor);
|
178
|
this.draftCustomizationOptions.identity.secondaryColor = this.copyObject(original.identity.secondaryColor);
|
179
|
|
180
|
// this.appliedCustomizationOptions.identity.isDefault = original.identity.isDefault;
|
181
|
this.appliedCustomizationOptions.identity.mainColor = this.copyObject(original.identity.mainColor);
|
182
|
this.appliedCustomizationOptions.identity.secondaryColor = this.copyObject(original.identity.secondaryColor);
|
183
|
}
|
184
|
resetIdentity() {
|
185
|
this.resetIdentityTo( this.publishedCustomizationOptions);
|
186
|
|
187
|
}
|
188
|
resetIdentityToDefault() {
|
189
|
this.resetIdentityTo( this.defaultCustomizationOptions);
|
190
|
|
191
|
}
|
192
|
changeEntityTemplate(){
|
193
|
if(this.draftCustomizationOptions.identity.isDefault){
|
194
|
this.resetIdentityToDefault();
|
195
|
}
|
196
|
}
|
197
|
changeStyle(colorOptions, borderOptions = null) {
|
198
|
let style = '';
|
199
|
if (!borderOptions) {
|
200
|
borderOptions = colorOptions;
|
201
|
}
|
202
|
if (colorOptions.color) {
|
203
|
style = style.concat('; color:' + colorOptions.color);
|
204
|
}
|
205
|
if (colorOptions.backgroundColor) {
|
206
|
style = style.concat( '; background-color:' + colorOptions.backgroundColor);
|
207
|
}
|
208
|
if (colorOptions.borderColor) {
|
209
|
style = style.concat('; border-color:' + colorOptions.borderColor);
|
210
|
}
|
211
|
if (borderOptions.borderStyle) {
|
212
|
style = style.concat('; border-style:' + borderOptions.borderStyle);
|
213
|
}
|
214
|
if (borderOptions.borderWidth) {
|
215
|
style = style.concat( '; border-width:' + borderOptions.borderWidth + 'px');
|
216
|
}
|
217
|
if (borderOptions.borderRadius) {
|
218
|
style = style.concat('; border-radius:' + borderOptions.borderRadius + 'px');
|
219
|
}
|
220
|
|
221
|
return this.sanitizer.bypassSecurityTrustStyle(style);
|
222
|
}
|
223
|
|
224
|
|
225
|
changeFontsStyle(options, color = null) {
|
226
|
let style = '';
|
227
|
|
228
|
|
229
|
if (options.family) {
|
230
|
style = style.concat('; font-family:' + options.family);
|
231
|
}
|
232
|
|
233
|
if (options.size) {
|
234
|
style = style.concat('; font-size:' + options.size + 'px');
|
235
|
}
|
236
|
|
237
|
if (options.weight) {
|
238
|
style = style.concat('; font-weight:' + options.weight);
|
239
|
}
|
240
|
|
241
|
if (color) {
|
242
|
style = style.concat('; color:' + color);
|
243
|
}
|
244
|
|
245
|
return this.sanitizer.bypassSecurityTrustStyle(style);
|
246
|
}
|
247
|
|
248
|
getCommunityUrlSatinized(layout: string) {
|
249
|
return this.sanitizer.bypassSecurityTrustResourceUrl(this.getCommunityUrlNewLayout(layout));
|
250
|
}
|
251
|
getCommunityUrl() {
|
252
|
return 'https://'+ (this.properties.environment == 'production'?'':'beta.')+this.communityId+'.openaire.eu';
|
253
|
}
|
254
|
|
255
|
getCommunityUrlNewLayout(layout: string) {
|
256
|
this.previewCustomization = layout;
|
257
|
return this.getCommunityUrl()+'/?' + 'layout=' + StringUtils.URIEncode(layout);
|
258
|
}
|
259
|
|
260
|
copyObject(obj) {
|
261
|
return JSON.parse(JSON.stringify(obj));
|
262
|
}
|
263
|
|
264
|
back() {
|
265
|
if (JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions) ||
|
266
|
JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.appliedCustomizationOptions)) {
|
267
|
this.menuSelected = 'main';
|
268
|
} else {
|
269
|
this.backAlert.okButtonText = 'Yes';
|
270
|
this.backAlert.cancelButtonText = 'No';
|
271
|
this.backAlert.alertTitle = '';
|
272
|
this.backAlert.okButtonLeft = true;
|
273
|
this.backAlert.open();
|
274
|
}
|
275
|
|
276
|
}
|
277
|
|
278
|
resetToAppliedOptions() {
|
279
|
this.draftCustomizationOptions = this.copyObject(this.appliedCustomizationOptions);
|
280
|
this.menuSelected = 'main';
|
281
|
}
|
282
|
|
283
|
exit() {
|
284
|
if (JSON.stringify(this.appliedCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions)) {
|
285
|
this.exitCustomization();
|
286
|
} else {
|
287
|
this.exitAlert.okButtonText = 'Yes';
|
288
|
this.exitAlert.cancelButtonText = 'No';
|
289
|
this.exitAlert.alertTitle = '';
|
290
|
this.exitAlert.okButtonLeft = true;
|
291
|
this.exitAlert.open();
|
292
|
}
|
293
|
|
294
|
}
|
295
|
|
296
|
exitCustomization() {
|
297
|
this._router.navigate(['/dashboard'], {
|
298
|
queryParams: {'communityId': this.communityId}
|
299
|
});
|
300
|
}
|
301
|
downloadCustomization(){
|
302
|
this.errorMessage = "";
|
303
|
let options= JSON.parse(JSON.stringify(this.publishedCustomizationOptions));
|
304
|
delete options['_id'];
|
305
|
console.info("Here!" +JSON.stringify(options));
|
306
|
if(typeof document !== 'undefined') {
|
307
|
let dataStr = JSON.stringify(options);
|
308
|
let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
|
309
|
|
310
|
let exportFileDefaultName = 'layoutOptions.json';
|
311
|
|
312
|
let linkElement = document.createElement('a');
|
313
|
linkElement.setAttribute('href', dataUri);
|
314
|
linkElement.setAttribute('download', exportFileDefaultName);
|
315
|
linkElement.click();
|
316
|
}
|
317
|
}
|
318
|
fileChangeEvent(fileInput: any) {
|
319
|
this.errorMessage = "";
|
320
|
let filesToUpload = <Array<File>>fileInput.target.files;
|
321
|
|
322
|
|
323
|
if (filesToUpload.length == 0) {
|
324
|
this.errorMessage = "There is no selected file to upload.";
|
325
|
return;
|
326
|
} else {
|
327
|
if (filesToUpload[0].name.indexOf(".json") == -1 ||
|
328
|
(filesToUpload[0].type != "application/json" )) {
|
329
|
this.errorMessage = "No valid file type. The required type is json "+filesToUpload[0].type;
|
330
|
return;
|
331
|
}
|
332
|
}
|
333
|
|
334
|
this.makeFileRequest(this.properties.utilsService + '/upload?type=json', [], filesToUpload).then((result) => {
|
335
|
let layout:CustomizationOptions = JSON.parse(result.toString());
|
336
|
if( layout && layout.backgrounds && layout.buttons){
|
337
|
this.draftCustomizationOptions = layout;
|
338
|
//put the same id to not have any difference
|
339
|
if(this.publishedCustomizationOptions['_id']){
|
340
|
this.draftCustomizationOptions['_id'] = this.publishedCustomizationOptions['_id'];
|
341
|
}
|
342
|
}else{
|
343
|
this.errorMessage = "No valid file";
|
344
|
}
|
345
|
|
346
|
}, (error) => {
|
347
|
this.errorMessage = "No valid file";
|
348
|
});
|
349
|
}
|
350
|
|
351
|
|
352
|
makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
|
353
|
return new Promise((resolve, reject) => {
|
354
|
const formData: any = new FormData();
|
355
|
const xhr = new XMLHttpRequest();
|
356
|
for (let i = 0; i < files.length; i++) {
|
357
|
formData.append("uploads[]", files[i], files[i].name);
|
358
|
}
|
359
|
xhr.onreadystatechange = function () {
|
360
|
if (xhr.readyState == 4) {
|
361
|
if (xhr.status == 200) {
|
362
|
resolve(xhr.response);
|
363
|
} else {
|
364
|
reject(xhr.response);
|
365
|
}
|
366
|
}
|
367
|
}
|
368
|
xhr.open("POST", url, true);
|
369
|
xhr.send(formData);
|
370
|
});
|
371
|
}
|
372
|
|
373
|
|
374
|
|
375
|
|
376
|
}
|