File

assets/angular/record_search/record-search.component.ts

Description

Record Search component

Author: Shilo Banihit

Extends

LoadableComponent

Metadata

moduleId module.id
providers Location { : , : }
selector record-search
templateUrl ./record_search.html

Index

Properties
Methods
Inputs

Constructor

constructor(elm: ElementRef, LocationService: Location, recordsService: RecordsService, dashboardService: DashboardService, translationService: TranslationService)
Parameters :
Name Type Optional Description
elm ElementRef
LocationService Location
recordsService RecordsService
dashboardService DashboardService
translationService TranslationService

Inputs

record_type

Type: string

search_str

Type: string

search_url

Type: string

Methods

ngOnInit
ngOnInit()
Returns : void
resetSearch
resetSearch()
Returns : void
search
search(refinerConfig: RecordSearchRefiner, shouldSyncLoc: boolean)
Parameters :
Name Type Optional Description
refinerConfig RecordSearchRefiner
shouldSyncLoc boolean
Returns : void
setSearchLabel
setSearchLabel()
Returns : void
syncLoc
syncLoc()
Returns : void
toggleAdvancedSearch
toggleAdvancedSearch()
Returns : void

Properties

advancedSearchLabel
advancedSearchLabel: string
Type : string
advanceMode
advanceMode: boolean
Type : boolean
isSearching
isSearching: boolean
Type : boolean
params
params: RecordSearchParams
Type : RecordSearchParams
plans
plans: any[]
Type : any[]
queryStr
queryStr: string
Type : string
searchMsg
searchMsg: string
Type : string
searchMsgType
searchMsgType: string
Type : string
Public translationService
translationService: TranslationService
Type : TranslationService
import { Component, Inject, Input, Output, ElementRef, EventEmitter } from '@angular/core';
import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Role, User, LoginResult, SaveResult } from '../shared/user-models';
import * as _ from "lodash-lib";
import { LoadableComponent } from '../shared/loadable.component';
import { TranslationService } from '../shared/translation-service';
import { RecordsService, RecordSearchParams, RecordSearchRefiner} from '../shared/form/records.service';
import { DashboardService } from '../shared/dashboard-service';

declare var pageData :any;
declare var jQuery: any;
/**
 * Record Search component
 *
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 */
@Component({
  moduleId: module.id,
  selector: 'record-search',
  templateUrl: './record_search.html',
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]
})
export class RecordSearchComponent extends LoadableComponent {
  @Input() record_type: string;
  @Input() search_str: string;
  @Input() search_url: string;

  plans: any[];
  advanceMode: boolean;
  advancedSearchLabel: string;
  params: RecordSearchParams;
  isSearching: boolean;
  searchMsgType: string;
  searchMsg: string;
  queryStr: string;

  constructor(
   elm: ElementRef,
   @Inject(Location) protected LocationService: Location,
   protected recordsService: RecordsService,
   protected dashboardService: DashboardService,
   public translationService:TranslationService) {
    super();
    this.initTranslator(translationService);
    this.record_type = elm.nativeElement.getAttribute('record_type');
    this.search_str = elm.nativeElement.getAttribute('search_str');
    this.search_url = elm.nativeElement.getAttribute('search_url');
    this.queryStr = elm.nativeElement.getAttribute("full_url").split('?')[1];
    this.params = new RecordSearchParams(this.record_type);
  }

  ngOnInit() {
    this.translationService.isReady((tService:any)=> {
      this.recordsService.getType(this.record_type).then((typeConf: any) => {
        const searchFilterConfig = [];
        _.forEach(typeConf.searchFilters, (searchConfig:any)=> {
          searchFilterConfig.push(new RecordSearchRefiner(searchConfig));
        });
        this.params.setRefinerConfig(searchFilterConfig);
        this.setLoading(false);
        if (!_.isEmpty(this.queryStr)) {
          console.log(`Using query string: ${this.queryStr}`);
          this.params.parseQueryStr(this.queryStr);
          this.search(null, false);
        }
        this.LocationService.subscribe((popState:any) => {
          this.queryStr = popState.url.split('?')[1];
          this.params.parseQueryStr(this.queryStr);
          this.search(null, false);
        }, (exception: any) => {
          console.log(`Error on location service monitoring.`);
          console.log(exception);
        }, ()=> {
        });
      });
    });
  }

  resetSearch() {
    this.params.clear();
    this.plans = null;
    this.LocationService.go(this.search_url);
    this.searchMsg = null;
  }

