index.component.ts 2.9 KB
Newer Older
X
fix: qs  
xjh22222228 已提交
1 2
import { Component } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
X
xjh22222228 已提交
3
import { GIT_REPO_URL } from '../../../../config'
X
xjh22222228 已提交
4
import { INavProps, INavThreeProp } from '../../../types'
X
xjh22222228 已提交
5 6 7 8 9 10 11
import {
  debounce,
  fuzzySearch,
  randomBgImg,
  onImgError,
  queryString,
  getWebsiteList,
X
xjh22222228 已提交
12 13
  setWebsiteList,
  toggleCollapseAll,
X
xjh22222228 已提交
14
  imgErrorInRemove
X
xjh22222228 已提交
15
} from '../../../utils'
X
xjh22222228 已提交
16
import { initRipple, setAnnotate } from '../../../utils/ripple'
X
Open  
xjh22222228 已提交
17 18 19 20 21 22

@Component({
  selector: 'app-home',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
X
xjh22222228 已提交
23
export default class HomeComponent {
X
Open  
xjh22222228 已提交
24 25

  constructor (private router: Router, private activatedRoute: ActivatedRoute) {}
X
xjh22222228 已提交
26

X
xjh22222228 已提交
27 28
  websiteList: INavProps[] = getWebsiteList()
  currentList: INavThreeProp[] = []
X
fix: qs  
xjh22222228 已提交
29 30 31 32 33
  id: number = 0
  page: number = 0
  searchKeyword: string = ''
  showInput = false
  GIT_REPO_URL: string = GIT_REPO_URL
X
Open  
xjh22222228 已提交
34

X
xjh22222228 已提交
35
  ngOnInit() {
X
fix: qs  
xjh22222228 已提交
36
    randomBgImg()
X
xjh22222228 已提交
37

X
Open  
xjh22222228 已提交
38
    const initList = () => {
X
xjh22222228 已提交
39
      this.currentList = this.websiteList[this.page].nav[this.id].nav
X
fix: qs  
xjh22222228 已提交
40 41 42 43 44 45 46 47 48 49
    }

    this.activatedRoute.queryParams.subscribe(() => {
      const tempPage = this.page
      const { id, page, q } = queryString()
      this.searchKeyword = q
      this.page = page
      this.id = id

      if (q) {
X
xjh22222228 已提交
50
        this.currentList = fuzzySearch(this.websiteList, q)
X
Open  
xjh22222228 已提交
51
      } else {
X
fix: qs  
xjh22222228 已提交
52
        initList()
X
xjh22222228 已提交
53
      }
X
xjh22222228 已提交
54 55

      if (tempPage !== page) {
X
xjh22222228 已提交
56
        setAnnotate()
X
xjh22222228 已提交
57
      }
X
xjh22222228 已提交
58 59

      setWebsiteList(this.websiteList)
X
fix: qs  
xjh22222228 已提交
60
    })
X
Open  
xjh22222228 已提交
61 62

    this.handleSearch = debounce(() => {
X
fix: qs  
xjh22222228 已提交
63 64 65
      if (!this.searchKeyword) {
        initList()
        return
X
Open  
xjh22222228 已提交
66 67
      }

X
fix: qs  
xjh22222228 已提交
68
      const params = queryString()
X
xjh22222228 已提交
69
      this.router.navigate(['/light'], {
X
xjh22222228 已提交
70 71
        queryParams: {
          ...params,
X
fix: qs  
xjh22222228 已提交
72
          q: this.searchKeyword
X
xjh22222228 已提交
73
        }
X
fix: qs  
xjh22222228 已提交
74
      })
X
xjh22222228 已提交
75 76
    }, 1000, true)
  }
X
xjh22222228 已提交
77

X
xjh22222228 已提交
78 79 80
  onSearch(v) {
    this.searchKeyword = v
    this.handleSearch()
X
Open  
xjh22222228 已提交
81 82
  }

X
xjh22222228 已提交
83 84 85 86 87 88 89 90
  handleOnClickSearch() {
    this.showInput = true
    setTimeout(() => {
      const searchEl = document.querySelector('.search') as HTMLInputElement
      if (searchEl) {
        searchEl.focus()
      }
    }, 0)
X
Open  
xjh22222228 已提交
91 92
  }

X
xjh22222228 已提交
93 94
  handleCilckTopNav(index) {
    const id = this.websiteList[index].id || 0
X
xjh22222228 已提交
95
    this.router.navigate(['/light'], {
X
Open  
xjh22222228 已提交
96 97
      queryParams: {
        page: index,
X
xjh22222228 已提交
98
        id,
X
Open  
xjh22222228 已提交
99 100
        _: Date.now()
      }
X
fix: qs  
xjh22222228 已提交
101
    })
X
Open  
xjh22222228 已提交
102 103
  }

X
xjh22222228 已提交
104
  handleSidebarNav(index) {
X
fix: qs  
xjh22222228 已提交
105
    const { page } = queryString()
X
xjh22222228 已提交
106
    this.websiteList[page].id = index
X
xjh22222228 已提交
107
    this.router.navigate(['/light'], { 
X
Open  
xjh22222228 已提交
108 109 110 111 112
      queryParams: {
        page,
        id: index,
        _: Date.now()
      }
X
fix: qs  
xjh22222228 已提交
113
    })
X
Open  
xjh22222228 已提交
114 115 116
  }

  ngAfterViewInit () {
X
xjh22222228 已提交
117 118
    setAnnotate();
    initRipple()
X
xjh22222228 已提交
119
  }
X
xjh22222228 已提交
120

X
xjh22222228 已提交
121 122 123 124 125 126
  onCollapse = (item, index) => {
    item.collapsed = !item.collapsed
    this.websiteList[this.page].nav[this.id].nav[index] = item
    setWebsiteList(this.websiteList)
  }

X
xjh22222228 已提交
127 128 129 130
  onCollapseAll = () => {
    toggleCollapseAll(this.websiteList)
  }

X
fix: qs  
xjh22222228 已提交
131
  handleSearch = null
X
xjh22222228 已提交
132
  onImgError = onImgError
X
xjh22222228 已提交
133
  onSideLogoError = imgErrorInRemove
X
Open  
xjh22222228 已提交
134
}