Execute script to add behavior to searched buttons

This commit is contained in:
Nicolas Margulies 2023-10-26 00:10:56 +02:00
parent 0cc130092f
commit 405479e5ad
1 changed files with 16 additions and 12 deletions

View File

@ -182,7 +182,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
<script type="text/javascript">
{% for button in highlighted %}
{% if button.display %}
$("#highlighted_button{{ button.id }}").click(function() {
document.getElementById("highlighted_button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
@ -193,7 +193,7 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% for category in categories %}
{% for button in category.templates_filtered %}
{% if button.display %}
$("#button{{ button.id }}").click(function() {
document.getElementById("button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
@ -202,15 +202,6 @@ SPDX-License-Identifier: GPL-3.0-or-later
{% endfor %}
{% endfor %}
{% for button in search_results %}
{% if button.display %}
$("#search_button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
});
{% endif %}
{% endfor %}
searchbar = document.getElementById("search-input")
const parser = new DOMParser();
@ -224,8 +215,10 @@ SPDX-License-Identifier: GPL-3.0-or-later
xhr.open("GET", location.pathname + "?search=" +
encodeURI(pattern) + "#search", true)
xhr.onload = () => {
let newdoc = parser.parseFromString(xhr.responseText, "text/html");
document.getElementById("search-results").innerHTML =
parser.parseFromString(xhr.responseText, "text/html").getElementById("search-results").innerHTML
newdoc.getElementById("search-results").innerHTML;
eval(newdoc.getElementById("search-script").text);
};
xhr.send();
}
@ -234,4 +227,15 @@ SPDX-License-Identifier: GPL-3.0-or-later
debounce(updateSearch)()
});
</script>
<script type="text/javascript" id="search-script">
{% for button in search_results %}
{% if button.display %}
document.getElementById("search_button{{ button.id }}").click(function() {
addConso({{ button.destination_id }}, {{ button.amount }},
{{ polymorphic_ctype }}, {{ button.category_id }}, "{{ button.category.name|escapejs }}",
{{ button.id }}, "{{ button.name|escapejs }}");
});
{% endif %}
{% endfor %}
</script>
{% endblock %}