src/app/search/filters/filters.page.ts
OnInit
OnDestroy
selector | app-filters |
styleUrls | ./filters.page.scss |
templateUrl | ./filters.page.html |
Properties |
Methods |
|
constructor(contentService: ContentService, popCtrl: PopoverController, events: Events, commonUtilService: CommonUtilService, platform: Platform, location: Location, router: Router, headerService: AppHeaderService, telemetryGeneratorService: TelemetryGeneratorService, formAndFrameworkUtilService: FormAndFrameworkUtilService)
|
|||||||||||||||||||||||||||||||||
Defined in src/app/search/filters/filters.page.ts:34
|
|||||||||||||||||||||||||||||||||
Parameters :
|
Async applyFilter |
applyFilter()
|
Defined in src/app/search/filters/filters.page.ts:106
|
Returns :
any
|
Public Async applyInterimFilter |
applyInterimFilter()
|
Defined in src/app/search/filters/filters.page.ts:213
|
Returns :
any
|
Private Async fetchChannelIdName |
fetchChannelIdName()
|
Defined in src/app/search/filters/filters.page.ts:60
|
Returns :
any
|
getFilterValues | ||||||
getFilterValues(facet: string)
|
||||||
Defined in src/app/search/filters/filters.page.ts:191
|
||||||
Parameters :
Returns :
any
|
getSelectedOptionCount | ||||
getSelectedOptionCount(facet)
|
||||
Defined in src/app/search/filters/filters.page.ts:130
|
||||
Parameters :
Returns :
string
|
handleBackButton |
handleBackButton()
|
Defined in src/app/search/filters/filters.page.ts:207
|
It will hndle the device back button functionality
Returns :
void
|
init |
init()
|
Defined in src/app/search/filters/filters.page.ts:146
|
Returns :
void
|
Async ionViewWillEnter |
ionViewWillEnter()
|
Defined in src/app/search/filters/filters.page.ts:65
|
Returns :
any
|
ionViewWillLeave |
ionViewWillLeave()
|
Defined in src/app/search/filters/filters.page.ts:69
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Defined in src/app/search/filters/filters.page.ts:253
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Defined in src/app/search/filters/filters.page.ts:55
|
Returns :
void
|
Async openFilterOptions | ||||
openFilterOptions(facet)
|
||||
Defined in src/app/search/filters/filters.page.ts:75
|
||||
Parameters :
Returns :
any
|
reset |
reset()
|
Defined in src/app/search/filters/filters.page.ts:100
|
Returns :
void
|
facetsFilter |
Type : Array<any>
|
Default value : []
|
Defined in src/app/search/filters/filters.page.ts:29
|
filterCriteria |
Type : any
|
Defined in src/app/search/filters/filters.page.ts:26
|
initialFilterCriteria |
Type : any
|
Defined in src/app/search/filters/filters.page.ts:27
|
shouldEnableFilter |
Default value : true
|
Defined in src/app/search/filters/filters.page.ts:33
|
source |
Type : string
|
Defined in src/app/search/filters/filters.page.ts:32
|
supportedUserTypesConfig |
Type : Array<any>
|
Default value : []
|
Defined in src/app/search/filters/filters.page.ts:34
|
unregisterBackButton |
Type : Subscription
|
Defined in src/app/search/filters/filters.page.ts:31
|
import { Component, Inject, OnDestroy, OnInit } from '@angular/core';
import { PopoverController, Platform } from '@ionic/angular';
import { Events } from '@app/util/events';
import find from 'lodash/find';
import { CommonUtilService } from '@app/services/common-util.service';
import { Subscription } from 'rxjs';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
import { FilteroptionComponent } from '@app/app/components/filteroption/filteroption.component';
import { AppHeaderService } from '@app/services/app-header.service';
import { TelemetryGeneratorService } from '@app/services/telemetry-generator.service';
import {
Environment, InteractSubtype, InteractType, PageId
} from '@app/services/telemetry-constants';
import { ContentService, ContentSearchResult, SearchType } from 'sunbird-sdk';
import { ContentUtil } from '@app/util/content-util';
import { FormAndFrameworkUtilService } from '@app/services';
@Component({
selector: 'app-filters',
templateUrl: './filters.page.html',
styleUrls: ['./filters.page.scss']
})
export class FiltersPage implements OnInit, OnDestroy {
filterCriteria: any;
initialFilterCriteria: any;
facetsFilter: Array<any> = [];
unregisterBackButton: Subscription;
source: string;
shouldEnableFilter = true;
supportedUserTypesConfig: Array<any> = [];
constructor(
@Inject('CONTENT_SERVICE') private contentService: ContentService,
private popCtrl: PopoverController,
private events: Events,
private commonUtilService: CommonUtilService,
private platform: Platform,
private location: Location,
private router: Router,
private headerService: AppHeaderService,
private telemetryGeneratorService: TelemetryGeneratorService,
private formAndFrameworkUtilService: FormAndFrameworkUtilService
) {
this.filterCriteria = this.router.getCurrentNavigation().extras.state.filterCriteria;
this.initialFilterCriteria = this.router.getCurrentNavigation().extras.state.initialfilterCriteria;
this.source = this.router.getCurrentNavigation().extras.state.source;
this.supportedUserTypesConfig = this.router.getCurrentNavigation().extras.state.supportedUserTypesConfig;
this.handleBackButton();
}
ngOnInit(): void {
this.fetchChannelIdName();
this.init();
}
private async fetchChannelIdName() {
this.filterCriteria = await this.formAndFrameworkUtilService.changeChannelIdToName(this.filterCriteria);
this.initialFilterCriteria = await this.formAndFrameworkUtilService.changeChannelIdToName(this.initialFilterCriteria);
}
async ionViewWillEnter() {
this.headerService.showHeaderWithBackButton([]);
}
ionViewWillLeave() {
if (this.unregisterBackButton) {
this.unregisterBackButton.unsubscribe();
}
}
async openFilterOptions(facet) {
const popUp = await this.popCtrl.create({
component: FilteroptionComponent,
componentProps:
{
facet,
source: this.source
},
cssClass: 'option-box'
});
const values = new Map();
values['facetsClicked'] = facet.name;
this.telemetryGeneratorService.generateInteractTelemetry(InteractType.TOUCH,
InteractSubtype.FILTER_CLICKED,
Environment.HOME,
(this.source && this.source.match('courses')) ? PageId.COURSE_SEARCH_FILTER : PageId.LIBRARY_SEARCH_FILTER,
undefined,
values);
await popUp.present();
const { data } = await popUp.onDidDismiss();
if (data && data.isFilterApplied) {
this.applyInterimFilter();
}
}
reset() {
this.filterCriteria = JSON.parse(JSON.stringify(this.initialFilterCriteria));
this.facetsFilter = [];
this.init();
}
async applyFilter() {
const values = {
appliedFilter: {}
};
values.appliedFilter = this.filterCriteria;
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.TOUCH,
InteractSubtype.APPLY_FILTER_CLICKED,
Environment.HOME,
PageId.COURSE_SEARCH_FILTER,
undefined,
values);
this.telemetryGeneratorService.generateInteractTelemetry(
InteractType.TOUCH,
InteractSubtype.APPLY_FILTER_CLICKED,
Environment.HOME,
(this.source && this.source.match('courses')) ? PageId.COURSE_SEARCH_FILTER : PageId.LIBRARY_SEARCH_FILTER,
undefined,
values);
this.filterCriteria = await this.formAndFrameworkUtilService.changeChannelNameToId(this.filterCriteria);
this.events.publish('search.applyFilter', this.filterCriteria);
this.location.back();
}
getSelectedOptionCount(facet) {
let count = 0;
facet.values.forEach((value) => {
if (value.apply) {
count += 1;
}
});
if (count > 0) {
return `${count} ` + this.commonUtilService.translateMessage('FILTER_ADDED');
}
return '';
}
init() {
const filters: Array<any> = [];
this.filterCriteria.facets.forEach(facet => {
const data = this.getFilterValues(facet);
if (data) {
filters.push(data);
}
});
if (filters && filters.length) {
this.filterCriteria.facetFilters.length = 0;
this.filterCriteria.facetFilters = filters;
}
this.filterCriteria.facetFilters.forEach(facet => {
if (facet.values && facet.values.length > 0) {
if (facet.name === 'gradeLevel') {
const maxIndex: number = facet.values.reduce((acc, val) => (val.index && (val.index > acc)) ? val.index : acc, 0);
facet.values.sort((i, j) => (i.index || maxIndex + 1) - (j.index || maxIndex + 1));
} else if (facet.name === 'audience') {
facet.values.sort((i, j) => i.name.localeCompare(j.name));
facet.values.forEach((element, index) => {
this.supportedUserTypesConfig.forEach((userType, newIndex) => {
if (userType['ambiguousFilters'].includes(element.name)) {
element.name = userType['code'];
}
});
});
facet.values = this.commonUtilService.deDupe(facet.values, 'name');
} else {
facet.values.sort((i, j) => i.name.localeCompare(j.name));
}
facet.values.forEach((element, index) => {
if (element.name.toUpperCase() === 'other'.toUpperCase()) {
const elementVal = element;
facet.values.splice(index, 1);
facet.values.push(elementVal);
}
});
this.facetsFilter.push(facet);
}
});
}
getFilterValues(facet: string) {
if (facet) {
const filterName = find(this.filterCriteria.facetFilters, ['name', facet]);
if (filterName && filterName.values && filterName.values.length) {
return filterName;
} else {
return false;
}
} else {
return false;
}
}
/**
* It will hndle the device back button functionality
*/
handleBackButton() {
this.unregisterBackButton = this.platform.backButton.subscribeWithPriority(10, () => {
this.location.back();
});
}
public async applyInterimFilter() {
this.filterCriteria.mode = 'hard';
this.filterCriteria.searchType = SearchType.FILTER;
this.filterCriteria.fields = [];
this.shouldEnableFilter = false;
const loader = await this.commonUtilService.getLoader();
await loader.present();
this.filterCriteria = await this.formAndFrameworkUtilService.changeChannelNameToId(this.filterCriteria);
const modifiedCriteria = JSON.parse(JSON.stringify(this.filterCriteria));
modifiedCriteria.facetFilters.forEach(facet => {
if (facet.values && facet.values.length > 0) {
if (facet.name === 'audience') {
facet.values = ContentUtil.getAudienceFilter(facet, this.supportedUserTypesConfig);
}
}
});
this.contentService.searchContent(modifiedCriteria).toPromise()
.then(async (responseData: ContentSearchResult) => {
this.shouldEnableFilter = true;
if (responseData) {
this.facetsFilter = [];
this.filterCriteria = undefined;
this.filterCriteria = await this.formAndFrameworkUtilService.changeChannelIdToName(responseData.filterCriteria);
responseData.filterCriteria.facetFilters.forEach(element => {
this.initialFilterCriteria.facetFilters.forEach(item => {
if (element.name === item.name) {
element['translatedName'] = item.translatedName;
return;
}
});
});
await loader.dismiss();
this.init();
}
}).catch(async () => {
await loader.dismiss();
this.shouldEnableFilter = true;
});
}
ngOnDestroy() {
this.events.publish('update_back_header', true);
}
}
<ion-content>
<div class="subtitle ion-padding ion-no-margin">
<h6 style="display:inline-block;" >{{ 'FILTER_BY' | translate }}</h6>
<ion-button color="dark" class="reset_button" fill="outline" (click)="reset()">{{ 'RESET' | translate}}
<ion-icon role="button" aria-label="filter by" name="refresh"></ion-icon>
</ion-button>
</div>
<div *ngIf="facetsFilter && facetsFilter.length">
<div *ngFor="let facet of facetsFilter">
<div class="flex-card-container filter-item" (click)="openFilterOptions(facet)">
<div class="card-text">
<ion-label class="ion-text-capitalize">{{ facet?.translatedName }}</ion-label>
<ion-label class="text-end">{{ getSelectedOptionCount(facet) }}</ion-label>
</div>
<div class="card-icon">
<ion-icon name="arrow-forward"></ion-icon>
</div>
</div>
</div>
</div>
</ion-content>
<ion-footer class="footer">
<ion-row>
<ion-col>
<ion-button expand="block" [disabled]="!shouldEnableFilter" (click)="applyFilter()">{{ 'APPLY_FILTER' | translate }}</ion-button>
</ion-col>
</ion-row>
</ion-footer>
./filters.page.scss
@import "src/assets/styles/_variables.scss";
:host {
.subtitle {
text-transform: uppercase;
font-size: 1.3rem;
font-weight: 500 !important;
color: map-get($colors, primary_black);
background-color: map-get($colors, white_f0);
}
.filter-item {
width: auto;
margin: 16px !important;
background-color: map-get($colors, white_f0);
color: map-get($colors, dark_gray);
font-size: 1rem;
font-weight: 600 !important;
height: 2rem;
ion-icon {
color: map-get($colors, dark_gray);
}
}
.item-inner>ion-icon {
min-height: 2rem !important;
font-size: 2rem !important;
}
.apply-button {
margin: 16px !important;
width: 90%;
}
.flex-card-container {
display: flex;
flex-wrap: nowrap;
align-items:center;
justify-content:center;
background-color: map-get($colors, white_f1);
padding: 23px;
margin-bottom: 12px;
.card-text {
width: 100%;
}
.card-icon{
width: 1.25rem;
}
}
.text-end {
float: right;
}
.footer{
background: #FFF;
bottom: 0;
}
.reset_button{
float: right;
height: 1.875rem;
margin-top: 8px;
}
}