openaire-library/landingPages/landing-utils/addThis.component.ts

66 lines
2.0 KiB
TypeScript
Raw Normal View History

import {Component, EventEmitter, Inject, OnInit, Output, RendererFactory2, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {DOCUMENT} from "@angular/common";
interface addthis {
layers: Refresh;
init: Function;
toolbox: Function;
}
interface Refresh {
refresh: Function;
}
declare var addthis: addthis;
@Component({
selector: 'addThis',
template: `
<div class="addthis_inline_share_toolbox_lcx9"></div>
`
})
export class AddThisComponent implements OnInit {
sub;
constructor(private route: ActivatedRoute, @Inject(DOCUMENT) private document, private rendererFactory: RendererFactory2) {}
ngOnDestroy() {
if(this.sub) {
this.sub.unsubscribe();
}
}
ngOnInit() {
this.sub = this.route.queryParams.subscribe(data => {
try {
if (!this.document.getElementById('addThisScript') && typeof document !== 'undefined') {
// console.log(" create script AddThis");
const renderer = this.rendererFactory.createRenderer(this.document, {
id: '-1',
encapsulation: ViewEncapsulation.None,
styles: [],
data: {}
});
const head = this.document.body;
if (head === null) {
throw new Error('<head> not found within DOCUMENT.');
}
const script = renderer.createElement('script');
renderer.setAttribute(script, "id", "addThisScript");
renderer.setAttribute(script, "src", "https://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-559d24521cd8c080");
renderer.setAttribute(script, "type", "text/javascript");
renderer.appendChild(head, script);
}
if (typeof document !== 'undefined') {
if(typeof addthis !== 'undefined' && addthis.layers && addthis.layers.refresh) {
// console.log("Add This: Call Refresh")
addthis.layers.refresh();
}
}
}catch (e) {
// console.error(e)
}
});
}
}