import {Component} from '@angular/core';
import {EnvProperties} from '../openaireLibrary/utils/properties/env-properties';
import {ActivatedRoute, Router} from "@angular/router";
import {CommunityService} from "../openaireLibrary/connect/community/community.service";
import {HelperService} from "../openaireLibrary/utils/helper/helper.service";
import {Meta, Title} from "@angular/platform-browser";
import {SEOService} from "../openaireLibrary/sharedComponents/SEO/SEO.service";
import {PiwikService} from "../openaireLibrary/utils/piwik/piwik.service";
import {PiwikHelper} from "../utils/piwikHelper";
import {StringUtils} from "../openaireLibrary/utils/string-utils.class";
import {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
import {properties} from "../../environments/environment";
import {Subscriber, Subscription} from "rxjs";
@Component({
selector: 'subjects',
template: `
Subjects
0">
0"
[texts]="pageContents['top']">
1" class="uk-margin-large-top uk-grid uk-child-width-1-4@m" uk-grid>
{{groupedSubjects[index].group}}
`
})
export class SubjectsComponent {
public subjects: string[];
public communityId = null;
public showLoading = true;
public properties: EnvProperties = properties;
public pageContents = null;
public divContents = null;
public groupedSubjects = [];
public index: number = 0;
public subjectsLimit: number = 6;
public viewAll: boolean = false;
public maxCharacters: number = 25;
public subjectsColumns = [];
public url: string = null;
public pageTitle: string = "Subjects";
public breadcrumbs: Breadcrumb[] = [{name: 'Home', route: '/'}, {name: 'About - Subjects'}];
subs: Subscription[] = [];
constructor(private route: ActivatedRoute,
private communityService: CommunityService,
private _router: Router,
private helper: HelperService,
private _meta: Meta,
private _title: Title,
private seoService: SEOService,
private _piwikService: PiwikService) {
}
ngOnInit() {
this.showLoading = true;
this.url = this.properties.domain + this._router.url;
this.seoService.createLinkForCanonicalURL(this.url);
this.updateUrl(this.url);
this.updateTitle(this.pageTitle);
this.updateDescription("OpenAIRE - Connect, Community Gateway, research community");
this.subs.push(this.communityService.getCommunityAsObservable().subscribe(community => {
if (community) {
this.communityId = community.communityId;
if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
this.subs.push(this._piwikService.trackView(this.properties, this.pageTitle, PiwikHelper.getSiteId(this.communityId)).subscribe());
}
//this.getDivContents();
this.getPageContents();
this.subjects = community.subjects;
this.subjects.sort((a,b) => {
if(!a || a.toLocaleUpperCase() < b.toLocaleUpperCase()) {
return -1;
} else if (!b || a.toLocaleUpperCase() > b.toLocaleUpperCase()) {
return 1;
}
return 0;
});
this.groupSubjects();
this.showLoading = false;
}
}));
}
createParams(param) {
return StringUtils.quote(StringUtils.URIEncode(param));
}
ngOnDestroy() {
for (let sub of this.subs) {
if (sub instanceof Subscriber) {
sub.unsubscribe();
}
}
}
private getPageContents() {
this.subs.push(this.helper.getPageHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
this.pageContents = contents;
}));
}
private getDivContents() {
this.subs.push(this.helper.getDivHelpContents(this.properties, this.communityId, this._router.url).subscribe(contents => {
this.divContents = contents;
}));
}
// private sortSubjects(a, b) {
// }
private groupSubjects() {
if(this.subjects.length === 0) {
return [];
}
this.groupedSubjects = Object.values(
this.subjects.reduce((acc, subject) => {
let firstLetter = subject[0].toLocaleUpperCase();
if(!acc[firstLetter]) {
acc[firstLetter] = {group: firstLetter, data: [subject]};
} else {
acc[firstLetter].data.push(subject);
}
return acc;
},{})
)
if(this.subjects.length > 1) {
this.groupedSubjects.unshift({group: 'All', data: this.subjects});
}
}
public changeDisplayedSubjects(i, group) {
this.subjectsColumns = [];
this.index = i;
if(group.data.length > this.subjectsLimit && group.group != 'All') {
this.divideSubjects(group);
}
}
public divideSubjects(group) {
let columns = [];
for(let i = 0; i < (group.data.length / this.subjectsLimit); i++) {
columns.push(group.data.slice(i * this.subjectsLimit, ((i + 1) * this.subjectsLimit)));
}
this.subjectsColumns = columns;
}
private updateDescription(description: string) {
this._meta.updateTag({content: description}, "name='description'");
this._meta.updateTag({content: description}, "property='og:description'");
}
private updateTitle(title: string) {
var _title = ((title.length > 50) ? title.substring(0, 50) : title);
this._title.setTitle(_title);
this._meta.updateTag({content: _title}, "property='og:title'");
}
private updateUrl(url: string) {
this._meta.updateTag({content: url}, "property='og:url'");
}
}