From 5c641d6386a4087beab667dd2656691a8d35890d Mon Sep 17 00:00:00 2001 From: "argiro.kokogiannaki" Date: Mon, 12 Feb 2018 13:38:30 +0000 Subject: [PATCH] Refactoring - change file name of Meta service & module git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@50684 d315682c-612b-4755-9ff5-7f18f6832af3 --- app.module.ts | 2 +- claims/claimsAdmin/claimsAdmin.component.ts | 2 +- .../claimsByToken/claimsByToken.component.ts | 2 +- .../directLinking/directLinking.component.ts | 2 +- claims/linking/linkingGeneric.component.ts | 2 +- claims/myClaims/myClaims.component.ts | 2 +- .../depositBySubjectResult.component.ts | 2 +- deposit/deposit.component.ts | 2 +- deposit/depositResult.component.ts | 2 +- error/error.module.ts | 2 +- error/errorPage.component.ts | 2 +- .../dataProvider/dataProvider.component.ts | 2 +- landingPages/dataset/dataset.component.ts | 2 +- landingPages/dataset/dataset.module.ts | 2 +- .../htmlProjectReport.component.ts | 2 +- .../organization/organization.component.ts | 2 +- landingPages/project/project.component.ts | 2 +- .../publication/publication.component.ts | 2 +- .../publication/publication.module.ts | 2 +- landingPages/software/software.component.ts | 2 +- login/user.component.ts | 2 +- login/user.module.ts | 2 +- searchPages/find/search.component.ts | 2 +- .../advancedSearchPage.component.ts | 2 +- .../searchUtils/searchPage.component.ts | 2 +- .../searchPageTableView.component.ts | 2 +- sharedComponents/metaService.module.ts | 15 ++ sharedComponents/metaService.ts | 220 ++++++++++++++++++ utils/metaTags/openaireMetaTags.class.ts | 2 +- 29 files changed, 262 insertions(+), 27 deletions(-) create mode 100644 sharedComponents/metaService.module.ts create mode 100644 sharedComponents/metaService.ts diff --git a/app.module.ts b/app.module.ts index 1bb51f5d..24abd143 100755 --- a/app.module.ts +++ b/app.module.ts @@ -9,7 +9,7 @@ import { RouterModule } from '@angular/router'; import { AppComponent } from './app.component'; import {NavigationBarModule} from './sharedComponents/navigationBar.module'; import { CookieLawModule } from './sharedComponents/cookie-law/cookie-law.module'; -import {Meta} from '../angular2-meta'; +import {Meta} from './sharedComponents/metaService'; // import { ErrorModule } from './error/error.module'; import { ConfigurationService } from './utils/configuration/configuration.service'; import {MainSearchModule} from './searchPages/find/mainSearch.module'; diff --git a/claims/claimsAdmin/claimsAdmin.component.ts b/claims/claimsAdmin/claimsAdmin.component.ts index c498cc6f..d05b2376 100644 --- a/claims/claimsAdmin/claimsAdmin.component.ts +++ b/claims/claimsAdmin/claimsAdmin.component.ts @@ -1,7 +1,7 @@ import {Component, ViewChild, Input} from '@angular/core'; import {Location} from '@angular/common'; import {Observable} from 'rxjs/Observable'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; @Component({ selector: 'claims-admin', diff --git a/claims/claimsByToken/claimsByToken.component.ts b/claims/claimsByToken/claimsByToken.component.ts index 71532793..ccb0b148 100644 --- a/claims/claimsByToken/claimsByToken.component.ts +++ b/claims/claimsByToken/claimsByToken.component.ts @@ -10,7 +10,7 @@ import {ModalLoading} from '../../utils/modal/loading.component'; import {Session} from '../../login/utils/helper.class'; import {RouterHelper} from '../../utils/routerHelper.class'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {ClaimsDatatablePipe} from '../../utils/pipes/claimsDatatable.pipe'; import { DataTableDirective } from 'angular-datatables'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/claims/directLinking/directLinking.component.ts b/claims/directLinking/directLinking.component.ts index db945592..97627d42 100644 --- a/claims/directLinking/directLinking.component.ts +++ b/claims/directLinking/directLinking.component.ts @@ -8,7 +8,7 @@ import {SearchDatasetsService} from '../../services/searchDatasets.service'; import {SearchSoftwareService} from '../../services/searchSoftware.service'; import{EnvProperties} from '../../utils/properties/env-properties'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; @Component({ selector: 'directLinking', diff --git a/claims/linking/linkingGeneric.component.ts b/claims/linking/linkingGeneric.component.ts index 789d072f..14448bd2 100644 --- a/claims/linking/linkingGeneric.component.ts +++ b/claims/linking/linkingGeneric.component.ts @@ -5,7 +5,7 @@ import {EntitiesSearchService} from '../../utils/entitiesAutoComplete/entitySear import {ClaimProject, ClaimResult} from '../claim-utils/claimEntities.class'; import {SearchPublicationsService} from '../../services/searchPublications.service'; import {SearchDatasetsService} from '../../services/searchDatasets.service'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import{EnvProperties} from '../../utils/properties/env-properties'; declare var UIkit:any; diff --git a/claims/myClaims/myClaims.component.ts b/claims/myClaims/myClaims.component.ts index 7d740def..74b865ec 100644 --- a/claims/myClaims/myClaims.component.ts +++ b/claims/myClaims/myClaims.component.ts @@ -1,6 +1,6 @@ import {Component, Input} from '@angular/core'; import {Observable} from 'rxjs/Observable'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; @Component({ diff --git a/deposit/datasets/depositBySubjectResult.component.ts b/deposit/datasets/depositBySubjectResult.component.ts index 54f0342b..21a7bb16 100644 --- a/deposit/datasets/depositBySubjectResult.component.ts +++ b/deposit/datasets/depositBySubjectResult.component.ts @@ -7,7 +7,7 @@ import { FetchDataproviders } from '../../utils/fetchEntitiesClasses/fetchDatapr import { SearchDataprovidersService } from '../../services/searchDataproviders.service'; import {OrganizationService} from '../../services/organization.service'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {RouterHelper} from '../../utils/routerHelper.class'; import {PiwikService} from '../../utils/piwik/piwik.service'; diff --git a/deposit/deposit.component.ts b/deposit/deposit.component.ts index 6d4c1010..37b9b0db 100644 --- a/deposit/deposit.component.ts +++ b/deposit/deposit.component.ts @@ -1,7 +1,7 @@ import {Component, Input} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import { Router, ActivatedRoute} from '@angular/router'; -import { Meta} from '../../angular2-meta'; +import { Meta} from '../sharedComponents/metaService'; import {PiwikService} from '../utils/piwik/piwik.service'; import{EnvProperties} from '../utils/properties/env-properties'; diff --git a/deposit/depositResult.component.ts b/deposit/depositResult.component.ts index c3b5b199..eff752d9 100644 --- a/deposit/depositResult.component.ts +++ b/deposit/depositResult.component.ts @@ -6,7 +6,7 @@ import { FetchDataproviders } from '../utils/fetchEntitiesClasses/fetchDataprovi import { SearchDataprovidersService } from '../services/searchDataproviders.service'; import {OrganizationService} from '../services/organization.service'; -import { Meta} from '../../angular2-meta'; +import { Meta} from '../sharedComponents/metaService'; import {RouterHelper} from '../utils/routerHelper.class'; import {PiwikService} from '../utils/piwik/piwik.service'; diff --git a/error/error.module.ts b/error/error.module.ts index 7cea4c20..41fb24ca 100644 --- a/error/error.module.ts +++ b/error/error.module.ts @@ -3,7 +3,7 @@ import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import {ErrorPageComponent } from './errorPage.component'; -import {MetaModule} from '../../meta.module'; +import {MetaModule} from '../sharedComponents/metaService.module'; @NgModule({ imports: [ diff --git a/error/errorPage.component.ts b/error/errorPage.component.ts index 30029577..8c2bb227 100644 --- a/error/errorPage.component.ts +++ b/error/errorPage.component.ts @@ -1,6 +1,6 @@ import { Component, Input } from '@angular/core'; import { Location } from '@angular/common'; -import { Meta} from '../../angular2-meta'; +import { Meta} from '../sharedComponents/metaService'; import {ActivatedRoute} from '@angular/router'; @Component({ diff --git a/landingPages/dataProvider/dataProvider.component.ts b/landingPages/dataProvider/dataProvider.component.ts index 653b15c8..c0e593c2 100644 --- a/landingPages/dataProvider/dataProvider.component.ts +++ b/landingPages/dataProvider/dataProvider.component.ts @@ -3,7 +3,7 @@ import {Observable} from 'rxjs/Observable'; import {DataProviderService} from './dataProvider.service'; import {DataProviderInfo} from '../../utils/entities/dataProviderInfo'; import {ActivatedRoute, Router} from '@angular/router'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import { FetchPublications } from '../../utils/fetchEntitiesClasses/fetchPublications.class'; import { SearchPublicationsService } from '../../services/searchPublications.service'; import { FetchDatasets } from '../../utils/fetchEntitiesClasses/fetchDatasets.class'; diff --git a/landingPages/dataset/dataset.component.ts b/landingPages/dataset/dataset.component.ts index 8661c099..4b49d89e 100644 --- a/landingPages/dataset/dataset.component.ts +++ b/landingPages/dataset/dataset.component.ts @@ -4,7 +4,7 @@ import {DatasetService} from './dataset.service'; import {DatasetInfo} from '../../utils/entities/datasetInfo'; import {ActivatedRoute, Router} from '@angular/router'; import {RouterHelper} from '../../utils/routerHelper.class'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/landingPages/dataset/dataset.module.ts b/landingPages/dataset/dataset.module.ts index 8aeb5940..85540d73 100644 --- a/landingPages/dataset/dataset.module.ts +++ b/landingPages/dataset/dataset.module.ts @@ -18,7 +18,7 @@ import { LandingModule } from '../landing-utils/landing.module'; import {FreeGuard} from'../../login/freeGuard.guard'; import {IsRouteEnabled} from '../../error/isRouteEnabled.guard'; -import {MetaModule} from '../../../meta.module'; +import {MetaModule} from '../../sharedComponents/metaService.module'; import { ConfigurationServiceModule } from '../../utils/configuration/configurationService.module'; diff --git a/landingPages/htmlProjectReport/htmlProjectReport.component.ts b/landingPages/htmlProjectReport/htmlProjectReport.component.ts index d318ec0f..9898f5d3 100644 --- a/landingPages/htmlProjectReport/htmlProjectReport.component.ts +++ b/landingPages/htmlProjectReport/htmlProjectReport.component.ts @@ -3,7 +3,7 @@ import {Observable} from 'rxjs/Observable'; import {ActivatedRoute, Params, Router} from '@angular/router'; import {HtmlProjectReportService} from './htmlProjectReport.service'; import {ProjectService} from '../project/project.service'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/landingPages/organization/organization.component.ts b/landingPages/organization/organization.component.ts index c0cd9348..814d4650 100644 --- a/landingPages/organization/organization.component.ts +++ b/landingPages/organization/organization.component.ts @@ -12,7 +12,7 @@ import {FetchDataproviders} from '../../utils/fetchEntitiesClasses/fetchDataprov import {SearchPublicationsService} from '../../services/searchPublications.service'; import {SearchDataprovidersService} from '../../services/searchDataproviders.service'; import {SearchProjectsService} from '../../services/searchProjects.service'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {ErrorCodes} from '../../utils/properties/openaireProperties'; import {SearchingProjectsTabComponent} from '../landing-utils/searchingProjectsInTab.component'; import {RouterHelper} from '../../utils/routerHelper.class'; diff --git a/landingPages/project/project.component.ts b/landingPages/project/project.component.ts index beef051a..9fab4a19 100644 --- a/landingPages/project/project.component.ts +++ b/landingPages/project/project.component.ts @@ -16,7 +16,7 @@ import {ModalLoading} from '../../utils/modal/loading.component'; import {ReportsService} from '../../services/reports.service'; import { ErrorCodes} from '../../utils/properties/openaireProperties'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/landingPages/publication/publication.component.ts b/landingPages/publication/publication.component.ts index 419bf79d..8582ee2b 100644 --- a/landingPages/publication/publication.component.ts +++ b/landingPages/publication/publication.component.ts @@ -7,7 +7,7 @@ import {RouterHelper} from '../../utils/routerHelper.class'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; diff --git a/landingPages/publication/publication.module.ts b/landingPages/publication/publication.module.ts index b2dd29c6..5472c560 100644 --- a/landingPages/publication/publication.module.ts +++ b/landingPages/publication/publication.module.ts @@ -18,7 +18,7 @@ import { ResultLandingModule } from '../landing-utils/resultLanding.module'; import { LandingModule } from '../landing-utils/landing.module'; import {FreeGuard} from'../../login/freeGuard.guard'; import {IsRouteEnabled} from '../../error/isRouteEnabled.guard'; -import {MetaModule} from '../../../meta.module'; +import {MetaModule} from '../../sharedComponents/metaService.module'; import { ConfigurationServiceModule } from '../../utils/configuration/configurationService.module'; @NgModule({ imports: [ diff --git a/landingPages/software/software.component.ts b/landingPages/software/software.component.ts index 018c3213..f8a49f95 100644 --- a/landingPages/software/software.component.ts +++ b/landingPages/software/software.component.ts @@ -4,7 +4,7 @@ import {SoftwareService} from './software.service'; import {SoftwareInfo} from '../../utils/entities/softwareInfo'; import {ActivatedRoute, Router} from '@angular/router'; import {RouterHelper} from '../../utils/routerHelper.class'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/login/user.component.ts b/login/user.component.ts index f8f03c57..a89206ff 100644 --- a/login/user.component.ts +++ b/login/user.component.ts @@ -5,7 +5,7 @@ import {ActivatedRoute, Router} from '@angular/router'; // import {LoginService} from './login.service'; import {User,Session} from './utils/helper.class'; import {RouterHelper} from '../utils/routerHelper.class'; -import { Meta} from '../../angular2-meta'; +import { Meta} from '../sharedComponents/metaService'; import{EnvProperties} from '../utils/properties/env-properties'; @Component({ diff --git a/login/user.module.ts b/login/user.module.ts index 676d3fd3..48d36ffa 100644 --- a/login/user.module.ts +++ b/login/user.module.ts @@ -7,7 +7,7 @@ import { UserRoutingModule } from './user-routing.module'; import {PreviousRouteRecorder} from '../utils/piwik/previousRouteRecorder.guard'; import {FreeGuard} from './freeGuard.guard'; import {LoginGuard} from './loginGuard.guard'; -import {MetaModule} from '../../meta.module'; +import {MetaModule} from '../sharedComponents/metaService.module'; // import {LoginService} from './login.service'; @NgModule({ diff --git a/searchPages/find/search.component.ts b/searchPages/find/search.component.ts index 4b150028..7a407ef4 100644 --- a/searchPages/find/search.component.ts +++ b/searchPages/find/search.component.ts @@ -3,7 +3,7 @@ import {Observable} from 'rxjs/Observable'; import {ActivatedRoute, Router} from '@angular/router'; import {Location} from '@angular/common'; -import { Meta, MetaDefinition} from '../../../angular2-meta'; +import { Meta, MetaDefinition} from '../../sharedComponents/metaService'; import { FetchPublications } from '../../utils/fetchEntitiesClasses/fetchPublications.class'; import { FetchDataproviders } from '../../utils/fetchEntitiesClasses/fetchDataproviders.class'; diff --git a/searchPages/searchUtils/advancedSearchPage.component.ts b/searchPages/searchUtils/advancedSearchPage.component.ts index 26e23fb6..5a2c1ffd 100644 --- a/searchPages/searchUtils/advancedSearchPage.component.ts +++ b/searchPages/searchUtils/advancedSearchPage.component.ts @@ -8,7 +8,7 @@ import {SearchFields, FieldDetails} from '../../utils/properties/searchFields'; import {SearchUtilsClass} from './searchUtils.class'; import {ModalLoading} from '../../utils/modal/loading.component'; import {StringUtils, Dates} from '../../utils/string-utils.class'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import {PiwikService} from '../../utils/piwik/piwik.service'; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/searchPages/searchUtils/searchPage.component.ts b/searchPages/searchUtils/searchPage.component.ts index 038e1618..8019a25f 100644 --- a/searchPages/searchUtils/searchPage.component.ts +++ b/searchPages/searchUtils/searchPage.component.ts @@ -9,7 +9,7 @@ import {SearchFields, FieldDetails} from '../../utils/properties/searchFields'; import {SearchUtilsClass} from './searchUtils.class'; import {DOI, StringUtils} from '../../utils/string-utils.class'; import {ModalLoading} from '../../utils/modal/loading.component'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import{SearchFilterComponent} from './searchFilter.component'; import {SearchFilterModalComponent} from './searchFilterModal.component'; import { ErrorCodes} from '../../utils/properties/openaireProperties'; diff --git a/searchPages/searchUtils/searchPageTableView.component.ts b/searchPages/searchUtils/searchPageTableView.component.ts index 03445b2c..b8fb98a9 100644 --- a/searchPages/searchUtils/searchPageTableView.component.ts +++ b/searchPages/searchUtils/searchPageTableView.component.ts @@ -10,7 +10,7 @@ import {SearchFields, FieldDetails} from '../../utils/properties/searchFields'; import {SearchUtilsClass} from './searchUtils.class'; import {DOI, StringUtils} from '../../utils/string-utils.class'; import {ModalLoading} from '../../utils/modal/loading.component'; -import { Meta} from '../../../angular2-meta'; +import { Meta} from '../../sharedComponents/metaService'; import{SearchFilterComponent} from './searchFilter.component'; import {SearchFilterModalComponent} from './searchFilterModal.component'; import { ErrorCodes} from '../../utils/properties/openaireProperties'; diff --git a/sharedComponents/metaService.module.ts b/sharedComponents/metaService.module.ts new file mode 100644 index 00000000..84aa629e --- /dev/null +++ b/sharedComponents/metaService.module.ts @@ -0,0 +1,15 @@ +import { NgModule} from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { FormsModule } from '@angular/forms'; +import { RouterModule } from '@angular/router'; +import {Meta} from './metaService'; + + +@NgModule({ + imports: [ + CommonModule + + ], + providers:[ Meta] +}) +export class MetaModule {} diff --git a/sharedComponents/metaService.ts b/sharedComponents/metaService.ts new file mode 100644 index 00000000..a1d0ddc0 --- /dev/null +++ b/sharedComponents/metaService.ts @@ -0,0 +1,220 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {Injectable, Inject} from '@angular/core'; +// es6-modules are used here +import { ɵgetDOM as getDOM} from '@angular/platform-browser'; +import {DOCUMENT} from '@angular/platform-browser'; +/** + * Represent meta element. + * + * ### Example + * + * ```ts + * { name: 'application-name', content: 'Name of my application' }, + * { name: 'description', content: 'A description of the page', id: 'desc' } + * // ... + * // Twitter + * { name: 'twitter:title', content: 'Content Title' } + * // ... + * // Google+ + * { itemprop: 'name', content: 'Content Title' }, + * { itemprop: 'description', content: 'Content Title' } + * // ... + * // Facebook / Open Graph + * { property: 'fb:app_id', content: '123456789' }, + * { property: 'og:title', content: 'Content Title' } + * ``` + * + * @experimental + */ +export interface MetaDefinition { + charset?: string; + content?: string; + httpEquiv?: string; + id?: string; + itemprop?: string; + name?: string; + property?: string; + scheme?: string; + url?: string; + [prop: string]: string; +} + +/** + * A service that can be used to get and add meta tags. + * + * @experimental + */ +@Injectable() +export class Meta { + private _dom: any = getDOM(); + constructor( @Inject(DOCUMENT) private _document: any) { } + /** + * Sets the title of the page + */ + setTitle(title: string) { + this._document.title = title + } + /** + * Adds a new meta tag to the dom. + * + * ### Example + * + * ```ts + * const name: MetaDefinition = {name: 'application-name', content: 'Name of my application'}; + * const desc: MetaDefinition = {name: 'description', content: 'A description of the page'}; + * const tags: HTMLMetaElement[] = this.meta.addTags([name, desc]); + * ``` + * + * @param tags + * @returns {HTMLMetaElement[]} + */ + addTags(...tags: Array): HTMLMetaElement[] { + const presentTags = this._flattenArray(tags); + if (presentTags.length === 0) return []; + return presentTags.map((tag: MetaDefinition) => this._addInternal(tag)); + } + + /** + * Gets the meta tag by the given selector. Returns element or null + * if there's no such meta element. + * + * ### Example + * + * ```ts + * const meta: HTMLMetaElement = this.meta.getTag('name=description'); + * const twitterMeta: HTMLMetaElement = this.meta.getTag('name="twitter:title"'); + * const fbMeta: HTMLMetaElement = this.meta.getTag('property="fb:app_id"'); + * ``` + * + * @param selector + * @returns {HTMLMetaElement} + */ + // getTag(selector: string): HTMLMetaElement { + // if (!selector) return null; + // return this._dom.query(`meta[${selector}]`); + // } + getTag(attrSelector: string): HTMLMetaElement|null { + if (!attrSelector) return null; + return this._dom.querySelector(this._document, `meta[${attrSelector}]`); +} + + /** + * Updates the meta tag with the given selector. + * + * * ### Example + * + * ```ts + * const meta: HTMLMetaElement = this.meta.updateTag('name=description', {name: 'description', + * content: 'New description'}); + * console.log(meta.content); // 'New description' + * ``` + * + * @param selector + * @param tag updated tag definition + * @returns {HTMLMetaElement} + */ + updateTag(selector: string, tag: MetaDefinition): HTMLMetaElement { + const meta: HTMLMetaElement = this.getTag(selector); + if (!meta) { + // create element if it doesn't exist + return this._addInternal(tag); + } + return this._prepareMetaElement(tag, meta); + } + + updateMeta(name, content) { + const head = this._document.head; + let childNodesAsList = this._dom.childNodesAsList(head); + let metaEl = childNodesAsList.find(el => el['attribs'] ? el['attribs'].name == name : false); + if (metaEl) metaEl['attribs'].content = content; + } + updateProperty(property, content) { + const head = this._document.head; + let childNodesAsList = this._dom.childNodesAsList(head); + let metaEl = childNodesAsList.find(el => el['attribs'] ? el['attribs'].property == property : false); + if (metaEl) metaEl['attribs'].content = content; + } + + /** + * Removes meta tag with the given selector from the dom. + * + * ### Example + * + * ```ts + * this.meta.removeTagBySelector('name=description'); + * ``` + * + * @param selector + */ + removeTagBySelector(selector: string): void { + const meta: HTMLMetaElement = this.getTag(selector); + this.removeTagElement(meta); + } + + /** + * Removes given meta element from the dom. + * + * ### Example + * ```ts + * const elem: HTMLMetaElement = this.meta.getTag('name=description'); + * this.meta.removeTagElement(elem); + * ``` + * + * @param meta meta element + */ + removeTagElement(meta: HTMLMetaElement): void { + if (meta) { + this._removeMetaElement(meta); + } + } + + private _addInternal(tag: MetaDefinition): HTMLMetaElement { + const meta: HTMLMetaElement = this._createMetaElement(); + this._prepareMetaElement(tag, meta); + this._appendMetaElement(meta); + return meta; + } + + private _createMetaElement(): HTMLMetaElement { + return this._dom.createElement('meta') as HTMLMetaElement; + } + + private _prepareMetaElement(tag: MetaDefinition, el: HTMLMetaElement): HTMLMetaElement { + Object.keys(tag).forEach((prop: string) => this._dom.setAttribute(el, prop, tag[prop])); + return el; + } + + // private _appendMetaElement(meta: HTMLMetaElement): void { + // const head = this._dom.getElementsByTagName(this._dom.defaultDoc(), 'head')[0]; + // this._dom.appendChild(head, meta); + // } + private _appendMetaElement(meta: HTMLMetaElement): void { + const head = this._document.head; + this._dom.appendChild(head, meta); + } + private _removeMetaElement(meta: HTMLMetaElement): void { + const head = this._dom.parentElement(meta); + this._dom.removeChild(head, meta); + } + + private _flattenArray(input: any[], out: any[] = []): any[] { + if (input) { + for (let i = 0; i < input.length; i++) { + const item: any = input[i]; + if (Array.isArray(item)) { + this._flattenArray(item, out); + } else if (item) { + out.push(item); + } + } + } + return out; + } +} diff --git a/utils/metaTags/openaireMetaTags.class.ts b/utils/metaTags/openaireMetaTags.class.ts index 3b78aa42..4d77a0f0 100644 --- a/utils/metaTags/openaireMetaTags.class.ts +++ b/utils/metaTags/openaireMetaTags.class.ts @@ -1,4 +1,4 @@ -import {Meta} from '../../../angular2-meta'; +import {Meta} from '../../sharedComponents/metaService'; export class OpenaireMetaTags{