Commits

Nathan Buckingham authored 99e5b9c8983
w2p-85140 ds-rss component now adds a button to all search pages and community and collection, link-head service adds the rss to the head element
No tags

src/app/core/services/link-head.service.ts

Added
1 +import { Injectable, Optional, RendererFactory2, ViewEncapsulation, Inject } from '@angular/core';
2 +import { DOCUMENT } from '@angular/common';
3 +
4 +@Injectable()
5 +export class LinkHeadService {
6 + constructor(
7 + private rendererFactory: RendererFactory2,
8 + @Inject(DOCUMENT) private document
9 + ) {
10 +
11 + }
12 + addTag(tag: LinkDefinition, forceCreation?: boolean) {
13 +
14 + try {
15 + const renderer = this.rendererFactory.createRenderer(this.document, {
16 + id: '-1',
17 + encapsulation: ViewEncapsulation.None,
18 + styles: [],
19 + data: {}
20 + });
21 +
22 + const link = renderer.createElement('link');
23 +
24 + const head = this.document.head;
25 + const selector = this._parseSelector(tag);
26 +
27 + if (head === null) {
28 + throw new Error('<head> not found within DOCUMENT.');
29 + }
30 +
31 + Object.keys(tag).forEach((prop: string) => {
32 + return renderer.setAttribute(link, prop, tag[prop]);
33 + });
34 +
35 + renderer.appendChild(head, link);
36 +
37 + } catch (e) {
38 + console.error('Error within linkService : ', e);
39 + }
40 + }
41 +
42 + removeTag(attrSelector: string) {
43 + if (attrSelector) {
44 + try {
45 + const renderer = this.rendererFactory.createRenderer(this.document, {
46 + id: '-1',
47 + encapsulation: ViewEncapsulation.None,
48 + styles: [],
49 + data: {}
50 + });
51 + const head = this.document.head;
52 + if (head === null) {
53 + throw new Error('<head> not found within DOCUMENT.');
54 + }
55 + const linkTags = this.document.querySelectorAll('link[' + attrSelector + ']');
56 + for (const link of linkTags) {
57 + renderer.removeChild(head, link);
58 + }
59 + } catch (e) {
60 + console.log('Error while removing tag ' + e.message);
61 + }
62 + }
63 + }
64 +
65 + private _parseSelector(tag: LinkDefinition): string {
66 + const attr: string = tag.rel ? 'rel' : 'hreflang';
67 + return `${attr}="${tag[attr]}"`;
68 + }
69 +}
70 +
71 +export declare type LinkDefinition = {
72 + charset?: string;
73 + crossorigin?: string;
74 + href?: string;
75 + hreflang?: string;
76 + media?: string;
77 + rel?: string;
78 + rev?: string;
79 + sizes?: string;
80 + target?: string;
81 + type?: string;
82 +} & {
83 + [prop: string]: string;
84 + };

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut