-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSortable.php
135 lines (110 loc) · 3.5 KB
/
Sortable.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
<?php
/**
* @copyright Copyright © Oleg Martemjanov, 2017
* @package yii2-jquery-sortable
* @version 1.0
*/
namespace demogorgorn\jquerysortable;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
/**
* Create a flexible, opinionated sorting plugin for jQuery
*
* @see https://johnny.github.io/jquery-sortable/
* @author Oleg Martemjanov <[email protected]>
* @since 1.0
*/
class Sortable extends \yii\base\Widget
{
public $options = [];
public $clientOptions = [];
/**
* @var array the sortable items configuration for rendering elements within the sortable
* list / grid. You can set the following properties:
* - content: string, the list item content (this is not HTML encoded)
* - disabled: bool, whether the list item is disabled
* - options: array, the HTML attributes for the list item.
*/
public $items = [];
public $listTag = 'ul';
/**
* var bool | string If false drag handle is disabled.
* If you want to use drag handle just specify it's html code: e.g., <i class="fa fa-bars"></i>
* Please notice that you should set the handle param in clientOptions manually. E.g. '.fa-bars'
*/
public $useDragHandle = false;
/**
* @var bool create nested list items.
* By default Sortable support nested lists only in cases when these nested lists were defined when Sortable was initialized.
*/
public $autoNestedEnabled = false;
public $appendElement = false;
/**
* Initializes the widget
*/
public function init()
{
parent::init();
if (!isset($this->options['id']))
$this->options['id'] = $this->getId();
$this->registerAssets();
}
/**
* Runs the widget
*
* @return string|void
*/
public function run()
{
echo $this->createList($this->items, $this->options);
}
/**
* Render the list items for the sortable widget
*
* @return string
*/
public function createList($items, $options = []) {
$htmlTree = Html::beginTag($this->listTag, $options);
foreach ($items as $item) {
$htmlTree .= $this->renderItem($item);
}
$htmlTree .= Html::endTag($this->listTag);
return $htmlTree;
}
public function renderItem($item)
{
if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' option is required.");
}
$content = ArrayHelper::getValue($item, 'content', '');
$options = ArrayHelper::getValue($item, 'options', []);
$items = '';
if (isset($item['items']) ) {
$items = $this->createList($item['items']);
} else {
if ($this->autoNestedEnabled == true) {
$items = $this->createList([]);
}
}
if ($this->useDragHandle !== false) {
$content = $this->useDragHandle . $content;
}
if ($this->appendElement !== false) {
$content .= $this->appendElement;
}
return Html::tag('li', $content . $items, $options);
}
/**
* Register client assets
*/
public function registerAssets()
{
$view = $this->getView();
SortableAsset::register($view);
$id = $this->options['id'];
$clientOptions = Json::encode($this->clientOptions, JSON_FORCE_OBJECT);
$js = '$("#' . $id . '").sortable(' . $clientOptions . ');';
$view->registerJs($js);
}
}