Created
September 12, 2014 16:03
-
-
Save alej0varas/64e07b1f585f46f0fab2 to your computer and use it in GitHub Desktop.
Wagtail CMS color picker panel. Requires jQuery plugin http://www.eyecon.ro/colorpicker/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.utils.safestring import mark_safe | |
from wagtail.wagtailadmin.edit_handlers import BaseFieldPanel | |
class BaseColorFieldPanel(BaseFieldPanel): | |
def render_js(self): | |
big_javascript_block = """ | |
(function() { | |
var cpjs = document.createElement('script'); | |
cpjs.type = 'text/javascript'; | |
cpjs.async = true; | |
cpjs.src = '%s'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(cpjs, s); | |
var cpcss = document.createElement('link'); | |
cpcss.rel = 'stylesheet'; | |
cpcss.media = 'screen'; | |
cpcss.type = 'text/css'; | |
cpcss.href = '%s'; | |
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(cpcss, s); | |
setTimeout(function () { | |
var options = { | |
'onSubmit': function (a, color, c, input) { | |
$(input).attr('value', '#' + color); | |
}, | |
'onShow': function () { | |
$('.colorpicker').css('z-index', 10); | |
} | |
} | |
$('#%%s').ColorPicker(options); | |
}, 3000); | |
})(); | |
""" % ( | |
settings.STATIC_URL + 'js/colorpicker.js', | |
settings.STATIC_URL + 'css/colorpicker.css' | |
) | |
return mark_safe(big_javascript_block % self.bound_field.id_for_label) | |
def ColorFieldPanel(field_name): | |
return type(str('_ColorFieldPanel'), (BaseColorFieldPanel,), { | |
'field_name': field_name, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm trying to use this snippet with Wagtail 1.1 and is not working. "render_js" is never being called, I could not find it in BaseFieldPanel class. Have you use this code successfully sometime? Thanks!