Project

General

Profile

1
/**
2
 * @license
3
 * Copyright Google Inc. All Rights Reserved.
4
 *
5
 * Use of this source code is governed by an MIT-style license that can be
6
 * found in the LICENSE file at https://angular.io/license
7
 */
8

    
9
import {Injectable, Inject} from '@angular/core';
10
// es6-modules are used here
11
import { ɵgetDOM as getDOM} from '@angular/platform-browser';
12
import {DOCUMENT} from '@angular/platform-browser';
13
/**
14
 * Represent meta element.
15
 *
16
 * ### Example
17
 *
18
 * ```ts
19
 * { name: 'application-name', content: 'Name of my application' },
20
 * { name: 'description', content: 'A description of the page', id: 'desc' }
21
 * // ...
22
 * // Twitter
23
 * { name: 'twitter:title', content: 'Content Title' }
24
 * // ...
25
 * // Google+
26
 * { itemprop: 'name', content: 'Content Title' },
27
 * { itemprop: 'description', content: 'Content Title' }
28
 * // ...
29
 * // Facebook / Open Graph
30
 * { property: 'fb:app_id', content: '123456789' },
31
 * { property: 'og:title', content: 'Content Title' }
32
 * ```
33
 *
34
 * @experimental
35
 */
36
export interface MetaDefinition {
37
  charset?: string;
38
  content?: string;
39
  httpEquiv?: string;
40
  id?: string;
41
  itemprop?: string;
42
  name?: string;
43
  property?: string;
44
  scheme?: string;
45
  url?: string;
46
  [prop: string]: string;
47
}
48

    
49
/**
50
 * A service that can be used to get and add meta tags.
51
 *
52
 * @experimental
53
 */
