Test Setup Failed
Push — master ( e7215a...b47f60 )
by Georgi
05:19
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\jQuery;
9
10
class NavMenu extends BaseMenu
11
{
12
    public $ui = 'inverted nav menu';
13
    
14
//     public $defaultTemplate = 'layout/maestro-sidenav.html';
15
    
16
    protected function init(): void
17
	{		
18
		parent::init();
19
		
20
		$this->addHeader($this->getApp()->title);
21
		
22
		$items = collect();
23
		foreach(NavMenuJoint::collect() as $joint) {
24
			$items = $items->merge($joint->items());
25
		}
26
27
		$this->addItems($this, $items);
28
		
29
// 		$this->js(true)->find('.toggle-group .header')->click(new jsFunction(['e'], [new jsExpression('$(e.target).next(".menu").slideToggle()')]))->click();
30
		
31
// 		$this->getApp()->addStyle('
32
// 			.toggle-group .header {
33
// 				cursor: pointer;
34
// 			}
35
// 		');
36
	}
37
38
	public static function addItems($menu, Collection $items)
39
	{
40
		$items->sort(function ($entry1, $entry2) {
41
			$weight1 = $entry1['weight']?? 10;
42
			$weight2 = $entry2['weight']?? 10;
43
			
44
			return $weight1 <=> $weight2;
45
		})->map(function($entry, $caption) use ($menu) {
46
			if (! ($entry['access'] ?? true)) return;
47
48
			if (!is_array($entry)) {
49
				$entry = ['action' => $entry];
50
			}
51
			
52
			$entry['item'] = $entry['item'] ?? $caption;
53
			
54
			if (is_array($entry['item'])) {
55
				$entry['item'] = [$caption] + $entry['item'];
56
			}
57
			
58
			if ($subitems = $entry['menu'] ?? []) {		
59
				$submenu = $menu->addMenu($entry['item']);
60
				
61
// 				$submenu->addClass('right pointing');
62
// 				$submenu->js = ['transition' => 'swing left', 'on' => 'click'];
63
64
				self::addItems($submenu, collect($subitems));
65
			}
66
			elseif ($subitems = $entry['group'] ?? []) {
67
			    $subgroup = $menu->addGroup($entry['item']);
68
69
				self::addItems($subgroup, collect($subitems));
70
			}
71
			else {
72
			    $menu->addItem($entry['item'], $entry['action'] ?? '');
73
			}
74
		});
75
	}
76
	
77
// 	public function addGroup($name, string $template = 'menugroup.html')
78
// 	{
79
// 	    return parent::addGroup($name, $template)->addClass('atk-maestro-sidenav')->removeClass('item');
80
// 	}
81
	
82
// 	public function addItem($item = null, $action = null)
83
// 	{
84
// 	    return parent::addItem($item, $action)->addClass('atk-maestro-sidenav');
85
// 	}
86
	
87
// 	public function renderView(): void
88
// 	{
89
// 	    parent::renderView();
90
	    
91
// 	    //initialize all menu group at ounce.
92
// 	    //since atkSideNav plugin default setting are for Maestro, no need to pass settings to initialize it.
93
// 	    $js = (new jQuery('.atk-maestro-sidenav'))->atkSidenav();
94
	    
95
// 	    $this->js(true, $js);
96
// 	}
97
}