Passed
Push — master ( fd1497...9b6942 )
by Bruno
07:31
created

Pagination   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 95
c 1
b 0
f 0
dl 0
loc 135
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
B render() 0 79 5
A getMetadata() 0 6 1
A script() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Frontend\HTML\Element;
4
5
use Formularium\Element;
6
use Formularium\Exception\ClassNotFoundException;
7
use Formularium\HTMLNode;
8
use Formularium\Framework;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
12
class Pagination extends Element
13
{
14
    public function render(array $parameters, HTMLNode $previous): HTMLNode
15
    {
16
        $node = HTMLNode::factory(
17
            'nav',
18
            ['class' => 'formularium-pagination-wrapper', 'aria-label' => "Page navigation"],
19
            HTMLNode::factory(
20
                'ul',
21
                ['class' => 'formularium-pagination'],
22
                [
23
                    HTMLNode::factory(
24
                        'li',
25
                        [
26
                            "class" => "formularium-pagination-item",
27
                            "v-show" => "currentPage > pagesAround",
28
                            "@click" => "\$emit('page', 1)"
29
                        ],
30
                        HTMLNode::factory(
31
                            'a',
32
                            ["class" => "formularium-pagination-link",
33
                             ":href" => "basePath + '/1'",
34
                            "@click.prevent" => ""
35
                        ],
36
                            "1"
37
                        )
38
                    ),
39
                    HTMLNode::factory(
40
                        'li',
41
                        ["class" => "formularium-pagination-item",
42
                        "v-show" => "currentPage > pagesAround"
43
                    ],
44
                        "..."
45
                    ),
46
                    HTMLNode::factory(
47
                        'li',
48
                        [
49
                            "class" => "formularium-pagination-item",
50
                            "v-for" => "p in pages",
51
                            "v-bind:key" => "p.page",
52
                            "@click" => "\$emit('page', p.page)"
53
                        ],
54
                        HTMLNode::factory(
55
                            'a',
56
                            [
57
                                "class" => "formularium-pagination-link",
58
                                ":href" => "basePath + /' + p.page",
59
                                "@click.prevent" => ""
60
                            ],
61
                            "{{p.page}}"
62
                        ),
63
                    ),
64
                    HTMLNode::factory(
65
                        'li',
66
                        [
67
                            "class" => "formularium-pagination-item",
68
                            "v-show" => "lastPage > currentPage + pagesAround",
69
                        ],
70
                        "..."
71
                    )
72
                ]
73
            )
74
        );
75
76
        foreach ($this->composer->getFrameworks() as $framework) {
77
            /**
78
             * @var Framework $framework
79
             */
80
            $f =$framework->getName();
81
            if ($f === 'HTML' || $f === 'Vue') {
82
                continue;
83
            }
84
            try {
85
                $element = $framework->getElement($this->getName());
86
                $node = $element->render($parameters, $node);
87
            } catch (ClassNotFoundException $e) {
88
                continue; // element default
89
            }
90
        }
91
92
        return $node;
93
    }
94
95
    public static function script(): string
96
    {
97
        return <<<EOF
98
export default {
99
    props: {
100
        basePath: {
101
            type: String,
102
            default: "/post"
103
        },
104
        currentPage: {
105
            type: Number,
106
            required: true
107
        },
108
        perPage: {
109
            type: Number,
110
            required: true
111
        },
112
        lastPage: {
113
            type: Number,
114
            required: true
115
        },
116
        total: {
117
            type: Number,
118
            required: true
119
        },
120
        pagesAround: {
121
            type: Number,
122
            default: 4
123
        },
124
    },
125
126
    computed: {
127
        pages() {
128
            let first = Math.max(1, this.currentPage - this.pagesAround);
129
            let last = Math.min(this.lastPage, this.currentPage + this.pagesAround);
130
            let pages = [];
131
            for (let i = first; i <= last; i++) {
132
                pages.push({page: i});
133
            }
134
            return pages;
135
        }
136
    }
137
}
138
EOF;
139
    }
140
141
    public static function getMetadata(): Metadata
142
    {
143
        return new Metadata(
144
            'Pagination',
145
            'Creates a pagination element',
146
            [
147
            ]
148
        );
149
    }
150
}
151