Test Failed
Push — master ( 794710...4acb1e )
by Georgi
03:38
created

NavMenu::addItems()   A

Complexity

Conditions 6
Paths 1

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 1
nop 2
dl 0
loc 35
rs 9.0111
c 0
b 0
f 0
1
<?php 
2
3
namespace Epesi\Core\Layout\View;
4
5
use Illuminate\Support\Collection;
6
use atk4\ui\Menu as BaseMenu;
7
use Epesi\Core\Layout\Integration\Joints\NavMenuJoint;
8
use atk4\ui\jsChain;
9
10
class NavMenu extends BaseMenu
11
{
12
    public $ui = 'inverted nav menu';
13
    
14
	public function init()
15
	{		
16
		parent::init();
17
		
18
		$this->addHeader($this->app->title);
19
		
20
		$items = collect();
21
		foreach(NavMenuJoint::collect() as $joint) {
22
			$items = $items->merge($joint->items());
23
		}
24
25
		$this->addItems($this, $items);
26
		
27
// 		$this->js(true)->find('.toggle-group .header')->click(new jsFunction(['e'], [new jsExpression('$(e.target).next(".menu").slideToggle()')]))->click();
28
		
29
// 		$this->app->addStyle('
30
// 			.toggle-group .header {
31
// 				cursor: pointer;
32
// 			}
33
// 		');
34
	}
35
36
	public static function addItems($menu, Collection $items)
37
	{
38
		$items->sort(function ($entry1, $entry2) {
39
			$weight1 = $entry1['weight']?? 10;
40
			$weight2 = $entry2['weight']?? 10;
41
			
42
			return $weight1 <=> $weight2;
43
		})->map(function($entry, $caption) use ($menu) {
44
			if (! ($entry['access'] ?? true)) return;
45
46
			if (!is_array($entry)) {
47
				$entry = ['action' => $entry];
48
			}
49
			
50
			$entry['item'] = $entry['item'] ?? $caption;
51
			
52
			if (is_array($entry['item'])) {
53
				$entry['item'] = [$caption] + $entry['item'];
54
			}
55
			
56
			if ($subitems = $entry['menu'] ?? []) {		
57
				$submenu = $menu->addMenu($entry['item']);
58
				
59
// 				$submenu->addClass('right pointing');
60
// 				$submenu->js = ['transition' => 'swing left', 'on' => 'click'];
61
62
				self::addItems($submenu, collect($subitems));
63
			}
64
			elseif ($subitems = $entry['group'] ?? []) {
65
			    $subgroup = $menu->addGroup($entry['item']);
66
67
				self::addItems($subgroup, collect($subitems));
68
			}
69
			else {
70
			    $menu->addItem($entry['item'], $entry['action'] ?? '');
71
			}
72
		});
73
	}
74
}