Skip to content

Commit

Permalink
feat: react pages meta and title from config
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammad-haji committed Aug 14, 2018
1 parent 62c54eb commit bbfe42a
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 194 deletions.
42 changes: 32 additions & 10 deletions src/app/@core/services/page-title-tags.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,37 @@
*/
import {ActivatedRoute, NavigationEnd, Router} from "@angular/router";
import {Meta, Title} from "@angular/platform-browser";
import {Injectable} from "@angular/core";
import {PAGE_TITLE_TAGS_CONST} from "../../config/tags/page-title-tags.const";
import {Injectable, OnInit} from "@angular/core";
import {getNestedValue} from "../utils/nested-object-value";
import {HttpClient} from "@angular/common/http";

@Injectable()
export class PageTitleTagsService {
export class PageTitleTagsService implements OnInit {
private titleTags: any;
private JSON_DIR: string = '/assets/config/title-tags/title-tags.config.json';

constructor(private readonly router: Router,
private readonly activatedRoute: ActivatedRoute,
private titleService: Title,
private httpClient: HttpClient,
private meta: Meta) {

}

ngOnInit() {
this.loadTitleTags();
}


/**
* load title tags
*/
loadTitleTags() {
this.httpClient.get(this.JSON_DIR).subscribe(titleTags => {
this.titleTags = titleTags;
})
}

/**
* update page title tags
*/
Expand All @@ -32,14 +50,18 @@ export class PageTitleTagsService {
.filter((route) => route.outlet === 'primary')
.mergeMap((route) => route.data)
.subscribe((event) => {
if (event['TITLE_TAG_KEY']) {
let PAGE_TITLE_TAGS = getNestedValue(PAGE_TITLE_TAGS_CONST, event['TITLE_TAG_KEY']);
PAGE_TITLE_TAGS['metaTags'].forEach((tag: any) => {
this.meta.updateTag(tag, `name='${tag.name}'`);
});
this.titleService.setTitle(PAGE_TITLE_TAGS['title']);
if (this.titleTags) {
if (event['TITLE_TAG_KEY']) {
let PAGE_TITLE_TAGS = getNestedValue(this.titleTags, event['TITLE_TAG_KEY']);
PAGE_TITLE_TAGS['metaTags'].forEach((tag: any) => {
this.meta.updateTag(tag, `name='${tag.name}'`);
});
this.titleService.setTitle(PAGE_TITLE_TAGS['title']);
} else {
this.titleService.setTitle('NG_IR')
}
} else {
this.titleService.setTitle('NG_IR')
this.loadTitleTags();
}
});
}
Expand Down
12 changes: 9 additions & 3 deletions src/app/config/interceptors/notification.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ import {
HttpInterceptor,
} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import {CONFIG} from '../config'
import {BodyOutputType, Toast, ToasterService} from "angular2-toaster";

const exceptionList: Array<any> = ['/assets/i18n/en.json', '/assets/i18n/fa.json'];
const exceptionList: Array<any> = ['/assets/'];

@Injectable()
export class NotificationInterceptor implements HttpInterceptor {
constructor(private toasterService: ToasterService) {
}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const {url} = request;
return next.handle(request).map((event: any) => {
if (event && event.body) {
if (event.url.indexOf(exceptionList[0]) === -1 && event.url.indexOf(exceptionList[1]) === -1) {
let isUrlException: boolean = false;
exceptionList.forEach((exception) => {
if (url.includes(exception)) {
isUrlException = true;
}
});
if (!isUrlException) {
this.popNotification('با موفقیت انجام شد', event.body.message);
}
}
Expand Down
82 changes: 0 additions & 82 deletions src/app/config/tags/page-title-tags.const.ts

This file was deleted.

178 changes: 178 additions & 0 deletions src/assets/config/title-tags/title-tags.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
{
"default": {
"title": "ngir",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "@chidna"
},
{
"name": "twitter:description",
"content": "Your final Chidna shop"
},
{
"name": "twitter:title",
"content": "chidna is Great"
}
]
},
"dashboard": {
"title": "داشبورد",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "@chidna"
},
{
"name": "twitter:description",
"content": "Your final Chidna shop"
},
{
"name": "twitter:title",
"content": "chidna is Great"
}
]
},
"users": {
"add": {
"title": "افزودن کاربر جدید",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir"
},
{
"name": "twitter:description",
"content": "بهترین فروشگاه ایران"
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
},
"list": {
"title": "لیست کاربران",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir"
},
{
"name": "twitter:description",
"content": "بهترین فروشگاه ایران"
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
}
},
"products": {
"add": {
"title": "افزودن محصول جدید",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir - کاشی"
},
{
"name": "twitter:description",
"content": "بهترین محصولات کاشی و سرامیک و ..."
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
}
},
"categories": {
"add": {
"title": "افزودن دسته جدید",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir - کاشی"
},
{
"name": "twitter:description",
"content": "بهترین محصولات کاشی و سرامیک و ..."
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
}
},
"reviews": {
"add": {
"title": "افزودن دیدگاه جدید",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir - کاشی"
},
{
"name": "twitter:description",
"content": "بهترین محصولات کاشی و سرامیک و ..."
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
},
"list": {
"title": "لیست دیدگاه ها",
"metaTags": [
{
"name": "twitter:card",
"content": "summary_large_image"
},
{
"name": "twitter:site",
"content": "ngir - کاشی"
},
{
"name": "twitter:description",
"content": "بهترین محصولات کاشی و سرامیک و ..."
},
{
"name": "twitter:title",
"content": "ngir عالی است"
}
]
}
}
}

0 comments on commit bbfe42a

Please sign in to comment.