Skip to content

Commit a1b2796

Browse files
Replace jscolor with coloris. #168 (#223)
* build: replace jscolor with coloris * feat: make choices case insensitive * fix: apply feedback * fix: use json_script template tag * fix: add last tweaks * fix: remove useless argument
1 parent 11fbcae commit a1b2796

File tree

17 files changed

+2009
-3557
lines changed

17 files changed

+2009
-3557
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
colorfield/static/colorfield/jscolor/* linguist-vendored=true
1+
colorfield/static/colorfield/coloris/* linguist-vendored=true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ __pycache__/
88
## Local setup
99
.vscode/
1010
.venv/
11+
venv/
1112

1213
# Distribution / packaging
1314
.Python

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class MyForm(forms.Form):
132132
```
133133

134134
### Admin
135-
The admin will kindly provide a simple [color picker](http://jscolor.com/) for all color fields. :)
135+
The admin will kindly provide a simple [color picker](https://coloris.js.org/) for all color fields. :)
136136

137137
---
138138

colorfield/fields.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ def __init__(self, *args, **kwargs):
5757
"you can set only one of the two for a ColorField instance."
5858
)
5959

60+
# change choices to lower case (case-insensitive workaround)
61+
if self.choices:
62+
self.choices = [(k.lower(), *v) for (k, *v) in self.choices]
63+
6064
def formfield(self, **kwargs):
6165
palette = []
6266
if self.choices:
@@ -67,11 +71,10 @@ def formfield(self, **kwargs):
6771
kwargs["widget"] = ColorWidget(
6872
attrs={
6973
"default": self.get_default(),
70-
"format": self.format,
71-
"palette": palette,
72-
# # TODO: in case choices is defined,
73-
# # this will be used to hide the widget color spectrum
74-
# 'palette_choices_only': bool(self.choices),
74+
"format": self.format[0:3] if self.format else "hex",
75+
"alpha": self.format[-1] == "a" if self.format else False,
76+
"swatches": palette,
77+
"swatches_only": bool(self.choices),
7578
}
7679
)
7780
return super().formfield(**kwargs)
@@ -89,6 +92,12 @@ def deconstruct(self):
8992
kwargs["image_field"] = self.image_field
9093
return name, path, args, kwargs
9194

95+
def validate(self, value, *args, **kwargs):
96+
"""
97+
Override validation logic to make it case-insensitive.
98+
"""
99+
super().validate(value.lower() if value else value, *args, **kwargs)
100+
92101
def _get_image_field_color(self, instance):
93102
color = ""
94103
image_file = getattr(instance, self.image_field)
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
/** global: django */
1+
window.addEventListener('load', function () {
2+
const inputs = document.getElementsByClassName('colorfield_field coloris');
3+
for (const input of inputs) {
4+
const colorisId = input.getAttribute('data-coloris-options-json-script-id');
5+
const script = document.querySelector(`script[id='${colorisId}']`);
6+
const options = JSON.parse(script.textContent);
27

3-
window.addEventListener('load', function (event) {
4-
if (typeof django !== 'undefined' && typeof django.jQuery !== 'undefined') {
5-
(function ($) {
6-
// add colopicker to inlines added dynamically
7-
$(document).on('formset:added', function onFormsetAdded(event, row) {
8-
jscolor.install();
9-
});
10-
})(django.jQuery);
11-
}
8+
const id = input.getAttribute('id');
9+
Coloris.setInstance(`.colorfield_field.coloris.${id}`, options);
10+
}
1211
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
https://github.com/mdbassit/Coloris
2+
3+
Release: v0.24.0

0 commit comments

Comments
 (0)