Completed
Push — master ( f57612...3cee74 )
by Nikolas
06:59
created

ActionListRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 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
use rtens\domin\reflection\CommentParser;
8
9
class ActionListRenderer implements WebRenderer {
10
11
    /** @var CommentParser */
12
    private $parser;
13
14
    /**
15
     * @param CommentParser $parser
16
     */
17
    public function __construct(CommentParser $parser) {
18
        $this->parser = $parser;
19
    }
20
21
    /**
22
     * @param mixed $value
23
     * @return bool
24
     */
25
    public function handles($value) {
26
        return $value instanceof ActionList;
27
    }
28
29
    /**
30
     * @param ActionList $value
31
     * @return mixed
32
     */
33
    public function render($value) {
34
        $elements = [];
35
36
        if ($value->hasGroups()) {
37
            foreach ($value->getGroups() as $group) {
38
                $elements[] = $this->renderGroup($group, $value->getActionsOf($group));
39
            }
40
            $elements[] = $this->renderGroup('All', $value->getAllActions());
41
            $elements[] = $this->collapseScript();
42
        } else {
43
            $elements[] = $this->renderList($value->getAllActions());
44
        }
45
46
        return new Element('div', [], $elements);
47
    }
48
49
    /**
50
     * @param ActionList $value
51
     * @return array|Element[]
52
     */
53
    public function headElements($value) {
54
        return [
55
            HeadElements::jquery(),
56
            HeadElements::bootstrap(),
57
            HeadElements::bootstrapJs(),
58
        ];
59
    }
60
61
    private function renderGroup($group, $actions) {
62
        return new Element('div', ['class' => 'action-group'], [
63
            new Element('h2', ['class' => 'group-name'], [
64
                new Element('small', [], [
65
                    new Element('span', ['class' => 'toggle-group glyphicon glyphicon-chevron-right']),
66
                    new Element('span', ['class' => 'toggle-group glyphicon glyphicon-chevron-down', 'style' => 'display: none;'])
67
                ]),
68
                $group
69
            ]),
70
            $this->renderList($actions)
71
        ]);
72
    }
73
74
    /**
75
     * @param ActionListItem[] $actions
76
     * @return Element
77
     */
78
    private function renderList($actions) {
79
        $items = [];
80
        foreach ($actions as $action) {
81
            $caption = [$action->getCaption()];
82
83
            if ($action->getDescription()) {
84
                $caption[] = new Element('small', [], [
85
                    '- ' . $this->parser->shorten($action->getDescription())
86
                ]);
87
            }
88
89
            $items[] = new Element('a', [
90
                'href' => $action->getId(),
91
                'class' => 'list-group-item'
92
            ], $caption);
93
        }
94
        return new Element('div', ['class' => 'list-group'], $items);
95
    }
96
97
    private function collapseScript() {
98
        return "
99
            <script>
100
                $(function () {
101
                    $('.list-group').hide();
102
103
                    var groupName = $('.group-name');
104
                    groupName.css('cursor', 'pointer');
105
                    groupName.click(function () {
106
                        $(this).closest('.action-group').find('.list-group').toggle();
107
                        $(this).find('.toggle-group').toggle();
108
                    });
109
                });
110
            </script>";
111
    }
112
}