diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-03-18 19:55:10 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2020-03-18 19:55:10 +0100 |
commit | bccb066ff04fa3add398d8d8dbf3d57c7146d1ca (patch) | |
tree | 2d15e77e6c721489eff62f5713e8f4fdfffafee7 /ishtar_common/widgets.py | |
parent | 17dc2e0fe41229687149f44ca7f84be3cfe55a85 (diff) | |
download | Ishtar-bccb066ff04fa3add398d8d8dbf3d57c7146d1ca.tar.bz2 Ishtar-bccb066ff04fa3add398d8d8dbf3d57c7146d1ca.zip |
Autocomplete widget: add modify action to autocomplete field - better layout for previous value
Diffstat (limited to 'ishtar_common/widgets.py')
-rw-r--r-- | ishtar_common/widgets.py | 63 |
1 files changed, 42 insertions, 21 deletions
diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index bf0f90a6c..3410d0824 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -599,7 +599,7 @@ class ModelJQueryAutocompleteField(ModelFieldMixin, forms.CharField): class JQueryAutoComplete(forms.TextInput): def __init__(self, source, associated_model=None, options=None, attrs=None, new=False, url_new='', multiple=False, limit=None, - dynamic_limit=None, tips=""): + dynamic_limit=None, modify=False, tips=""): """ Source can be a list containing the autocomplete values or a string containing the url used for the request. @@ -621,6 +621,7 @@ class JQueryAutoComplete(forms.TextInput): self.new = new self.url_new = url_new + self.modify = modify self.multiple = multiple self.limit = limit or {} self.dynamic_limit = dynamic_limit or [] @@ -665,6 +666,7 @@ class JQueryAutoComplete(forms.TextInput): attrs_hidden = self.build_attrs(attrs, {"name": name}) attrs_select = self.build_attrs(attrs) attrs_select['placeholder'] = _(u"Search...") + values = [] if value: hiddens = [] selects = [] @@ -703,20 +705,32 @@ class JQueryAutoComplete(forms.TextInput): if 'class' not in attrs_select: attrs_select['class'] = 'autocomplete' + has_previous_value = 'value' in attrs_select and attrs_select['value'] attrs_select['class'] += ' form-control' - new = '' - html = u"" - if self.tips or self.new: - html = "<div class='input-group'>" + new = "" + html = "" + if self.tips or self.new or self.modify: + klass = "input-group" + if has_previous_value: + klass += " has-previous-value" + html = "<div class='{}'>".format(klass) + # WARNING: the modal for the form must be in the main template + # "extra_form_modals" list is used for that in form or view + model_name = self.associated_model._meta.object_name.lower() if self.tips: - new = """<span class="input-group-append"> + new += """<span class="input-group-append"> <span class="add-button input-group-text"><em>{}</em></span></span> """.format(self.tips) + if self.modify and len(values) == 1: + url_modify = 'modify-' + model_name + url_modify = reverse(url_modify, args=[values[0]]) + new += """ + <span class="input-group-append"> + <a href="#" class="modify-button input-group-text" + onclick="dt_qa_open('{}', 'modal-dynamic-form-{}');"> + <i class="fa fa-pencil"></i></a> + </span>""".format(url_modify, model_name, model_name) if self.new: - # WARNING: the modal for the form must be in the main template - # "extra_form_modals" list is used for that in form or view - - model_name = self.associated_model._meta.object_name.lower() limits = [] for k in self.limit: limits.append(k + "__" + "-".join( @@ -737,18 +751,25 @@ class JQueryAutoComplete(forms.TextInput): new += "</div>" old_value = "" - if 'value' in attrs_select and attrs_select['value']: - old_value = u"""<span class='previous-value'> - <em>{}</em> <span id="{}">{}</span> - <button class="btn btn-secondary small-button" type="button" - id="{}"> - <i class="fa fa-refresh" aria-hidden="true"></i> - </button> -</span>""".format( - _(u"Previous value:"), - attrs_hidden['id'] + u"_previous_label", + if has_previous_value: + old_value = """<div class='input-group previous-value'> + <div class="input-group-prepend"> + <span class="input-group-text font-italic">{}</span> + </div> + <input class="form-control" id="{}" type="text" value="{}" + disabled="disabled" /> + <span class="input-group-append"> + <button class="btn btn-secondary small-button" type="button" + id="{}" title="{}"> + <i class="fa fa-refresh" aria-hidden="true"></i> + </button> + </span> + </div>""".format( + _("Prev.:"), + attrs_hidden['id'] + "_previous_label", attrs_select['value'], - attrs_hidden['id'] + u"_previous_button", + attrs_hidden['id'] + "_previous_button", + _("Restore previous") ) attrs_hidden_previous = attrs_hidden.copy() attrs_hidden_previous['name'] += u"_previous" |