-
Notifications
You must be signed in to change notification settings - Fork 104
/
JsSortable.php
105 lines (93 loc) · 2.73 KB
/
JsSortable.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
<?php
declare(strict_types=1);
namespace atk4\ui;
class JsSortable extends JsCallback
{
/**
* The html element that contains others element for reordering.
*
* @var string
*/
public $container = 'tbody';
/**
* The html element inside the container that need reordering.
*
* @var string
*/
public $draggable = 'tr';
/**
* The data label set as data-label attribute on the html element.
* The callback will send source parameter on the moved element using this attribute.
* default to data-id.
*
* If the data-{label} attribute is not set for each list element, then the $_POST['order']
* value will be empty. Only org_idx and new_idx will be sent in callback request.
*
* @var string
*/
public $dataLabel = 'id';
/**
* The css class name of the handle element for dragging purpose.
* if null, the entire element become the dragging handle.
*
* @var string|null
*/
public $handleClass;
/**
* Whether callback will be fire automatically or not.
*
* @var bool
*/
public $autoFireCb = true;
/**
* The View that need reordering.
*
* @var View|null
*/
public $view;
public function init(): void
{
parent::init();
if (!$this->view) {
$this->view = $this->owner;
}
$this->app->requireJs('https://cdn.jsdelivr.net/npm/@shopify/draggable@1.0.0-beta.5/lib/draggable.bundle.js');
$this->view->js(true)->atkJsSortable([
'uri' => $this->getJsUrl(),
'uri_options' => $this->args,
'container' => $this->container,
'draggable' => $this->draggable,
'handleClass' => $this->handleClass,
'dataLabel' => $this->dataLabel,
'autoFireCb' => $this->autoFireCb,
]);
}
/**
* Callback when container has been reorder.
*
* @param callable|null $fx
*/
public function onReorder($fx = null)
{
if (is_callable($fx)) {
$sortOrders = explode(',', $_POST['order'] ?? '');
$source = $_POST['source'] ?? null;
$newIdx = $_POST['new_idx'] ?? null;
$orgIdx = $_POST['org_idx'] ?? null;
$this->set(function () use ($fx, $sortOrders, $source, $newIdx, $orgIdx) {
return call_user_func_array($fx, [$sortOrders, $source, $newIdx, $orgIdx]);
});
}
}
/**
* return js action to retrieve order.
*
* @param array|null $uriOptions
*
* @return mixed
*/
public function jsGetOrders($uriOptions = null)
{
return $this->view->js()->atkJsSortable('getSortOrders', [$uriOptions]);
}
}