refactor: move javascript to babybuddy.js

having it directly in the page html is not ideal, now its properly
isolated.

Also use the new classname
This commit is contained in:
billybonks 2024-02-07 15:11:52 +08:00 committed by Christopher Charbonneau Wells
parent b7743674d8
commit 66ccc1c6c8
3 changed files with 19 additions and 18 deletions

View File

@ -43,3 +43,19 @@ $("form").off("submit", preventDoubleSubmit);
$("form").on("submit", function () { $("form").on("submit", function () {
$(this).on("submit", preventDoubleSubmit); $(this).on("submit", preventDoubleSubmit);
}); });
BabyBuddy.RememberAdvancedToggle = function (ptr) {
localStorage.setItem("advancedForm", event.newState);
};
(function toggleAdvancedFields() {
window.addEventListener("load", function () {
if (localStorage.getItem("advancedForm") !== "open") {
return;
}
document.querySelectorAll(".advanced-fields").forEach(function (node) {
node.open = true;
});
});
})();

View File

@ -44,22 +44,6 @@
<script src="{% static "babybuddy/js/vendor.js" %}"></script> <script src="{% static "babybuddy/js/vendor.js" %}"></script>
<script src="{% static "babybuddy/js/app.js" %}"></script> <script src="{% static "babybuddy/js/app.js" %}"></script>
{% if user.is_authenticated %}<script>BabyBuddy.PullToRefresh.init()</script>{% endif %} {% if user.is_authenticated %}<script>BabyBuddy.PullToRefresh.init()</script>{% endif %}
{% block javascript %} {% block javascript %}{% endblock %}
<script type="application/javascript">
function rememberAdvancedToggle(event, element){
localStorage.setItem("advancedForm",event.newState)
}
window.addEventListener('load', function () {
if(localStorage.getItem("advancedForm") !== "open"){
return;
}
document.querySelectorAll("form details").forEach(function(node){
node.open = true
});
})
</script>
{% endblock %}
</body> </body>
</html> </html>

View File

@ -1,7 +1,8 @@
{% load i18n %} {% load i18n %}
<div class="row"> <div class="row">
<div class="row mb-3"> <div class="row mb-3">
<details ontoggle="rememberAdvancedToggle(event)" class="advanced-fields"> <details ontoggle="BabyBuddy.RememberAdvancedToggle(event)"
class="advanced-fields">
<summary class="advanced-fields__summary"> <summary class="advanced-fields__summary">
<h4>{% trans "Advanced Fields" %}</h4> <h4>{% trans "Advanced Fields" %}</h4>
</summary> </summary>