connect/src/app/utils/customization/customization.component.ts

150 lines
8.9 KiB
TypeScript

import {Component, Inject, Input, RendererFactory2, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Router} from '@angular/router';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {LayoutService} from "../../openaireLibrary/services/layout.service";
import {CustomizationOptions} from "../../openaireLibrary/connect/community/CustomizationOptions";
import {DOCUMENT} from "@angular/common";
import {DomSanitizer} from "@angular/platform-browser";
import {StringUtils} from "../../openaireLibrary/utils/string-utils.class";
@Component({
selector: 'customization',
template: ``
})
export class CustomizationComponent {
@Input() communityId;
@Input() layout: CustomizationOptions;
@Input() properties:EnvProperties;
customizationCss:string = "";
constructor(private route: ActivatedRoute,
private router: Router, private _layoutService: LayoutService, @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2, private sanitizer: DomSanitizer
) {
}
public ngOnInit() {
console.log("cust init")
this._layoutService.getLayout(this.properties, this.communityId).subscribe(
layout => {
if (layout) {
console.debug("service");
this.layout = CustomizationOptions.checkForObsoleteVersion(layout.layoutOptions,this.communityId);
} else {
this.layout = new CustomizationOptions(CustomizationOptions.getIdentity(this.communityId).mainColor, CustomizationOptions.getIdentity(this.communityId).secondaryColor);
console.debug("default");
}
this.privateAppendCss();
},
error => {
console.log(" Layout not found - use default");
this.layout = new CustomizationOptions();
this.privateAppendCss();
}
);
this.route.queryParams.subscribe(params => {
if(params['layout']) {
this.layout = JSON.parse(StringUtils.URIDecode(params['layout']));
this.privateAppendCss();
}else{
}
});
// this.privateAppendCss();
}
privateAppendCss(){
this.buildCss();
try {
const head = this.document.getElementsByTagName('head')[0];
let customizationCssFile = this.document.getElementById('customCssfile' ) as HTMLLinkElement;
if (customizationCssFile) {
customizationCssFile.href = "/assets/customization.css";
} else {
const style = this.document.createElement('link');
style.id = 'customCssfile';
style.rel = 'stylesheet';
style.href = "/assets/customization.css";
head.appendChild(style);
}
let customCss = this.document.getElementById('customStyle' ) as HTMLLinkElement;
if (customCss) {
customCss.append(this.customizationCss);
} else {
const style = this.document.createElement('style');
style.id = 'customStyle';
head.appendChild(style);
const renderer = this.rendererFactory.createRenderer(this.document, {
id: '-1',
encapsulation: ViewEncapsulation.None,
styles: [],
data: {}
});
let CSSElement = renderer.createText(this.customizationCss);
renderer.appendChild(style,CSSElement);
}
} catch (e) {
console.error('Renderrer Error to append style ', e);
}
}
private buildCss() {
this.customizationCss = `
:root {
--portal-main-color: ` + this.layout.identity.mainColor+`;
--portal-dark-color: ` + this.layout.identity.secondaryColor+`;
--background-light-color: ` + this.layout.backgrounds.light.color+`;
}
`;
//Search SVG
let search = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2000 532"><defs><style>.cls-1{isolation:isolate;}.cls-2{fill:{{color}};mix-blend-mode:multiply;}</style></defs><title>Asset 3</title><g class="cls-1"><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path class="cls-2" d="M0,431s362,109,841,62,632,68,1159-9V0H0Z"/><path class="cls-2" d="M0,514s1401,71,2000-69V0H0Z"/></g></g></g></svg>`;;
let svg = 'data:image/svg+xml,' + encodeURIComponent(search.replace('{{color}}', this.layout.backgrounds.form.color));
this.customizationCss = this.customizationCss.concat('.generalSearchForm,.publicationsSearchForm,.projectsSearchForm, .projectsTableSearchForm,.organizationsSearchForm,.datasourcesSearchForm {');
this.customizationCss = this.customizationCss.concat(' background: ' + "url('" + svg + "') transparent no-repeat center bottom }");
this.customizationCss = this.customizationCss.concat(' .communityPanelBackground, .communityPanelBackground .uk-section-primary {');
this.customizationCss = this.customizationCss.concat(' background-color: ' + this.layout.backgrounds.dark.color + ';}');
this.customizationCss = this.customizationCss.concat(' .uk-button:not(.uk-button-text){');
this.customizationCss = this.customizationCss.concat(' border-radius:' + (this.layout.buttons.lightBackground.borderRadius != null ? this.layout.buttons.lightBackground.borderRadius : '4') + 'px;');
this.customizationCss = this.customizationCss.concat(' }');
this.customizationCss = this.customizationCss.concat(' .uk-button:not(.uk-button-text):not(.uk-button-default):not(.uk-button-primary), .portal-button {');
this.customizationCss = this.customizationCss.concat(' background-color:' + (this.layout.buttons.lightBackground.backgroundColor != null ? this.layout.buttons.lightBackground.backgroundColor : `#003052`) + ';');
this.customizationCss = this.customizationCss.concat(' color: ' + (this.layout.buttons.lightBackground.color != null ? this.layout.buttons.lightBackground.color : `white`) + ';');
this.customizationCss = this.customizationCss.concat('border-color: ' + (this.layout.buttons.lightBackground.borderColor != null ? this.layout.buttons.lightBackground.borderColor : `transparent`) + ';');
this.customizationCss = this.customizationCss.concat(' border-style: ' + (this.layout.buttons.lightBackground.borderStyle != null ? this.layout.buttons.lightBackground.borderStyle : `solid`) + ';');
this.customizationCss = this.customizationCss.concat(' border-width: ' + (this.layout.buttons.lightBackground.borderWidth != null ? this.layout.buttons.lightBackground.borderWidth : `1`) + 'px;');
this.customizationCss = this.customizationCss.concat(' }');
this.customizationCss = this.customizationCss.concat(' .uk-button:not(.uk-button-text):not(.uk-button-default):not(.uk-button-primary):hover, .portal-button:hover {');
this.customizationCss = this.customizationCss.concat(' background-color:' + (this.layout.buttons.lightBackground.onHover.backgroundColor != null ? this.layout.buttons.lightBackground.onHover.backgroundColor : '#154B71') + ';');
this.customizationCss = this.customizationCss.concat(' color: '+ (this.layout.buttons.lightBackground.onHover.color != null ? this.layout.buttons.lightBackground.onHover.color : 'white') + ';');
this.customizationCss = this.customizationCss.concat(' border-color: ' + (this.layout.buttons.lightBackground.onHover.borderColor != null ? this.layout.buttons.lightBackground.onHover.borderColor : 'transparent') + ';');
this.customizationCss = this.customizationCss.concat(' }');
/*Buttons*/
this.customizationCss = this.customizationCss.concat(' .communityPanelBackground .uk-button:not(.ignoreCommunityPanelBackground) {');
this.customizationCss = this.customizationCss.concat( 'background-color: ' + this.layout.buttons.darkBackground.backgroundColor + ' !important;');
this.customizationCss = this.customizationCss.concat(' color: ' + this.layout.buttons.darkBackground.color + ' !important;');
this.customizationCss = this.customizationCss.concat(' border-color: ' + this.layout.buttons.darkBackground.borderColor + ' !important;');
this.customizationCss = this.customizationCss.concat(' border-style: ' + this.layout.buttons.darkBackground.borderStyle + ' !important;');
this.customizationCss = this.customizationCss.concat( 'border-width: ' + this.layout.buttons.darkBackground.borderWidth + 'px !important;');
this.customizationCss = this.customizationCss.concat( 'border-radius:' + this.layout.buttons.darkBackground.borderRadius + 'px !important;');
this.customizationCss = this.customizationCss.concat(' }');
this.customizationCss = this.customizationCss.concat(' .communityPanelBackground .uk-button:not(.ignoreCommunityPanelBackground):hover {');
this.customizationCss = this.customizationCss.concat(' background-color: ' + this.layout.buttons.darkBackground.onHover.backgroundColor + ' !important;');
this.customizationCss = this.customizationCss.concat( 'color: ' + this.layout.buttons.darkBackground.onHover.color + ' !important;');
this.customizationCss = this.customizationCss.concat( 'border-color:' + this.layout.buttons.darkBackground.onHover.borderColor + ' !important;');
this.customizationCss = this.customizationCss.concat(' }');
// console.debug(this.customizationCss)
}
}