Skip to content

Commit

Permalink
Setup Flatpickr locale from ui persistence data
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 1, 2021
1 parent d4be0d5 commit df0315c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 63 deletions.
18 changes: 0 additions & 18 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,6 @@ parameters:
-
path: 'src/Form.php'
message: '~^Call to an undefined method Atk4\\Ui\\JsChain::preventFormLeave\(\)\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Access to an undefined property Atk4\\Ui\\JsChain::\$l10ns\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Call to an undefined method Atk4\\Ui\\JsChain::localize\(\)\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Access to an undefined property Atk4\\Ui\\Jquery::\$_flatpickr\.$~'
Expand Down Expand Up @@ -1207,18 +1201,6 @@ parameters:
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::setOption\(\) has parameter \$value with no type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::setFirstDayOfWeek\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::requireLocale\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::setLocale\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::setDayOfWeek\(\) has no return type specified\.$~'
-
path: 'src/Form/Control/Calendar.php'
message: '~^Method Atk4\\Ui\\Form\\Control\\Calendar::onChange\(\) has no return type specified\.$~'
Expand Down
4 changes: 4 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ public function initIncludes()
// flatpickr
$this->requireJs($this->cdn['flatpickr'] . '/flatpickr.min.js');
$this->requireCss($this->cdn['flatpickr'] . '/flatpickr.min.css');
if ($this->ui_persistence->locale !== 'en') {
$this->requireJs($this->cdn['flatpickr'] . '/l10n/' . $this->ui_persistence->locale . '.js');
$this->html->js(true, new JsExpression('flatpickr.localize(window.flatpickr.l10ns.' . $this->ui_persistence->locale . ')'));
}

// Agile UI
$this->requireJs($this->cdn['atk'] . '/atkjs-ui.min.js');
Expand Down
62 changes: 17 additions & 45 deletions src/Form/Control/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Atk4\Ui\Form\Control;

use Atk4\Ui\App;
use Atk4\Ui\Jquery;
use Atk4\Ui\JsChain;
use Atk4\Ui\JsExpression;

/**
Expand All @@ -33,49 +31,12 @@ public function setOption($name, $value)
$this->options[$name] = $value;
}

/**
* Set first day of week globally.
*/
public static function setFirstDayOfWeek(App $app, int $day)
{
$app->html->js(true, (new JsExpression('flatpickr.l10ns.default.firstDayOfWeek = [day]', ['day' => $day])));
}

/**
* Load flatpickr locale file.
* Pass it has an option when adding Calendar input.
* Form\Control\Calendar::requireLocale($app, 'fr');
* $form->getControl('date')->options['locale'] = 'fr';.
*/
public static function requireLocale(App $app, string $locale)
{
$app->requireJs($app->cdn['flatpickr'] . '/l10n/' . $locale . '.js');
}

/**
* Apply locale globally to all flatpickr instance.
*/
public static function setLocale(App $app, string $locale)
{
self::requireLocale($app, $locale);
$app->html->js(true, (new JsChain('flatpickr'))->localize((new JsChain('flatpickr'))->l10ns->{$locale}));
}

/**
* Set first day of week for calendar display.
* Applied globally to all flatpickr instance.
*/
public static function setDayOfWeek(App $app, int $day)
{
$app->html->js(true, (new JsExpression('flatpickr.l10ns.default.firstDayOfWeek = [day]', ['day' => $day])));
}

protected function init(): void
{
parent::init();

// get format from Persistence\Date.
$format = $this->translateFormat($this->getApp()->ui_persistence->{$this->type . '_format'});
// setup format
$format = $this->translateFormatToFlatpickr($this->getApp()->ui_persistence->{$this->type . '_format'});
$this->options['dateFormat'] = $format;

if ($this->type === 'datetime' || $this->type === 'time') {
Expand All @@ -89,6 +50,11 @@ protected function init(): void
// Allow edit if microseconds is set.
$this->options['allowInput'] ??= $this->allowMicroSecondsInput($this->options['altFormat'] ?? $this->options['dateFormat']);
}

// setup locale
$this->options['locale'] = [
'firstDayOfWeek' => $this->getApp()->ui_persistence->firstDayOfWeek,
];
}

protected function renderView(): void
Expand Down Expand Up @@ -142,12 +108,18 @@ public function getJsInstance(): JsExpression
return (new Jquery('#' . $this->id . '_input'))->get(0)->_flatpickr;
}

public function translateFormat(string $format): string
protected function translateFormatToFlatpickr(string $phpFormat): string
{
// translate from php to flatpickr.
$format = preg_replace(['~[aA]~', '~[s]~', '~[g]~'], ['K', 'S', 'G'], $format);
$res = $phpFormat;
foreach ([
'~[aA]~' => 'K',
'~[s]~' => 'S',
'~[g]~' => 'G',
] as $k => $v) {
$res = preg_replace($k, $v, $res);
}

return $format;
return $res;
}

public function use24hrTimeFormat(string $format): bool
Expand Down
3 changes: 3 additions & 0 deletions src/Persistence/Ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
class Ui extends Persistence
{
/** @var string */
public $locale = 'en';

/** @var string */
public $currency = '€';
/** @var int Default decimal count for 'atk4_money' type. */
Expand Down

0 comments on commit df0315c

Please sign in to comment.