Skip to content

Commit

Permalink
API HTML5 DateField, remove member prefs (fixes silverstripe#6626)
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Mar 30, 2017
1 parent 09cdec8 commit 0518364
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 626 deletions.
130 changes: 27 additions & 103 deletions src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,25 @@ class DateField extends TextField
protected $rawValue = null;

/**
* Check if calendar should be shown on the frontend
*
* @var bool
*/
protected $useHTML5 = false;

/**
* @return bool
*/
public function getShowCalendar()
public function getUseHTML5()
{
return $this->showCalendar;
return $this->useHTML5;
}

/**
* Set if calendar should be shown on the frontend.
* @internal WARNING: Experimental and volatile API.
*
* @param bool $show
* @param boolean $bool
* @return $this
*/
public function setShowCalendar($show)
public function setUseHTML5($bool)
{
$this->showCalendar = $show;
$this->useHTML5 = $bool;
return $this;
}

Expand Down Expand Up @@ -220,7 +220,10 @@ protected function getFormatter()
);

// Don't invoke getDateFormat() directly to avoid infinite loop
if ($this->dateFormat) {
if ($this->getUseHTML5()) {
// Browsers expect ISO 8601 dates, localisation is handled on the client
$formatter->setPattern('y-MM-dd');
} else if ($this->dateFormat) {
$ok = $formatter->setPattern($this->dateFormat);
if (!$ok) {
throw new InvalidArgumentException("Invalid date format {$this->dateFormat}");
Expand All @@ -236,59 +239,39 @@ protected function getFormatter()
*/
protected function getISO8601Formatter()
{
$locale = i18n::config()->uninherited('default_locale');
$formatter = IntlDateFormatter::create(
i18n::config()->uninherited('default_locale'),
IntlDateFormatter::MEDIUM,
IntlDateFormatter::NONE
);
$formatter->setLenient(false);
// CLDR iso8601 date.
// CLDR ISO 8601 date.
$formatter->setPattern('y-MM-dd');
return $formatter;
}

public function FieldHolder($properties = array())
{
return $this->renderWithClientView(function () use ($properties) {
return parent::FieldHolder($properties);
});
}

public function SmallFieldHolder($properties = array())
{
return $this->renderWithClientView(function () use ($properties) {
return parent::SmallFieldHolder($properties);
});
}
if ($this->getUseHTML5()) {
$this->setAttribute('type', 'date');

/**
* Generate field with client view enabled
*
* @param callable $callback
* @return string
*/
protected function renderWithClientView($callback)
{
$clientView = null;
if ($this->getShowCalendar()) {
$clientView = $this->getClientView();
$clientView->onBeforeRender();
// Browsers expect ISO 8601 dates, localisation is handled on the client
$this->setDateFormat('y-MM-dd');
}
$html = $callback();
if ($clientView) {
$html = $clientView->onAfterRender($html);
}
return $html;

return parent::FieldHolder($properties);
}

public function getAttributes()
{
$attributes = parent::getAttributes();

// Merge with client config
$config = $this->getClientConfig();
foreach ($config as $key => $value) {
$attributes["data-{$key}"] = $value;
$attributes['lang'] = i18n::convert_rfc1766($this->getLocale());

if ($this->getUseHTML5()) {
$attributes['min'] = $this->getMinDate();
$attributes['max'] = $this->getMaxDate();
}

return $attributes;
Expand Down Expand Up @@ -438,29 +421,6 @@ public function setLocale($locale)
return $this;
}

/**
* Get locale code for client-side. Will default to getLocale() if omitted.
*
* @return string
*/
public function getClientLocale()
{
if ($this->clientLocale) {
return $this->clientLocale;
}
return $this->getLocale();
}

/**
* @param string $clientLocale
* @return DateField
*/
public function setClientLocale($clientLocale)
{
$this->clientLocale = $clientLocale;
return $this;
}

public function getSchemaValidation()
{
$rules = parent::getSchemaValidation();
Expand Down Expand Up @@ -504,35 +464,6 @@ public function setMaxDate($maxDate)
return $this;
}

/**
* Get client data properties for this field
*
* @return array
*/
public function getClientConfig()
{
$view = $this->getClientView();
$config = [
'showcalendar' => $this->getShowCalendar() ? 'true' : null,
'date-format' => $view->getDateFormat(), // https://api.jqueryui.com/datepicker/#option-dateFormat
'locale' => $view->getLocale(),
];

// Format min/maxDate in format expected by jquery datepicker
$min = $this->getMinDate();
if ($min) {
// https://api.jqueryui.com/datepicker/#option-minDate
$config['min-date'] = $this->iso8601ToLocalised($min);
}
$max = $this->getMaxDate();
if ($max) {
// https://api.jqueryui.com/datepicker/#option-maxDate
$config['max-date'] = $this->iso8601ToLocalised($max);
}

return $config;
}

/**
* Convert date localised in the current locale to ISO 8601 date
*
Expand Down Expand Up @@ -599,11 +530,4 @@ public function tidyISO8601($date)
return $formatter->format($timestamp);
}

/**
* @return DateField_View_JQuery
*/
protected function getClientView()
{
return DateField_View_JQuery::create($this);
}
}
197 changes: 0 additions & 197 deletions src/Forms/DateField_View_JQuery.php

This file was deleted.

Loading

0 comments on commit 0518364

Please sign in to comment.