| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui\Table; |
| 6: | |
| 7: | use Atk4\Data\Field; |
| 8: | use Atk4\Data\Model; |
| 9: | use Atk4\Ui\Js\Jquery; |
| 10: | use Atk4\Ui\Js\JsExpression; |
| 11: | use Atk4\Ui\Js\JsExpressionable; |
| 12: | use Atk4\Ui\JsCallback; |
| 13: | use Atk4\Ui\Popup; |
| 14: | use Atk4\Ui\Table; |
| 15: | use Atk4\Ui\View; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class Column |
| 23: | { |
| 24: | use \Atk4\Core\AppScopeTrait; |
| 25: | use \Atk4\Core\DiContainerTrait; |
| 26: | use \Atk4\Core\InitializerTrait; |
| 27: | use \Atk4\Core\NameTrait; |
| 28: | use \Atk4\Core\TrackableTrait; |
| 29: | |
| 30: | public const HOOK_GET_HTML_TAGS = self::class . '@getHtmlTags'; |
| 31: | public const HOOK_GET_HEADER_CELL_HTML = self::class . '@getHeaderCellHtml'; |
| 32: | |
| 33: | |
| 34: | public $table; |
| 35: | |
| 36: | |
| 37: | public array $attr = []; |
| 38: | |
| 39: | |
| 40: | public $caption; |
| 41: | |
| 42: | |
| 43: | public $sortable = true; |
| 44: | |
| 45: | |
| 46: | public $columnData; |
| 47: | |
| 48: | |
| 49: | public $hasHeaderAction = false; |
| 50: | |
| 51: | |
| 52: | public $headerActionTag; |
| 53: | |
| 54: | public function __construct(array $defaults = []) |
| 55: | { |
| 56: | $this->setDefaults($defaults); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function addPopup(Popup $popup = null, $icon = 'table-filter-off') |
| 68: | { |
| 69: | $id = $this->name . '_ac'; |
| 70: | |
| 71: | $popup = $this->table->getOwner()->add($popup ?? [Popup::class])->setHoverable(); |
| 72: | |
| 73: | $this->setHeaderPopup($icon, $id); |
| 74: | |
| 75: | $popup->triggerBy = '#' . $id; |
| 76: | $popup->popOptions = array_merge( |
| 77: | $popup->popOptions, |
| 78: | [ |
| 79: | 'on' => 'click', |
| 80: | 'position' => 'bottom left', |
| 81: | 'movePopup' => $this->columnData ? true : false, |
| 82: | 'target' => $this->columnData ? 'th[data-column=' . $this->columnData . ']' : false, |
| 83: | 'distanceAway' => -12, |
| 84: | ] |
| 85: | ); |
| 86: | $popup->stopClickEvent = true; |
| 87: | |
| 88: | return $popup; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function setHeaderPopup($class, $id): void |
| 98: | { |
| 99: | $this->hasHeaderAction = true; |
| 100: | |
| 101: | $this->headerActionTag = ['div', ['class' => 'atk-table-dropdown'], |
| 102: | [ |
| 103: | ['i', ['id' => $id, 'class' => $class . ' icon'], ''], |
| 104: | ], |
| 105: | ]; |
| 106: | } |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | public function setHeaderPopupIcon($icon): void |
| 114: | { |
| 115: | $this->headerActionTag = ['div', ['class' => 'atk-table-dropdown'], |
| 116: | [ |
| 117: | ['i', ['id' => $this->name . '_ac', 'class' => $icon . ' icon'], ''], |
| 118: | ], |
| 119: | ]; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | public function addDropdown(array $items, \Closure $fx, $icon = 'caret square down', $menuId = null): void |
| 130: | { |
| 131: | $menuItems = []; |
| 132: | foreach ($items as $key => $item) { |
| 133: | $menuItems[] = ['name' => is_int($key) ? $item : $key, 'value' => $item]; |
| 134: | } |
| 135: | |
| 136: | $cb = $this->setHeaderDropdown($menuItems, $icon, $menuId); |
| 137: | |
| 138: | $cb->onSelectItem(static function (string $menu, string $item) use ($fx) { |
| 139: | return $fx($item, $menu); |
| 140: | }); |
| 141: | } |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | public function setHeaderDropdown($items, string $icon = 'caret square down', string $menuId = null): JsCallback |
| 153: | { |
| 154: | $this->hasHeaderAction = true; |
| 155: | $id = $this->name . '_ac'; |
| 156: | $this->headerActionTag = ['div', ['class' => 'atk-table-dropdown'], [ |
| 157: | [ |
| 158: | 'div', ['id' => $id, 'class' => 'ui top left pointing dropdown', 'data-menu-id' => $menuId], |
| 159: | [['i', ['class' => $icon . ' icon'], '']], |
| 160: | ], |
| 161: | ]]; |
| 162: | |
| 163: | $cb = Column\JsHeaderDropdownCallback::addTo($this->table); |
| 164: | |
| 165: | $function = new JsExpression('function (value, text, item) { |
| 166: | if (value === undefined || value === \'\' || value === null) { |
| 167: | return; |
| 168: | } |
| 169: | $(this).api({ |
| 170: | on: \'now\', |
| 171: | url: \'' . $cb->getJsUrl() . '\', |
| 172: | data: { item: value, id: $(this).data(\'menu-id\') } |
| 173: | }); |
| 174: | }'); |
| 175: | |
| 176: | $chain = new Jquery('#' . $id); |
| 177: | $chain->dropdown([ |
| 178: | 'action' => 'hide', |
| 179: | 'values' => $items, |
| 180: | 'onChange' => $function, |
| 181: | ]); |
| 182: | |
| 183: | |
| 184: | $chain->on('click', new JsExpression('function (e) { e.stopPropagation(); }')); |
| 185: | |
| 186: | $this->table->js(true, $chain); |
| 187: | |
| 188: | return $cb; |
| 189: | } |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | |
| 200: | public function addClass($class, $position = 'body') |
| 201: | { |
| 202: | $this->attr[$position]['class'][] = $class; |
| 203: | |
| 204: | return $this; |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | |
| 221: | public function setAttr($attr, $value, $position = 'body') |
| 222: | { |
| 223: | $this->attr[$position][$attr] = $value; |
| 224: | |
| 225: | return $this; |
| 226: | } |
| 227: | |
| 228: | public function getTagAttributes(string $position, array $attr = []): array |
| 229: | { |
| 230: | |
| 231: | |
| 232: | foreach (['all', $position] as $key) { |
| 233: | if (isset($this->attr[$key])) { |
| 234: | $attr = array_merge_recursive($attr, $this->attr[$key]); |
| 235: | } |
| 236: | } |
| 237: | |
| 238: | return $attr; |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | |
| 249: | public function getTag(string $position, $value, array $attr = []): string |
| 250: | { |
| 251: | $attr = $this->getTagAttributes($position, $attr); |
| 252: | |
| 253: | if (isset($attr['class'])) { |
| 254: | $attr['class'] = implode(' ', $attr['class']); |
| 255: | } |
| 256: | |
| 257: | return $this->getApp()->getTag($position === 'body' ? 'td' : 'th', $attr, $value); |
| 258: | } |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | public function getHeaderCellHtml(Field $field = null, $value = null): string |
| 267: | { |
| 268: | $tags = $this->table->hook(self::HOOK_GET_HEADER_CELL_HTML, [$this, $field, $value]); |
| 269: | if ($tags) { |
| 270: | return reset($tags); |
| 271: | } |
| 272: | |
| 273: | if ($field === null) { |
| 274: | return $this->getTag('head', $this->caption ?? '', $this->table->sortable ? ['class' => ['disabled']] : []); |
| 275: | } |
| 276: | |
| 277: | |
| 278: | $caption = $this->caption ?? $field->getCaption(); |
| 279: | |
| 280: | $attr = [ |
| 281: | 'data-column' => $this->columnData, |
| 282: | ]; |
| 283: | |
| 284: | $class = 'atk-table-column-header'; |
| 285: | |
| 286: | if ($this->hasHeaderAction) { |
| 287: | $attr['id'] = $this->name . '_th'; |
| 288: | |
| 289: | |
| 290: | $caption = [$this->headerActionTag, $this->getApp()->encodeHtml($caption)]; |
| 291: | } |
| 292: | |
| 293: | if ($this->table->sortable) { |
| 294: | $attr['data-sort'] = $field->shortName; |
| 295: | |
| 296: | if ($this->sortable) { |
| 297: | $attr['class'] = ['sortable']; |
| 298: | } |
| 299: | |
| 300: | |
| 301: | if ($this->table->sortBy === $field->shortName) { |
| 302: | $class .= ' sorted ' . ['asc' => 'ascending', 'desc' => 'descending'][$this->table->sortDirection]; |
| 303: | |
| 304: | if ($this->table->sortDirection === 'asc') { |
| 305: | $attr['data-sort'] = '-' . $attr['data-sort']; |
| 306: | } elseif ($this->table->sortDirection === 'desc') { |
| 307: | $attr['data-sort'] = ''; |
| 308: | } |
| 309: | } |
| 310: | } |
| 311: | |
| 312: | return $this->getTag('head', [['div', ['class' => $class], $caption]], $attr); |
| 313: | } |
| 314: | |
| 315: | |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | public function getTotalsCellHtml(Field $field, $value): string |
| 321: | { |
| 322: | return $this->getTag('foot', $this->getApp()->uiPersistence->typecastSaveField($field, $value)); |
| 323: | } |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | public function getDataCellHtml(Field $field = null, array $attr = []): string |
| 338: | { |
| 339: | return $this->getTag('body', [$this->getDataCellTemplate($field)], $attr); |
| 340: | } |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | public function getDataCellTemplate(Field $field = null): string |
| 354: | { |
| 355: | if ($field) { |
| 356: | return '{$' . $field->shortName . '}'; |
| 357: | } |
| 358: | |
| 359: | return '{_$' . $this->shortName . '}'; |
| 360: | } |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | public function getHtmlTags(Model $row, ?Field $field): array |
| 369: | { |
| 370: | return []; |
| 371: | } |
| 372: | } |
| 373: | |