This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
refinement-list.ts
138 lines (124 loc) · 3.63 KB
/
refinement-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { Component, Input, Inject, forwardRef } from "@angular/core";
import { connectRefinementList } from "instantsearch.js/es/connectors";
import { BaseWidget } from "../base-widget";
import { NgAisInstantSearch } from "../instantsearch/instantsearch";
import { parseNumberInput, noop } from "../utils";
export type RefinementListState = {
canRefine: boolean;
canToggleShowMore: boolean;
createURL: Function;
isShowingMore: boolean;
items: {}[];
refine: Function;
toggleShowMore: Function;
searchForItems: Function;
isFormSearch: boolean;
};
@Component({
selector: "ais-refinement-list",
template: `
<div
[class]="cx()"
*ngIf="!isHidden"
>
<div
*ngIf="searchable"
[class]="cx('searchBox')"
>
<ais-facets-search
[search]="state.searchForItems"
[searchPlaceholder]="searchPlaceholder"
>
</ais-facets-search>
</div>
<ul [class]="cx('list')">
<li
[class]="getItemClass(item)"
*ngFor="let item of items"
(click)="refine($event, item)"
>
<label [class]="cx('label')">
<input
[class]="cx('checkbox')"
type="checkbox"
value="{{item.value}}"
[checked]="item.isRefined"
/>
<span [class]="cx('labelText')">
<ais-highlight attribute="highlighted" [hit]="item"></ais-highlight>
</span>
<span [class]="cx('count')">{{item.count}}</span>
</label>
</li>
</ul>
<button
*ngIf="showMoreLimit && state.canToggleShowMore"
(click)="state.toggleShowMore()"
>
{{state.isShowingMore ? showLessLabel : showMoreLabel}}
</button>
</div>
`
})
export class NgAisRefinementList extends BaseWidget {
// render options
@Input() public showMoreLabel: string = "Show more";
@Input() public showLessLabel: string = "Show less";
@Input() public transformItems?: Function;
@Input() public searchable?: boolean;
@Input() public searchPlaceholder: string = "Search here...";
// connectors options
@Input() public attribute: string;
@Input() public operator: "or" | "and" = "or";
@Input() public limit: number | string = 10;
@Input() public showMoreLimit: number | string;
@Input() public sortBy: string[] | ((item: object) => number);
public state: RefinementListState = {
canRefine: false,
canToggleShowMore: false,
createURL: noop,
isShowingMore: false,
items: [],
refine: noop,
toggleShowMore: noop,
searchForItems: noop,
isFormSearch: false
};
get isHidden() {
return this.state.items.length === 0 && this.autoHideContainer;
}
constructor(
@Inject(forwardRef(() => NgAisInstantSearch))
public instantSearchParent: any
) {
super("RefinementList");
}
get items() {
return typeof this.transformItems === "function"
? this.transformItems(this.state.items)
: this.state.items;
}
public ngOnInit() {
this.createWidget(connectRefinementList, {
limit: parseNumberInput(this.limit),
showMoreLimit: parseNumberInput(this.showMoreLimit),
attributeName: this.attribute,
sortBy: this.sortBy,
escapeFacetValues: true
});
super.ngOnInit();
}
public refine(
event: MouseEvent,
item: { isRefined: boolean; value: string }
) {
event.preventDefault();
event.stopPropagation();
if (this.state.canRefine) {
// update UI directly, it will update the checkbox state
item.isRefined = !item.isRefined;
// refine through Algolia API
this.state.refine(item.value);
}
}
}