ENHANCEMENT Passing 'timeformat' and other metadata to markup generated in DateField, TimeField and DatetimeField, to allow for easier integration with third party libraries

This commit is contained in:
Ingo Schommer 2011-09-26 15:01:26 +02:00
parent b807983d09
commit 7c147fed14
4 changed files with 41 additions and 37 deletions

View File

@ -56,17 +56,6 @@ require_once 'Zend/Date.php';
*/
class DateField extends TextField {
/**
* @var array
*/
protected static $default_config = array(
'showcalendar' => false,
'dmyfields' => false,
'dmyseparator' => false,
'dateformat' => false,
'locale' => false
);
/**
* @var array
*/
@ -132,6 +121,23 @@ class DateField extends TextField {
}
function Field() {
$config = array(
'showcalendar' => $this->getConfig('showcalendar'),
'isoDateformat' => $this->getConfig('dateformat'),
'jqueryDateformat' => DateField_View_JQuery::convert_iso_to_jquery_format($this->getConfig('dateformat')),
'min' => $this->getConfig('min'),
'max' => $this->getConfig('max')
);
// Add other jQuery UI specific, namespaced options (only serializable, no callbacks etc.)
// TODO Move to DateField_View_jQuery once we have a properly extensible HTML5 attribute system for FormField
foreach($this->getConfig() as $k => $v) {
if(preg_match('/^jQueryUI\.(.*)/', $k, $matches)) $config[$matches[1]] = $v;
}
$config = array_filter($config);
$this->addExtraClass(Convert::raw2json($config));
// Three separate fields for day, month and year
if($this->getConfig('dmyfields')) {
// values
@ -576,31 +582,7 @@ class DateField_View_JQuery {
return $this->field;
}
/**
*
*/
function onBeforeRender() {
if($this->getField()->getConfig('showcalendar')) {
// Inject configuration into existing HTML
$format = self::convert_iso_to_jquery_format($this->getField()->getConfig('dateformat'));
$conf = array(
'showcalendar' => true,
'dateFormat' => $format
);
// add min/max to datepicker. ISO format or strtotime() compatible only
foreach(array('min', 'max') as $type) {
if($this->getField()->getConfig($type)) {
$min = new Zend_Date(strtotime($this->getField()->getConfig($type)), null, $this->getField()->locale);
$conf[$type . 'Date'] = $min->toString($this->getField()->getConfig('dateformat'));
}
}
// Add other namespaced options (only serializable, no callbacks etc.)
foreach($this->getField()->getConfig() as $k => $v) {
if(preg_match('/^jQueryUI\.(.*)/', $k, $matches)) $conf[$matches[1]] = $v;
}
$this->getField()->addExtraClass(str_replace('"', '\'', Convert::raw2json($conf)));
}
}
/**

View File

@ -72,6 +72,16 @@ class DatetimeField extends FormField {
$this->timezoneField->setForm($form);
}
function FieldHolder() {
$config = array(
'datetimeorder' => $this->getConfig('datetimeorder'),
);
$config = array_filter($config);
$this->addExtraClass(Convert::raw2json($config));
return parent::FieldHolder();
}
function Field() {
Requirements::css(SAPPHIRE_DIR . '/css/DatetimeField.css');

View File

@ -63,6 +63,15 @@ class TimeField extends TextField {
parent::__construct($name,$title,$value);
}
function Field() {
$config = array(
'timeformat' => $this->getConfig('timeformat')
);
$config = array_filter($config);
$this->addExtraClass(Convert::raw2json($config));
return parent::Field();
}
/**
* Sets the internal value to ISO date format.
*

View File

@ -9,13 +9,16 @@
fields.siblings("button").addClass("ui-icon ui-icon-calendar");
fields.live('click', function() {
var holder = $(this).parents('.field.date:first'), config = holder.metadata({type: 'class'});
var holder = $(this).parents('.field.date:first'), config = $(this).metadata({type: 'class'});
if(!config.showcalendar) return;
if(config.locale && $.datepicker.regional[config.locale]) {
config = $.extend(config, $.datepicker.regional[config.locale], {});
}
// Initialize and open a datepicker
// live() doesn't have "onmatch", and jQuery.entwine is a bit too heavyweight for this, so we need to do this onclick.
config.dateFormat = config.jqueryDateformat;
$(this).datepicker(config);
$(this).datepicker('show');
});