|
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
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* MaterializeWidgetTrait is the trait, which provides basic for all Materialize widgets features. |
|
14
|
|
|
* |
|
15
|
|
|
* Note: class, which uses this trait must declare public field named `options` with the array default value: |
|
16
|
|
|
* |
|
17
|
|
|
* ```php |
|
18
|
|
|
* class MyWidget extends \yii\base\Widget |
|
19
|
|
|
* { |
|
20
|
|
|
* use MaterializeWidgetTrait; |
|
21
|
|
|
* |
|
22
|
|
|
* public $options = []; |
|
23
|
|
|
* } |
|
24
|
|
|
* ``` |
|
25
|
|
|
* |
|
26
|
|
|
* This field is not present in the trait in order to avoid possible PHP Fatal error on definition conflict. |
|
27
|
|
|
* |
|
28
|
|
|
* @package widgets |
|
29
|
|
|
*/ |
|
30
|
|
|
trait MaterializeWidgetTrait |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array the options for the underlying Materialize JS plugin. |
|
34
|
|
|
* Please refer to the corresponding Materialize plugin Web page for possible options. |
|
35
|
|
|
* For example, [this page](https://materializecss.com/modals.html) shows |
|
36
|
|
|
* how to use the "Modal" plugin and the supported options. |
|
37
|
|
|
* |
|
38
|
|
|
* @see http://materializecss.com/ |
|
39
|
|
|
*/ |
|
40
|
|
|
public $clientOptions = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array the event handlers for the underlying Materialize JS plugin. |
|
44
|
|
|
* Please refer to the corresponding Materialize plugin Web page for possible events. |
|
45
|
|
|
* For example, [this page](https://materializecss.com/modals.html) shows |
|
46
|
|
|
* how to use the "Modal" plugin and the supported events. |
|
47
|
|
|
* |
|
48
|
|
|
* @see http://materializecss.com/ |
|
49
|
|
|
*/ |
|
50
|
|
|
public $clientEvents = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initializes the widget. |
|
54
|
|
|
* This method will register the Materialize asset bundle. If you override this method, |
|
55
|
|
|
* make sure you call the parent implementation first. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function init() |
|
58
|
|
|
{ |
|
59
|
|
|
parent::init(); |
|
60
|
|
|
|
|
61
|
|
|
if (!isset($this->options['id'])) { |
|
62
|
|
|
$this->options['id'] = $this->getId(); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Registers a specific Materialize plugin and the related events |
|
68
|
|
|
* @param string $name the name of the Materialize plugin |
|
69
|
|
|
* |
|
70
|
|
|
* @uses [[MaterializePluginAsset::register()]] |
|
71
|
|
|
* @uses [[registerClientEvents()]] |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function registerPlugin($name) |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$view = $this->getView(); |
|
76
|
|
|
MaterializePluginAsset::register($view); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->clientOptions !== false) { |
|
|
|
|
|
|
79
|
|
|
//... |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->registerClientEvents(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Registers JS event handlers that are listed in [[clientEvents]]. |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function registerClientEvents() |
|
89
|
|
|
{ |
|
90
|
|
|
if (!empty($this->clientEvents)) { |
|
91
|
|
|
//... |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \yii\web\View the view object that can be used to render views or view files. |
|
97
|
|
|
* @see \yii\base\Widget::getView() |
|
98
|
|
|
*/ |
|
99
|
|
|
abstract function getView(); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|