UNPKG

@rero/ng-core

Version:

RERO angular core library.

436 lines (435 loc) 17.2 kB
import { Component, ElementRef, EventEmitter, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { FormlyFieldConfig } from '@ngx-formly/core'; import { TranslateService } from '@ngx-translate/core'; import { NgxSpinnerService } from 'ngx-spinner'; import { MenuItem } from 'primeng/api'; import { ToggleSwitchChangeEvent } from 'primeng/toggleswitch'; import { BehaviorSubject, Observable, Subscription } from 'rxjs'; import { ApiService } from '../../api/api.service'; import { Error } from '../../error/error'; import { ActionStatus } from '../action-status'; import { JSONSchema7 } from '../editor/utils'; import { Aggregation, SearchField, SearchFilter, SearchFilterSection, SearchResult } from '../record'; import { RecordUiService } from '../record-ui.service'; import { RecordService } from '../record.service'; import { ChangeEvent } from './paginator/paginator.component'; import { AggregationsFilter, RecordSearchService } from './record-search.service'; import { IChecked } from './search-filters/search-filters.component'; import * as i0 from "@angular/core"; export interface SearchParams { currentType: string; index: string; q: string; page: number; size: number; aggregationsFilters: Array<AggregationsFilter>; sort: string; searchFields: Array<SearchField>; searchFilters: Array<SearchFilter | SearchFilterSection>; } export interface SortOption { value: string; label: string; defaultQuery?: boolean; defaultNoQuery?: boolean; icon?: string; } export declare class RecordSearchComponent implements OnInit, OnChanges, OnDestroy { protected recordService: RecordService; protected recordUiService: RecordUiService; protected recordSearchService: RecordSearchService; protected translateService: TranslateService; protected spinner: NgxSpinnerService; protected apiService: ApiService; protected activatedRoute: ActivatedRoute; /** Page anchor to scroll on the top */ topScroll: ElementRef; /** Current selected resource type */ currentType: string; /** Search query */ q: string; /** Define the current record's page */ page: number; /** Define the number of records per page */ size: number; /** Defined the sort order */ sort: string; /** If admin mode is disabled, no action can be performed on a record (as add, update or remove) */ adminMode: ActionStatus; /** Custom URL to notice detail */ detailUrl: string; /** Resources types available */ types: { key: string; label: string; index?: string; component?: Component; total?: number; canAdd?: any; canUpdate?: any; canDelete?: any; canRead?: any; permissions?: any; aggregations?: any; preFilters?: any; defaultSearchInputFilters?: Array<AggregationsFilter>; listHeaders?: any; itemHeaders?: any; aggregationsName?: any; aggregationsOrder?: Array<string>; aggregationsExpand?: Array<string> | (() => Array<string>); aggregationsHide?: Array<string> | (() => Array<string>); aggregationsBucketSize?: number; showSearchInput?: boolean; pagination?: { boundaryLinks?: boolean; maxSize?: number; pageReport?: boolean; rowsPerPageOptions?: number[]; }; formFieldMap?: (field: FormlyFieldConfig, jsonSchema: JSONSchema7) => FormlyFieldConfig; hideInTabs?: boolean; }[]; /** Output current state when parameters change. */ parametersChanged: EventEmitter<any>; /** Output current result of records by type. */ recordsSearched: EventEmitter<SearchResult>; /** Current aggregations filters applied */ aggregationsFilters: Array<AggregationsFilter>; /** Contain result row data */ hits: any; /** Facets retrieved from requested result */ aggregations: Array<Aggregation>; /** Aggregation keys to always hide (defined into the config) */ aggregationsToHide: Array<string>; /** Error message when something wrong happens during a search */ error: Error; /** Check if record can be added */ addStatus: ActionStatus; /** List of fields on which we can do a specific search. */ searchFields: Array<SearchField>; /** List of fields on which we can filter. */ searchFilters: Array<SearchFilter | SearchFilterSection>; /** If we need to show the empty search message info. */ showEmptySearchMessage: boolean; /** Export formats configuration. */ exportOptions: { label: string; url: string; disabled?: boolean; disabled_message?: string; }[]; /** JSON dumping of last search parameters (used for checking if we have to do a search or not).*/ protected _searchParameters: BehaviorSubject<SearchParams>; /** Define if search input have to be displayed or not. */ protected _showSearchInput: boolean; /** Define if title have to be displayed or not. */ protected _showLabel: boolean; /** Subscriptions to observables. */ protected _subscriptions: Subscription; /** Store configuration for type. */ protected config: any; availableTypes: any[]; loaded: boolean; /** Check if pagination have to be displayed or not. */ get showPagination(): boolean; /** Does it require to activate the first and last button on pagination. */ get paginationBoundaryLinks(): boolean; /** Get the number of pages showed on pagination. */ get paginationMaxSize(): number; /** Request result record hits. */ get records(): Array<any>; /** Total records number corresponding to the request. */ get total(): number; /** Get the text for displaying results text. */ get resultsText$(): Observable<string>; /** Get showSearchInput value, given either by config or by local value. */ get showSearchInput(): boolean; /** Setter for `showSearchInput`. */ set showSearchInput(showSearchInput: boolean); /** Get showLabel value, given either by config or by local value. */ get showLabel(): boolean; /** Setter for `showLabel`. */ set showLabel(showLabel: boolean); /** Return the current page (used in pagination, has we cannot use `page` property directly) */ get currentPage(): number; /** * Used only for binding with pagination. * Avoid side effect if "page" property is bound to pagination * (infinite calls to get records). * @param page - number, new page */ set currentPage(page: number); /** * Return the list of search fields that are selected. * @return List of selected search fields. */ get selectedSearchFields(): Array<SearchField>; /** * Get the message if there's no record for the type. * The message can be customized with the `noRecordMessage` property in route configuration. * @returns A message indicating there's no record. */ get emptyRecordMessage(): string; /** * Check if the current type has no record. * @returns True if no record is found and no search query is done. */ get hasNoRecord(): boolean; /** Return a message containing the reasons why record list cannot be exported. */ get exportInfoMessage(): string; get selectedSortValue(): MenuItem; /** * OnInit hook. * Subscribes to the observable emitting the aggregations filters. * Loads total count of records for each resource. */ ngOnInit(): void; /** * Methods called each time an input property is modified. * * If current type is changed, the configuration for the new type is loaded. * * The first time current type is changed, the types property is set in the * RecordUiServices. A better way to do that is to detect route change in the * service, but at this time it is done like that. * * This method does a search at the end but only if the search parameters have * changed. We want to avoid a double search when the following process is * done: * * 1. A parameter is changed in the search interface. * 2. Local property is updated. * 3. A search is done. * 4. The component outputs the search parameters. * 5. The parent component (RecordSearchPage) updates the URL * 6. The parent component detects route parameters changes. * 7. The parent component update the input value of the component. * 8. This method is triggered. * * @param changes Object containing all the changed properties. */ ngOnChanges(changes: SimpleChanges): void; /** * OnDestroy hook * Unsubscribes from the observable of the aggregations filters. */ ngOnDestroy(): void; /** * Internal notification that notify the search parameters has changed. * @param resetPage reset the pager to the first page */ protected _searchParamsHasChanged(resetPage?: boolean): void; /** Move to the top of the page if you change page */ pageChanged(): void; /** * Change number of items per page value. * @param event - Event, dom event triggered * @param size - number, new page size */ changeSize(event: Event, size: number): void; /** * Change sorting. * @param sort sort name. */ changeSorting(sort: string): void; /** * Change query text. * @param event - string, new query text */ searchByQuery(event: string): void; /** * Change type of records. * @param type - resource type name. */ changeType(type: string): void; /** * Delete a record by its PID. * @param event - object */ deleteRecord(event: { pid: string; type?: string; }): void; /** * Show a modal containing message given in parameter. * @param message - message to display into modal */ showDeleteMessage(message: string): void; /** * Get component view for the current resource type. * @return A component for displaying result item. */ getResultItemComponentView(): any; /** * Get Export formats for the current resource given by configuration. * @return Array of export format to generate an `export as` button or an empty array. */ protected _exportFormats(): Array<any>; /** * Get export format url. * @param format - export format object * @return formatted url for an export format. */ getExportFormatUrl(format: any): string; /** * Check if a record list can be exported * @param format - export format object * @return Boolean */ canExport(format: any): boolean; /** * Check if a record can be updated * @param record - object, record to check * @return Observable */ canUpdateRecord$(record: object): Observable<ActionStatus>; /** * Check if a record can be deleted * @param record - object, record to check * @return Observable */ canDeleteRecord$(record: object): Observable<ActionStatus>; /** * Check if a record can be used * @param record - object, record to check * @return Observable */ canUseRecord$(record: object): Observable<ActionStatus>; /** * Filter the aggregations with given configuration function. * If no configuration is given, return the original aggregations. * @param records - Result records * @return Observable containing aggregations corresponding to actual records. */ aggregations$(aggregations: object): Observable<any>; /** * Returns an observable which emits the URL value for given record. * In case record cannot be read, returns null. * @param record - Generate detail URL for this record. * @return Observable emitting detail URL object */ resolveDetailUrl$(record: any): Observable<any>; searchInField(event: ToggleSwitchChangeEvent, path: string): void; onChangeSearchFilter(event: IChecked): void; /** * Check if a filter is selected. * * @param filter SearchFilter * @returns true if the given filter is selected. */ isFilterActive(filter: SearchFilter): boolean; /** * Load buckets for the given aggregation. * @param event - Object outputted when an aggregation is clicked. */ loadAggregationBuckets(event: { key: string; expanded: boolean; }): void; /** * Search for records. * @param size - number : force the number of records to return. If `null` the default component `size` attribute will be used. */ protected _getRecords(size?: number): Observable<any>; /** * Emit new parameters when a search is done. */ protected _emitNewParameters(): void; /** * Get configuration for the current resource type. * @param type Type of resource */ protected _loadConfigurationForType(type: string): void; /** * Get Resource config * @param paramName - Name of parameter * @param defaultValue - Default value is returned if the parameter is not defined * @return A config value or the given default value instead */ protected _getResourceConfig(paramName: string, defaultValue: any): any; /** * Serialize all the search parameters with JSON.stringify method. * @return The serialized string. */ protected _serializeSearchParameters(): SearchParams; /** * Load search fields stored in configuration and assign to them the * `selected` property. * * @returns void */ protected _loadSearchFields(): void; /** * Build query string with possibly search fields. * * @return Final query string. */ protected _buildQueryString(): string; /** * Return the name of the index defined either by the index key or key. * * @return string, current index defined by keys index or key */ protected _currentIndex(): any; /** * Get personalized name for current aggregation * @param key - string, aggregation key * @return string or null */ protected _aggregationName(key: string): string | null; /** * Extract persistent search filters on current url * @return Array of aggregations filter */ protected _extractPersistentAggregationsFilters(): Array<AggregationsFilter>; /** * Make all search filters on same array level * @returns - A filters array */ protected _flatSearchFilters(): SearchFilter[]; /** * Map aggregation records data to corresponding aggregation. * * @param aggregation Aggregation object. * @param recordsAggregation Aggregation retrieved from record. */ protected _mapAggregation(aggregation: Aggregation, recordsAggregation: any): void; /** * Enrich the bucket with several properties: indeterminate, parent, aggregationKey. * * @param bucket elastic bucket to process * @param aggregationKey bucket parent key * @returns the indeterminate state of the bucket */ processBuckets(bucket: any, aggregationKey: any): boolean; /** * Compile facets keys to get only 'included' facets or having a filter selected. * @returns List of facets. */ protected _getFacetsParameter(): Array<string>; /** Set the default sort. */ protected _setDefaultSort(): void; /** * Show the filter's section * @param searchFilterSection - Collection of filter * @returns true if the filter's section is show */ showFilterSection(searchFilterSection: SearchFilterSection): boolean; /** * Show the filter * @param searchFilter - search Filter * @returns true if the filter is show */ showFilter(searchFilter: SearchFilter): boolean; paginatorChange(event: ChangeEvent): void; /** * Remove filter. * * @param filter - the filter to remove */ removeFilter(filter: any): void; static ɵfac: i0.ɵɵFactoryDeclaration<RecordSearchComponent, never>; static ɵcmp: i0.ɵɵComponentDeclaration<RecordSearchComponent, "ng-core-record-search", never, { "currentType": { "alias": "currentType"; "required": false; }; "q": { "alias": "q"; "required": false; }; "page": { "alias": "page"; "required": false; }; "size": { "alias": "size"; "required": false; }; "sort": { "alias": "sort"; "required": false; }; "adminMode": { "alias": "adminMode"; "required": false; }; "detailUrl": { "alias": "detailUrl"; "required": false; }; "types": { "alias": "types"; "required": false; }; "showSearchInput": { "alias": "showSearchInput"; "required": false; }; "showLabel": { "alias": "showLabel"; "required": false; }; }, { "parametersChanged": "parametersChanged"; "recordsSearched": "recordsSearched"; }, never, ["[header]", "[afterInputSearch]", "[beforeResult]", "[top-search-result]", "[top-facets]", "[top-result]", "[footer-result]", "*", "[footer-search-result]"], false, never>; }