| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Core\HookTrait; |
| 8: | use Atk4\Data\Model; |
| 9: | |
| 10: | class Lister extends View |
| 11: | { |
| 12: | use HookTrait; |
| 13: | |
| 14: | public const HOOK_BEFORE_ROW = self::class . '@beforeRow'; |
| 15: | public const HOOK_AFTER_ROW = self::class . '@afterRow'; |
| 16: | |
| 17: | public $ui = 'list'; |
| 18: | |
| 19: | public $defaultTemplate; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | public $tRow; |
| 29: | |
| 30: | |
| 31: | public $tEmpty; |
| 32: | |
| 33: | |
| 34: | public $jsPaginator; |
| 35: | |
| 36: | |
| 37: | public $ipp; |
| 38: | |
| 39: | |
| 40: | public $currentRow; |
| 41: | |
| 42: | #[\Override] |
| 43: | protected function init(): void |
| 44: | { |
| 45: | parent::init(); |
| 46: | |
| 47: | $this->initChunks(); |
| 48: | } |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | protected function initChunks(): void |
| 54: | { |
| 55: | if (!$this->template) { |
| 56: | throw new Exception('Lister does not have default template. Either supply your own HTML or use "defaultTemplate" => "lister.html"'); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | if ($this->template->hasTag('empty')) { |
| 61: | $this->tEmpty = $this->template->cloneRegion('empty'); |
| 62: | $this->template->del('empty'); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | if ($this->template->hasTag('row')) { |
| 67: | $this->tRow = $this->template->cloneRegion('row'); |
| 68: | $this->template->del('rows'); |
| 69: | } else { |
| 70: | $this->tRow = clone $this->template; |
| 71: | $this->template->del('_top'); |
| 72: | } |
| 73: | } |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function addJsPaginator($ipp, $options = [], $container = null, $scrollRegion = null) |
| 88: | { |
| 89: | $this->ipp = $ipp; |
| 90: | $this->jsPaginator = JsPaginator::addTo($this, ['view' => $container, 'options' => $options]); |
| 91: | |
| 92: | |
| 93: | $this->model->setLimit($ipp); |
| 94: | |
| 95: | |
| 96: | $this->jsPaginator->onScroll(function (int $p) use ($ipp, $scrollRegion) { |
| 97: | |
| 98: | $this->model->setLimit($ipp, ($p - 1) * $ipp); |
| 99: | |
| 100: | |
| 101: | $jsonArr = $this->renderToJsonArr($scrollRegion); |
| 102: | |
| 103: | |
| 104: | $jsonArr['noMoreScrollPages'] = $this->_renderedRowsCount < $ipp; |
| 105: | |
| 106: | |
| 107: | $this->getApp()->terminateJson($jsonArr); |
| 108: | }); |
| 109: | |
| 110: | return $this; |
| 111: | } |
| 112: | |
| 113: | |
| 114: | protected $_renderedRowsCount = 0; |
| 115: | |
| 116: | #[\Override] |
| 117: | protected function renderView(): void |
| 118: | { |
| 119: | if (!$this->template) { |
| 120: | throw new Exception('Lister requires you to specify template explicitly'); |
| 121: | } |
| 122: | |
| 123: | |
| 124: | if (!$this->model) { |
| 125: | parent::renderView(); |
| 126: | |
| 127: | return; |
| 128: | } |
| 129: | |
| 130: | |
| 131: | $this->_renderedRowsCount = 0; |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | $modelBackup = $this->model; |
| 137: | $tRowBackup = $this->tRow; |
| 138: | try { |
| 139: | foreach ($this->model as $this->model) { |
| 140: | $this->currentRow = $this->model; |
| 141: | $this->tRow = clone $tRowBackup; |
| 142: | if ($this->hook(self::HOOK_BEFORE_ROW) === false) { |
| 143: | continue; |
| 144: | } |
| 145: | |
| 146: | $this->renderRow(); |
| 147: | |
| 148: | ++$this->_renderedRowsCount; |
| 149: | } |
| 150: | } finally { |
| 151: | $this->model = $modelBackup; |
| 152: | $this->tRow = $tRowBackup; |
| 153: | } |
| 154: | |
| 155: | |
| 156: | if ($this->_renderedRowsCount === 0) { |
| 157: | if (!$this->jsPaginator || !$this->jsPaginator->getPage()) { |
| 158: | $empty = $this->tEmpty !== null ? $this->tEmpty->renderToHtml() : ''; |
| 159: | if ($this->template->hasTag('rows')) { |
| 160: | $this->template->dangerouslyAppendHtml('rows', $empty); |
| 161: | } else { |
| 162: | $this->template->dangerouslyAppendHtml('_top', $empty); |
| 163: | } |
| 164: | } |
| 165: | } |
| 166: | |
| 167: | |
| 168: | if ($this->jsPaginator && ($this->_renderedRowsCount < $this->ipp)) { |
| 169: | $this->jsPaginator->jsIdle(); |
| 170: | } |
| 171: | |
| 172: | parent::renderView(); |
| 173: | } |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | public function renderRow(): void |
| 180: | { |
| 181: | $this->tRow->trySet($this->currentRow); |
| 182: | |
| 183: | if ($this->tRow->hasTag('_title')) { |
| 184: | $this->tRow->set('_title', $this->model->getTitle()); |
| 185: | } |
| 186: | if ($this->tRow->hasTag('_href')) { |
| 187: | $this->tRow->set('_href', $this->url(['id' => $this->currentRow->getId()])); |
| 188: | } |
| 189: | $this->tRow->trySet('_id', $this->name . '-' . $this->currentRow->getId()); |
| 190: | |
| 191: | $html = $this->tRow->renderToHtml(); |
| 192: | if ($this->template->hasTag('rows')) { |
| 193: | $this->template->dangerouslyAppendHtml('rows', $html); |
| 194: | } else { |
| 195: | $this->template->dangerouslyAppendHtml('_top', $html); |
| 196: | } |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | #[\Override] |
| 207: | public function renderToJsonArr(string $region = null): array |
| 208: | { |
| 209: | $this->renderAll(); |
| 210: | |
| 211: | |
| 212: | if ($region !== null) { |
| 213: | if (!isset($this->_jsActions['click'])) { |
| 214: | $this->_jsActions['click'] = []; |
| 215: | } |
| 216: | array_unshift($this->_jsActions['click'], $this->js()->off()); |
| 217: | } |
| 218: | |
| 219: | return [ |
| 220: | 'success' => true, |
| 221: | 'atkjs' => $this->getJs(), |
| 222: | 'html' => $this->template->renderToHtml($region), |
| 223: | 'id' => $this->name, |
| 224: | ]; |
| 225: | } |
| 226: | } |
| 227: | |