Skip to content

Commit

Permalink
[CHANGE] Doctrine dropdown to autocomplete input field
Browse files Browse the repository at this point in the history
  • Loading branch information
ppfeufer committed Nov 8, 2024
1 parent 1b49834 commit 82b5cb0
Show file tree
Hide file tree
Showing 13 changed files with 1,507 additions and 32 deletions.
45 changes: 14 additions & 31 deletions afat/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@

# Alliance Auth AFAT
from afat.app_settings import AFAT_DEFAULT_FATLINK_EXPIRY_TIME
from afat.helper.fatlinks import get_doctrines
from afat.models import Doctrine, FleetType, Setting

# The first line dropdown is blank
# Usage example: dropdown = form_choices_blank + [(o.id, o.name) for o in objects]
form_choices_blank = [("", "---------")]


def get_mandatory_form_label_text(text):
"""
Expand Down Expand Up @@ -54,23 +49,17 @@ class AFatEsiFatForm(forms.Form):
label=_("Fleet type (optional)"),
queryset=FleetType.objects.filter(is_enabled=True),
)
doctrine_esi = forms.ChoiceField(
doctrine_esi = forms.CharField(
required=False,
label=_("Doctrine (optional)"),
choices=(),
widget=forms.TextInput(
attrs={
"data-datalist": "afat-fleet-doctrine-list",
"data-full-width": "true",
}
),
)

def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super().__init__(*args, **kwargs)

# Due to the dynamic nature of this dropdown field, we need to initialize it here.
# This is a workaround to avoid settings being cached and not updated when changed.
# This is also the reason we cannot use a ModelChoiceField here.
self.fields["doctrine_esi"].choices = form_choices_blank + [
(str(o), str(o)) for o in get_doctrines()
]


class AFatManualFatForm(forms.Form):
"""
Expand Down Expand Up @@ -111,10 +100,15 @@ class AFatClickFatForm(forms.Form):
label=_("Fleet type (optional)"),
queryset=FleetType.objects.filter(is_enabled=True),
)
doctrine = forms.ChoiceField(
doctrine = forms.CharField(
required=False,
label=_("Doctrine (optional)"),
choices=(),
widget=forms.TextInput(
attrs={
"data-datalist": "afat-fleet-doctrine-list",
"data-full-width": "true",
}
),
)
duration = forms.IntegerField(
required=True,
Expand All @@ -124,17 +118,6 @@ class AFatClickFatForm(forms.Form):
widget=forms.TextInput(attrs={"placeholder": _("Expiry time in minutes")}),
)

def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super().__init__(*args, **kwargs)

# Due to the dynamic nature of this dropdown field, we need to initialize it here.
# This is a workaround to avoid settings being cached and not updated when changed.
# This is also the reason we cannot use a ModelChoiceField here.
self.fields["doctrine"].choices = form_choices_blank + [
(str(o), str(o)) for o in get_doctrines()
]


class FatLinkEditForm(forms.Form):
"""
Expand Down
25 changes: 25 additions & 0 deletions afat/static/afat/javascript/afat-fatlink-add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Autocomplete from '/static/afat/libs/bootstrap5-autocomplete/1.1.25/autocomplete.min.js';

$(document).ready(() => {
'use strict';

const autoCompleteDropdown = (element) => {
const autoCompleteDoctrine = new Autocomplete( // eslint-disable-line no-unused-vars
element,
Object.assign(
{},
{
onSelectItem: console.log,
},
{
onRenderItem: (item, label) => {
return `<l-i set="fl" name="${item.value.toLowerCase()}" size="16"></l-i> ${label}`;
},
}
)
);
};

autoCompleteDropdown(document.getElementById('id_doctrine_esi'));
autoCompleteDropdown(document.getElementById('id_doctrine'));
});
2 changes: 2 additions & 0 deletions afat/static/afat/javascript/afat-fatlink-add.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions afat/static/afat/javascript/afat-fatlink-add.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions afat/static/afat/libs/bootstrap5-autocomplete/1.1.25/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Thomas Portelange

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 82b5cb0

Please sign in to comment.