1
1
requirejs ( [ 'algoliaAdminBundle' ] , function ( algoliaBundle ) {
2
2
algoliaBundle . $ ( function ( $ ) {
3
3
handleLatestVersion ( $ ) ;
4
-
4
+
5
5
if ( $ ( '#search_box' ) . length > 0 ) {
6
6
initDocSearch ( ) ;
7
7
initDiscourseSearch ( ) ;
8
8
}
9
-
9
+
10
10
if ( $ ( '.algolia_contact_form #subject' ) . length > 0 ) {
11
11
initContactDocSearch ( ) ;
12
12
}
@@ -25,18 +25,12 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
25
25
hitsPerPage : 10
26
26
} ,
27
27
searchFunction : function ( helper ) {
28
- if ( helper . state . query !== '' ) {
29
- algoliaEventsTracking . postEvent ( 'Performed Search' , {
30
- source : 'magento2.help.search' ,
31
- search : helper . state . query
32
- } ) ;
33
- }
34
28
helper . search ( ) ;
35
29
}
36
30
} ) ;
37
-
31
+
38
32
documentationSearch . addWidget ( getSearchBoxWidget ( false ) ) ;
39
-
33
+
40
34
documentationSearch . addWidget (
41
35
algoliaBundle . instantsearch . widgets . hits ( {
42
36
container : '.doc.links' ,
@@ -52,7 +46,7 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
52
46
}
53
47
} )
54
48
) ;
55
-
49
+
56
50
documentationSearch . addWidget (
57
51
algoliaBundle . instantsearch . widgets . stats ( {
58
52
container : '.doc.stats' ,
@@ -61,13 +55,13 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
61
55
}
62
56
} )
63
57
) ;
64
-
58
+
65
59
documentationSearch . addWidget (
66
60
algoliaBundle . instantsearch . widgets . stats ( {
67
61
container : '.doc.footer' ,
68
62
transformData : function ( hit ) {
69
63
hit [ 'morePages' ] = hit . nbPages > 1 ;
70
-
64
+
71
65
return hit ;
72
66
} ,
73
67
templates : {
@@ -81,10 +75,10 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
81
75
}
82
76
} )
83
77
) ;
84
-
78
+
85
79
documentationSearch . start ( ) ;
86
80
}
87
-
81
+
88
82
function initDiscourseSearch ( ) {
89
83
const discourseSearch = algoliaBundle . instantsearch ( {
90
84
appId : 'G25OKIW19Q' ,
@@ -96,9 +90,9 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
96
90
} ,
97
91
searchFunction : searchFunction
98
92
} ) ;
99
-
93
+
100
94
discourseSearch . addWidget ( getSearchBoxWidget ( true ) ) ;
101
-
95
+
102
96
discourseSearch . addWidget (
103
97
algoliaBundle . instantsearch . widgets . hits ( {
104
98
container : '.links.forum' ,
@@ -117,15 +111,15 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
117
111
hit . content = escapeHighlightedString (
118
112
hit . _snippetResult . content . value
119
113
) ;
120
-
114
+
121
115
hit . tags = hit . _highlightResult . topic . tags ;
122
-
116
+
123
117
return hit ;
124
118
}
125
119
}
126
120
} )
127
121
) ;
128
-
122
+
129
123
discourseSearch . addWidget (
130
124
algoliaBundle . instantsearch . widgets . stats ( {
131
125
container : '.forum.stats' ,
@@ -134,13 +128,13 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
134
128
}
135
129
} )
136
130
) ;
137
-
131
+
138
132
discourseSearch . addWidget (
139
133
algoliaBundle . instantsearch . widgets . stats ( {
140
134
container : '.forum.footer' ,
141
135
transformData : function ( hit ) {
142
136
hit [ 'morePages' ] = hit . nbPages > 1 ;
143
-
137
+
144
138
return hit ;
145
139
} ,
146
140
templates : {
@@ -154,16 +148,16 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
154
148
}
155
149
} )
156
150
) ;
157
-
151
+
158
152
discourseSearch . on ( 'render' , function ( ) {
159
153
// Loop over every containers
160
154
document . querySelectorAll ( ".ais-hits--item a" ) . forEach ( container => {
161
155
const content = container . querySelector ( '.content' ) ;
162
-
156
+
163
157
// 186 - 3 lines, each line 62 chars max
164
158
if ( content . textContent . replace ( / \s / g, "" ) . length >= 186 ) {
165
-
166
-
159
+
160
+
167
161
var style = document . createElement ( "style" ) ;
168
162
style . innerHTML = `
169
163
#${ container . id } .content:after {
@@ -175,17 +169,17 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
175
169
background:#FFF;
176
170
padding: 0 0.5ch;
177
171
}` ;
178
-
172
+
179
173
// And then insert this before the current container
180
174
container . parentNode . insertAdjacentElement ( "afterbegin" , style ) ;
181
175
}
182
176
} ) ;
183
-
177
+
184
178
} ) ;
185
-
179
+
186
180
discourseSearch . start ( ) ;
187
181
}
188
-
182
+
189
183
function initContactDocSearch ( ) {
190
184
const documentationSearch = algoliaBundle . instantsearch ( {
191
185
appId : 'BH4D9OD16A' ,
@@ -196,7 +190,7 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
196
190
hitsPerPage : 3
197
191
}
198
192
} ) ;
199
-
193
+
200
194
documentationSearch . addWidget (
201
195
algoliaBundle . instantsearch . widgets . searchBox ( {
202
196
container : '#subject' ,
@@ -205,7 +199,7 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
205
199
magnifier : false
206
200
} )
207
201
) ;
208
-
202
+
209
203
documentationSearch . addWidget (
210
204
algoliaBundle . instantsearch . widgets . hits ( {
211
205
container : '.contact_results' ,
@@ -215,23 +209,23 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
215
209
}
216
210
} )
217
211
) ;
218
-
212
+
219
213
documentationSearch . start ( ) ;
220
214
}
221
-
215
+
222
216
function searchFunction ( helper ) {
223
217
const $results = $ ( '#results' ) ;
224
218
const $landing = $ ( '#landing' ) ;
225
-
219
+
226
220
if ( helper . state . query === '' ) {
227
221
$results . hide ( ) ;
228
222
$landing . show ( ) ;
229
-
223
+
230
224
return ;
231
225
}
232
-
226
+
233
227
helper . search ( ) ;
234
-
228
+
235
229
$results . show ( ) ;
236
230
$landing . hide ( ) ;
237
231
}
@@ -260,23 +254,23 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
260
254
} ) ;
261
255
}
262
256
} ) ;
263
-
257
+
264
258
function handleLatestVersion ( $ ) {
265
259
$ . getJSON ( 'https://api.github.com/repos/algolia/algoliasearch-magento-2/releases/latest' , function ( payload ) {
266
260
const latestVersion = payload . name ;
267
-
261
+
268
262
if ( compareVersions ( algoliaSearchExtentionsVersion , latestVersion ) > 0 ) {
269
263
const ghLink = 'https://github.com/algolia/algoliasearch-magento-2/releases/tag/' + latestVersion ;
270
264
const latestVersionLink = '<a href="' + ghLink + '" target="_blank">' + latestVersion + '</a>' ;
271
-
265
+
272
266
$ ( '#current_version' ) . text ( algoliaSearchExtentionsVersion ) ;
273
267
$ ( '.version.latest_version' ) . html ( latestVersionLink ) ;
274
-
268
+
275
269
$ ( '.legacy_version' ) . show ( ) ;
276
270
}
277
271
} ) ;
278
272
}
279
-
273
+
280
274
function getSearchBoxWidget ( showIcons = false ) {
281
275
return algoliaBundle . instantsearch . widgets . searchBox ( {
282
276
container : '#search_box' ,
@@ -285,78 +279,78 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
285
279
magnifier : showIcons
286
280
} ) ;
287
281
}
288
-
282
+
289
283
function getDocumentationTemplate ( ) {
290
284
return `
291
285
<a href="{{url}}?utm_source=magento&utm_medium=extension&utm_campaign=support-page" target="_blank" id="doc_{{objectID}}">
292
286
<span class="heading">
293
287
{{#hierarchy.lvl0}}
294
288
{{{_highlightResult.hierarchy.lvl0.value}}}
295
289
{{/hierarchy.lvl0}}
296
-
290
+
297
291
{{#hierarchy.lvl1}}
298
292
> {{{_highlightResult.hierarchy.lvl1.value}}}
299
293
{{/hierarchy.lvl1}}
300
-
294
+
301
295
{{#hierarchy.lvl2}}
302
296
> {{{_highlightResult.hierarchy.lvl2.value}}}
303
297
{{/hierarchy.lvl2}}
304
-
298
+
305
299
{{#hierarchy.lvl3}}
306
300
> {{{_highlightResult.hierarchy.lvl3.value}}}
307
301
{{/hierarchy.lvl3}}
308
-
302
+
309
303
{{#hierarchy.lvl4}}
310
304
> {{{_highlightResult.hierarchy.lvl4.value}}}
311
305
{{/hierarchy.lvl4}}
312
306
</span>
313
-
307
+
314
308
<span class="content">
315
309
{{{#content}}}
316
310
{{{_highlightResult.content.value}}}
317
311
{{{/content}}}
318
312
</span>
319
313
</div>` ;
320
314
}
321
-
315
+
322
316
function getDiscourseTemplate ( ) {
323
317
return `
324
318
<a href="https://discourse.algolia.com{{url}}/?utm_source=magento&utm_medium=extension&utm_campaign=support-page" target="_blank" id="disc_{{objectID}}">
325
319
<span class="heading">
326
320
{{{ _highlightResult.topic.title.value }}}
327
321
</span>
328
-
322
+
329
323
<span class="content">
330
324
{{{content}}}
331
325
</span>
332
326
</a>` ;
333
327
}
334
-
328
+
335
329
function escapeHighlightedString ( str , highlightPreTag , highlightPostTag ) {
336
330
highlightPreTag = highlightPreTag || '<em>' ;
337
331
var pre = document . createElement ( 'div' ) ;
338
332
pre . appendChild ( document . createTextNode ( highlightPreTag ) ) ;
339
-
333
+
340
334
highlightPostTag = highlightPostTag || '</em>' ;
341
335
var post = document . createElement ( 'div' ) ;
342
336
post . appendChild ( document . createTextNode ( highlightPostTag ) ) ;
343
-
337
+
344
338
var div = document . createElement ( 'div' ) ;
345
339
div . appendChild ( document . createTextNode ( str ) ) ;
346
-
340
+
347
341
return div . innerHTML
348
342
. replace ( RegExp ( escapeRegExp ( pre . innerHTML ) , 'g' ) , highlightPreTag )
349
343
. replace ( RegExp ( escapeRegExp ( post . innerHTML ) , 'g' ) , highlightPostTag )
350
344
}
351
-
345
+
352
346
function escapeRegExp ( str ) {
353
347
return str . replace ( / [ \- \[ \] \/ \{ \} \( \) \* \+ \? \. \\ \^ \$ \| ] / g, '\\$&' ) ;
354
348
}
355
-
349
+
356
350
function compareVersions ( left , right ) {
357
351
left = sanitizeVersion ( left ) ;
358
352
right = sanitizeVersion ( right ) ;
359
-
353
+
360
354
for ( var i = 0 ; i < Math . max ( left . length , right . length ) ; i ++ ) {
361
355
if ( left [ i ] > right [ i ] ) {
362
356
return - 1 ;
@@ -365,10 +359,10 @@ requirejs(['algoliaAdminBundle'], function(algoliaBundle) {
365
359
return 1 ;
366
360
}
367
361
}
368
-
362
+
369
363
return 0 ;
370
364
}
371
-
365
+
372
366
function sanitizeVersion ( input ) {
373
367
return input . split ( '.' ) . map ( function ( n ) {
374
368
return parseInt ( n , 10 ) ;
0 commit comments