54
@Injectable()
55
export class Meta {
56
  private _dom: any = getDOM();
57
  constructor( @Inject(DOCUMENT) private _document: any) { }
58
  /**
59
   * Sets the title of the page
60
   */
61
  setTitle(title: string) {
62
    this._document.title = title
63
  }
64
  /**
65
   * Adds a new meta tag to the dom.
66
   *
67
   *  ### Example
68
   *
69
   * ```ts
70
   * const name: MetaDefinition = {name: 'application-name', content: 'Name of my application'};
71
   * const desc: MetaDefinition = {name: 'description', content: 'A description of the page'};
72
   * const tags: HTMLMetaElement[] = this.meta.addTags([name, desc]);
73
   * ```
74
   *
75
   * @param tags
76
   * @returns {HTMLMetaElement[]}
77
   */
78
  addTags(...tags: Array<MetaDefinition|MetaDefinition[]>): HTMLMetaElement[] {
79
    const presentTags = this._flattenArray(tags);
80
    if (presentTags.length === 0) return [];
81
    return presentTags.map((tag: MetaDefinition) => this._addInternal(tag));
82
  }
83

    
84
  /**
85
   * Gets the meta tag by the given selector. Returns element or null
86
   * if there's no such meta element.
87
   *
88
   *  ### Example
89
   *
90
   * ```ts
91
   * const meta: HTMLMetaElement = this.meta.getTag('name=description');
92
   * const twitterMeta: HTMLMetaElement = this.meta.getTag('name="twitter:title"');
93
   * const fbMeta: HTMLMetaElement = this.meta.getTag('property="fb:app_id"');
94
   * ```
95
   *
96
   * @param selector
97
   * @returns {HTMLMetaElement}
98
   */
99
  // getTag(selector: string): HTMLMetaElement {
100
  //   if (!selector) return null;
101
  //   return this._dom.query(`meta[${selector}]`);
102
  // }
103
  getTag(attrSelector: string): HTMLMetaElement|null {
104
  if (!attrSelector) return null;
105
  return this._dom.querySelector(this._document, `meta[${attrSelector}]`);
106
}
107

    
108
  /**
109
   * Updates the meta tag with the given selector.
110
   *
111
   * *  ### Example
112
   *
113
   * ```ts
114
   * const meta: HTMLMetaElement = this.meta.updateTag('name=description', {name: 'description',
115
   * content: 'New description'});
116
   * console.log(meta.content); // 'New description'
117
   * ```
118
   *
119
   * @param selector
120
   * @param tag updated tag definition
121
   * @returns {HTMLMetaElement}
122
   */
123
  updateTag(selector: string, tag: MetaDefinition): HTMLMetaElement {
124
    const meta: HTMLMetaElement = this.getTag(selector);
125
    if (!meta) {
126
      // create element if it doesn't exist
127
      return this._addInternal(tag);
128
    }
129
    return this._prepareMetaElement(tag, meta);
130
  }
131

    
132
  updateMeta(name, content) {
133
       const head = this._document.head;
134
       let childNodesAsList = this._dom.childNodesAsList(head);
135
       let metaEl = childNodesAsList.find(el => el['attribs'] ?  el['attribs'].name == name : false);
136
       if (metaEl) metaEl['attribs'].content = content;
137
    }
138
    updateProperty(property, content) {
139
         const head = this._document.head;
140
         let childNodesAsList = this._dom.childNodesAsList(head);
141
         let metaEl = childNodesAsList.find(el => el['attribs'] ?  el['attribs'].property == property : false);
142
         if (metaEl) metaEl['attribs'].content = content;
143
      }
144

    
145
  /**
146
   * Removes meta tag with the given selector from the dom.
147
   *
148
   *  ### Example
149
   *
150
   * ```ts
151
   * this.meta.removeTagBySelector('name=description');
152
   * ```
153
   *
154
   * @param selector
155
   */
156
  removeTagBySelector(selector: string): void {
157
    const meta: HTMLMetaElement = this.getTag(selector);
158
    this.removeTagElement(meta);
159
  }
160

    
161
  /**
162
   * Removes given meta element from the dom.
163
   *
164
   *  ### Example
165
   *  ```ts
166
   * const elem: HTMLMetaElement = this.meta.getTag('name=description');
167
   * this.meta.removeTagElement(elem);
168
   * ```
169
   *
170
   * @param meta meta element
171
   */
172
  removeTagElement(meta: HTMLMetaElement): void {
173
    if (meta) {
174
      this._removeMetaElement(meta);
175
    }
176
  }
177

    
178
  private _addInternal(tag: MetaDefinition): HTMLMetaElement {
179
    const meta: HTMLMetaElement = this._createMetaElement();
180
    this._prepareMetaElement(tag, meta);
181
    this._appendMetaElement(meta);
182
    return meta;
183
  }
184

    
185
  private _createMetaElement(): HTMLMetaElement {
186
    return this._dom.createElement('meta') as HTMLMetaElement;
187
  }
188

    
189
  private _prepareMetaElement(tag: MetaDefinition, el: HTMLMetaElement): HTMLMetaElement {
190
    Object.keys(tag).forEach((prop: string) => this._dom.setAttribute(el, prop, tag[prop]));
191
    return el;
192
  }
193

    
194
  // private _appendMetaElement(meta: HTMLMetaElement): void {
195
  //   const head = this._dom.getElementsByTagName(this._dom.defaultDoc(), 'head')[0];
196
  //   this._dom.appendChild(head, meta);
197
  // }
198
  private _appendMetaElement(meta: HTMLMetaElement): void {
199
    const head = this._document.head;
200
    this._dom.appendChild(head, meta);
201
  }
202
  private _removeMetaElement(meta: HTMLMetaElement): void {
203
    const head = this._dom.parentElement(meta);
204
    this._dom.removeChild(head, meta);
205
  }
206

    
207
  private _flattenArray(input: any[], out: any[] = []): any[] {
208
    if (input) {
209
      for (let i = 0; i < input.length; i++) {
210
        const item: any = input[i];
211
        if (Array.isArray(item)) {
212
          this._flattenArray(item, out);
213
        } else if (item) {
214
          out.push(item);
215
        }
216
      }
217
    }
218
    return out;
219
  }
220
}
(1-1/8)