Skip to content

Commit

Permalink
fix loading locale
Browse files Browse the repository at this point in the history
+ fix syntax
  • Loading branch information
Beaten-Sect0r committed Jun 4, 2017
1 parent 3d07118 commit 371296b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use bs\Flatpickr\Widget as Flatpickr;

<?= $form->field($model, 'published_at')->widget(Flatpickr::className(), [
'locale' => strtolower(substr(Yii::$app->language, 0, 2)),
'groupBtnShow' => true,
'options' => [
'class' => 'form-control',
],
Expand Down
54 changes: 32 additions & 22 deletions src/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,28 @@ class Widget extends InputWidget

/**
* Disable input
*
* @var bool
*/
public $disabled = false;

/**
* Show group buttons
*
* @var bool
*/
public $groupBtnShow = false;

/**
* Buttons template
*
* @var string
*/
public $groupBtnTemplate = '{toggle}{clear}';
public $groupBtnTemplate = '{toggle} {clear}';

/**
* Buttons
*
* @var array
*/
public $groupBtn = [
Expand Down Expand Up @@ -79,99 +83,105 @@ public function init()
*/
public function run()
{
if ($this->groupBtnShow)
if ($this->groupBtnShow) {
$this->clientOptions['wrap'] = true;
else
} else {
$this->clientOptions['wrap'] = false;
}

$this->registerClientScript();
$content = '';
$options['data-input'] = '';
if ($this->disabled)
if ($this->disabled) {
$options['disabled'] = 'disabled';
}

if ($this->groupBtnShow) {
$content .= '<div class="flatpickr-' . $this->options['id'] . ' input-group">';

if ($this->hasModel()) {
$content .= Html::activeTextInput($this->model, $this->attribute, array_merge($this->options, $options));
$content .= Html::activeTextInput($this->model, $this->attribute, ArrayHelper::merge($this->options, $options));
} else {
$content .= Html::textInput($this->name, $this->value, array_merge($this->options, $options));
$content .= Html::textInput($this->name, $this->value, ArrayHelper::merge($this->options, $options));
}

$content .= '<div class="input-group-btn">';
if (preg_match_all('/{(toggle|clear)}/i', $this->groupBtnTemplate, $matches)) {
foreach ($matches[1] as $btnName)
foreach ($matches[1] as $btnName) {
$content .= $this->renderGroupBtn($btnName);
}
}
$content .= '</div>';
$content .= '</div>';
} else {
if ($this->hasModel()) {
$content = Html::activeTextInput($this->model, $this->attribute, array_merge($this->options, $options));
$content = Html::activeTextInput($this->model, $this->attribute, ArrayHelper::merge($this->options, $options));
} else {
$content = Html::textInput($this->name, $this->value, array_merge($this->options, $options));
$content = Html::textInput($this->name, $this->value, ArrayHelper::merge($this->options, $options));
}
}

return $content;
}

/**
/**
* Register widget client scripts.
*/
protected function registerClientScript()
{
$view = $this->getView();

if ($this->groupBtnShow)
if ($this->groupBtnShow) {
$selector = Json::encode('.flatpickr-' . $this->options['id']);
else
} else {
$selector = Json::encode('#' . $this->options['id']);
}

$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';

FlatpickrAsset::register($view);
if (!empty($this->theme)) {
FlatpickrAsset::register($view)->css[] = 'themes/' . $this->theme . '.css';
}
if (!empty($this->locale)) {
if ($this->locale !== null && $this->locale !== 'en') {
FlatpickrAsset::register($view)->js[] = 'l10n/' . $this->locale . '.js';
}

$view->registerJs("flatpickr($selector, {$options});");
}



/**

/**
* @param string $btnName
* @return string
*/
private function renderGroupBtn($btnName)
{
$content = '';
if (isset($this->groupBtn[$btnName])) {
if (isset($this->groupBtn[$btnName]['btnClass']))
if (isset($this->groupBtn[$btnName]['btnClass'])) {
$btnClass = $this->groupBtn[$btnName]['btnClass'];
else
} else {
$btnClass = 'btn btn-default';
}

if (isset($this->groupBtn[$btnName]['iconClass']))
if (isset($this->groupBtn[$btnName]['iconClass'])) {
$iconClass = $this->groupBtn[$btnName]['iconClass'];
else
} else {
$iconClass = '';
}

$disabled = '';
if ($this->disabled)
if ($this->disabled) {
$disabled = 'disabled="disabled"';
}

$content = <<<HTML
<button class="$btnClass" type="button" $disabled data-$btnName>
<span class="$iconClass"></span>
</button>
HTML;
}

return $content;
}
}

0 comments on commit 371296b

Please sign in to comment.