-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathListJs.php
More file actions
151 lines (126 loc) · 4.17 KB
/
Copy pathListJs.php
File metadata and controls
151 lines (126 loc) · 4.17 KB
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
147
148
149
150
151
<?php
/**
* @copyright Copyright © Saranga Abeykoon, nterms.com, 2014
* @package yii2-mediaelement-widget
* @version 1.0.0
*/
namespace nterms\listjs;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
use yii\base\Widget;
class ListJs extends Widget
{
/**
* @var array the HTML attributes (name-value pairs) for the container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array list.js options. Read [this](http://www.listjs.com/docs/options) for list of options.
*/
public $clientOptions = [];
/**
* @var string HTML content, preferably a list or table.
* If the widget is used in content capturing mode this will be ignored.
*/
public $content = '';
/**
* @var string name of the view file to render the content.
* If the widget is used in content capturing mode or a string is assigned to [[content]] property this will be ignored.
*/
public $view;
/**
* @var boolean whether to show the search field
*/
public $search;
/**
* @var array HTML attributes (name-value pairs) for the search input tag.
*/
public $searchOptions = [];
/**
* @var array list of name-value pairs for rendering the sorting buttons list.
* Value being the HTML attributes for the button. Special parameter `label` is used
* as the button text
* ```
* ...
* 'sort' => [
* 'name' => [
* 'class' => 'sort',
* 'label' => Yii::t('app', 'Sort by name'),
* ],
* ],
* ...
* ```
*/
public $sort = [];
/**
* @var string name of the view file to render the content.
* If the widget is used in content capturing mode or a string is assigned to [[content]] property this will be ignored.
*/
public $layout = "{search}\n{sort}\n{content}";
/**
* @var array parameters to be passed to [[view]] when it is being rendered.
* This property is used only when [[view]] is rendered to generate the content of the widget.
*/
public $viewParams = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
if(!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if(!isset($this->clientOptions['id'])) {
$this->clientOptions['id'] = $this->options['id'];
}
if(empty($this->clientOptions['valueNames'])) {
throw new InvalidConfigException('The "valueNames" property of "clientOptions" should be set.');
}
ob_start();
}
/**
* Runs the widget.
*/
public function run()
{
$content = ob_get_clean();
$search = '';
$sort = [];
$view = $this->getView();
ListJsAsset::register($view);
$js = "var yii2lj" . ucfirst(strtolower(str_replace('-', '', $this->clientOptions['id']))) . " = new List('" . $this->clientOptions['id'] . "', " . json_encode($this->clientOptions) . ");";
$view->registerJs($js, $view::POS_READY);
if(empty($content)) {
if(empty($this->content)) {
$content = $this->content;
} elseif($this->view != null && is_string($this->view)) {
$content = $view->render($this->view, $this->viewParams);
}
}
if($this->search) {
$search = Html::tag('input', '', array_merge([
'class' => 'form-control search',
'placeholder' => 'Search',
], $this->searchOptions));
}
if(!empty($this->sort)) {
foreach($this->sort as $key => $field) {
if(is_string($field) && in_array($field, $this->clientOptions['valueNames'])) {
$sort[] = Html::button($field, ['class' => 'btn btn-default sort', 'data-sort' => $field]);
} elseif(is_array($field) && in_array($key, $this->clientOptions['valueNames'])) {
$label = isset($field['label']) ? $field['label'] : $key;
$sort[] = Html::button($label, array_merge([
'class' => 'btn btn-default sort',
'data-sort' => $key,
], $field));
}
}
}
$html = str_replace(['{search}', '{sort}', '{content}'], [$search, implode(' ', $sort), $content], $this->layout);
echo Html::tag('div', $html, $this->options);
}
}