|
1 | | -import { Component, OnInit } from '@angular/core'; |
2 | | - |
3 | | -import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/forms'; |
4 | | -import {ErrorStateMatcher} from '@angular/material/core'; |
5 | | - |
6 | | -import { FetchService } from '../services/fetch.service'; |
7 | | -import { MessageService } from '../../services/message.service'; |
8 | | - |
9 | | -import { |
10 | | - ICloudBook, |
11 | | - IMessage |
12 | | -} from '../../vendor'; |
13 | | - |
14 | | -/** Error when invalid control is dirty, touched, or submitted. */ |
15 | | -export class MyErrorStateMatcher implements ErrorStateMatcher { |
16 | | - isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { |
17 | | - const isSubmitted = form && form.submitted; |
18 | | - return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted)); |
19 | | - } |
20 | | -} |
| 1 | +import { Component, OnInit, Input } from '@angular/core'; |
21 | 2 |
|
| 3 | +import { ICloudBook } from '../../vendor'; |
22 | 4 | @Component({ |
23 | 5 | selector: 'app-books-cloud-search', |
24 | 6 | templateUrl: './search.component.html', |
25 | 7 | styleUrls: ['./search.component.scss'], |
26 | 8 | }) |
27 | 9 | export class SearchComponent implements OnInit { |
28 | | - keywordsFormControl = new FormControl('', [ |
29 | | - Validators.required |
30 | | - ]); |
31 | | - |
32 | | - bookList: Array<ICloudBook> = []; |
33 | | - |
34 | | - platforms = [{ |
35 | | - name: 'github.com', |
36 | | - icon: 'assets/images/github-favicon.svg', |
37 | | - },{ |
38 | | - name: 'gitee.com', |
39 | | - icon: 'assets/images/logo_gitee_g_red.svg', |
40 | | - },{ |
41 | | - name: 'gitlab.com', |
42 | | - icon: 'assets/images/gitlab-seeklogo.com.svg', |
43 | | - }]; |
44 | | - |
45 | | - matcher = new MyErrorStateMatcher(); |
46 | | - keywords: string = ''; |
47 | | - platformSelected: string = 'github.com'; |
48 | | - bookListCount: number; |
49 | | - |
50 | | - constructor( |
51 | | - private fetchService: FetchService, |
52 | | - private message: MessageService |
53 | | - ) { } |
54 | | - |
55 | | - ngOnInit() { |
56 | | - this.message.getMessage().subscribe(async (msg: IMessage) => { |
57 | | - if(msg.event === 'scrolled-to-end'){ |
58 | | - if(this.bookList.length%20 === 0 && !(/gitlab/.test(this.platformSelected))){ |
59 | | - this.search(this.bookList.length/20 + 1); |
60 | | - } |
61 | | - } |
62 | | - }); |
63 | | - } |
64 | | - |
65 | | - clearKeywords = () => { |
66 | | - this.keywords = ''; |
67 | | - } |
68 | | - /* |
69 | | - * |
70 | | - * @page, 传入 1 表示首次搜索,需要清空原有 bookList |
71 | | - */ |
72 | | - search = async (page: number) => { |
73 | | - const res = await this.fetchService.searchBooks(this.platformSelected, this.keywords, page) as object[] | object; |
74 | | - |
75 | | - let _bookList: object[] |
76 | | - if(/github/.test(this.platformSelected)){ |
77 | | - _bookList = res['items'].slice(); |
78 | | - this.bookListCount = res['total_count']; |
79 | | - } else { |
80 | | - _bookList = (res as object[]).slice(); |
81 | | - } |
82 | | - |
83 | | - if(page === 1) this.bookList = [].slice(); |
84 | | - _bookList.map((bookRaw: object) => { |
85 | | - const book: ICloudBook = { |
86 | | - fullName: '', |
87 | | - url: '', |
88 | | - desc: '', |
89 | | - writerName: '', |
90 | | - writerAvatarUrl: '', |
91 | | - dateUpdated: new Date(), |
92 | | - stars: 0 |
93 | | - }; |
94 | | - |
95 | | - book.desc = bookRaw['description'] ? bookRaw['description'] : ''; |
| 10 | + @Input() bookList: Array<ICloudBook>; |
| 11 | + @Input() searching: boolean; |
96 | 12 |
|
97 | | - if(/gitlab/.test(this.platformSelected)){ |
98 | | - book.dateUpdated = bookRaw['last_activity_at']; |
99 | | - book.writerName = bookRaw['namespace']['name']; |
100 | | - book.writerAvatarUrl = bookRaw['namespace']['avatar_url']; |
101 | | - book.url = bookRaw['http_url_to_repo']; |
102 | | - book.stars = bookRaw['star_count']; |
103 | | - book.fullName = bookRaw['path_with_namespace']; |
104 | | - } |
105 | | - if(!(/gitlab/.test(this.platformSelected))){ |
106 | | - book.dateUpdated = bookRaw['updated_at']; |
107 | | - book.writerName = /github/.test(this.platformSelected) ? bookRaw['owner']['login'] : bookRaw['owner']['name']; |
108 | | - book.writerAvatarUrl = bookRaw['owner']['avatar_url']; |
109 | | - book.url = /github/.test(this.platformSelected) ? bookRaw['clone_url'] : bookRaw['html_url']; |
110 | | - book.stars = bookRaw['stargazers_count']; |
111 | | - book.fullName = bookRaw['full_name']; |
112 | | - } |
| 13 | + constructor() {} |
113 | 14 |
|
114 | | - this.bookList.push(book); |
115 | | - }); |
116 | | - } |
| 15 | + ngOnInit() {} |
117 | 16 | } |
0 commit comments