Skip to content

Commit

Permalink
ENHANCEMENT Passing 'timeformat' and other metadata to markup generat…
Browse files Browse the repository at this point in the history
…ed in DateField, TimeField and DatetimeField, to allow for easier integration with third party libraries
  • Loading branch information
chillu committed Oct 7, 2011
1 parent b807983 commit 7c147fe
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
52 changes: 17 additions & 35 deletions forms/DateField.php
Expand Up @@ -55,17 +55,6 @@
* @subpackage fields-datetime
*/
class DateField extends TextField {

/**
* @var array
*/
protected static $default_config = array(
'showcalendar' => false,
'dmyfields' => false,
'dmyseparator' => false,
'dateformat' => false,
'locale' => false
);

/**
* @var array
Expand Down Expand Up @@ -132,6 +121,23 @@ function FieldHolder() {
}

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
Expand Down Expand Up @@ -576,31 +582,7 @@ function getField() {
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)));
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions forms/DatetimeField.php
Expand Up @@ -72,6 +72,16 @@ function setForm($form) {
$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');

Expand Down
9 changes: 9 additions & 0 deletions forms/TimeField.php
Expand Up @@ -63,6 +63,15 @@ function __construct($name, $title = null, $value = ""){
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.
*
Expand Down
7 changes: 5 additions & 2 deletions javascript/DateField.js
Expand Up @@ -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');
});
Expand Down

0 comments on commit 7c147fe

Please sign in to comment.