@@ -13,68 +13,140 @@ const searchClient = algoliasearch(appId, apiKey);
13
13
insightsClient ( 'init' , { appId, apiKey } ) ;
14
14
const algoliaInsightsPlugin = createAlgoliaInsightsPlugin ( { insightsClient } ) ;
15
15
16
+ // define locations to separate invocation for mobile and desktop
17
+ const locations = [ '#autocomplete' , '#autocomplete-mobile' ] ;
16
18
17
- const search = autocomplete ( {
18
- container : '#autocomplete' ,
19
- placeholder : 'Search the Segment documentation' ,
20
- debug : false ,
21
- openOnFocus : false ,
22
- keyboardShortcuts : [ 's' , 191 ] ,
23
- plugins : [ algoliaInsightsPlugin , ] ,
24
- detachedMediaQuery :'none' ,
25
- getSources ( { query} ) {
26
- return [
27
- {
28
- sourceId : 'articles' ,
29
- getItemUrl ( { item } ) {
30
- if ( item . anchor != null ) {
31
- var itemUrl = item . url + "#" + item . anchor ;
32
- } else {
33
- var itemUrl = item . url ;
34
- }
35
- return itemUrl ;
36
- } ,
37
- getItems ( ) {
38
- return getAlgoliaHits ( {
39
- searchClient,
40
- queries : [
41
- {
42
- indexName : 'segment-docs' ,
43
- query,
44
- params : {
45
- hitsPerPage : 7 ,
46
- facetFilters : [ 'hidden:-true' ] ,
47
- clickAnalytics : true ,
48
- } ,
49
- } ,
50
- ] ,
51
- } ) ;
52
- } ,
53
- templates : {
54
- item ( { item } ) {
19
+ function initAutocomplete ( item ) {
20
+ const search = autocomplete ( {
21
+ container : item ,
22
+ placeholder : 'Search the Segment documentation' ,
23
+ debug : false ,
24
+ openOnFocus : false ,
25
+ keyboardShortcuts : [ 's' , 191 ] ,
26
+ plugins : [ algoliaInsightsPlugin , ] ,
27
+ detachedMediaQuery :'none' ,
28
+ getSources ( { query} ) {
29
+ return [
30
+ {
31
+ sourceId : 'articles' ,
32
+ getItemUrl ( { item } ) {
55
33
if ( item . anchor != null ) {
56
- var anchorLink = "#" + item . anchor ;
34
+ var itemUrl = item . url + "#" + item . anchor ;
57
35
} else {
58
- var anchorLink = "" ;
36
+ var itemUrl = item . url ;
37
+ }
38
+ return itemUrl ;
39
+ } ,
40
+ getItems ( ) {
41
+ return getAlgoliaHits ( {
42
+ searchClient,
43
+ queries : [
44
+ {
45
+ indexName : 'segment-docs' ,
46
+ query,
47
+ params : {
48
+ hitsPerPage : 7 ,
49
+ facetFilters : [ 'hidden:-true' ] ,
50
+ clickAnalytics : true ,
51
+ } ,
52
+ } ,
53
+ ] ,
54
+ } ) ;
55
+ } ,
56
+ templates : {
57
+ item ( { item } ) {
58
+ if ( item . anchor != null ) {
59
+ var anchorLink = "#" + item . anchor ;
60
+ } else {
61
+ var anchorLink = "" ;
62
+ }
63
+ return html `< a class ="aa-link " href ="/docs ${ item . url } ${ anchorLink } ">
64
+ < p class ="aa-title " > ${ highlightHit ( { hit : item , attribute : 'title' } ) } </ h3 >
65
+ < p class ="aa-heading "> ${ item . headings . join ( ' >' ) } </ p >
66
+ < p class ="aa-content "> ${ highlightHit ( { hit : item , attribute : 'content' } ) } </ p > </ a >
67
+ ` ;
68
+ } ,
69
+ noResults ( ) {
70
+ return html `< p class ="aa-content "> No results for < strong > ${ query } </ strong > </ p > ` ;
59
71
}
60
- return html `< a class ="aa-link " href ="/docs ${ item . url } ${ anchorLink } ">
61
- < p class ="aa-title " > ${ highlightHit ( { hit : item , attribute : 'title' } ) } </ h3 >
62
- < p class ="aa-heading "> ${ item . headings . join ( ' >' ) } </ p >
63
- < p class ="aa-content "> ${ highlightHit ( { hit : item , attribute : 'content' } ) } </ p > </ a >
64
- ` ;
65
72
} ,
66
- noResults ( ) {
67
- return html `< p class ="aa-content "> No results for < strong > ${ query } </ strong > </ p > ` ;
68
- }
73
+
69
74
} ,
70
-
71
- } ,
72
- ] ;
73
- } ,
74
- navigator : {
75
- navigate ( { itemUrl } ) {
76
- window . location . assign ( '/docs' + itemUrl ) ;
75
+ ] ;
77
76
} ,
78
- }
79
- } ) ;
77
+ navigator : {
78
+ navigate ( { itemUrl } ) {
79
+ window . location . assign ( '/docs' + itemUrl ) ;
80
+ } ,
81
+ }
82
+ } ) ;
83
+
84
+ }
85
+
86
+ locations . forEach ( initAutocomplete ) ;
87
+
88
+
89
+ // const search_mobile = autocomplete({
90
+ // container: '#autocomplete-mobile',
91
+ // placeholder: 'Search the Segment documentation',
92
+ // debug: false,
93
+ // openOnFocus: false,
94
+ // keyboardShortcuts: ['s', 191],
95
+ // plugins: [algoliaInsightsPlugin,],
96
+ // detachedMediaQuery:'none',
97
+ // getSources( {query} ) {
98
+ // return [
99
+ // {
100
+ // sourceId: 'articles',
101
+ // getItemUrl({ item }){
102
+ // if (item.anchor != null) {
103
+ // var itemUrl = item.url+"#" + item.anchor;
104
+ // } else {
105
+ // var itemUrl = item.url;
106
+ // }
107
+ // return itemUrl;
108
+ // },
109
+ // getItems() {
110
+ // return getAlgoliaHits({
111
+ // searchClient,
112
+ // queries: [
113
+ // {
114
+ // indexName: 'segment-docs',
115
+ // query,
116
+ // params: {
117
+ // hitsPerPage: 7,
118
+ // facetFilters: ['hidden:-true'],
119
+ // clickAnalytics: true,
120
+ // },
121
+ // },
122
+ // ],
123
+ // });
124
+ // },
125
+ // templates: {
126
+ // item({ item }){
127
+ // if (item.anchor != null) {
128
+ // var anchorLink = "#" + item.anchor;
129
+ // } else {
130
+ // var anchorLink = "";
131
+ // }
132
+ // return html `<a class="aa-link" href="/docs${item.url}${anchorLink}">
133
+ // <p class="aa-title" >${highlightHit({hit: item, attribute: 'title'})}</h3>
134
+ // <p class="aa-heading">${item.headings.join(' >')}</p>
135
+ // <p class="aa-content">${highlightHit({hit: item, attribute: 'content'})}</p></a>
136
+ // `;
137
+ // },
138
+ // noResults() {
139
+ // return html `<p class="aa-content">No results for <strong>${query}</strong></p>`;
140
+ // }
141
+ // },
142
+
143
+ // },
144
+ // ];
145
+ // },
146
+ // navigator: {
147
+ // navigate({ itemUrl }) {
148
+ // window.location.assign('/docs'+itemUrl);
149
+ // },
150
+ // }
151
+ // });
80
152
0 commit comments