Search fixes

This commit is contained in:
Roman Hotsiy 2017-02-02 22:03:47 +02:00
parent 82575d6a77
commit 555767feb5
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 42 additions and 11 deletions

View File

@ -2,7 +2,7 @@
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">
<path d="M968.2,849.4L667.3,549c83.9-136.5,66.7-317.4-51.7-435.6C477.1-25,252.5-25,113.9,113.4c-138.5,138.3-138.5,362.6,0,501C219.2,730.1,413.2,743,547.6,666.5l301.9,301.4c43.6,43.6,76.9,14.9,104.2-12.4C981,928.3,1011.8,893,968.2,849.4z M524.5,522c-88.9,88.7-233,88.7-321.8,0c-88.9-88.7-88.9-232.6,0-321.3c88.9-88.7,233-88.7,321.8,0C613.4,289.4,613.4,433.3,524.5,522z"/>
</svg>
<input #search (keyup)="update(search.value)" placeholder="Search">
<input #search (keyup)="update($event, search.value)" [value]="searchTerm" placeholder="Search">
</div>
<ul class="search-results" [hidden]="!items.length">
<li class="result" *ngFor="let item of items"

View File

@ -1,6 +1,7 @@
'use strict';
import { Component, ChangeDetectionStrategy, ChangeDetectorRef, OnInit, HostBinding } from '@angular/core';
import { Marker, SearchService, MenuService, MenuItem } from '../../services/';
import { throttle } from '../../utils/';
@Component({
selector: 'redoc-search',
@ -11,6 +12,8 @@ import { Marker, SearchService, MenuService, MenuItem } from '../../services/';
export class RedocSearch implements OnInit {
logo:any = {};
items: { menuItem: MenuItem, pointers: string[] }[] = [];
searchTerm = '';
throttledSearch: Function;
_subscription;
@ -23,14 +26,36 @@ export class RedocSearch implements OnInit {
cdr.markForCheck();
cdr.detectChanges();
});
this.throttledSearch = throttle(() => {
this.updateSearch();
cdr.markForCheck();
cdr.detectChanges();
}, 300, this);
}
init() {
this.search.indexAll();
}
update(val) {
let searchRes = this.search.search(val);
update(event:KeyboardEvent, val) {
if (event && event.keyCode === 27) { // escape
this.searchTerm = '';
} else {
this.searchTerm = val;
}
this.throttledSearch();
}
updateSearch() {
if (!this.searchTerm || this.searchTerm.length < 2) {
this.items = [];
this.marker.unmark();
return;
}
let searchRes = this.search.search(this.searchTerm);
this.items = Object.keys(searchRes).map(id => ({
menuItem: this.menu.getItemById(id),
pointers: searchRes[id].map(el => el.pointer)
@ -41,7 +66,7 @@ export class RedocSearch implements OnInit {
else if (a.menuItem.depth < b.menuItem.depth) return -1;
else return 0;
});
this.marker.mark(val);
this.marker.mark(this.searchTerm);
}
clickSearch(item) {

View File

@ -199,7 +199,6 @@ class SchemaDereferencer {
dereference(schema: Reference, pointer:string):any {
if (!schema || !schema.$ref) return schema;
window['derefCount'] = window['derefCount'] ? window['derefCount'] + 1 : 1;
let $ref = schema.$ref;
let resolved = this._spec.byPointer($ref);
if (!this._refCouner.visited($ref)) {

View File

@ -200,12 +200,19 @@ export class SearchService {
}
}
this.index({
pointer: absolutePointer,
menuId: menuPointer,
title,
body
});
if (schema.type === 'string' && schema.enum) {
body += ' ' + schema.enum.join(' ');
}
if (!parent) {
// redoc doesn't display top level descriptions and titles
this.index({
pointer: absolutePointer,
menuId: menuPointer,
title,
body
});
}
if (schema.properties) {
Object.keys(schema.properties).forEach(propName => {