File

src/app/page-filter/page-filter.page.ts

Metadata

Index

Properties
Methods

Constructor

constructor(frameworkUtilService: FrameworkUtilService, popCtrl: PopoverController, navParams: NavParams, platform: Platform, translate: TranslateService, appGlobalService: AppGlobalService, events: Events, telemetryGeneratorService: TelemetryGeneratorService, commonUtilService: CommonUtilService, formAndFrameworkUtilService: FormAndFrameworkUtilService, menuCtrl: MenuController)
Parameters :
Name Type Optional
frameworkUtilService FrameworkUtilService No
popCtrl PopoverController No
navParams NavParams No
platform Platform No
translate TranslateService No
appGlobalService AppGlobalService No
events Events No
telemetryGeneratorService TelemetryGeneratorService No
commonUtilService CommonUtilService No
formAndFrameworkUtilService FormAndFrameworkUtilService No
menuCtrl MenuController No

Methods

Async apply
apply()
Returns : any
Async cancel
cancel()
Returns : any
Async getFrameworkData
getFrameworkData(frameworkId: string, currentCategory: string, index: number)

This will internally call framework API

Parameters :
Name Type Optional Description
frameworkId string No
currentCategory string No
  • request Parameter passing to the framework API
index number No
  • Local variable name to hold the list data
Returns : unknown
getRootOrganizations
getRootOrganizations(index)
Parameters :
Name Optional
index No
Returns : void
getSelectedOptionCount
getSelectedOptionCount(facet)
Parameters :
Name Optional
facet No
Returns : string
Async initFilterValues
initFilterValues()
Returns : any
ionViewWillEnter
ionViewWillEnter()
Returns : void
ionViewWillLeave
ionViewWillLeave()
Returns : void
onLanguageChange
onLanguageChange()
Returns : void
Async openFilterOptions
openFilterOptions(facet)
Parameters :
Name Optional
facet No
Returns : any

Properties

backButtonFunc
Default value : undefined
backupFilters
callback
Type : PageFilterCallback
categories
Type : Array<FrameworkCategoryCode>
Default value : FrameworkCategoryCodesGroup.DEFAULT_FRAMEWORK_CATEGORIES
facetsFilter
filters
Type : []
Default value : []
pageId
pagetAssemblefilter
Type : PageAssembleFilter
Default value : {}
selectedLanguage
Type : string
Default value : 'en'
import { Component, Inject, ViewEncapsulation } from '@angular/core';
import { NavParams, Platform, PopoverController, MenuController } from '@ionic/angular';
import { Events } from '@app/util/events';
import map from 'lodash/map';
import cloneDeep from 'lodash/cloneDeep';
import { TranslateService } from '@ngx-translate/core';
import {
  CategoryTerm,
  FrameworkCategoryCode,
  FrameworkCategoryCodesGroup,
  FrameworkUtilService,
  GetFrameworkCategoryTermsRequest,
  PageAssembleFilter,
  CachedItemRequestSourceFrom
} from 'sunbird-sdk';

import { PageFilterOptionsPage } from './page-filter-options/page-filter-options.page';
import { AppGlobalService } from 'services/app-global-service.service';
import { TelemetryGeneratorService } from 'services/telemetry-generator.service';
import { CommonUtilService } from 'services/common-util.service';
import { FormAndFrameworkUtilService } from 'services/formandframeworkutil.service';
import { PageId, ImpressionType, Environment, InteractSubtype, InteractType } from '@app/services';

@Component({
  selector: 'app-page-filter',
  templateUrl: './page-filter.page.html',
  styleUrls: ['./page-filter.page.scss'],
  encapsulation: ViewEncapsulation.None
})
export class PageFilterPage {
  pagetAssemblefilter: PageAssembleFilter = {};
  callback: PageFilterCallback;
  filters = [];
  pageId;
  facetsFilter;
  backupFilters;

  backButtonFunc = undefined;
  selectedLanguage = 'en';
  categories: Array<FrameworkCategoryCode> = FrameworkCategoryCodesGroup.DEFAULT_FRAMEWORK_CATEGORIES;

  constructor(
    @Inject('FRAMEWORK_UTIL_SERVICE') private frameworkUtilService: FrameworkUtilService,
    private popCtrl: PopoverController,
    private navParams: NavParams,
    private platform: Platform,
    private translate: TranslateService,
    private appGlobalService: AppGlobalService,
    private events: Events,
    private telemetryGeneratorService: TelemetryGeneratorService,
    private commonUtilService: CommonUtilService,
    private formAndFrameworkUtilService: FormAndFrameworkUtilService,
    private menuCtrl: MenuController
  ) {
    this.callback = this.navParams.get('callback');
    this.initFilterValues();

    this.backButtonFunc = this.platform.backButton.subscribeWithPriority(11, () => {
      this.popCtrl.dismiss();
    });

    this.events.subscribe('onAfterLanguageChange:update', () => {
      this.onLanguageChange();
    });
  }

