summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Navigation/Renderer/NavigationRenderer.php
blob: 00c0f9af2c4abdcdb9974ea44275058c84894629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Navigation\Renderer;

use ArrayIterator;
use Exception;
use RecursiveIterator;
use Icinga\Application\Icinga;
use Icinga\Exception\IcingaException;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\View;

/**
 * Renderer for single level navigation
 */
class NavigationRenderer implements RecursiveIterator, NavigationRendererInterface
{
    /**
     * The tag used for the outer element
     *
     * @var string
     */
    protected $elementTag;

    /**
     * The CSS class used for the outer element
     *
     * @var string
     */
    protected $cssClass;

    /**
     * The navigation's heading text
     *
     * @var string
     */
    protected $heading;

    /**
     * The content rendered so far
     *
     * @var array
     */
    protected $content;

    /**
     * Whether to skip rendering the outer element
     *
     * @var bool
     */
    protected $skipOuterElement;

    /**
     * The navigation's iterator
     *
     * @var ArrayIterator
     */
    protected $iterator;

    /**
     * The navigation
     *
     * @var Navigation
     */
    protected $navigation;

    /**
     * View
     *
     * @var View
     */
    protected $view;

    /**
     * Create a new NavigationRenderer
     *
     * @param   Navigation  $navigation
     * @param   bool        $skipOuterElement
     */
    public function __construct(Navigation $navigation, $skipOuterElement = false)
    {
        $this->skipOuterElement = $skipOuterElement;
        $this->iterator = $navigation->getIterator();
        $this->navigation = $navigation;
        $this->content = array();
    }

    /**
     * {@inheritdoc}
     */
    public function setElementTag($tag)
    {
        $this->elementTag = $tag;
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getElementTag()
    {
        return $this->elementTag ?: static::OUTER_ELEMENT_TAG;
    }

    /**
     * {@inheritdoc}
     */
    public function setCssClass($class)
    {
        $this->cssClass = $class;
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getCssClass()
    {
        return $this->cssClass;
    }

    /**
     * {@inheritdoc}
     */
    public function setHeading($heading)
    {
        $this->heading = $heading;
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getHeading()
    {
        return $this->heading;
    }

    /**
     * Return the view
     *
     * @return View
     */
    public function view()
    {
        if ($this->view === null) {
            $this->setView(Icinga::app()->getViewRenderer()->view);
        }

        return $this->view;
    }

    /**
     * Set the view
     *
     * @param   View    $view
     *
     * @return  $this
     */
    public function setView(View $view)
    {
        $this->view = $view;
        return $this;
    }

    public function getChildren(): NavigationRenderer
    {
        return new static($this->current()->getChildren(), $this->skipOuterElement);
    }

    public function hasChildren(): bool
    {
        return $this->current()->hasChildren();
    }

    public function current(): NavigationItem
    {
        return $this->iterator->current();
    }

    public function key(): int
    {
        return $this->iterator->key();
    }

    public function next(): void
    {
        $this->iterator->next();
    }

    public function rewind(): void
    {
        $this->iterator->rewind();
        if (! $this->skipOuterElement) {
            $this->content[] = $this->beginMarkup();
        }
    }

    public function valid(): bool
    {
        $valid = $this->iterator->valid();
        if (! $this->skipOuterElement && !$valid) {
            $this->content[] = $this->endMarkup();
        }

        return $valid;
    }

    /**
     * Return the opening markup for the navigation
     *
     * @return  string
     */
    public function beginMarkup()
    {
        $content = array();
        $content[] = sprintf(
            '<%s%s role="navigation">',
            $this->getElementTag(),
            $this->getCssClass() !== null ? ' class="' . $this->getCssClass() . '"' : ''
        );
        if (($heading = $this->getHeading()) !== null) {
            $content[] = sprintf(
                '<h%1$d id="navigation" class="sr-only" tabindex="-1">%2$s</h%1$d>',
                static::HEADING_RANK,
                $this->view()->escape($heading)
            );
        }
        $content[] = $this->beginChildrenMarkup();
        return join("\n", $content);
    }

    /**
     * Return the closing markup for the navigation
     *
     * @return  string
     */
    public function endMarkup()
    {
        $content = array();
        $content[] = $this->endChildrenMarkup();
        $content[] = '</' . $this->getElementTag() . '>';
        return join("\n", $content);
    }

    /**
     * Return the opening markup for multiple navigation items
     *
     * @param   int $level
     *
     * @return  string
     */
    public function beginChildrenMarkup($level = 1)
    {
        $cssClass = array(static::CSS_CLASS_NAV);
        if ($this->navigation->getLayout() === Navigation::LAYOUT_TABS) {
            $cssClass[] = static::CSS_CLASS_NAV_TABS;
        } elseif ($this->navigation->getLayout() === Navigation::LAYOUT_DROPDOWN) {
            $cssClass[] = static::CSS_CLASS_NAV_DROPDOWN;
        }

        $cssClass[] = 'nav-level-' . $level;

        return '<ul class="' . join(' ', $cssClass) . '">';
    }

    /**
     * Return the closing markup for multiple navigation items
     *
     * @return  string
     */
    public function endChildrenMarkup()
    {
        return '</ul>';
    }

    /**
     * Return the opening markup for the given navigation item
     *
     * @param   NavigationItem  $item
     *
     * @return  string
     */
    public function beginItemMarkup(NavigationItem $item)
    {
        $cssClasses = array(static::CSS_CLASS_ITEM);

        if ($item->hasChildren() && $item->getChildren()->getLayout() === Navigation::LAYOUT_DROPDOWN) {
            $cssClasses[] = static::CSS_CLASS_DROPDOWN;
            $item
                ->setAttribute('class', static::CSS_CLASS_DROPDOWN_TOGGLE)
                ->setIcon(static::DROPDOWN_TOGGLE_ICON)
                ->setUrl('#');
        }

        if ($item->getActive()) {
            $cssClasses[] = static::CSS_CLASS_ACTIVE;
        }

        if ($item->getSelected()) {
            $cssClasses[] = static::CSS_CLASS_SELECTED;
        }

        if ($cssClass = $item->getCssClass()) {
            $cssClasses[] = $cssClass;
        }

        $content = sprintf(
            '<li class="%s">',
            join(' ', $cssClasses)
        );
        return $content;
    }

    /**
     * Return the closing markup for a navigation item
     *
     * @return  string
     */
    public function endItemMarkup()
    {
        return '</li>';
    }

    /**
     * {@inheritdoc}
     */
    public function render()
    {
        foreach ($this as $item) {
            /** @var NavigationItem $item */
            if ($item->shouldRender()) {
                $content = $item->render();
                $this->content[] = $this->beginItemMarkup($item);
                $this->content[] = $content;
                $this->content[] = $this->endItemMarkup();
            }
        }

        return join("\n", $this->content);
    }

    /**
     * {@inheritdoc}
     */
    public function __toString()
    {
        try {
            return $this->render();
        } catch (Exception $e) {
            return IcingaException::describe($e);
        }
    }
}