Completed
Push — master ( d98c10...e4052f )
by Nikolas
04:37
created

ActionListRenderer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A handles() 0 3 1
A render() 0 15 3
A headElements() 0 7 1
A renderGroup() 0 12 1
A renderList() 0 9 2
A collapseScript() 0 15 1
1
<?php
2
namespace rtens\domin\delivery\web\home;
3
4
use rtens\domin\delivery\web\Element;
5
use rtens\domin\delivery\web\HeadElements;
6
use rtens\domin\delivery\web\WebRenderer;
7
8
class ActionListRenderer implements WebRenderer {
9
10
    /**
11
     * @param mixed $value
12
     * @return bool
13
     */
14
    public function handles($value) {
15
        return $value instanceof ActionList;
16
    }
17
18
    /**
19
     * @param ActionList $value
20
     * @return mixed
21
     */
22
    public function render($value) {
23
        $elements = [];
24
25
        if ($value->hasGroups()) {
26
            foreach ($value->getGroups() as $group) {
27
                $elements[] = $this->renderGroup($group, $value->getActionsOf($group));
28
            }
29
            $elements[] = $this->renderGroup('All', $value->getAllActions());
30
            $elements[] = $this->collapseScript();
31
        } else {
32
            $elements[] = $this->renderList($value->getAllActions());
33
        }
34
35
        return new Element('div', [], $elements);
36
    }
37
38
    /**
39
     * @param ActionList $value
40
     * @return array|Element[]
41
     */
42
    public function headElements($value) {
43
        return [
44
            HeadElements::jquery(),
45
            HeadElements::bootstrap(),
46
            HeadElements::bootstrapJs(),
47
        ];
48
    }
49
50
    private function renderGroup($group, $actions) {
51
        return new Element('div', ['class' => 'action-group'], [
52
            new Element('h2', ['class' => 'group-name'], [
53
                new Element('small', [], [
54
                    new Element('span', ['class' => 'toggle-group glyphicon glyphicon-chevron-right']),
55
                    new Element('span', ['class' => 'toggle-group glyphicon glyphicon-chevron-down', 'style' => 'display: none;'])
56
                ]),
57
                $group
58
            ]),
59
            $this->renderList($actions)
60
        ]);
61
    }
62
63
    /**
64
     * @param ActionListItem[] $actions
65
     * @return Element
66
     */
67
    private function renderList($actions) {
68
        $items = [];
69
        foreach ($actions as $action) {
70
            $items[] = new Element('a', ['class' => 'list-group-item', 'href' => $action->getId()], [
71
                $action->getCaption()
72
            ]);
73
        }
74
        return new Element('div', ['class' => 'list-group'], $items);
75
    }
76
77
    private function collapseScript() {
78
        return "
79
            <script>
80
                $(function () {
81
                    $('.list-group').hide();
82
83
                    var groupName = $('.group-name');
84
                    groupName.css('cursor', 'pointer');
85
                    groupName.click(function () {
86
                        $(this).closest('.action-group').find('.list-group').toggle();
87
                        $(this).find('.toggle-group').toggle();
88
                    });
89
                });
90
            </script>";
91
    }
92
}