mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-11 00:33:17 +00:00
Swapped out jQueryUI datetimepicker for xdsoft datetimepicker
This commit is contained in:
parent
0d7471b94f
commit
614b49213d
3 changed files with 24 additions and 191 deletions
|
|
@ -23,133 +23,12 @@ from wagtail.wagtailcore.util import camelcase_to_underscore
|
|||
from wagtail.wagtailcore.fields import RichTextArea
|
||||
|
||||
|
||||
class FriendlyDateInput(forms.DateInput):
|
||||
"""
|
||||
A custom DateInput widget that formats dates as "05 Oct 2013"
|
||||
and adds class="friendly_date" to be picked up by jquery datepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'friendly_date'}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
|
||||
super(FriendlyDateInput, self).__init__(attrs=default_attrs, format='%d %b %Y')
|
||||
|
||||
|
||||
class FriendlyTimeInput(forms.TimeInput):
|
||||
"""
|
||||
A custom TimeInput widget that formats dates as "5.30pm"
|
||||
and adds class="friendly_time" to be picked up by jquery timepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'friendly_time'}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
|
||||
super(FriendlyTimeInput, self).__init__(attrs=default_attrs, format='%I.%M%p')
|
||||
|
||||
|
||||
class FriendlyTimeField(forms.CharField):
|
||||
def to_python(self, time_string):
|
||||
# Check if the string is blank
|
||||
if not time_string:
|
||||
return None
|
||||
|
||||
# Look for time in the string
|
||||
expr = re.compile("^(?P<hour>\d+)(?:(?:.|:)(?P<minute>\d+))?(?P<am_pm>am|pm)")
|
||||
match = expr.match(time_string.lower())
|
||||
if match:
|
||||
# Pull out values from string
|
||||
hour_string, minute_string, am_pm = match.groups()
|
||||
|
||||
# Convert hours and minutes to integers
|
||||
hour = int(hour_string)
|
||||
if minute_string:
|
||||
minute = int(minute_string)
|
||||
else:
|
||||
minute = 0
|
||||
|
||||
# Create python time
|
||||
if am_pm == "pm" and hour < 12:
|
||||
hour += 12
|
||||
|
||||
if am_pm == "am" and hour >= 12:
|
||||
hour -= 12
|
||||
|
||||
return datetime.time(hour=hour, minute=minute)
|
||||
else:
|
||||
raise ValidationError(_("Please type a valid time"))
|
||||
|
||||
|
||||
class LocalizedDateInput(forms.DateInput):
|
||||
"""
|
||||
A custom DateInput widget that formats localized dates
|
||||
and adds class="friendly_date" to be picked up by jquery datepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'localized_date', 'localize':True}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
|
||||
super(LocalizedDateInput, self).__init__(attrs=default_attrs)
|
||||
|
||||
|
||||
class LocalizedTimeInput(forms.TimeInput):
|
||||
"""
|
||||
A custom TimeInput widget that formats dates as "5.30pm"
|
||||
and adds class="friendly_time" to be picked up by jquery timepicker.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
default_attrs = {'class': 'localized_time'}
|
||||
if attrs:
|
||||
default_attrs.update(attrs)
|
||||
# Just use 24-hour format
|
||||
super(LocalizedTimeInput, self).__init__(attrs=default_attrs, format='%H:%M')
|
||||
|
||||
|
||||
class LocalizedTimeField(forms.CharField):
|
||||
def to_python(self, time_string):
|
||||
# Check if the string is blank
|
||||
if not time_string:
|
||||
return None
|
||||
|
||||
# Look for time in the string
|
||||
expr = re.compile("^(?P<hour>\d+)(?:(?:.|:)(?P<minute>\d+))?")
|
||||
match = expr.match(time_string.lower())
|
||||
if match:
|
||||
# Pull out values from string
|
||||
hour_string, minute_string= match.groups()
|
||||
|
||||
# Convert hours and minutes to integers
|
||||
hour = int(hour_string)
|
||||
if minute_string:
|
||||
minute = int(minute_string)
|
||||
else:
|
||||
minute = 0
|
||||
if hour>=24 or hour < 0 or minute >=60 or minute < 0:
|
||||
raise ValidationError(_("Please type a valid time"))
|
||||
|
||||
return datetime.time(hour=hour, minute=minute)
|
||||
else:
|
||||
raise ValidationError(_("Please type a valid time") )
|
||||
|
||||
|
||||
if hasattr(settings, 'USE_L10N') and settings.USE_L10N==True:
|
||||
FORM_FIELD_OVERRIDES = {
|
||||
models.DateField: {'widget': LocalizedDateInput},
|
||||
models.TimeField: {'widget': LocalizedTimeInput, 'form_class': LocalizedTimeField},
|
||||
}
|
||||
else: # Fall back to friendly date/time
|
||||
FORM_FIELD_OVERRIDES = {
|
||||
models.DateField: {'widget': FriendlyDateInput},
|
||||
models.TimeField: {'widget': FriendlyTimeInput, 'form_class': FriendlyTimeField},
|
||||
}
|
||||
FORM_FIELD_OVERRIDES = {}
|
||||
|
||||
WIDGET_JS = {
|
||||
FriendlyDateInput: (lambda id: "initFriendlyDateChooser(fixPrefix('%s'));" % id),
|
||||
FriendlyTimeInput: (lambda id: "initFriendlyTimeChooser(fixPrefix('%s'));" % id),
|
||||
LocalizedDateInput: (lambda id: "initLocalizedDateChooser(fixPrefix('%s'));" % id),
|
||||
LocalizedTimeInput: (lambda id: "initLocalizedTimeChooser(fixPrefix('%s'));" % id),
|
||||
forms.DateInput: (lambda id: "initDateChooser(fixPrefix('%s'));" % id),
|
||||
forms.TimeInput: (lambda id: "initTimeChooser(fixPrefix('%s'));" % id),
|
||||
forms.DateTimeInput: (lambda id: "initDateTimeChooser(fixPrefix('%s'));" % id),
|
||||
RichTextArea: (lambda id: "makeRichTextEditable(fixPrefix('%s'));" % id),
|
||||
TagWidget: (
|
||||
lambda id: "initTagField(fixPrefix('%s'), '%s');" % (
|
||||
|
|
|
|||
|
|
@ -53,56 +53,23 @@ function insertRichTextDeleteControl(elem) {
|
|||
});
|
||||
}
|
||||
|
||||
function initDateChoosers(context) {
|
||||
$('input.friendly_date', context).datepicker({
|
||||
dateFormat: 'd M yy', constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
function initDateChooser(id) {
|
||||
$('#' + id).datetimepicker({
|
||||
timepicker: false,
|
||||
format: 'Y-m-d'
|
||||
});
|
||||
|
||||
if(window.overrideDateInputFormat && window.overrideDateInputFormat !='') {
|
||||
$('input.localized_date', context).datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
} else {
|
||||
$('input.localized_date', context).datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
function initFriendlyDateChooser(id) {
|
||||
$('#' + id).datepicker({
|
||||
dateFormat: 'd M yy', constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
function initLocalizedDateChooser(id) {
|
||||
if(window.overrideDateInputFormat && window.overrideDateInputFormat !='') {
|
||||
$('#' + id).datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
} else {
|
||||
$('#' + id).datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function initTimeChoosers(context) {
|
||||
$('input.friendly_time', context).timepicker({
|
||||
timeFormat: 'g.ia'
|
||||
});
|
||||
$('input.localized_time', context).timepicker({
|
||||
timeFormat: 'H:i', maxTime: '23:59'
|
||||
function initTimeChooser(id) {
|
||||
$('#' + id).datetimepicker({
|
||||
datepicker: false,
|
||||
format: 'H:i'
|
||||
});
|
||||
}
|
||||
function initFriendlyTimeChooser(id) {
|
||||
$('#' + id).timepicker({
|
||||
timeFormat: 'g.ia'
|
||||
});
|
||||
}
|
||||
function initLocalizedTimeChooser(id) {
|
||||
$('#' + id).timepicker({
|
||||
timeFormat: 'H:i', maxTime: '23:59'
|
||||
|
||||
function initDateTimeChooser(id) {
|
||||
$('#' + id).datetimepicker({
|
||||
format: 'Y-m-d H:i'
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -309,8 +276,6 @@ function initCollapsibleBlocks(){
|
|||
}
|
||||
|
||||
$(function() {
|
||||
initDateChoosers();
|
||||
initTimeChoosers();
|
||||
initSlugAutoPopulate();
|
||||
initSlugCleaning();
|
||||
initErrorDetection();
|
||||
|
|
|
|||
|
|
@ -4,27 +4,16 @@
|
|||
{% block titletag %}{% blocktrans with form_title=form_page.title|capfirst %}Submissions of {{ form_title }}{% endblocktrans %}{% endblock %}
|
||||
{% block bodyclass %}menu-snippets{% endblock %}
|
||||
{% block extra_js %}
|
||||
{% get_localized_datepicker_js %}
|
||||
{% get_date_format_override as format_override %}
|
||||
|
||||
<script>
|
||||
window.overrideDateInputFormat ='{{ format_override }}';
|
||||
$(function() {
|
||||
if(window.overrideDateInputFormat && window.overrideDateInputFormat !='') {
|
||||
$('#id_date_from').datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
$('#id_date_to').datepicker({
|
||||
dateFormat: window.overrideDateInputFormat, constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
} else {
|
||||
$('#id_date_from').datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
$('#id_date_to').datepicker({
|
||||
constrainInput: false, /* showOn: 'button', */ firstDay: 1
|
||||
});
|
||||
}
|
||||
$('#id_date_from').datetimepicker({
|
||||
timepicker: false,
|
||||
format: 'Y-m-d'
|
||||
});
|
||||
$('#id_date_to').datetimepicker({
|
||||
timepicker: false,
|
||||
format: 'Y-m-d'
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in a new issue