Skip to content

Commit 928a229

Browse files
fix : update the js compactible with require.js module loader (#308)
1 parent c8021e7 commit 928a229

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

src/ansys_sphinx_theme/theme/ansys_sphinx_theme/components/search-field.html

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,18 @@
3333
{% endif %}
3434
<i class="fa-solid fa-magnifying-glass"></i>
3535
<!-- Include the MeiliSearch JavaScript library for the search bar -->
36-
<script src="https://cdn.jsdelivr.net/npm/docs-searchbar.js@latest/dist/cdn/docs-searchbar.min.js"></script>
36+
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
3737

3838
{% if not theme_use_meilisearch.host %} {% set HOST_URL =
3939
"https://backend.search.pyansys.com/" %} {% else %} {% set HOST_URL =
4040
theme_use_meilisearch.host %} {% endif %}
41-
4241
<script>
43-
document.body.style.overflow = "hidden !important";
44-
// Initialize the MeiliSearch bar with the given API key and host
45-
// inspect the first value of index UID as default
46-
var theSearchBar = docsSearchBar({
47-
hostUrl: "{{ HOST_URL }}",
48-
apiKey: "{{ API_KEY }}",
49-
indexUid: "{{ theme_use_meilisearch.index_uids.items()|first|first }}",
50-
inputSelector: "#search-bar-input",
51-
debug: true, // Set debug to true if you want to inspect the dropdown
52-
meilisearchOptions: {
53-
limit: 1000,
54-
},
55-
});
56-
57-
// Listen for changes in the dropdown selector and update the index uid and suggestion accordingly
58-
document
59-
.getElementById("indexUidSelector")
60-
.addEventListener("change", function () {
61-
theSearchBar.indexUid = this.value;
62-
theSearchBar.suggestionIndexUid = this.value;
63-
theSearchBar.autocomplete.autocomplete.setVal("");
64-
});
65-
66-
// Listen for changes in the search bar input and update the suggestion accordingly
67-
document
68-
.getElementById("search-bar-input")
69-
.addEventListener("change", function () {
70-
theSearchBar.suggestionIndexUid =
71-
document.getElementById("indexUidSelector").value;
72-
});
73-
74-
// Set the focus on the search bar input
75-
document.getElementById("searchbar").focus();
76-
77-
// Set the focus on the dropdown selector
78-
document
79-
.getElementById("searchbar")
80-
.addEventListener("indexUidSelector", function () {
81-
theSearchBar.autocomplete.autocomplete.close();
82-
});
42+
var HOST_URL = "{{ HOST_URL }}";
43+
var API_KEY = "{{ API_KEY }}";
44+
var indexUid = "{{ theme_use_meilisearch.index_uids.items()|first|first }}";
8345
</script>
46+
47+
<script src="{{ pathto('_static/js/meilisearch_theme_wrap.js', 1) }}"></script>
8448
</form>
8549
{% else %}
8650
<!-- If there is no MiliSearch enabled, use the PyData search -->
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require.config({
2+
paths: {
3+
docsSearchBar:
4+
"https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn/docs-searchbar.min",
5+
},
6+
});
7+
8+
require(["docsSearchBar"], function (docsSearchBar) {
9+
document.body.style.overflow = "hidden !important";
10+
// Initialize the MeiliSearch bar with the given API key and host
11+
// inspect the first value of index UID as default
12+
13+
var theSearchBar = docsSearchBar({
14+
hostUrl: HOST_URL,
15+
apiKey: API_KEY,
16+
indexUid: indexUid,
17+
inputSelector: "#search-bar-input",
18+
debug: true, // Set debug to true if you want to inspect the dropdown
19+
meilisearchOptions: {
20+
limit: 1000,
21+
},
22+
});
23+
24+
// Listen for changes in the dropdown selector and update the index uid and suggestion accordingly
25+
document
26+
.getElementById("indexUidSelector")
27+
.addEventListener("change", function () {
28+
theSearchBar.indexUid = this.value;
29+
theSearchBar.suggestionIndexUid = this.value;
30+
theSearchBar.autocomplete.autocomplete.setVal("");
31+
});
32+
33+
// Listen for changes in the search bar input and update the suggestion accordingly
34+
document
35+
.getElementById("search-bar-input")
36+
.addEventListener("change", function () {
37+
theSearchBar.suggestionIndexUid =
38+
document.getElementById("indexUidSelector").value;
39+
});
40+
41+
// Set the focus on the search bar input
42+
document.getElementById("searchbar").focus();
43+
44+
// Set the focus on the dropdown selector
45+
document
46+
.getElementById("searchbar")
47+
.addEventListener("indexUidSelector", function () {
48+
theSearchBar.autocomplete.autocomplete.close();
49+
});
50+
});

0 commit comments

Comments
 (0)