index.component.ts 4.5 KB
Newer Older
X
xjh22222228 已提交
1
import nav from '../../../../data';
X
Open  
xjh22222228 已提交
2 3
import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
X
xjh22222228 已提交
4 5
import { debounce } from '../../../utils';
import { appLanguage, GIT_REPO_URL } from '../../../../config';
X
xjh22222228 已提交
6 7 8
import { annotate } from 'rough-notation';

let equeue = [];
X
Open  
xjh22222228 已提交
9 10 11 12 13 14

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

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

19
  nav: any[] = nav;
X
Open  
xjh22222228 已提交
20 21 22 23 24 25
  id: number = 0;
  page: number = 0;
  list: any[] = [];
  search: string = '';
  showInput = false;
  searchLoading = false;
26
  language: string[] = appLanguage;
X
xjh22222228 已提交
27
  GIT_REPO_URL: string = GIT_REPO_URL;
X
Open  
xjh22222228 已提交
28 29

  ngOnInit () {
X
xjh22222228 已提交
30
    const that = this;
X
Open  
xjh22222228 已提交
31 32 33 34 35
    const initList = () => {
      this.list = this.nav[this.page].nav[this.id].nav;
    };

    this.activatedRoute.queryParams.subscribe(query => {
X
xjh22222228 已提交
36
      const tempPage = this.page;
X
Open  
xjh22222228 已提交
37 38
      const id = +query.id || 0;
      const page = +query.page || 0;
X
xjh22222228 已提交
39 40
      this.search = query.q || '';

X
Open  
xjh22222228 已提交
41 42 43 44 45 46 47 48 49 50 51
      if (page > this.nav.length - 1) {
        this.page = this.nav.length - 1;
        this.id = 0;
      } else {
        this.page = page;
        if (id <= this.nav[this.page].nav.length - 1) {
          this.id = id;
        } else {
          this.id = this.nav[this.page].nav.length - 1;
        }
      }
X
xjh22222228 已提交
52 53 54 55 56 57
      
      if (this.search) {
        this.list = fuzzySearch()();
      } else {
        initList();
      }
X
xjh22222228 已提交
58 59 60 61

      if (tempPage !== page) {
        this.setAnnotate();
      }
X
Open  
xjh22222228 已提交
62 63
    });

X
xjh22222228 已提交
64
    function fuzzySearch() {
X
Open  
xjh22222228 已提交
65
      let searchList = [{ nav: [] }];
X
xjh22222228 已提交
66
      that.searchLoading = false;
X
Open  
xjh22222228 已提交
67 68 69 70 71 72 73 74 75 76

      return function f(arr?: any[]) {
        arr = arr || that.nav;

        for (let i = 0; i < arr.length; i++) {

          if (Array.isArray(arr[i].nav)) {
            f(arr[i].nav);
          }

X
xjh22222228 已提交
77
          if (arr[i].name) {
X
Open  
xjh22222228 已提交
78 79 80 81 82 83 84 85 86
            const name = arr[i].name.toLocaleLowerCase();
            const desc = arr[i].desc.toLocaleLowerCase();
            const search = that.search.toLocaleLowerCase();
            if (~name.indexOf(search) || ~desc.indexOf(search)) {
              try {
                let result = Object.assign({}, arr[i]);
                const regex = new RegExp(`(${that.search})`, 'i');
                result.name = result.name.replace(regex, `$1`.bold())
                result.desc = result.desc.replace(regex, `$1`.bold())
X
xjh22222228 已提交
87 88 89 90 91

                const idx = searchList[0].nav.findIndex(item => item.name === result.name);
                if (idx === -1) {
                  searchList[0].nav.push(result);
                }
X
Open  
xjh22222228 已提交
92 93 94 95
              } catch (err) {}
            }
          }
        }
X
xjh22222228 已提交
96

X
Open  
xjh22222228 已提交
97 98 99 100 101 102 103 104 105 106 107
        return searchList;
      };
    }

    this.handleSearch = debounce(() => {
      if (!this.search) {
        initList();
        this.searchLoading = false;
        return;
      }

X
xjh22222228 已提交
108 109 110 111 112 113 114 115 116
      const hash = window.location.hash;
      const params = new URLSearchParams(hash.slice(hash.indexOf('?')));
      this.router.navigate(['/index'], {
        queryParams: {
          ...params,
          q: this.search
        }
      });

X
Open  
xjh22222228 已提交
117 118 119 120
      this.searchLoading = true;
    }, 1000, false);
  }

X
xjh22222228 已提交
121 122
  handleSearch = null

X
Open  
xjh22222228 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  handleScroll(e) {
    const target = e.target;
    const top = target.scrollTop;
    (<any>window).sessionStorage.top = top;
  }

  handleCilckNav(index) {
    this.router.navigate(['/index'], {
      queryParams: {
        page: index,
        _: Date.now()
      }
    });
  }

  handleOnClickSearch() {
    this.showInput = true;
    setTimeout(() => {
      try {
        (<any>document).querySelector('.search').focus();
      } catch (err) {}
    }, 0);
  }

  handleSidebarNav (index) {
    const page = +this.activatedRoute.snapshot.queryParams.page || 0;
    this.router.navigate(['/index'], { 
      queryParams: {
        page,
        id: index,
        _: Date.now()
      }
    });
  }

  ngAfterViewInit () {
X
xjh22222228 已提交
159 160
    this.setAnnotate();

X
Open  
xjh22222228 已提交
161 162 163 164 165 166 167 168 169
    (<any>window).$.ripple('.ripple-btn', {
      multi: true,
      debug: false,
      opacity: .2,
    });
    try {
      document.getElementById('main').scrollTop = +(<any>window).sessionStorage.top || 0;
    } catch (e) {}
  }
X
xjh22222228 已提交
170

X
xjh22222228 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  setAnnotate() {
    const elList = document.querySelectorAll('.top-nav .ripple-btn') || [];
    if (elList.length === 0) return;

    equeue.forEach(item => item.hide());
    equeue = [];

    const annotation = annotate(elList[this.page], {
      type: 'underline',
      color: 'red',
      padding: 3,
      strokeWidth: 3
    });
    equeue.push(annotation);
    annotation.show();
  }
X
Open  
xjh22222228 已提交
187
}