58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import {Post} from '../model/post';
|
|
|
|
@Injectable()
|
|
export class StateService {
|
|
|
|
//this keeps track of which type is enabled / disabled
|
|
tagFilter:any = [];
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
shouldDisplay(post:Post): boolean {
|
|
//check whether all filters are true or false: that case, just show everything
|
|
let foundTrue:boolean = false;
|
|
let foundFalse:boolean = false;
|
|
for(let 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(let 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];
|
|
}
|
|
}
|