33 Licensed under the Apache License, Version 2.0 (the "License");
44 you may not use this file except in compliance with the License.
55 You may obtain a copy of the License at
6- http://www.apache.org/licenses/LICENSE-2.0
6+ http://www.apache.org/licenses/LICENSE-2.0
77 Unless required by applicable law or agreed to in writing, software
88 distributed under the License is distributed on an "AS IS" BASIS,
99 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,115 +20,115 @@ import { MatInputModule } from '@angular/material/input';
2020import { MatTooltipModule } from '@angular/material/tooltip' ;
2121import { MatFormFieldModule } from '@angular/material/form-field' ;
2222import {
23- FormControl ,
24- FormsModule ,
25- ReactiveFormsModule ,
26- Validators ,
23+ FormControl ,
24+ FormsModule ,
25+ ReactiveFormsModule ,
26+ Validators ,
2727} from '@angular/forms' ;
2828import { MatProgressSpinnerModule } from '@angular/material/progress-spinner' ;
29- import { Book , FunctionResponse , FunctionSearch , JsonResult } from '../model/functions' ;
29+ import { Book , FunctionResponse , FunctionSearch , JsonBook , JsonResult } from '../model/functions' ;
3030import { FunctionSearchService } from '../service/function-search.service' ;
3131import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
3232import { Subscription , interval , map , tap } from 'rxjs' ;
3333import { NestedTreeControl } from '@angular/cdk/tree' ;
3434import { MatIconModule } from '@angular/material/icon' ;
35- import { MatRadioModule } from '@angular/material/radio' ;
35+ import { MatRadioModule } from '@angular/material/radio' ;
3636
3737interface TreeNode {
38- value1 : string ;
39- value2 : string ;
40- children ?: TreeNode [ ] ;
38+ value1 : string ;
39+ value2 : string ;
40+ children ?: TreeNode [ ] ;
4141}
4242
4343@Component ( {
44- selector : 'app-function-search' ,
45- imports : [
46- CommonModule ,
47- MatToolbarModule ,
48- MatButtonModule ,
49- MatInputModule ,
50- MatTooltipModule ,
51- MatTreeModule ,
52- MatIconModule ,
44+ selector : 'app-function-search' ,
45+ imports : [
46+ CommonModule ,
47+ MatToolbarModule ,
48+ MatButtonModule ,
49+ MatInputModule ,
50+ MatTooltipModule ,
51+ MatTreeModule ,
52+ MatIconModule ,
5353 MatRadioModule ,
54- MatFormFieldModule ,
55- FormsModule ,
56- ReactiveFormsModule ,
57- MatProgressSpinnerModule ,
58- ] ,
59- templateUrl : './function-search.component.html' ,
60- styleUrl : './function-search.component.scss'
54+ MatFormFieldModule ,
55+ FormsModule ,
56+ ReactiveFormsModule ,
57+ MatProgressSpinnerModule ,
58+ ] ,
59+ templateUrl : './function-search.component.html' ,
60+ styleUrl : './function-search.component.scss'
6161} )
6262export class FunctionSearchComponent {
63- private repeatSub : Subscription | null = null ;
64- protected searchValueControl = new FormControl ( '' , [
65- Validators . required ,
66- Validators . minLength ( 3 ) ,
67- ] ) ;
68- protected searching = false ;
69- protected msWorking = 0 ;
70- protected treeControl = new NestedTreeControl < TreeNode > (
71- ( node ) => node . children
72- ) ;
73- protected dataSource = new MatTreeNestedDataSource < TreeNode > ( ) ;
74- protected responseText = '' ;
75- protected responseJson = { value1 : "" , value2 : "" } as TreeNode ;
76- protected resultFormats = [ 'text' , 'json' ] ;
77- protected resultFormatControl = new FormControl ( this . resultFormats [ 0 ] ) ;
63+ private repeatSub : Subscription | null = null ;
64+ protected searchValueControl = new FormControl ( '' , [
65+ Validators . required ,
66+ Validators . minLength ( 3 ) ,
67+ ] ) ;
68+ protected searching = false ;
69+ protected msWorking = 0 ;
70+ protected treeControl = new NestedTreeControl < TreeNode > (
71+ ( node ) => node . children
72+ ) ;
73+ protected dataSource = new MatTreeNestedDataSource < TreeNode > ( ) ;
74+ protected responseText = '' ;
75+ protected responseJson = [ { value1 : "" , value2 : "" } as TreeNode ] ;
76+ protected resultFormats = [ 'text' , 'json' ] ;
77+ protected resultFormatControl = new FormControl ( this . resultFormats [ 0 ] ) ;
7878
79- constructor (
80- private router : Router ,
81- private destroyRef : DestroyRef ,
82- private functionSearchService : FunctionSearchService
83- ) { }
79+ constructor (
80+ private router : Router ,
81+ private destroyRef : DestroyRef ,
82+ private functionSearchService : FunctionSearchService
83+ ) { }
8484
85- protected hasChild = ( _ : number , node : TreeNode ) =>
86- ! ! node . children && node . children . length > 0 ;
85+ protected hasChild = ( _ : number , node : TreeNode ) =>
86+ ! ! node . children && node . children . length > 0 ;
8787
88- protected showList ( ) : void {
89- this . router . navigate ( [ '/doclist' ] ) ;
90- }
88+ protected showList ( ) : void {
89+ this . router . navigate ( [ '/doclist' ] ) ;
90+ }
9191
92- protected search ( ) : void {
93- this . searching = true ;
94- this . dataSource . data = [ ] ;
95- const startDate = new Date ( ) ;
96- this . repeatSub ?. unsubscribe ( ) ;
97- this . repeatSub = interval ( 100 )
98- . pipe (
99- map ( ( ) => new Date ( ) ) ,
100- takeUntilDestroyed ( this . destroyRef )
101- )
102- . subscribe (
103- ( newDate ) => ( this . msWorking = newDate . getTime ( ) - startDate . getTime ( ) )
104- ) ;
105- this . functionSearchService
106- . postLibraryFunction ( {
107- question : this . searchValueControl . value ,
108- resultFormat : this . resultFormatControl . value
109- } as FunctionSearch )
110- . pipe (
111- tap ( ( ) => this . repeatSub ?. unsubscribe ( ) ) ,
112- takeUntilDestroyed ( this . destroyRef ) ,
113- tap ( ( ) => ( this . searching = false ) )
114- )
115- . subscribe ( value => this . resultFormatControl . value === this . resultFormats [ 0 ] ?
116- this . responseText = value . result || '' : this . responseJson = this . addToDataSource ( this . mapResult ( value . jsonResult || { author : "" , books : [ ] } as JsonResult ) )
117- ) ;
118- }
92+ protected search ( ) : void {
93+ this . searching = true ;
94+ this . dataSource . data = [ ] ;
95+ const startDate = new Date ( ) ;
96+ this . repeatSub ?. unsubscribe ( ) ;
97+ this . repeatSub = interval ( 100 )
98+ . pipe (
99+ map ( ( ) => new Date ( ) ) ,
100+ takeUntilDestroyed ( this . destroyRef )
101+ )
102+ . subscribe (
103+ ( newDate ) => ( this . msWorking = newDate . getTime ( ) - startDate . getTime ( ) )
104+ ) ;
105+ this . functionSearchService
106+ . postLibraryFunction ( {
107+ question : this . searchValueControl . value ,
108+ resultFormat : this . resultFormatControl . value
109+ } as FunctionSearch )
110+ . pipe (
111+ tap ( ( ) => this . repeatSub ?. unsubscribe ( ) ) ,
112+ takeUntilDestroyed ( this . destroyRef ) ,
113+ tap ( ( ) => ( this . searching = false ) )
114+ )
115+ . subscribe ( value => this . resultFormatControl . value === this . resultFormats [ 0 ] ?
116+ this . responseText = value . result || '' : this . responseJson = this . addToDataSource ( this . mapResult ( value . jsonResult || [ { author : "" , books : [ ] } ] as JsonResult [ ] ) )
117+ ) ;
118+ }
119119
120- private addToDataSource ( treeNode : TreeNode ) : TreeNode {
121- this . dataSource . data = [ treeNode ] ;
122- return treeNode ;
123- }
124-
125- private mapResult ( jsonResult : JsonResult ) : TreeNode {
126- const children = jsonResult ?. books . map ( value => ( { value1 : value . title , value2 : value . summary } as TreeNode ) ) ;
127- const rootNode = { value1 : jsonResult . author , value2 : "" , children : children } as TreeNode ;
128- return rootNode ;
129- }
120+ private addToDataSource ( treeNodes : TreeNode [ ] ) : TreeNode [ ] {
121+ this . dataSource . data = treeNodes ;
122+ return treeNodes ;
123+ }
130124
131- protected logout ( ) : void {
132- console . log ( 'logout' ) ;
133- }
125+ private mapResult ( jsonResults : JsonResult [ ] ) : TreeNode [ ] {
126+ const createChildren = ( books : JsonBook [ ] ) => books . map ( value => ( { value1 : value . title , value2 : value . summary } as TreeNode ) ) ;
127+ const rootNode = jsonResults . map ( myValue => ( { value1 : myValue . author , value2 : "" , children : createChildren ( myValue . books ) } as TreeNode ) ) ;
128+ return rootNode ;
129+ }
130+
131+ protected logout ( ) : void {
132+ console . log ( 'logout' ) ;
133+ }
134134}
0 commit comments