File

libs/video-stat-dashboard/src/views-filter/views-filter-display.component.ts

Implements

OnDestroy

Metadata

selector views-filter-display
templateUrl ./views-filter-display.component.html

Index

Properties
Methods
Inputs
Outputs
Accessors

Constructor

constructor(formBuilder: FormBuilder)
Parameters :
Name Type Optional
formBuilder FormBuilder no

Inputs

age

Type: []

dateFrom

Type: number

dateTo

Type: number

region

Type: string

Outputs

ageToggle $event type: EventEmitter
dateFromChange $event type: EventEmitter
dateToChange $event type: EventEmitter
regionChange $event type: EventEmitter

Methods

ngOnDestroy
ngOnDestroy()
Returns : void

Properties

filterFormGroup
filterFormGroup: FormGroup
Type : FormGroup
Private formSubs
formSubs: Subscription[]
Type : Subscription[]
Default value : []

Accessors

region
setregion(value: string)
Parameters :
Name Type Optional
value string no
Returns : void
dateTo
setdateTo(value: number)
Parameters :
Name Type Optional
value number no
Returns : void
dateFrom
setdateFrom(value: number)
Parameters :
Name Type Optional
value number no
Returns : void
age
setage(value: [])
Parameters :
Name Type Optional
value [] no
Returns : void
import { Component, OnDestroy, Output, Input, EventEmitter } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { Subscription } from 'rxjs/Subscription';
import * as moment from 'moment';
import { mapValues, forEach } from 'lodash';

@Component({
  selector: 'views-filter-display',
  templateUrl: './views-filter-display.component.html'
})
export class ViewsFilterDisplayComponent implements OnDestroy {

  filterFormGroup: FormGroup;
  @Input() set region(value: string) {
    this.filterFormGroup.controls['region'].setValue(value, { emitEvent: false });
  };
  @Input() set dateTo(value: number) {
    this.filterFormGroup.controls['dateTo'].setValue(moment(value).format('YYYY-MM-DD'), { emitEvent: false });
  };
  @Input() set dateFrom(value: number) {
    this.filterFormGroup.controls['dateFrom'].setValue(moment(value).format('YYYY-MM-DD'), { emitEvent: false });
  };
  @Input() set age(value: boolean[]) {
    const names = ['minor', 'adults', 'middleAged', 'retired'];
    names.forEach((name, index) =>
      this.filterFormGroup.controls[name].setValue(value[index], { emitEvent: false })
    );
  };

  @Output() regionChange = new EventEmitter<string>();
  @Output() dateToChange = new EventEmitter<number>();
  @Output() dateFromChange = new EventEmitter<number>();
  @Output() ageToggle = new EventEmitter<number>();

  private formSubs: Subscription[] = [];

  constructor(formBuilder: FormBuilder) {
    const fields = {
      region: makeField([], newRegion => this.regionChange.emit(newRegion)),
      dateTo: makeField([], newDateTo => this.dateToChange.emit(moment(newDateTo).valueOf())),
      dateFrom: makeField([], newDateFrom => this.dateFromChange.emit(moment(newDateFrom).valueOf())),
      minor: makeField([], _newMinor => this.ageToggle.emit(0)),
      adults: makeField([], _newAdults => this.ageToggle.emit(1)),
      middleAged: makeField([], _newAdults => this.ageToggle.emit(2)),
      retired: makeField([], _newRetired => this.ageToggle.emit(3))
    };
    // Set up the form group
    this.filterFormGroup = formBuilder.group(mapValues(fields, f => f.fieldConfig));

    // Observe changes on the form and pass to smart component
    forEach(fields, (v, k) => this.formSubs.push(this.filterFormGroup.controls[k].valueChanges
      .subscribe(v.changeHandler)));
  }

  // Tear down any subscriptions made while setting up the form
  ngOnDestroy() {
    this.formSubs.forEach(sub => sub.unsubscribe());
  }
}

function makeField(fieldConfig: any[], changeHandler: (any) => void) {
  return { fieldConfig, changeHandler };
}
<div class="card">
  <div class="card-content">
    <span class="card-title">Filter Views</span>
    <form [formGroup]="filterFormGroup">

      <h6>Region</h6>
      <div>
        <input class="with-gap" name="region" value="North America" type="radio" id="test1" formControlName="region">
        <label for="test1">North America</label>
      </div>
      <div>
        <input class="with-gap" name="region" value="Europe" type="radio" id="test3" formControlName="region">
        <label for="test3">Europe</label>
      </div>
      <div>
        <input class="with-gap" name="region" value="Asia" type="radio" id="test4" formControlName="region">
        <label for="test4">Asia</label>
      </div>
      <div>
        <input class="with-gap" name="region" value="All" type="radio" id="test2" formControlName="region">
        <label for="test2">All</label>
      </div>

      <h6>Date Range</h6>
      <div>
        <label for="dateFrom">From</label>
        <input id="dateFrom" type="date" formControlName="dateFrom">
      </div>
      <div>
        <label for="dateTo">To</label>
        <input id="dateTo" type="date" formControlName="dateTo">
      </div>

      <h6>Age Groups</h6>
      <div>
        <input type="checkbox" id="age1" formControlName="minor">
        <label for="age1">Under 18</label>
      </div>
      <div>
        <input type="checkbox" id="age2" formControlName="adults">
        <label for="age2">18 - 40</label>
      </div>
      <div>
        <input type="checkbox" id="age3" formControlName="middleAged">
        <label for="age3">40 - 60</label>
      </div>
      <div>
        <input type="checkbox" id="age4" formControlName="retired">
        <label for="age4">Over 60</label>
      </div>
    </form>
  </div>

  <div class="card-action">
    <a href="#">Basic</a>
    <a href="#">Advanced</a>
  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""