| 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: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | class Accordion extends View |
| 17: | { |
| 18: | public $defaultTemplate = 'accordion.html'; |
| 19: | |
| 20: | public $ui = 'accordion'; |
| 21: | |
| 22: | |
| 23: | public $type; |
| 24: | |
| 25: | |
| 26: | public $settings = []; |
| 27: | |
| 28: | |
| 29: | public $sections = []; |
| 30: | |
| 31: | |
| 32: | public $activeSection = -1; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public function addSection($title, \Closure $callback = null, $icon = 'dropdown') |
| 46: | { |
| 47: | $section = AccordionSection::addTo($this, ['title' => $title, 'icon' => $icon]); |
| 48: | |
| 49: | |
| 50: | if ($callback) { |
| 51: | $section->virtualPage = VirtualPage::addTo($section, ['ui' => '']); |
| 52: | $section->virtualPage->set($callback); |
| 53: | } |
| 54: | |
| 55: | $this->sections[] = $section; |
| 56: | |
| 57: | return $section; |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public function activate($section): void |
| 66: | { |
| 67: | $this->activeSection = $this->getSectionIdx($section); |
| 68: | } |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | public function jsOpen($section, $when = false): JsExpressionable |
| 77: | { |
| 78: | return $this->jsBehavior('open', [$this->getSectionIdx($section)], $when); |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | public function jsCloseOthers($when = false): JsExpressionable |
| 87: | { |
| 88: | return $this->jsBehavior('close others', [], $when); |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function jsClose($section, $when = false): JsExpressionable |
| 98: | { |
| 99: | return $this->jsBehavior('close', [$this->getSectionIdx($section)], $when); |
| 100: | } |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | public function jsToggle($section, $when = false): JsExpressionable |
| 109: | { |
| 110: | return $this->jsBehavior('toggle', [$this->getSectionIdx($section)], $when); |
| 111: | } |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | public function jsBehavior($behavior, array $args, $when = false): JsExpressionable |
| 125: | { |
| 126: | return $this->js($when)->accordion($behavior, ...$args); |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | public function getSectionIdx(AccordionSection $section) |
| 135: | { |
| 136: | $idx = -1; |
| 137: | foreach ($this->sections as $k => $v) { |
| 138: | if ($v->name === $section->name) { |
| 139: | $idx = $k; |
| 140: | |
| 141: | break; |
| 142: | } |
| 143: | } |
| 144: | |
| 145: | return $idx; |
| 146: | } |
| 147: | |
| 148: | #[\Override] |
| 149: | protected function renderView(): void |
| 150: | { |
| 151: | if ($this->type) { |
| 152: | $this->addClass($this->type); |
| 153: | } |
| 154: | |
| 155: | |
| 156: | |
| 157: | if ($this->getClosestOwner(AccordionSection::class) === null) { |
| 158: | $this->js(true)->accordion($this->settings); |
| 159: | } |
| 160: | |
| 161: | if ($this->activeSection > -1) { |
| 162: | $this->jsBehavior('open', [$this->activeSection], true); |
| 163: | } |
| 164: | |
| 165: | parent::renderView(); |
| 166: | } |
| 167: | } |
| 168: | |