-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add search form with portal_types vocabulary field on demo site
- Loading branch information
1 parent
02bc6f4
commit 4adf0eb
Showing
11 changed files
with
180 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export interface Term<T extends number | string> { | ||
'@id': string; | ||
token: T; | ||
title: string; | ||
} | ||
|
||
export class Vocabulary<T extends number | string> implements Iterable<Term<T>> { | ||
protected _terms: Term<T>[]; | ||
protected _byToken: any = {}; // can't declare indexer with generics | ||
|
||
constructor(terms: Term<T>[]) { | ||
this._terms = terms; | ||
for (const term of terms) { | ||
this._byToken[term.token] = term; | ||
} | ||
} | ||
|
||
public terms() { | ||
return this._terms; | ||
} | ||
|
||
public byToken(token: T): Term<T> { | ||
const term = this._byToken[token]; | ||
if (term === undefined) { | ||
throw Error(`${token} token doesn't exist in vocabulary`); | ||
} else { | ||
return term; | ||
} | ||
} | ||
|
||
[Symbol.iterator]() { | ||
let counter = 0; | ||
const terms = this._terms; | ||
|
||
return <Iterator<Term<T>>>{ | ||
|
||
next(): IteratorResult<Term<T>> { | ||
if (counter < terms.length) { | ||
return { done: false, value: terms[counter++] }; | ||
} else { | ||
return { done: true, value: undefined } as any as IteratorResult<Term<T>>; | ||
} | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<form #searchForm="ngForm" (ngSubmit)="search(searchForm)"> | ||
<select ngModel name="type"> | ||
<option value="">All types</option> | ||
<option *ngFor="let typeTerm of typesVocabulary" [value]="typeTerm.token"> | ||
{{ typeTerm.title }} | ||
</option> | ||
</select> | ||
<input ngModel type="text" name="text" /> | ||
<input type="submit" value="Search" /> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Services, Vocabulary } from '@plone/restapi-angular'; | ||
|
||
@Component({ | ||
selector: 'app-search', | ||
templateUrl: './search.html', | ||
}) | ||
export class Search implements OnInit { | ||
|
||
typesVocabulary: Vocabulary<string>; | ||
|
||
constructor(protected services: Services) { | ||
} | ||
|
||
ngOnInit() { | ||
|
||
this.services.resource.vocabulary('plone.app.vocabularies.ReallyUserFriendlyTypes') | ||
.subscribe((typesVocabulary: Vocabulary<string>) => { | ||
this.typesVocabulary = typesVocabulary; | ||
}); | ||
} | ||
|
||
search(form) { | ||
let searchString = `/@@search?SearchableText=${form.value.text}`; | ||
if (form.value.type) { | ||
searchString += `&portal_type=${form.value.type}`; | ||
} | ||
this.services.traverser.traverse(searchString); | ||
} | ||
|
||
} |