| 1 | from django import forms |
| 2 | from django.conf import settings |
| 3 | from django.utils.safestring import mark_safe |
| 4 | |
| 5 | |
| 6 | class ColorPickerWidget(forms.TextInput): |
| 7 | class Media: |
| 8 | css = { |
| 9 | "all": ("%s/%s" % (settings.STATIC_URL, "paintstore/css/colorpicker.css"),) |
| 10 | } |
| 11 | |
| 12 | js = ( |
| 13 | ("%s/%s" % (settings.STATIC_URL, "paintstore/jquery_1.7.2.js")), |
| 14 | ("%s/%s" % (settings.STATIC_URL, "paintstore/colorpicker.js")) |
| 15 | ) |
| 16 | |
| 17 | input_type = 'colorpicker' |
| 18 | |
| 19 | def render(self, name, value, attrs=None): |
| 20 | script = u"""<script type='text/javascript'> |
| 21 | $(document).ready(function(){ |
| 22 | $('#%s').ColorPicker({ |
| 23 | onSubmit: function(hsb, hex, rgb, el, parent) { |
| 24 | $(el).val('#' + hex); |
| 25 | $(el).ColorPickerHide(); |
| 26 | }, |
| 27 | onBeforeShow: function () { |
| 28 | $(this).ColorPickerSetColor(this.value); |
| 29 | } |
| 30 | }).bind('keyup', function(){ |
| 31 | $(this).ColorPickerSetColor(this.value.replace('#', '')); |
| 32 | }); |
| 33 | }); |
| 34 | </script> |
| 35 | """ % ("id_%s" % name,) |
| 36 | |
| 37 | super_render = super(ColorPickerWidget, self).render(name, value, attrs) |
| 38 | return mark_safe(u"%s%s" % (super_render, script)) |