Passed
Push — master ( 14b5da...159fc4 )
by Bruno
07:51
created

Framework::htmlHead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Formularium\Frontend\Vue;
4
5
use Formularium\HTMLElement;
6
use PHP_CodeSniffer\Generators\HTML;
7
8
class Framework extends \Formularium\Framework
9
{
10
    const VUE_MODE_SINGLE_FILE = 'VUE_MODE_SINGLE_FILE';
11
    const VUE_MODE_EMBEDDED = 'VUE_MODE_EMBEDDED';
12
13
    public function __construct(string $name = 'Vue')
14
    {
15
        parent::__construct($name);
16
    }
17
18
    public function htmlHead(): string
19
    {
20
        return '<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>';
21
        // return '<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>';
22
    }
23
24
    public function viewableCompose(\Formularium\Model $m, array $elements, string $previousCompose): string
25
    {
26
        return join('', $elements); // TODO
27
    }
28
29
    public function editableCompose(\Formularium\Model $m, array $elements, string $previousCompose): string
30
    {
31
        $data = [];
32
        foreach ($m->getFields() as $name => $field) {
33
            $data[$name] = $field->getDatatype()->getDefault();
34
        }
35
36
        $editableForm = join('', $elements);
37
        $jsonData = json_encode($data);
38
39
        $mode = self::VUE_MODE_EMBEDDED;
40
        if ($mode === self::VUE_MODE_SINGLE_FILE) {
0 ignored issues
show
introduced by
The condition $mode === self::VUE_MODE_SINGLE_FILE is always false.
Loading history...
41
            return <<<EOF
42
<template>
43
<div>
44
    $editableForm
45
</div>
46
</template>
47
<script>
48
module.exports = {
49
    data: function () {
50
        return $jsonData;
51
    }
52
};
53
</script>
54
<style>
55
</style>
56
EOF;
57
        } else {
58
            $id = 'vueapp';
59
            $t = new HTMLElement('div', ['id' => $id], $editableForm, true);
60
            $script = <<<EOF
61
            console.log("asdfasd");
62
var app = new Vue({
63
    el: '#$id',
64
    data: $jsonData
65
});
66
console.log(app);
67
EOF;
68
            $s = new HTMLElement('script', [], $script, true);
69
            return new HTMLElement('div', [], [$t, $s]);
70
        }
71
    }
72
}
73