  ionViewWillEnter() {
    this.menuCtrl.enable(false);
  }

  onLanguageChange() {
    if (this.filters) {
      this.filters.forEach(filter => {
        if (filter.code === 'contentType' && filter.hasOwnProperty('resourceTypeValues')) {
          const resourceTypes = [];
          filter.resourceTypeValues.forEach(element => {
            resourceTypes.push(this.commonUtilService.getTranslatedValue(element.translations, element.name));
          });
          filter.values = resourceTypes;
          if (filter.hasOwnProperty('selected')) {
            const translatedSected = [];
            filter.selectedValuesIndices.forEach(selectedIndex => {
              translatedSected.push(filter.values[selectedIndex]);
            });
            filter.selected = translatedSected;
          }
        }
      });
    }
  }

  async initFilterValues() {
    this.filters = this.navParams.get('filter');
    this.backupFilters = JSON.parse(JSON.stringify(this.filters));
    this.pageId = this.navParams.get('pageId');
    const loader = await this.commonUtilService.getLoader();
    await loader.present();
    if (this.pageId === PageId.COURSES) {
      this.pageId = PageId.COURSE_PAGE_FILTER;
      this.categories = FrameworkCategoryCodesGroup.COURSE_FRAMEWORK_CATEGORIES;
    } else if (this.pageId === PageId.LIBRARY) {
      this.pageId = PageId.LIBRARY_PAGE_FILTER;
    }
    this.telemetryGeneratorService.generateImpressionTelemetry(
      ImpressionType.VIEW, '',
      this.pageId,
      Environment.HOME
    );

    if (this.filters) {
      const filterNames = [];
      this.filters.forEach(element => {
        element.name = this.commonUtilService.getTranslatedValue(element.translations, element.name);
        filterNames.push(element.code);
      });

      const values = new Map();
      values['categories'] = filterNames;
      this.telemetryGeneratorService.generateInteractTelemetry(
        InteractType.OTHER,
        InteractSubtype.FILTER_CONFIG,
        Environment.HOME,
        this.pageId,
        undefined,
        values
      );
    }

    this.filters.forEach(filter => {
      if (filter.code === 'contentType' && !filter.hasOwnProperty('resourceTypeValues')) {
        filter.resourceTypeValues = cloneDeep(filter.values);
        const resourceTypes = [];
        filter.values.forEach(element => {
          resourceTypes.push(this.commonUtilService.getTranslatedValue(element.translations, element.name));
        });
        filter.values = resourceTypes;
      }
    });

    const syllabus: Array<string> = this.appGlobalService.getCurrentUser().syllabus;
    let frameworkId;

    if (this.pageId === PageId.COURSE_PAGE_FILTER) {
      frameworkId = await this.formAndFrameworkUtilService.getCourseFrameworkId();
    } else {
      frameworkId = (syllabus && syllabus.length > 0) ? syllabus[0] : undefined;
    }
    let index = 0;
    for (const element of this.filters) {
      try {
        if (!element.frameworkCategory && this.pageId === PageId.COURSE_PAGE_FILTER) {
          await this.getRootOrganizations(index);
        } else {
          await this.getFrameworkData(frameworkId, element.code, index);
        }
      } catch (error) {
        console.log('error: ' + error);
      }
      // Framework API doesn't return domain and content Type exclude them
      if (index === this.filters.length - 1) {
        this.facetsFilter = this.filters;
        await loader.dismiss();
      }
      index++;
    }
  }

  /**
   * This will internally call framework API
   * @param  currentCategory - request Parameter passing to the framework API
   * @param index - Local variable name to hold the list data
   */
  async getFrameworkData(frameworkId: string, currentCategory: string, index: number) {
    return new Promise((resolve, reject) => {
      const req: GetFrameworkCategoryTermsRequest = {
        from: CachedItemRequestSourceFrom.SERVER,
        currentCategoryCode: currentCategory,
        language: this.translate.currentLang,
        requiredCategories: this.categories,
        frameworkId
      };
      this.frameworkUtilService.getFrameworkCategoryTerms(req).toPromise()
        .then((res: CategoryTerm[]) => {
          const category = res;
          const responseArray = category;
          if (responseArray && responseArray.length > 0) {
            if (req.currentCategoryCode === 'topic' && this.pageId === PageId.COURSE_PAGE_FILTER) {
              for (let i = 0; i < responseArray.length; i++) {
                const name = responseArray[i].name;
                this.filters[index].values[i] = {};
                this.filters[index].values[i][name] = responseArray[i].children;
              }
              resolve();
            } else {
              resolve(this.filters[index].values = map(responseArray, 'name'));
            }
          }
        })
        .catch(err => {
          reject(err);
        });
    });
  }
  async openFilterOptions(facet) {
    const filterDialog = await this.popCtrl.create({
      component: PageFilterOptionsPage,
      componentProps: {
        facets: facet
      },
      cssClass: 'resource-filter-options'
    });
    await filterDialog.present();
  }

