Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

feat(operators): add searchbar #264

Closed
7 changes: 7 additions & 0 deletions src/app/operators/operators.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
[fixedInViewport]="true"
class="operator-list-sidenav"
#operatorSidenav>
<form>
<mat-form-field class="full-width-form">
<mat-label>Search</mat-label>
<input matInput value="" [formControl]="searchInput">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that this input is model driven and not template drive, could you move the value initialization of the formControl into the component? IMO is cleaner to not mix template/model driven when possible.

<mat-icon matSuffix>search</mat-icon>
</mat-form-field>
</form>
<mat-nav-list *ngFor="let category of categories"
class="operator-list">
<h3 mat-subheader
Expand Down
6 changes: 6 additions & 0 deletions src/app/operators/operators.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ $subheader-color: #333;
bottom: 10px;
z-index: 4;
}

.full-width-form{
width: 95%;
height: 48px;
padding: 6px;
}
43 changes: 36 additions & 7 deletions src/app/operators/operators.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
OnInit,
OnDestroy,
AfterContentInit,
ChangeDetectionStrategy,
ViewChild
} from '@angular/core';
import {
Expand All @@ -15,15 +14,20 @@ import {
animate,
transition
} from '@angular/animations';
import { Router, ActivatedRoute } from '@angular/router';
import { BreakpointObserver } from '@angular/cdk/layout';
import { MatSidenav } from '@angular/material';
import { Subscription } from 'rxjs/Subscription';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import { filter, takeUntil } from 'rxjs/operators';
import {
filter,
takeUntil,
flatMap,
toArray,
debounceTime
} from 'rxjs/operators';
import { OperatorDoc } from '../../operator-docs/operator.model';
import { OperatorMenuService } from '../core/services/operator-menu.service';
import { FormControl } from '@angular/forms';
import { from } from 'rxjs/observable/from';

const OPERATOR_MENU_GAP_LARGE = 64;
const OPERATOR_MENU_GAP_SMALL = 54;
Expand Down Expand Up @@ -66,15 +70,35 @@ export class OperatorsComponent implements OnInit, AfterContentInit, OnDestroy {
public categories: string[];
private _onDestroy = new Subject();

public searchInput: FormControl;

constructor(
private _breakpointObserver: BreakpointObserver,
private _operatorMenuService: OperatorMenuService,
@Inject(OPERATORS_TOKEN) public operators: OperatorDoc[]
) {}

ngOnInit() {
this.groupedOperators = groupOperatorsByType(this.operators);
this.categories = Object.keys(this.groupedOperators);
this.initOperatorList(this.operators);
this.searchInput = new FormControl();
const search$ = this.searchInput.valueChanges;
const entries$ = from(this.operators);
const searchResult$ = search$.pipe(
debounceTime(300),
flatMap(options =>
entries$.pipe(
filter(
(value: OperatorDoc) =>
value.name.indexOf(options) !== -1 ||
value.operatorType.indexOf(options) !== -1
),
toArray()
)
)
);
searchResult$.subscribe((search: OperatorDoc[]) => {
this.initOperatorList(search);
});
}

ngAfterContentInit() {
Expand Down Expand Up @@ -105,6 +129,11 @@ export class OperatorsComponent implements OnInit, AfterContentInit, OnDestroy {
ngOnDestroy() {
this._onDestroy.next();
}

private initOperatorList(operators: OperatorDoc[]) {
this.groupedOperators = groupOperatorsByType(operators);
this.categories = Object.keys(this.groupedOperators);
}
}

export function groupOperatorsByType(operators: OperatorDoc[]): OperatorDocMap {
Expand Down
3 changes: 3 additions & 0 deletions src/app/operators/operators.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { WalkthroughComponent } from './components/walkthrough/walkthrough.compo
import { HighlightJsDirective } from './directives/highlight-js.directive';
import { SafeUrlPipe } from './pipes/safe-url.pipe';
import { MaterialModule } from '../material/material.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the import of FormsModule isn't required. At least in the refactored component ngModel hasn't been used.

BTW, could we keep a consistent import style? Maybe the one explained in the angular style guide


@NgModule({
declarations: [
Expand All @@ -42,6 +43,8 @@ import { MaterialModule } from '../material/material.module';
MaterialModule,
OperatorsRoutingModule,
LayoutModule,
FormsModule,
ReactiveFormsModule,
TranslateModule
],
providers: [
Expand Down