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