connect-admin/src/app/pages/customization/customization.component.ts

314 lines
12 KiB
TypeScript
Raw Normal View History

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {Session} from '../../openaireLibrary/login/utils/helper.class';
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
import {Curator} from '../../openaireLibrary/utils/entities/CuratorInfo';
import {DomSanitizer} from '@angular/platform-browser';
import {CustomizationOptions} from '../../openaireLibrary/connect/community/CustomizationOptions';
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
import {LayoutService} from '../../openaireLibrary/services/layout.service';
import {AlertModal} from '../../openaireLibrary/utils/modal/alert';
@Component({
selector: 'customization',
templateUrl: './customization.component.html',
})
export class CustomizationComponent implements OnInit {
menuSelected = 'main';
color = 'white';
communityUrl = 'http://scoobydoo.di.uoa.gr:4200/?communityId=ee';
publishedCustomizationOptions: CustomizationOptions = null;
draftCustomizationOptions: CustomizationOptions = null;
appliedCustomizationOptions: CustomizationOptions = null;
previewUrl = null;
previewCustomization = null;
buttonDarkBackgroundPreview;
buttonLightBackgroundPreview;
linkDarkBackgroundPreview;
linkLightBackgroundPreview;
hoveredText;
public showLoading = true;
public updateErrorMessage = '';
public successfulSaveMessage = '';
public communityId = null;
public hasChanged = false;
public properties: EnvProperties = null;
private file: File = null;
private maxsize: number = 200 * 1024;
public enabled = true;
@ViewChild('backAlert') backAlert: AlertModal;
@ViewChild('exitAlert') exitAlert: AlertModal;
constructor(private element: ElementRef,
private route: ActivatedRoute,
private _router: Router,
private sanitizer: DomSanitizer,
private layoutService: LayoutService) {
}
ngOnInit() {
this.route.data.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
if (!Session.isLoggedIn()) {
this._router.navigate(['/user-info'], {
queryParams: {'errorCode': LoginErrorCodes.NOT_VALID, 'redirectUrl': this._router.url}
});
} else {
this.route.queryParams.subscribe((params) => {
this.communityId = params['communityId'];
this.showLoading = true;
this.updateErrorMessage = '';
this.layoutService.getLayout(this.communityId, this.properties.adminToolsAPIURL + 'community/').subscribe(layout => {
this.publishedCustomizationOptions = layout;
this.initializeCustomizationOptions(true);
}, error => {
this.publishedCustomizationOptions = new CustomizationOptions();
this.initializeCustomizationOptions(true);
});
});
}
});
}
saveLayout() {
this.layoutService.saveLayout(this.communityId, this.properties.adminToolsAPIURL + 'community/', this.draftCustomizationOptions).subscribe(layout => {
this.publishedCustomizationOptions = layout;
this.initializeCustomizationOptions(JSON.stringify(this.publishedCustomizationOptions) != this.previewCustomization);
});
}
initializeCustomizationOptions(updatePreviewUrl) {
this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
if(updatePreviewUrl){
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
}
this.buttonDarkBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.darkBackground);
this.buttonLightBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.buttons.lightBackground);
this.linkDarkBackgroundPreview = this.changeFontsStyle(this.draftCustomizationOptions.links.darkBackground, this.draftCustomizationOptions.links.darkBackground.color);
this.linkLightBackgroundPreview = this.changeStyle(this.draftCustomizationOptions.links.lightBackground, this.draftCustomizationOptions.links.lightBackground.color);
}
applyLayout() {
this.appliedCustomizationOptions = this.copyObject(this.draftCustomizationOptions);
if(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization){
this.previewUrl = this.getCommunityUrlSatinized(JSON.stringify(this.appliedCustomizationOptions));
}
}
resetLayout() {
if (this.menuSelected == 'backgrounds') {
this.resetBackgrounds();
} else if (this.menuSelected == 'fonts') {
this.resetFonts();
} else if (this.menuSelected == 'elements') {
this.resetElements();
} else if (this.menuSelected == 'buttons') {
this.resetButtons();
} else if (this.menuSelected == 'identity') {
this.resetIdentity();
} else {
this.draftCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
this.appliedCustomizationOptions = this.copyObject(this.publishedCustomizationOptions);
}
// console.log(JSON.stringify(this.appliedCustomizationOptions))
// console.log(this.previewCustomization)
this.initializeCustomizationOptions(JSON.stringify(this.appliedCustomizationOptions) != this.previewCustomization);
}
resetBackgrounds() {
this.draftCustomizationOptions.panel.onDarkBackground = this.publishedCustomizationOptions.panel.onDarkBackground;
this.draftCustomizationOptions.panel.background.color = this.publishedCustomizationOptions.panel.background.color;
this.appliedCustomizationOptions.panel.onDarkBackground = this.publishedCustomizationOptions.panel.onDarkBackground;
this.appliedCustomizationOptions.panel.background.color = this.publishedCustomizationOptions.panel.background.color;
}
resetFonts() {
this.resetFontsBig();
this.resetFontsSmall();
this.resetFontsLinksDark();
this.resetFontsLinksLight();
}
resetFontsBig() {
this.draftCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
this.appliedCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
}
resetFontsSmall() {
this.draftCustomizationOptions.panel.fonts = this.copyObject(this.publishedCustomizationOptions.panel.fonts);
this.appliedCustomizationOptions.panel.fonts = this.copyObject(this.publishedCustomizationOptions.panel.fonts);
}
resetFontsLinksDark() {
this.draftCustomizationOptions.links.darkBackground = this.copyObject(this.publishedCustomizationOptions.links.darkBackground);
this.appliedCustomizationOptions.links.darkBackground = this.copyObject(this.publishedCustomizationOptions.links.darkBackground);
}
resetFontsLinksLight() {
this.draftCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
this.draftCustomizationOptions.links.lightBackground = this.copyObject(this.publishedCustomizationOptions.links.lightBackground);
this.appliedCustomizationOptions.panel.title = this.copyObject(this.publishedCustomizationOptions.panel.title);
this.appliedCustomizationOptions.links.lightBackground = this.copyObject(this.publishedCustomizationOptions.links.lightBackground);
}
resetElements() {
this.draftCustomizationOptions.panel.panelElements = this.copyObject(this.publishedCustomizationOptions.panel.panelElements);
this.appliedCustomizationOptions.panel.panelElements = this.copyObject(this.publishedCustomizationOptions.panel.panelElements);
}
resetButtons() {
this.resetButtonsDark();
this.resetButtonsLight();
}
resetButtonsDark() {
this.draftCustomizationOptions.buttons.darkBackground = this.copyObject(this.publishedCustomizationOptions.buttons.darkBackground);
this.appliedCustomizationOptions.buttons.darkBackground = this.copyObject(this.publishedCustomizationOptions.buttons.darkBackground);
}
resetButtonsLight() {
this.draftCustomizationOptions.buttons.lightBackground = this.copyObject(this.publishedCustomizationOptions.buttons.lightBackground);
this.appliedCustomizationOptions.buttons.lightBackground = this.copyObject(this.publishedCustomizationOptions.buttons.lightBackground);
}
resetIdentity() {
this.draftCustomizationOptions.mainColor = this.publishedCustomizationOptions.mainColor;
this.draftCustomizationOptions.secondaryColor = this.publishedCustomizationOptions.secondaryColor;
this.appliedCustomizationOptions.mainColor = this.publishedCustomizationOptions.mainColor;
this.appliedCustomizationOptions.secondaryColor = this.publishedCustomizationOptions.secondaryColor;
}
changeStyle(colorOptions, borderOptions = null) {
let style = '';
if (!borderOptions) {
borderOptions = colorOptions;
}
if (colorOptions.color) {
style = style.concat('; color:' + colorOptions.color);
}
if (colorOptions.backgroundColor) {
style = style.concat( '; background-color:' + colorOptions.backgroundColor);
}
if (colorOptions.borderColor) {
style = style.concat('; border-color:' + colorOptions.borderColor);
}
if (borderOptions.borderStyle) {
style = style.concat('; border-style:' + borderOptions.borderStyle);
}
if (borderOptions.borderWidth) {
style = style.concat( '; border-width:' + borderOptions.borderWidth + 'px');
}
if (borderOptions.borderRadius) {
style = style.concat('; border-radius:' + borderOptions.borderRadius + 'px');
}
return this.sanitizer.bypassSecurityTrustStyle(style);
}
changeFontsStyle(options, color = null) {
let style = '';
if (options.family) {
style = style.concat('; font-family:' + options.family);
}
if (options.size) {
style = style.concat('; font-size:' + options.size + 'px');
}
if (options.weight) {
style = style.concat('; font-weight:' + options.weight);
}
if (color) {
style = style.concat('; color:' + color);
}
return this.sanitizer.bypassSecurityTrustStyle(style);
}
getCommunityUrlSatinized(layout: string) {
return this.sanitizer.bypassSecurityTrustResourceUrl(this.getCommunityUrlNewLayout(layout));
}
getCommunityUrlNewLayout(layout: string) {
this.previewCustomization = layout;
return this.communityUrl + '&layout=' + StringUtils.URIEncode(layout);
}
copyObject(obj) {
return JSON.parse(JSON.stringify(obj));
}
back() {
if (JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions) ||
JSON.stringify(this.draftCustomizationOptions) == JSON.stringify(this.appliedCustomizationOptions)) {
this.menuSelected = 'main';
} else {
this.backAlert.okButtonText = 'Yes';
this.backAlert.cancelButtonText = 'No';
this.backAlert.alertTitle = '';
this.backAlert.okButtonLeft = true;
this.backAlert.open();
}
}
resetToAppliedOptions() {
this.draftCustomizationOptions = this.copyObject(this.appliedCustomizationOptions);
this.menuSelected = 'main';
}
exit() {
if (JSON.stringify(this.appliedCustomizationOptions) == JSON.stringify(this.publishedCustomizationOptions)) {
this.exitCustomization();
} else {
this.exitAlert.okButtonText = 'Yes';
this.exitAlert.cancelButtonText = 'No';
this.exitAlert.alertTitle = '';
this.exitAlert.okButtonLeft = true;
this.exitAlert.open();
}
}
exitCustomization() {
this._router.navigate(['/dashboard'], {
queryParams: {'communityId': this.communityId}
});
}
}