-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathdropdown.js
77 lines (67 loc) · 1.97 KB
/
dropdown.js
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
/* eslint-disable ember/no-classic-components, ember/no-get, ember/require-tagless-components, prettier/prettier */
import {
classNames,
attributeBindings,
tagName,
} from '@ember-decorators/component';
import { get } from '@ember/object';
import Component from '@ember/component';
import { A } from '@ember/array';
@tagName('span')
@classNames('ds-dropdown-menu', 'ds-with-1')
@attributeBindings('role')
export default class Dropdown extends Component {
// Public API
role = 'listbox';
isVisible = false;
// show
// Massage data to make it easier for displaying on the template
// Returned object:
/**
* {
* lvl0Key: {
* lvl1Key: algoliaHit
* }
* }
*/
get _groupedResults() {
let results = get(this, 'results');
if (!results.length) {
return {};
}
const lvl0Group = results.reduce((previous, current) => {
// Remap all lowercase usages of 'guides' to 'Guides'
let lvl0 = get(current, 'hierarchy.lvl0');
// If lvl0 doesn't exist in the resulting object, create the array
if (!previous[lvl0]) {
previous[lvl0] = A();
}
// Insert the current item into the resulting object.
previous[lvl0].addObject(current);
return previous;
}, {});
/*
lvl0Group = {
lvl0key: algoliaHit
}
*/
// Iterate over every lvl0 group, group by lvl1
return Object.keys(lvl0Group).reduce((lvl0Result, lvl0Key) => {
// Inject lvl1 grouped results into lvl0
lvl0Result[lvl0Key] = lvl0Group[lvl0Key].reduce(
(lvl1Result, lvl1Item) => {
// lvl1 is sometimes null. Normalise to a string.
const lvl1Value = get(lvl1Item, 'hierarchy.lvl1');
const lvl1Key = lvl1Value ? lvl1Value : lvl0Key;
if (!lvl1Result[lvl1Key]) {
lvl1Result[lvl1Key] = A();
}
lvl1Result[lvl1Key].addObject(lvl1Item);
return lvl1Result;
},
{}
);
return lvl0Result;
}, {});
}
}