| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Ui\Js\JsChain; |
| 8: | use Atk4\Ui\Js\JsExpressionable; |
| 9: | |
| 10: | class JsSortable extends JsCallback |
| 11: | { |
| 12: | |
| 13: | public $container = 'tbody'; |
| 14: | |
| 15: | |
| 16: | public $draggable = 'tr'; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | public $dataLabel = 'id'; |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public $handleClass; |
| 37: | |
| 38: | |
| 39: | public $autoFireCb = true; |
| 40: | |
| 41: | |
| 42: | public $view; |
| 43: | |
| 44: | #[\Override] |
| 45: | protected function init(): void |
| 46: | { |
| 47: | parent::init(); |
| 48: | |
| 49: | if (!$this->view) { |
| 50: | $this->view = $this->getOwner(); |
| 51: | } |
| 52: | $this->getApp()->requireJs($this->getApp()->cdn['atk'] . '/external/@shopify/draggable/build/umd/index.min.js'); |
| 53: | |
| 54: | $this->view->js(true)->atkJsSortable([ |
| 55: | 'url' => $this->getJsUrl(), |
| 56: | 'urlOptions' => $this->args, |
| 57: | 'container' => $this->container, |
| 58: | 'draggable' => $this->draggable, |
| 59: | 'handleClass' => $this->handleClass, |
| 60: | 'dataLabel' => $this->dataLabel, |
| 61: | 'autoFireCb' => $this->autoFireCb, |
| 62: | ]); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function onReorder(\Closure $fx): void |
| 71: | { |
| 72: | $this->set(function () use ($fx) { |
| 73: | $sortOrders = explode(',', $this->getApp()->getRequestPostParam('order')); |
| 74: | $source = $this->getApp()->getRequestPostParam('source'); |
| 75: | $newIndex = (int) $this->getApp()->getRequestPostParam('newIndex'); |
| 76: | $origIndex = (int) $this->getApp()->getRequestPostParam('origIndex'); |
| 77: | |
| 78: | return $fx($sortOrders, $source, $newIndex, $origIndex); |
| 79: | }); |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | public function jsSendSortOrders($urlOptions = null): JsExpressionable |
| 90: | { |
| 91: | return $this->view->js()->atkJsSortable('sendSortOrders', [$urlOptions]); |
| 92: | } |
| 93: | } |
| 94: | |