-
Notifications
You must be signed in to change notification settings - Fork 5
/
Gridstack.php
executable file
·123 lines (115 loc) · 3.47 KB
/
Gridstack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @copyright Federico Nicolás Motta
* @author Federico Nicolás Motta <[email protected]>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @package yii2-gridstack
*/
namespace fedemotta\gridstack;
use yii\base\InvalidCallException;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Gridstack.js Yii2 widget
* @author Federico Nicolás Motta <[email protected]>
*/
class Gridstack extends Widget
{
/**
* @var array the HTML attributes for the widget main container tag.
*/
public $options = [];
/**
* @var string the main container tag
*/
public $tag = 'div';
/**
* @var array the options for the Gridstack widget.
*/
public $clientOptions = [];
/**
* @var the gridstack widgets that are currently active
*/
private $_widgets = [];
/**
* Generates a gridstack start tag.
* @param array $options
* @param string $tag
* @return string the generated tag.
* @see endContainer()
*/
public static function beginContainer($options=[], $tag='div',$subTag='ul')
{
return Html::beginTag($tag,$options);
}
/**
* Generates a gridstack end tag.
* @param string $tag
* @return string the generated tag.
* @see beginContainer()
*/
public static function endContainer($tag='div')
{
return Html::endTag($tag);
}
/**
* Generates a gridstack widget begin tag.
* This method will create a new gridstack widget and returns its opening tag.
* You should call [[endWidget()]] afterwards.
* @param array $options
* @param string $tag
* @return string the generated tag
* @see endWidget()
*/
public function beginWidget($options=[],$tag='div')
{
$widget = Html::beginTag($tag,$options);
$this->_widgets[] = $widget;
return $widget;
}
/**
* Generates a gridstack widget end tag.
* @param string $tag
* @return string the generated tag
* @see beginWidget()
*/
public function endWidget($tag='div')
{
$widget = array_pop($this->_widgets);
if (!is_null($widget)) {
return Html::endTag($tag);
} else {
throw new InvalidCallException('Mismatching endWidget() call.');
}
}
/**
* Initializes the widget.
* This method will register the bootstrap asset bundle. If you override this method,
* make sure you call the parent implementation first.
*/
public function init()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
echo self::beginContainer($this->options,$this->tag);
}
/**
* Runs the widget.
* This registers the necessary javascript code and renders the gridstack close tag.
* @throws InvalidCallException if `beginWidget()` and `endWidget()` calls are not matching
*/
public function run()
{
if (!empty($this->_widgets)) {
throw new InvalidCallException('Each beginWidget() should have a matching endWidget() call.');
}
$id = $this->options['id'];
$view = $this->getView();
$options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : Json::encode([]);
GridstackAsset::register($view);
$view->registerJs("jQuery('#$id').gridstack($options);");
echo self::endContainer($this->tag);
}
}