-
Notifications
You must be signed in to change notification settings - Fork 2
/
MasonryView.php
146 lines (121 loc) · 4.17 KB
/
MasonryView.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* @copyright Copyright (c); nerburish, 2016
* @package yii2-masonry-view
*/
namespace nerburish\masonryview;
use yii\widgets\ListView;
use nerburish\masonry\MasonryAsset;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* ListView widget improved to use Masonry (http://masonry.desandro.com/)
*
* @package nerburish\masonry
*/
class MasonryView extends ListView
{
const DEFAULT_GRID_CLASS = 'grid';
const DEFAULT_ITEM_CLASS = 'grid-item';
/**
* @var string the jquery selector where will be initialized the masonry plugin
*/
public $gridSelector;
/**
* @var array the HTML attributes (name-value pairs) for the field container tag.
* The values will be HTML-encoded using [[Html::encode()]].
* If a value is null, the corresponding attribute will not be rendered.
*/
public $options = [];
/**
* @var array the HTML attributes (name-value pairs) for the grid container tag.
* The values will be HTML-encoded using [[Html::encode()]].
*/
public $gridOptions = [];
/**
* @var array parameters accepted by masonary plugin, see http://masonry.desandro.com/
*/
public $clientOptions = [];
/**
* @var array use it for inject a custom css style. Array accepts the same parameters as \yii\base\View::registerCssFile()
* @see http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerCssFile()-detail
*/
public $cssFile = [];
/**
* @var string the layout that determines how different sections of the list view should be organized.
* The following tokens will be replaced with the corresponding section contents:
*
* - `{summary}`: the summary section. See [[renderSummary()]].
* - `{items}`: the list items. See [[renderItems()]].
* - `{sorter}`: the sorter. See [[renderSorter()]].
* - `{pager}`: the pager. See [[renderPager()]].
*/
public $layout;
/**
* Initializes the widget.
* 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();
}
if (isset($this->itemOptions['class'])) {
$this->clientOptions['itemSelector'] = '.' . $this->itemOptions['class'];
} else {
$this->itemOptions['class'] = self::DEFAULT_ITEM_CLASS;
$this->clientOptions['itemSelector'] = '.' . self::DEFAULT_ITEM_CLASS;
}
if (empty($this->layout)) {
if (empty($this->gridOptions)) {
$this->gridOptions = [
'class' => 'grid',
];
}
$tag = ArrayHelper::remove($this->gridOptions, 'tag', 'div');
$gridContainer = Html::tag($tag, '{items}', $this->gridOptions);
$this->layout = "{summary}$gridContainer\n{pager}";
}
if (empty($this->gridSelector)) {
$this->gridSelector = '#'. $this->id . ' .' . self::DEFAULT_GRID_CLASS;
}
parent::init();
}
/**
* Runs the widget.
*/
public function run()
{
if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
$content = preg_replace_callback("/{\\w+}/", function ($matches) {
$content = $this->renderSection($matches[0]);
return $content === false ? $matches[0] : $content;
}, $this->layout);
} else {
$content = $this->renderEmpty();
}
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
echo Html::tag($tag, $content, $options);
$this->registerWidget();
}
/**
* Register assets and initialize the widget
*/
protected function registerWidget()
{
$id = $this->options['id'];
$gridSelector = $this->gridSelector;
$view = $this->getView();
MasonryAsset::register($view);
$js = [];
$options = Json::encode($this->clientOptions);
$js[] = "var mscontainer$id = $('$gridSelector');";
$js[] = "var masonry$id = mscontainer$id.masonry($options);";
$view->registerJs(implode("\n", $js),$view::POS_READY);
if (!empty($this->cssFile)) {
call_user_func_array([$view, "registerCssFile"], $this->cssFile);
}
}
}