Skip to content

Commit f251fb0

Browse files
author
Christoph Erdmann
committed
Merge remote-tracking branch 'origin/develop'
2 parents 289dc43 + 8182a8a commit f251fb0

File tree

5 files changed

+244
-2
lines changed

5 files changed

+244
-2
lines changed

README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ The following widgets are currently available:
6565
* SwitchButton
6666
* GridView with ActionColumn
6767
* DetailView
68+
* Fixed Action Button
69+
* DatePicker
6870

6971
These widgets are planned for development:
7072

7173
* Collection
72-
* Pagination
7374
* Modal
7475
* Toast
7576
* Collapsible
@@ -96,3 +97,30 @@ $config['modules']['gii'] = [
9697
```
9798

9899
You can copy those templates to any location you wish for further customization. Make sure you adapt the path accordingly in your config.
100+
101+
## Known issues
102+
103+
Currently there is an issue with jQuery version 2.2.x and the datepicker pickadate.js.
104+
Please check out the issues at https://github.com/Dogfalo/materialize/issues/2808#issuecomment-191642171.
105+
106+
To circumvent problems with the datepicker, use jQuery version 2.1.4 until further notice.
107+
You can implement this in your asset bundle config:
108+
109+
```php
110+
// @app/config/main.php
111+
112+
'components' => [
113+
// more components
114+
'assetManager' => [
115+
'bundles' => [
116+
'yii\web\JqueryAsset' => [
117+
'sourcePath' => null,
118+
'js' => [
119+
'//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js',
120+
]
121+
],
122+
],
123+
],
124+
// more components
125+
],
126+
```

src/assets/MaterializeAsset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ class MaterializeAsset extends AssetBundle
1717
];
1818

1919
public $depends = [
20-
'macgyer\yii2materializecss\assets\MaterializeFontAsset'
20+
'macgyer\yii2materializecss\assets\MaterializeFontAsset',
2121
];
2222
}

src/assets/MaterializePluginAsset.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ class MaterializePluginAsset extends AssetBundle
1515
public $js = [
1616
'js/materialize.min.js'
1717
];
18+
19+
public $depends = [
20+
'yii\web\JqueryAsset'
21+
];
1822
}

src/widgets/FixedActionButton.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
namespace macgyer\yii2materializecss\widgets;
4+
5+
use macgyer\yii2materializecss\lib\BaseWidget;
6+
use macgyer\yii2materializecss\lib\Html;
7+
use yii\base\InvalidConfigException;
8+
use yii\helpers\ArrayHelper;
9+
10+
/**
11+
* Class FixedActionButton
12+
* @package macgyer\yii2materializecss\widgets
13+
*/
14+
class FixedActionButton extends BaseWidget
15+
{
16+
/**
17+
* @var array list of button items in the fixed action button. Each element can be either an HTML string
18+
* or an array representing a single item with the following specification:
19+
*
20+
* - label: string, required, the label of the item link
21+
* - url: string|array, optional, the url of the item link. This will be processed by [[Url::to()]].
22+
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
23+
* - linkOptions: array, optional, the HTML attributes of the item link.
24+
* - options: array, optional, the HTML attributes of the item.
25+
**/
26+
public $items;
27+
28+
/**
29+
* @var boolean whether the labels for header items should be HTML-encoded.
30+
*/
31+
public $encodeLabels = true;
32+
33+
/**
34+
* @var array the HTML attributes for the widget container tag
35+
*
36+
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
37+
*/
38+
public $options = ['class' => 'fixed-action-btn'];
39+
40+
/**
41+
* @var array the HTML attributes for the container around the button items
42+
*
43+
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
44+
*/
45+
public $itemsContainerOptions = [];
46+
47+
/**
48+
* @var bool whether the button items are only visible after click
49+
*/
50+
public $clickToToggle = false;
51+
52+
/**
53+
* @var bool whether to display a horizontal FAB
54+
*/
55+
public $horizontal = false;
56+
57+
/**
58+
* @var string the tag used to render the button
59+
*
60+
* Should be a for correct functionality of click to toggle FAB
61+
*/
62+
public $buttonTagName = 'a';
63+
64+
/**
65+
* @var string the label on the button
66+
*/
67+
public $buttonLabel = 'Button';
68+
69+
/**
70+
* @var bool whether the label should be HTML-encoded.
71+
*/
72+
public $buttonEncodeLabel = true;
73+
74+
/**
75+
* @var array the options for the optional icon
76+
*
77+
* To specify an icon you can use the following parameters
78+
*
79+
* ```php
80+
* [
81+
* 'name' => 'name of the icon', // required
82+
* 'position' => 'position of the icon', // optional, 'left' or 'right', defaults to 'left'
83+
* 'options' => 'the HTML attributes for the img', // optional
84+
* ]
85+
* ```
86+
*/
87+
public $buttonIcon;
88+
89+
/**
90+
* @var array the HTML attributes for the visible button
91+
*
92+
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
93+
*/
94+
public $buttonOptions = [];
95+
96+
/**
97+
* Initialize the widget.
98+
*/
99+
public function init()
100+
{
101+
parent::init();
102+
103+
Html::addCssClass($this->buttonOptions, ['widget' => 'btn-floating']);
104+
105+
if ($this->clickToToggle) {
106+
Html::addCssClass($this->options, ['container' => 'click-to-toggle']);
107+
}
108+
109+
if ($this->horizontal) {
110+
Html::addCssClass($this->options, ['containerLayout' => 'horizontal']);
111+
}
112+
}
113+
114+
/**
115+
* Executes the widget.
116+
* @return string the result of widget execution to be outputted.
117+
*/
118+
public function run()
119+
{
120+
$html = Html::beginTag('div', $this->options);
121+
122+
// Visible button
123+
$html .= Button::widget([
124+
'tagName' => $this->buttonTagName,
125+
'label' => $this->buttonLabel,
126+
'encodeLabel' => $this->buttonEncodeLabel,
127+
'options' => $this->buttonOptions,
128+
'icon' => $this->buttonIcon
129+
]);
130+
131+
$html .= $this->renderItems();
132+
$html .= Html::endTag('div');
133+
134+
return $html;
135+
}
136+
137+
/**
138+
* @return string
139+
* @throws InvalidConfigException
140+
*/
141+
protected function renderItems()
142+
{
143+
$elements = [];
144+
$items = $this->items;
145+
146+
foreach ($items as $item) {
147+
if (isset($item['visible']) && !$item['visible']) {
148+
continue;
149+
}
150+
151+
if (is_string($item)) {
152+
$elements[] = $item;
153+
continue;
154+
}
155+
156+
if (!array_key_exists('label', $item)) {
157+
throw new InvalidConfigException("The 'label' option is required.");
158+
}
159+
160+
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
161+
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
162+
163+
$itemOptions = ArrayHelper::getValue($item, 'options', []);
164+
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
165+
166+
$url = array_key_exists('url', $item) ? $item['url'] : null;
167+
if ($url === null) {
168+
$content = $label;
169+
} else {
170+
$content = Html::a($label, $url, $linkOptions);
171+
}
172+
173+
$elements[] = Html::tag('li', $content, $itemOptions);
174+
}
175+
176+
return Html::tag('ul', implode("\n", $elements), $this->itemsContainerOptions);
177+
}
178+
}

src/widgets/form/DatePicker.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace macgyer\yii2materializecss\widgets\form;
4+
5+
use macgyer\yii2materializecss\lib\BaseInputWidget;
6+
use macgyer\yii2materializecss\lib\Html;
7+
8+
/**
9+
* Class DatePicker
10+
* @package macgyer\yii2materializecss\widgets\form
11+
*/
12+
class DatePicker extends BaseInputWidget
13+
{
14+
/**
15+
* Executes the widget.
16+
* @return string the result of widget execution to be outputted.
17+
*/
18+
public function run()
19+
{
20+
$this->registerPlugin('pickadate');
21+
22+
Html::addCssClass($this->options, 'datepicker');
23+
24+
if ($this->hasModel()) {
25+
$this->options['data-value'] = isset($this->value) ? $this->value : Html::getAttributeValue($this->model, $this->attribute);
26+
return Html::activeInput('date', $this->model, $this->attribute, $this->options);
27+
} else {
28+
$this->options['data-value'] = $this->value;
29+
return Html::input('date', $this->name, $this->value, $this->options);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)