1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/DMGPage/yii2-materialize |
4
|
|
|
* @copyright Copyright (c) 2018 Dmitrijs Reinmanis |
5
|
|
|
* @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dmgpage\yii2materialize\widgets; |
9
|
|
|
|
10
|
|
|
use dmgpage\yii2materialize\assets\MaterializePluginAsset; |
11
|
|
|
use dmgpage\yii2materialize\helpers\Html; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
use yii\base\InvalidConfigException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* MaterializeWidgetTrait is the trait, which provides basic for all Materialize widgets features. |
17
|
|
|
* |
18
|
|
|
* Note: class, which uses this trait must declare public field named `options` with the array default value: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* class MyWidget extends \yii\base\Widget |
22
|
|
|
* { |
23
|
|
|
* use MaterializeWidgetTrait; |
24
|
|
|
* |
25
|
|
|
* public $options = []; |
26
|
|
|
* } |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict. |
30
|
|
|
* |
31
|
|
|
* @package widgets |
32
|
|
|
*/ |
33
|
|
|
trait MaterializeWidgetTrait |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array the options for the underlying Materialize JS plugin. |
37
|
|
|
* Please refer to the corresponding Materialize plugin Web page for possible options. |
38
|
|
|
* For example, [this page](https://materializecss.com/modals.html) shows |
39
|
|
|
* how to use the "Modal" plugin and the supported options. |
40
|
|
|
* |
41
|
|
|
* @see http://materializecss.com/ |
42
|
|
|
*/ |
43
|
|
|
public $clientOptions = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array the event handlers for the underlying Materialize JS plugin. |
47
|
|
|
* Please refer to the corresponding Materialize plugin Web page for possible events. |
48
|
|
|
* For example, [this page](https://materializecss.com/modals.html) shows |
49
|
|
|
* how to use the "Modal" plugin and the supported events. |
50
|
|
|
* |
51
|
|
|
* @see http://materializecss.com/ |
52
|
|
|
*/ |
53
|
|
|
public $clientEvents = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initializes the widget. |
57
|
|
|
* This method will register the Materialize asset bundle. If you override this method, |
58
|
|
|
* make sure you call the parent implementation first. |
59
|
|
|
*/ |
60
|
|
|
public function init() |
61
|
|
|
{ |
62
|
|
|
parent::init(); |
63
|
|
|
|
64
|
|
|
if (!isset($this->options['id'])) { |
65
|
|
|
$this->options['id'] = $this->getId(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Registers a specific Materialize plugin and the related events |
71
|
|
|
* @param string $name the name of the Materialize plugin |
72
|
|
|
* |
73
|
|
|
* @uses [[MaterializePluginAsset::register()]] |
74
|
|
|
* @uses [[registerClientEvents()]] |
75
|
|
|
*/ |
76
|
|
|
protected function registerPlugin($name) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$view = $this->getView(); |
79
|
|
|
MaterializePluginAsset::register($view); |
80
|
|
|
|
81
|
|
|
if (!empty($this->clientOptions)) { |
82
|
|
|
//... |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->registerClientEvents(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Registers JS event handlers that are listed in [[clientEvents]]. |
90
|
|
|
*/ |
91
|
|
|
protected function registerClientEvents() |
92
|
|
|
{ |
93
|
|
|
if (!empty($this->clientEvents)) { |
94
|
|
|
//... |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Renders an icon. |
100
|
|
|
* |
101
|
|
|
* @param string|array $icon the options for the optional icon. |
102
|
|
|
* @return string the rendered icon |
103
|
|
|
* @throws InvalidConfigException if icon name is not specified |
104
|
|
|
* |
105
|
|
|
* @uses ArrayHelper::getValue |
106
|
|
|
* @see Html::icon |
107
|
|
|
*/ |
108
|
|
|
protected function renderIcon($icon) |
109
|
|
|
{ |
110
|
|
|
$html = ''; |
111
|
|
|
|
112
|
|
|
if (!empty($icon)) { |
113
|
|
|
if (is_array($icon) && isset($icon['name'])) { |
114
|
|
|
$iconName = ArrayHelper::getValue($icon, 'name', null); |
115
|
|
|
} elseif (is_string($icon)) { |
116
|
|
|
$iconName = $icon; |
117
|
|
|
} else { |
118
|
|
|
throw new InvalidConfigException('The icon name must be specified.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$iconOptions = ArrayHelper::getValue($icon, 'options', []); |
122
|
|
|
$html = Html::icon($iconName, $iconOptions); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $html; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the ID of the widget. |
130
|
|
|
* |
131
|
|
|
* @param bool $autoGenerate whether to generate an ID if it is not set previously |
132
|
|
|
* @return string ID of the widget. |
133
|
|
|
* @see \yii\base\Widget::getId() |
134
|
|
|
*/ |
135
|
|
|
abstract public function getId($autoGenerate = true); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return \yii\web\View the view object that can be used to render views or view files. |
139
|
|
|
* @see \yii\base\Widget::getView() |
140
|
|
|
*/ |
141
|
|
|
abstract public function getView(); |
142
|
|
|
} |
143
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.