98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
import {Injectable, OnInit} from '@angular/core';
|
|
import {Post} from '../model/post';
|
|
import {ActivatedRoute, ParamMap, Router} from '@angular/router';
|
|
import 'rxjs/add/operator/filter';
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
@Injectable()
|
|
export class StateService {
|
|
// this keeps track of which type is enabled / disabled
|
|
tagFilter: any = [];
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
/**
|
|
* Get the language currently set in the router.
|
|
*
|
|
* @param {ActivatedRoute} route
|
|
* @returns {Observable<string>}
|
|
*/
|
|
getLanguageFromRoute(route: ActivatedRoute): Observable<string> {
|
|
return route.paramMap.map((params: ParamMap) => {
|
|
const temp = params.get('language');
|
|
return temp;
|
|
});
|
|
}
|
|
|
|
shouldDisplay(post: Post): boolean {
|
|
// check whether all filters are true or false: that case, just show everything
|
|
let foundTrue = false;
|
|
let foundFalse = false;
|
|
for (const tag of Object.keys(this.tagFilter)) {
|
|
if (this.tagFilter[tag] === true) {
|
|
foundTrue = true;
|
|
}
|
|
if (this.tagFilter[tag] === false) {
|
|
foundFalse = true;
|
|
}
|
|
}
|
|
// console.log('foundFalse: '+foundFalse);
|
|
// console.log('foundTrue: '+foundTrue);
|
|
if (!foundTrue && foundFalse) {
|
|
return true;
|
|
}
|
|
if (!foundTrue && !foundFalse) {
|
|
// some weird case, just show everything
|
|
return true;
|
|
}
|
|
|
|
//check whether the post should be displayed
|
|
for (const tag of Object.keys(this.tagFilter)) {
|
|
if (post.tags.indexOf(tag) != -1 && this.tagFilter[tag] === true) {
|
|
//found the tag and its filter is enabled
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
isActiveTag(tag: string): boolean {
|
|
// console.log('isActive');
|
|
// console.log(this.tagFilter);
|
|
return this.tagFilter[tag] === true;
|
|
}
|
|
|
|
toggleTag(tag: string) {
|
|
// console.log('toggle');
|
|
// console.log(this.tagFilter);
|
|
if (this.tagFilter[tag] == null) {
|
|
this.tagFilter[tag] = true;
|
|
} else {
|
|
this.tagFilter[tag] = !this.tagFilter[tag];
|
|
}
|
|
}
|
|
|
|
setLanguage(language: string, route: ActivatedRoute, router: Router) {
|
|
console.log('setting language: '+language);
|
|
// // check out the current route
|
|
// route.pathFromRoot.forEach(path=>{
|
|
// console.log(path);
|
|
// console.log(path.routeConfig);
|
|
// });
|
|
router.navigate([language]);
|
|
|
|
// route.url.subscribe(segments => {
|
|
// console.log(segments);
|
|
// //now also do the children
|
|
// route.children.forEach(child => {
|
|
// child.url.subscribe(childsegment => {
|
|
// console.log(childsegment);
|
|
// });
|
|
// });
|
|
// });
|
|
}
|
|
}
|