websetup/ts/websetup.classes.tagmanager.ts

64 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

2020-11-04 18:01:04 +00:00
import { TagLevel } from './websetup.classes.taglevel';
import * as plugins from './websetup.plugins';
import * as interfaces from './interfaces';
import { MetaTag } from './websetup.classes.tag.metatag';
import { JsonLdTag } from './websetup.classes.tag.jsonldtag';
import { OpengraphTag } from './websetup.classes.tag.opengraphtag';
export class TagManager {
2020-11-05 17:56:35 +00:00
public globalLevel: TagLevel = new TagLevel(this, 'global');
2020-11-04 18:01:04 +00:00
2020-11-05 17:56:35 +00:00
public baseLevel: TagLevel = new TagLevel(this, 'base');
2020-11-04 18:01:04 +00:00
public activeLevel: TagLevel;
public async setup(metaObjectArg: interfaces.IMetaObject) {
// global tag level
this.globalLevel.addTag(new MetaTag('google', 'notranslate'));
this.globalLevel.addTag(new MetaTag('revisit-after', '1 days'));
2020-12-07 05:06:30 +00:00
2020-11-06 01:11:26 +00:00
if (metaObjectArg.twitterHandle) {
this.globalLevel.addTag(new MetaTag('twitter:card', 'summary_large_image'));
this.globalLevel.addTag(new MetaTag('twitter:site', metaObjectArg.twitterHandle));
this.globalLevel.addTag(new MetaTag('twitter:creator', metaObjectArg.twitterHandle));
}
2020-11-04 18:01:04 +00:00
// base tag level
this.baseLevel.title = metaObjectArg.title;
2020-11-05 17:56:35 +00:00
if (metaObjectArg.description) {
this.baseLevel.addTag(new MetaTag('description', metaObjectArg.description));
}
2020-11-04 18:09:07 +00:00
if (metaObjectArg.canonicalDomain) {
this.baseLevel.addTag(new MetaTag('canonical', metaObjectArg.canonicalDomain));
2020-11-04 18:01:04 +00:00
}
2020-11-04 18:09:07 +00:00
2020-11-04 18:01:04 +00:00
if (metaObjectArg.ldCompany) {
this.baseLevel.addCompanyInfo(metaObjectArg.ldCompany);
}
await this.globalLevel.enable();
2020-11-05 18:36:08 +00:00
this.activeLevel = this.baseLevel;
await this.activeLevel.enable();
2020-11-04 18:01:04 +00:00
}
2020-11-05 18:36:08 +00:00
public async setSubPageLevel(metaObjectArg: interfaces.IMetaObject) {
2020-11-04 18:01:04 +00:00
const subPageLevel = new TagLevel(this, 'subpage');
subPageLevel.title = metaObjectArg.title;
2020-11-05 17:56:35 +00:00
if (metaObjectArg.description) {
2020-11-05 20:03:12 +00:00
subPageLevel.addTag(new MetaTag('description', metaObjectArg.description));
2020-11-05 17:56:35 +00:00
}
2020-11-05 18:36:08 +00:00
await this.activeLevel.disable();
2020-11-04 18:01:04 +00:00
this.activeLevel = subPageLevel;
2020-11-05 18:36:08 +00:00
await this.activeLevel.enable();
2020-11-05 15:35:31 +00:00
return subPageLevel;
2020-11-04 18:01:04 +00:00
}
2020-11-05 18:36:08 +00:00
public async revertToBaseLevel() {
2020-11-04 18:01:04 +00:00
if (this.activeLevel !== this.baseLevel) {
2020-11-05 18:36:08 +00:00
await this.activeLevel.disable();
2020-11-04 18:01:04 +00:00
this.activeLevel = this.baseLevel;
2020-11-05 18:36:08 +00:00
await this.activeLevel.enable();
2020-11-04 18:01:04 +00:00
}
}
2020-11-04 18:09:07 +00:00
}