  getSelectedOptionCount(facet) {
    if (facet.selected && facet.selected.length > 0) {
      this.pagetAssemblefilter[facet.code] = facet.selected;
      return `${facet.selected.length} ` + this.commonUtilService.translateMessage('FILTER_ADDED');
    }

    return '';
  }

  async apply() {
    if (this.callback) {
      const filters = cloneDeep(this.facetsFilter);
      filters.forEach(element => {
        if (element.code === 'contentType' && element.selectedValuesIndices && element.selectedValuesIndices.length) {
          const resourceTypeSelectedValues = [];
          element.resourceTypeValues.forEach((item, index) => {
            if (element.selectedValuesIndices.includes(index)) {
              resourceTypeSelectedValues.push(item.code);
            }
          });
          this.pagetAssemblefilter[element.code] = resourceTypeSelectedValues;
        }
      });
      this.callback.applyFilter(this.pagetAssemblefilter, this.facetsFilter, true);
    }
    await this.popCtrl.dismiss({apply: true});
  }

  async cancel() {
    this.telemetryGeneratorService.generateInteractTelemetry(InteractType.TOUCH,
      InteractSubtype.CANCEL,
      Environment.HOME,
      this.pageId);

    this.callback.applyFilter(this.pagetAssemblefilter, this.backupFilters, false);
    await this.popCtrl.dismiss();
  }

  getRootOrganizations(index) {
    this.formAndFrameworkUtilService.getRootOrganizations()
      .then(res => {
        this.filters[index].values = res;
      })
      .catch(error => {
        console.log(error, 'index', index);
      });
  }

  ionViewWillLeave(): void {
    if (this.backButtonFunc) {
      this.backButtonFunc.unsubscribe();
    }
    this.menuCtrl.enable(true);
  }
}

export interface PageFilterCallback {
  applyFilter(filter: PageAssembleFilter, appliedFilter: any, isChecked: boolean);
}

<ion-content>
  <h4 class="subtitle ion-padding ion-no-margin">{{ 'AVAILABLE_FILTERS' | translate }} </h4>
  <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 *ngIf="facet.name" class="ion-text-capitalize">{{ facet?.name | translate  }}</ion-label>
          <ion-label *ngIf="facet.orgName" class="ion-text-capitalize">{{ facet?.orgName | translate  }}</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>
  <ion-row>
    <ion-col size="6">
      <ion-button fill="outline" expand="block" (click)="cancel()"> {{ 'CANCEL' | translate }}</ion-button>
    </ion-col>
    <ion-col size="6">
      <ion-button expand="block" (click)="apply()"> {{ 'APPLY' | translate }}</ion-button>
    </ion-col>
  </ion-row>
</ion-footer>

./page-filter.page.scss

@import "src/assets/styles/_custom-mixins.scss";
@import "src/assets/styles/_variables.scss";
:host {
    .subtitle {
      text-transform: capitalize;
      font-size: 1.3rem;
      font-weight: 700 !important;
      color: map-get($colors, primary_black);
    } 
    .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);
      }
    }
    .apply-button {
      margin: 16px !important;
      width: 90%;
    }
     
    .scroll-list {
      overflow-y: scroll;
      max-height: 50vh !important;
      padding-bottom: 10vh !important;
    }
    .item-inner>ion-icon {
      min-height: 2rem !important;
      font-size: 2rem !important;
    }
    .item-inner {
      @include padding(null, 0, null, null !important);
    }
  }
  
  .resource-filter .popover-content {
    margin: 0;
    width: 100% !important;
    max-height: 70% !important;
    box-shadow: unset;
    position: fixed;
    @include ltr() {
      left: 0 !important;
    }
  
    @include rtl() {
      right: 0 !important;
    }
    top: unset !important;
    bottom: 0;
    transform-origin: bottom -100% !important;
    transform: translateY(0) !important;
  }
  
  .resource-filter ion-backdrop {
    background-color: map-get($colors, black) !important;
    opacity: 0.4 !important;
  }
  .item-native {
    width: auto;
    margin: 16px !important;
    background-color: map-get($colors, white_f0);
    color: map-get($colors, dark_gray);
    font-size: 1rem;
    height: 2rem;
    padding-top: 0.5em;
  }
  .text-end {
    float: right;
  } 

  .flex-card-container {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items:center !important;
    justify-content:center !important;
    background-color: map-get($colors, white_f1);
    padding: 23px;
    margin-bottom: 12px;
    .card-text {
      width: 100%;
    }
    .card-icon{
      width: 1.25rem;   
    }
  } 
  .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);
    }
  }
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""