  syncLoc() {
    this.LocationService.go(this.params.getHttpQuery(this.search_url));
  }

  toggleAdvancedSearch() {
    this.advanceMode = !this.advanceMode;
    this.setSearchLabel();
  }

  setSearchLabel() {
    if (this.advanceMode) {
      this.advancedSearchLabel = 'record-search-advanced-hide';
    } else {
      this.advancedSearchLabel = 'record-search-advanced-show';
    }
  }

  search(refinerConfig: RecordSearchRefiner = null, shouldSyncLoc:boolean = true) {
    if (!_.isEmpty(this.params.basicSearch)) {
      if (refinerConfig) {
        this.params.addActiveRefiner(refinerConfig);
      }
      this.isSearching = true;
      this.plans = null;
      this.searchMsgType = "info";
      this.searchMsg = `${this.translationService.t('record-search-searching')}${this.spinnerElem}`;
      if (shouldSyncLoc) {
        this.syncLoc();
      }
      this.recordsService.search(this.params).then((res:any) => {
        this.isSearching = false;
        this.searchMsgType = "success";
        this.searchMsg = `${this.translationService.t('record-search-results')}${res.records.length}`;
        this.dashboardService.setDashboardTitle(null, res.records);
        this.params.setFacetValues(res.facets);
        this.plans = res.records;
      }).catch((err:any) => {
        this.isSearching = false;
        this.searchMsg = err;
        this.searchMsgType = "danger";
      });
    }
  }
}

@Component({
  moduleId: module.id,
  selector: 'record-search-refiner',
  templateUrl: './record_search_refiner.html'
})
export class RecordSearchRefinerComponent {
  @Input() refinerConfig: RecordSearchRefiner;
  @Input() isSearching: boolean;
  @Output() onApplyFilter: EventEmitter<any> = new EventEmitter<any>();

  applyFilter(event:any, refinerValue:any = null) {
    event.preventDefault();
    if (this.hasValue()) {
      this.refinerConfig.activeValue = refinerValue;
      this.onApplyFilter.emit(this.refinerConfig);
    }
  }

  hasValue() {
    return !_.isEmpty(this.refinerConfig.value);
  }

}
<div class="row">
  <div class="col-md-offset-2 col-md-8" *ngIf="!isLoading">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title">
          {{ 'record-search-heading' | translate }}
        </h3>
      </div>
      <div class="panel-body" style="overflow:scroll" >
        <div class="input-group">
          <span class="input-group-addon" id="basic-search-addon">{{ 'record-search-basic-search' | translate }}</span>
          <input type="text" [disabled]="isSearching" [(ngModel)]="params.basicSearch" class="form-control" placeholder="{{ 'record-search-basic-search-placeholder' | translate}}" (keyup.enter)="search()" aria-describedby="basic-search-addon">
          <span class="input-group-btn"><button [disabled]="isSearching || !params.basicSearch" class="btn btn-primary" type='button' (click)="search()">{{ 'record-search-submit' | translate }}</button></span>
        </div>
        <div><br/></div>
        <button [disabled]="isSearching" class="btn btn-primary" type='button' (click)="resetSearch()">{{ 'record-search-reset' | translate }}</button>
        <div><br/></div>
        <div *ngIf="searchMsg" class="bg-{{searchMsgType}} text-center" [innerHtml]="searchMsg"></div>
      </div>
    </div>
  </div>
</div>
<!-- Results section -->
<div *ngIf="!isLoading && !isSearching && ((plans && plans.length > 0) || params.hasActiveRefiners())" class="row">
  <div class="col-md-2">
    <!-- Exact match and facets -->
    <record-search-refiner *ngFor="let refinerConfig of params.getRefinerConfigs()" [isSearching]="isSearching" [refinerConfig]="refinerConfig" (onApplyFilter)="search($event)" ></record-search-refiner>
  </div>
  <div class="col-md-10">
    <div class="panel panel-default" *ngFor="let plan of plans">
      <div class="panel-body">
        <h3>
          <a href="{{ recordsService.brandingAndPortalUrl }}/record/view/{{ plan.storage_id }}" target="_blank">{{ plan.dashboardTitle }}</a>
          <a *ngIf="plan.hasEditAccess" class="glyphicon glyphicon-pencil" href="{{ recordsService.brandingAndPortalUrl }}/record/edit/{{ plan.storage_id }}" tooltip="Edit" ></a>
        </h3>
        <div [innerHtml]="plan.description"></div>
      </div>
    </div>
  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""