nm-history-search
src/app/shared/widgets/apps/history/search/search.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
selector | nm-history-search |
styleUrls | search.component.scss |
templateUrl | ./search.component.html |
Widget inputs |
Widget outputs |
Properties |
|
Methods |
|
constructor(_widgetframeService: WidgetframeService, formBuilder: FormBuilder, _listService: ListService, localStorageService: LocalStorageService, _changeDetectorRef: ChangeDetectorRef)
|
||||||||||||||||||
Parameters :
|
clear |
clear()
|
Returns :
void
|
Protected configureWidget | ||||||
configureWidget(configuration: WidgetConfig
|
||||||
Decorators : WidgetConfigure
|
||||||
Parameters :
Returns :
void
|
doFreetextSearch | ||||
doFreetextSearch(value: )
|
||||
Parameters :
Returns :
void
|
doSearch | ||||
doSearch(value: )
|
||||
Parameters :
Returns :
void
|
ngAfterViewChecked |
ngAfterViewChecked()
|
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Returns :
void
|
Public ngOnInit |
ngOnInit()
|
Returns :
void
|
onChange | ||||
onChange(event: )
|
||||
Parameters :
Returns :
void
|
resetFromLocalStorage | ||||||
resetFromLocalStorage(entry: LocalStorageEntry)
|
||||||
Parameters :
Returns :
void
|
search |
search()
|
Returns :
void
|
Private updateTextField |
updateTextField(value: string, noOfMonths: string)
|
Returns :
void
|
Public _id |
_id:
|
Type : string
|
Decorators : WidgetId
|
Private attributes |
attributes:
|
Type : any
|
Private clearChannel |
clearChannel:
|
Type : Subject<any>
|
Default value : new Subject<any>()
|
Decorators : WidgetInput
|
Clears the value of the search-field and resets the search |
Public cols |
cols:
|
Type : any[]
|
Public configuration |
configuration:
|
Type : WidgetConfig<HistorySearchConfiguration>
|
Decorators : WidgetConfiguration
|
Public disableSearchOnEmptyField |
disableSearchOnEmptyField:
|
Type : boolean
|
Default value : false
|
Private encodeLink |
encodeLink:
|
Type : boolean
|
Default value : true
|
Public infoHeight |
infoHeight:
|
Type : string
|
Public infotext |
infotext:
|
Type : string
|
Public infoWidth |
infoWidth:
|
Type : string
|
Private inputChannel |
inputChannel:
|
Type : Subject<any>
|
Default value : new BehaviorSubject<any>(null)
|
Decorators : WidgetInput
|
Sets the value of the search-field and performs a search |
inputComponent |
inputComponent:
|
Decorators : ViewChild
|
Public inputLink |
inputLink:
|
Type : string
|
Public inputLinklistSearch |
inputLinklistSearch:
|
Type : string
|
Private localStorageSearchEntry |
localStorageSearchEntry:
|
Type : LocalStorageEntry
|
Public placeholderNoOfMonths |
placeholderNoOfMonths:
|
Type : string
|
Public placeholderSearchInput |
placeholderSearchInput:
|
Type : string
|
Public resetOutput |
resetOutput:
|
Type : Subject<any>
|
Default value : new Subject<any>()
|
Decorators : WidgetOutput
|
Emits when search is reset |
Private saveSearchTerm |
saveSearchTerm:
|
Type : Subject<any>
|
Default value : new Subject<any>()
|
Decorators : WidgetInput
|
Saves the search term in the local storage |
Public searchform |
searchform:
|
Type : FormGroup
|
Private searchTerm |
searchTerm:
|
Type : Subject<any>
|
Default value : new Subject<any>()
|
Decorators : WidgetOutput
|
Emits the value of the search term |
Private storageValue |
storageValue:
|
Type : Subject<any>
|
Default value : new ReplaySubject<any>(1)
|
Private sub |
sub:
|
Public title |
title:
|
Type : string
|
Public wikiLink |
wikiLink:
|
Type : string
|
import {
combineLatest as observableCombineLatest,
Subject,
Observable,
ReplaySubject,
BehaviorSubject,
} from "rxjs";
import { distinctUntilChanged, filter } from "rxjs/operators";
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
OnInit,
ViewChild,
} from "@angular/core";
import { WidgetframeService } from "../../../widgetframe/widgetframe.service";
import { WidgetConfig } from "../../../widget.configuration";
import {
WidgetComponent,
WidgetConfiguration,
WidgetConfigure,
WidgetId,
WidgetInput,
WidgetOutput,
} from "../../../widget.metadata";
import { FormBuilder, FormGroup } from "@angular/forms";
import { ListService } from "../../../list/index";
import * as uriTemplates_ from "uri-templates";
import {
LocalStorageEntry,
LocalStorageService,
} from "../../../../components/local-storage/local-storage.service";
import {
DeletionMode,
Scope,
} from "../../../../components/local-storage/local-storage-constants";
const uriTemplates = uriTemplates_;
export interface HistorySearchConfiguration {
/**
* Disables the search when the input search field is empty @default(false)
*/
disableSearchOnEmptyField?: boolean;
/**
* Sets CSS class name for the embedded header component
*/
header: string;
/**
* Sets the text of the dialog displayed when clicking the info icon
*/
infoText: string;
/**
* Sets the width size of the help icon tooltip
*/
infoWidth: string;
/**
* Sets the height size of the help icon tooltip
*/
infoHeight: string;
/**
* Sets the title of the dialog displayed when clicking the info icon
*/
infoTitle: string;
/**
* Sets the title shown in the widget header
*/
title: string;
/**
* Sets the link to external wiki system for more information
*/
wikiLink: string;
/**
* Defines the links object which contains the "search" link, used by the "uri" widget output to fill it with the current search url
*/
links: { search: string };
/**
* Sets the local storage entry key for the search input (concatenated with the "scope" configuration field value)
*/
"localstorage-search": string;
/**
* Sets the local storage entry key for the search input (concatenated with the "localstorage-search" configuration field value)
*/
scope: string;
/**
* Sets the text displayed as a place holder in the "search term" input field
*/
"placeholder-search-input": string;
/**
* Sets the text displayed as a place holder in the "no of months" input field
*/
"placeholder-no-of-months": string;
/**
* Sets the default no of months used for search if nothing is defined @default("6")
*/
"default-no-of-months"?: string;
}
@WidgetComponent("nm-history-search")
@Component({
selector: "nm-history-search",
templateUrl: "./search.component.html",
styleUrls: ["./search.component.scss"],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HistorySearchWidgetComponent implements AfterViewInit, OnInit {
public cols: any[];
private attributes: any;
public inputLink: string;
public wikiLink: string;
public infotext: string;
public infoWidth: string;
public infoHeight: string;
public title: string;
public inputLinklistSearch: string;
public placeholderSearchInput: string;
public placeholderNoOfMonths: string;
public disableSearchOnEmptyField: boolean = false;
private encodeLink: boolean = true;
private localStorageSearchEntry: LocalStorageEntry;
@WidgetConfiguration()
public configuration: WidgetConfig<HistorySearchConfiguration>;
/**
* If templating is enabled emits the url that can be used to fetch the list-search data (if in list-search mode), else uses inputLink and fills it with the current search
*/
@WidgetOutput("uri")
private uri: Subject<any> = new ReplaySubject<any>(1);
/**
* Sets the value of the search-field and performs a search
*/
@WidgetInput("set")
private inputChannel: Subject<any> = new BehaviorSubject<any>(null);
/**
* Clears the value of the search-field and resets the search
*/
@WidgetInput("clear")
private clearChannel: Subject<any> = new Subject<any>();
/**
* Emits the value of the search term
*/
@WidgetOutput("searchTerm")
private searchTerm: Subject<any> = new Subject<any>();
/**
* Emits when search is reset
*/
@WidgetOutput("reset")
public resetOutput: Subject<any> = new Subject<any>();
/**
* Saves the search term in the local storage
*/
@WidgetInput("saveSearchTerm")
private saveSearchTerm: Subject<any> = new Subject<any>();
private storageValue: Subject<any> = new ReplaySubject<any>(1);
@WidgetId()
public _id: string;
@ViewChild("inputComponent") inputComponent;
// @ViewChildren('inputComponentMonths') inputComponentMonths;
private sub;
public searchform: FormGroup;
constructor(
private _widgetframeService: WidgetframeService,
private formBuilder: FormBuilder,
private _listService: ListService,
private localStorageService: LocalStorageService,
private _changeDetectorRef: ChangeDetectorRef
) {
this.searchform = this.formBuilder.group({
noOfMonths: "",
searchInput: "",
});
}
search() {
let search = {
searchInput: this.searchform.value.searchInput,
noOfMonths: this.searchform.value.noOfMonths,
};
this.doFreetextSearch(search);
this._changeDetectorRef.markForCheck();
}
onChange(event) {
this.searchTerm.next(this.searchform.value.searchInput);
}
doFreetextSearch(value) {
if (this.localStorageSearchEntry) {
this.localStorageSearchEntry.value = JSON.stringify(value);
}
if (!(this.disableSearchOnEmptyField && value.length === 0)) {
this.uri.next(value);
}
this._changeDetectorRef.markForCheck();
}
resetFromLocalStorage(entry: LocalStorageEntry) {
if (entry.exists()) {
let value = JSON.parse(entry.value);
//this.storageValue.next(value);
this.updateTextField(value.searchInput, value.noOfMonths);
}
}
clear() {
if (this.localStorageSearchEntry) {
this.localStorageSearchEntry.clear();
}
this.updateTextField("", null);
this.resetOutput.next();
this._changeDetectorRef.markForCheck();
}
private updateTextField(value: string, noOfMonths: string) {
this.searchform.setValue({
searchInput: value || "",
noOfMonths:
noOfMonths ||
this.configuration.configuration["default-no-of-months"] ||
"6",
});
window.setTimeout(() => {
this.searchform.patchValue({
searchInput: value || "",
noOfMonths:
noOfMonths ||
this.configuration.configuration["default-no-of-months"] ||
"6",
});
this._changeDetectorRef.markForCheck();
}, 1);
}
doSearch(value) {
this.doFreetextSearch(value);
}
@WidgetConfigure()
protected configureWidget(
configuration: WidgetConfig<HistorySearchConfiguration>
) {
this.disableSearchOnEmptyField = this.configuration.configuration.disableSearchOnEmptyField;
this.wikiLink = this.configuration.configuration.wikiLink;
if (this.configuration.configuration.links) {
this.inputLink = this.configuration.configuration.links.search;
}
if (this.configuration.configuration["localstorage-search"]) {
this.localStorageSearchEntry = this.localStorageService.getLocalStorageEntry(
this.configuration.configuration.scope +
this.configuration.configuration["localstorage-search"],
Scope.GLOBAL,
DeletionMode.RESET
);
}
this.placeholderSearchInput = this.configuration.configuration[
"placeholder-search-input"
];
this.placeholderNoOfMonths = this.configuration.configuration[
"placeholder-no-of-months"
];
this.updateTextField("", null);
this.infotext = this.configuration.configuration.infoText;
this.title = this.configuration.configuration.title;
this.infoWidth = this.configuration.configuration.infoWidth;
this.infoHeight = this.configuration.configuration.infoHeight;
this.resetFromLocalStorage(this.localStorageSearchEntry);
this.clearChannel.asObservable().subscribe((reset) => {
this.clear();
});
this.saveSearchTerm.asObservable().subscribe((save) => {
this.localStorageSearchEntry.value = this.searchform.value.searchInput;
});
}
public ngOnInit() {
observableCombineLatest(
this.storageValue,
this.inputChannel,
(storage, input) => {
if (input) {
return input;
}
return storage;
}
)
.pipe(
filter((value) => value),
distinctUntilChanged()
)
.subscribe((value) => {
this.doSearch(value);
});
}
ngAfterViewInit() {
this.searchTerm.next(this.searchform.value.searchInput);
if (this.inputComponent.first != undefined) {
setTimeout(() => {
this.inputComponent.first.nativeElement.focus();
this._changeDetectorRef.markForCheck();
}, 1);
}
}
ngAfterViewChecked() {
this._changeDetectorRef.markForCheck();
}
}
<nm-widgetframe
[header]="configuration.configuration['header']"
[configuration]="configuration"
[width]="configuration.configuration['width']"
[infoTitle]="title"
[infoText]="infotext"
[infoWidth]="infoWidth"
[infoHeight]="infoHeight"
[infoPlacement]="'bottom'"
[wikiLink]="wikiLink"
widgetId="{{ _id }}"
[isCollapsible]="false"
>
<div slot="title" class="nm-widgetframe__title">
{{ title | translate }}
</div>
<div slot="content" class="nm-widgetframe__content">
<form class="login-form" [formGroup]="searchform">
<mat-form-field>
<input
matInput
#inputComponent
id="inputComponent"
(input)="onChange($event)"
class="nm-searchinput"
placeholder="{{ placeholderSearchInput | translate }}"
assetType="search"
formControlName="searchInput"
[nmAutofocus]="true"
/>
</mat-form-field>
<mat-form-field>
<input
matInput
#inputComponentMonths
id="inputComponentMonths"
type="number"
(input)="onChange($event)"
class="nm-searchinput"
placeholder="{{ placeholderNoOfMonths | translate }}"
formControlName="noOfMonths"
/>
</mat-form-field>
<div class="nm-HistorySearch__buttonGroup">
<button mat-button type="button" (click)="clear()">
{{ "button.reset" | translate }}
</button>
<button mat-raised-button color="primary" (click)="search()">
{{ "placeholder.search" | translate }}
</button>
</div>
</form>
</div>
</nm-widgetframe>