Skip to content

Commit 34070bf

Browse files
authored
Feature/sorted option for Attributes Widget (#48)
* Fix: alignment of add and remove buttons if djangocms-admin-style is NOT present * Fix: Leave line-height unchanged if djangocms-admin-style is present * Fix: Avoid overlap of attributes input fields * Fix: Remove two empty lines * Fix: Missing Changelog entry * Feature: sorted option for AttributesWidget * OK, back to node@6
1 parent 9f343b8 commit 34070bf

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

djangocms_attributes_field/widgets.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.forms import Widget
22
from django.forms.utils import flatatt
33
from django.utils.html import escape, mark_safe, strip_spaces_between_tags
4-
from django.utils.translation import ugettext as _
4+
from django.utils.translation import gettext as _
55

66

77
class AttributesWidget(Widget):
@@ -13,10 +13,11 @@ class AttributesWidget(Widget):
1313
# https://www.huyng.com/posts/django-custom-form-widget-for-dictionary-and-tuple-key-value-pairs
1414
def __init__(self, *args, **kwargs):
1515
"""
16-
Supports additional kwargs: `key_attr` and `val_attr`.
16+
Supports additional kwargs: `key_attr`, `val_attr`, `sorted`.
1717
"""
1818
self.key_attrs = kwargs.pop('key_attrs', {})
1919
self.val_attrs = kwargs.pop('val_attrs', {})
20+
self.sorted = sorted if kwargs.pop('sorted', True) else lambda x: x
2021
super().__init__(*args, **kwargs)
2122

2223
def _render_row(self, key, value, field_name, key_attrs, val_attrs):
@@ -69,7 +70,7 @@ def render(self, name, value, attrs=None, renderer=None):
6970

7071
output = '<div class="djangocms-attributes-field">'
7172
if value and isinstance(value, dict) and len(value) > 0:
72-
for key in sorted(value):
73+
for key in self.sorted(value):
7374
output += self._render_row(key, value[key], name, flatatt(self.key_attrs), flatatt(self.val_attrs))
7475

7576
# Add empty template
@@ -122,6 +123,9 @@ def render(self, name, value, attrs=None, renderer=None):
122123
width: 75% !important;
123124
float: none !important;
124125
}
126+
body:not(.djangocms-admin-style) .attributes-pair .field-box:first-child input {
127+
width: calc(100% - 1.3em);
128+
}
125129
.djangocms-attributes-field .attributes-pair .attributes-value {
126130
width: 60% !important;
127131
width: -webkit-calc(100% - 54px) !important;

0 commit comments

Comments
 (0)